cksum,df,digest: prefer signed types
[coreutils.git] / src / uname.c
blob883b9a490d5157f0bba5c7cf5691dfeac9374413
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 "quote.h"
49 #include "uname.h"
51 /* The official name of this program (e.g., no 'g' prefix). */
52 #define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch")
54 #define AUTHORS proper_name ("David MacKenzie")
55 #define ARCH_AUTHORS "David MacKenzie", "Karel Zak"
57 /* Values that are bitwise or'd into 'toprint'. */
58 /* Kernel name. */
59 #define PRINT_KERNEL_NAME 1
61 /* Node name on a communications network. */
62 #define PRINT_NODENAME 2
64 /* Kernel release. */
65 #define PRINT_KERNEL_RELEASE 4
67 /* Kernel version. */
68 #define PRINT_KERNEL_VERSION 8
70 /* Machine hardware name. */
71 #define PRINT_MACHINE 16
73 /* Processor type. */
74 #define PRINT_PROCESSOR 32
76 /* Hardware platform. */
77 #define PRINT_HARDWARE_PLATFORM 64
79 /* Operating system. */
80 #define PRINT_OPERATING_SYSTEM 128
82 static struct option const uname_long_options[] =
84 {"all", no_argument, nullptr, 'a'},
85 {"kernel-name", no_argument, nullptr, 's'},
86 {"sysname", no_argument, nullptr, 's'}, /* Obsolescent. */
87 {"nodename", no_argument, nullptr, 'n'},
88 {"kernel-release", no_argument, nullptr, 'r'},
89 {"release", no_argument, nullptr, 'r'}, /* Obsolescent. */
90 {"kernel-version", no_argument, nullptr, 'v'},
91 {"machine", no_argument, nullptr, 'm'},
92 {"processor", no_argument, nullptr, 'p'},
93 {"hardware-platform", no_argument, nullptr, 'i'},
94 {"operating-system", no_argument, nullptr, 'o'},
95 {GETOPT_HELP_OPTION_DECL},
96 {GETOPT_VERSION_OPTION_DECL},
97 {nullptr, 0, nullptr, 0}
100 static struct option const arch_long_options[] =
102 {GETOPT_HELP_OPTION_DECL},
103 {GETOPT_VERSION_OPTION_DECL},
104 {nullptr, 0, nullptr, 0}
107 void
108 usage (int status)
110 if (status != EXIT_SUCCESS)
111 emit_try_help ();
112 else
114 printf (_("Usage: %s [OPTION]...\n"), program_name);
116 if (uname_mode == UNAME_UNAME)
118 fputs (_("\
119 Print certain system information. With no OPTION, same as -s.\n\
121 -a, --all print all information, in the following order,\n\
122 except omit -p and -i if unknown:\n\
123 -s, --kernel-name print the kernel name\n\
124 -n, --nodename print the network node hostname\n\
125 -r, --kernel-release print the kernel release\n\
126 "), stdout);
127 fputs (_("\
128 -v, --kernel-version print the kernel version\n\
129 -m, --machine print the machine hardware name\n\
130 -p, --processor print the processor type (non-portable)\n\
131 -i, --hardware-platform print the hardware platform (non-portable)\n\
132 -o, --operating-system print the operating system\n\
133 "), stdout);
135 else
137 fputs (_("\
138 Print machine architecture.\n\
140 "), stdout);
143 fputs (HELP_OPTION_DESCRIPTION, stdout);
144 fputs (VERSION_OPTION_DESCRIPTION, stdout);
145 emit_ancillary_info (PROGRAM_NAME);
147 exit (status);
150 /* Print ELEMENT, preceded by a space if something has already been
151 printed. */
153 static void
154 print_element (char const *element)
156 static bool printed;
157 if (printed)
158 putchar (' ');
159 printed = true;
160 fputs (element, stdout);
163 /* Print ELEMENT, preceded by a space if something has already been
164 printed. But if the environment variable ENVVAR is set, print its
165 value instead of ELEMENT. */
167 static void
168 print_element_env (char const *element, MAYBE_UNUSED char const *envvar)
170 #ifdef __APPLE__
171 if (envvar)
173 char const *val = getenv (envvar);
174 if (val)
175 element = val;
177 #endif
178 print_element (element);
182 /* Set all the option flags according to the switches specified.
183 Return the mask indicating which elements to print. */
185 static int
186 decode_switches (int argc, char **argv)
188 int c;
189 unsigned int toprint = 0;
191 if (uname_mode == UNAME_ARCH)
193 while ((c = getopt_long (argc, argv, "",
194 arch_long_options, nullptr))
195 != -1)
197 switch (c)
199 case_GETOPT_HELP_CHAR;
201 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS);
203 default:
204 usage (EXIT_FAILURE);
207 toprint = PRINT_MACHINE;
209 else
211 while ((c = getopt_long (argc, argv, "asnrvmpio",
212 uname_long_options, nullptr))
213 != -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 error (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;