split: port ‘split -n N /dev/null’ better to macOS
[coreutils.git] / src / uname.c
blob921a8818d0ab138aa2027911c11f8c1b836c8fad
1 /* uname -- print system information
3 Copyright (C) 1989-2023 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/utsname.h>
24 #include <getopt.h>
26 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
27 # include <sys/systeminfo.h>
28 #endif
30 #if HAVE_SYS_SYSCTL_H && ! defined __GLIBC__ && ! defined __APPLE__
31 # if HAVE_SYS_PARAM_H
32 # include <sys/param.h> /* needed for OpenBSD 3.0 */
33 # endif
34 # include <sys/sysctl.h>
35 # ifdef HW_MODEL
36 # ifdef HW_MACHINE_ARCH
37 /* E.g., FreeBSD 4.5, NetBSD 1.5.2 */
38 # define UNAME_HARDWARE_PLATFORM HW_MODEL
39 # define UNAME_PROCESSOR HW_MACHINE_ARCH
40 # else
41 /* E.g., OpenBSD 3.0 */
42 # define UNAME_PROCESSOR HW_MODEL
43 # endif
44 # endif
45 #endif
47 #include "system.h"
48 #include "die.h"
49 #include "error.h"
50 #include "quote.h"
51 #include "uname.h"
53 /* The official name of this program (e.g., no 'g' prefix). */
54 #define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch")
56 #define AUTHORS proper_name ("David MacKenzie")
57 #define ARCH_AUTHORS "David MacKenzie", "Karel Zak"
59 /* Values that are bitwise or'd into 'toprint'. */
60 /* Kernel name. */
61 #define PRINT_KERNEL_NAME 1
63 /* Node name on a communications network. */
64 #define PRINT_NODENAME 2
66 /* Kernel release. */
67 #define PRINT_KERNEL_RELEASE 4
69 /* Kernel version. */
70 #define PRINT_KERNEL_VERSION 8
72 /* Machine hardware name. */
73 #define PRINT_MACHINE 16
75 /* Processor type. */
76 #define PRINT_PROCESSOR 32
78 /* Hardware platform. */
79 #define PRINT_HARDWARE_PLATFORM 64
81 /* Operating system. */
82 #define PRINT_OPERATING_SYSTEM 128
84 static struct option const uname_long_options[] =
86 {"all", no_argument, NULL, 'a'},
87 {"kernel-name", no_argument, NULL, 's'},
88 {"sysname", no_argument, NULL, 's'}, /* Obsolescent. */
89 {"nodename", no_argument, NULL, 'n'},
90 {"kernel-release", no_argument, NULL, 'r'},
91 {"release", no_argument, NULL, 'r'}, /* Obsolescent. */
92 {"kernel-version", no_argument, NULL, 'v'},
93 {"machine", no_argument, NULL, 'm'},
94 {"processor", no_argument, NULL, 'p'},
95 {"hardware-platform", no_argument, NULL, 'i'},
96 {"operating-system", no_argument, NULL, 'o'},
97 {GETOPT_HELP_OPTION_DECL},
98 {GETOPT_VERSION_OPTION_DECL},
99 {NULL, 0, NULL, 0}
102 static struct option const arch_long_options[] =
104 {GETOPT_HELP_OPTION_DECL},
105 {GETOPT_VERSION_OPTION_DECL},
106 {NULL, 0, NULL, 0}
109 void
110 usage (int status)
112 if (status != EXIT_SUCCESS)
113 emit_try_help ();
114 else
116 printf (_("Usage: %s [OPTION]...\n"), program_name);
118 if (uname_mode == UNAME_UNAME)
120 fputs (_("\
121 Print certain system information. With no OPTION, same as -s.\n\
123 -a, --all print all information, in the following order,\n\
124 except omit -p and -i if unknown:\n\
125 -s, --kernel-name print the kernel name\n\
126 -n, --nodename print the network node hostname\n\
127 -r, --kernel-release print the kernel release\n\
128 "), stdout);
129 fputs (_("\
130 -v, --kernel-version print the kernel version\n\
131 -m, --machine print the machine hardware name\n\
132 -p, --processor print the processor type (non-portable)\n\
133 -i, --hardware-platform print the hardware platform (non-portable)\n\
134 -o, --operating-system print the operating system\n\
135 "), stdout);
137 else
139 fputs (_("\
140 Print machine architecture.\n\
142 "), stdout);
145 fputs (HELP_OPTION_DESCRIPTION, stdout);
146 fputs (VERSION_OPTION_DESCRIPTION, stdout);
147 emit_ancillary_info (PROGRAM_NAME);
149 exit (status);
152 /* Print ELEMENT, preceded by a space if something has already been
153 printed. */
155 static void
156 print_element (char const *element)
158 static bool printed;
159 if (printed)
160 putchar (' ');
161 printed = true;
162 fputs (element, stdout);
165 /* Print ELEMENT, preceded by a space if something has already been
166 printed. But if the environment variable ENVVAR is set, print its
167 value instead of ELEMENT. */
169 static void
170 print_element_env (char const *element, MAYBE_UNUSED char const *envvar)
172 #ifdef __APPLE__
173 if (envvar)
175 char const *val = getenv (envvar);
176 if (val)
177 element = val;
179 #endif
180 print_element (element);
184 /* Set all the option flags according to the switches specified.
185 Return the mask indicating which elements to print. */
187 static int
188 decode_switches (int argc, char **argv)
190 int c;
191 unsigned int toprint = 0;
193 if (uname_mode == UNAME_ARCH)
195 while ((c = getopt_long (argc, argv, "",
196 arch_long_options, NULL)) != -1)
198 switch (c)
200 case_GETOPT_HELP_CHAR;
202 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS);
204 default:
205 usage (EXIT_FAILURE);
208 toprint = PRINT_MACHINE;
210 else
212 while ((c = getopt_long (argc, argv, "asnrvmpio",
213 uname_long_options, NULL)) != -1)
215 switch (c)
217 case 'a':
218 toprint = UINT_MAX;
219 break;
221 case 's':
222 toprint |= PRINT_KERNEL_NAME;
223 break;
225 case 'n':
226 toprint |= PRINT_NODENAME;
227 break;
229 case 'r':
230 toprint |= PRINT_KERNEL_RELEASE;
231 break;
233 case 'v':
234 toprint |= PRINT_KERNEL_VERSION;
235 break;
237 case 'm':
238 toprint |= PRINT_MACHINE;
239 break;
241 case 'p':
242 toprint |= PRINT_PROCESSOR;
243 break;
245 case 'i':
246 toprint |= PRINT_HARDWARE_PLATFORM;
247 break;
249 case 'o':
250 toprint |= PRINT_OPERATING_SYSTEM;
251 break;
253 case_GETOPT_HELP_CHAR;
255 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
257 default:
258 usage (EXIT_FAILURE);
263 if (argc != optind)
265 error (0, 0, _("extra operand %s"), quote (argv[optind]));
266 usage (EXIT_FAILURE);
269 return toprint;
273 main (int argc, char **argv)
275 static char const unknown[] = "unknown";
277 /* Mask indicating which elements to print. */
278 unsigned int toprint = 0;
280 initialize_main (&argc, &argv);
281 set_program_name (argv[0]);
282 setlocale (LC_ALL, "");
283 bindtextdomain (PACKAGE, LOCALEDIR);
284 textdomain (PACKAGE);
286 atexit (close_stdout);
288 toprint = decode_switches (argc, argv);
290 if (toprint == 0)
291 toprint = PRINT_KERNEL_NAME;
293 if (toprint
294 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
295 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
297 struct utsname name;
299 if (uname (&name) == -1)
300 die (EXIT_FAILURE, errno, _("cannot get system name"));
302 if (toprint & PRINT_KERNEL_NAME)
303 print_element_env (name.sysname, "UNAME_SYSNAME");
304 if (toprint & PRINT_NODENAME)
305 print_element_env (name.nodename, "UNAME_NODENAME");
306 if (toprint & PRINT_KERNEL_RELEASE)
307 print_element_env (name.release, "UNAME_RELEASE");
308 if (toprint & PRINT_KERNEL_VERSION)
309 print_element_env (name.version, "UNAME_VERSION");
310 if (toprint & PRINT_MACHINE)
311 print_element_env (name.machine, "UNAME_MACHINE");
314 if (toprint & PRINT_PROCESSOR)
316 char const *element = unknown;
317 #ifdef __APPLE__
318 # if defined __arm__ || defined __arm64__
319 element = "arm";
320 # elif defined __i386__ || defined __x86_64__
321 element = "i386";
322 # elif defined __ppc__ || defined __ppc64__
323 element = "powerpc";
324 # endif
325 #endif
326 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
327 if (element == unknown)
329 static char processor[257];
330 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
331 element = processor;
333 #endif
334 #ifdef UNAME_PROCESSOR
335 if (element == unknown)
337 static char processor[257];
338 size_t s = sizeof processor;
339 static int mib[] = { CTL_HW, UNAME_PROCESSOR };
340 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
341 element = processor;
343 #endif
344 if (! (toprint == UINT_MAX && element == unknown))
345 print_element (element);
348 if (toprint & PRINT_HARDWARE_PLATFORM)
350 char const *element = unknown;
351 #if HAVE_SYSINFO && defined SI_PLATFORM
353 static char hardware_platform[257];
354 if (0 <= sysinfo (SI_PLATFORM,
355 hardware_platform, sizeof hardware_platform))
356 element = hardware_platform;
358 #endif
359 #ifdef UNAME_HARDWARE_PLATFORM
360 if (element == unknown)
362 static char hardware_platform[257];
363 size_t s = sizeof hardware_platform;
364 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
365 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
366 element = hardware_platform;
368 #endif
369 if (! (toprint == UINT_MAX && element == unknown))
370 print_element (element);
373 if (toprint & PRINT_OPERATING_SYSTEM)
374 print_element (HOST_OPERATING_SYSTEM);
376 putchar ('\n');
378 return EXIT_SUCCESS;