1 /* uname -- print system information
3 Copyright (C) 1989-2024 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> */
22 #include <sys/types.h>
23 #include <sys/utsname.h>
26 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
27 # include <sys/systeminfo.h>
30 #if HAVE_SYS_SYSCTL_H && ! defined __GLIBC__ && ! defined __APPLE__
32 # include <sys/param.h> /* needed for OpenBSD 3.0 */
34 # include <sys/sysctl.h>
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
41 /* E.g., OpenBSD 3.0 */
42 # define UNAME_PROCESSOR HW_MODEL
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'. */
59 #define PRINT_KERNEL_NAME 1
61 /* Node name on a communications network. */
62 #define PRINT_NODENAME 2
65 #define PRINT_KERNEL_RELEASE 4
68 #define PRINT_KERNEL_VERSION 8
70 /* Machine hardware name. */
71 #define PRINT_MACHINE 16
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}
110 if (status
!= EXIT_SUCCESS
)
114 printf (_("Usage: %s [OPTION]...\n"), program_name
);
116 if (uname_mode
== UNAME_UNAME
)
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\
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\
138 Print machine architecture.\n\
143 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
144 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
145 emit_ancillary_info (PROGRAM_NAME
);
150 /* Print ELEMENT, preceded by a space if something has already been
154 print_element (char const *element
)
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. */
168 print_element_env (char const *element
, MAYBE_UNUSED
char const *envvar
)
173 char const *val
= getenv (envvar
);
178 print_element (element
);
182 /* Set all the option flags according to the switches specified.
183 Return the mask indicating which elements to print. */
186 decode_switches (int argc
, char **argv
)
189 unsigned int toprint
= 0;
191 if (uname_mode
== UNAME_ARCH
)
193 while ((c
= getopt_long (argc
, argv
, "",
194 arch_long_options
, nullptr))
199 case_GETOPT_HELP_CHAR
;
201 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, ARCH_AUTHORS
);
204 usage (EXIT_FAILURE
);
207 toprint
= PRINT_MACHINE
;
211 while ((c
= getopt_long (argc
, argv
, "asnrvmpio",
212 uname_long_options
, nullptr))
222 toprint
|= PRINT_KERNEL_NAME
;
226 toprint
|= PRINT_NODENAME
;
230 toprint
|= PRINT_KERNEL_RELEASE
;
234 toprint
|= PRINT_KERNEL_VERSION
;
238 toprint
|= PRINT_MACHINE
;
242 toprint
|= PRINT_PROCESSOR
;
246 toprint
|= PRINT_HARDWARE_PLATFORM
;
250 toprint
|= PRINT_OPERATING_SYSTEM
;
253 case_GETOPT_HELP_CHAR
;
255 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
258 usage (EXIT_FAILURE
);
265 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
266 usage (EXIT_FAILURE
);
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
);
291 toprint
= PRINT_KERNEL_NAME
;
294 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
295 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
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
;
318 # if defined __arm__ || defined __arm64__
320 # elif defined __i386__ || defined __x86_64__
322 # elif defined __ppc__ || defined __ppc64__
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
))
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)
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
;
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
;
369 if (! (toprint
== UINT_MAX
&& element
== unknown
))
370 print_element (element
);
373 if (toprint
& PRINT_OPERATING_SYSTEM
)
374 print_element (HOST_OPERATING_SYSTEM
);