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> */
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
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'. */
61 #define PRINT_KERNEL_NAME 1
63 /* Node name on a communications network. */
64 #define PRINT_NODENAME 2
67 #define PRINT_KERNEL_RELEASE 4
70 #define PRINT_KERNEL_VERSION 8
72 /* Machine hardware name. */
73 #define PRINT_MACHINE 16
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
},
102 static struct option
const arch_long_options
[] =
104 {GETOPT_HELP_OPTION_DECL
},
105 {GETOPT_VERSION_OPTION_DECL
},
112 if (status
!= EXIT_SUCCESS
)
116 printf (_("Usage: %s [OPTION]...\n"), program_name
);
118 if (uname_mode
== UNAME_UNAME
)
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\
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\
140 Print machine architecture.\n\
145 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
146 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
147 emit_ancillary_info (PROGRAM_NAME
);
152 /* Print ELEMENT, preceded by a space if something has already been
156 print_element (char const *element
)
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. */
170 print_element_env (char const *element
, MAYBE_UNUSED
char const *envvar
)
175 char const *val
= getenv (envvar
);
180 print_element (element
);
184 /* Set all the option flags according to the switches specified.
185 Return the mask indicating which elements to print. */
188 decode_switches (int argc
, char **argv
)
191 unsigned int toprint
= 0;
193 if (uname_mode
== UNAME_ARCH
)
195 while ((c
= getopt_long (argc
, argv
, "",
196 arch_long_options
, NULL
)) != -1)
200 case_GETOPT_HELP_CHAR
;
202 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, ARCH_AUTHORS
);
205 usage (EXIT_FAILURE
);
208 toprint
= PRINT_MACHINE
;
212 while ((c
= getopt_long (argc
, argv
, "asnrvmpio",
213 uname_long_options
, NULL
)) != -1)
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 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
;
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
);