1 /* uname -- print system information
3 Copyright (C) 1989, 1992, 1993, 1996, 1997, 1999-2005, 2007
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
24 #include <sys/types.h>
25 #include <sys/utsname.h>
28 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
29 # include <sys/systeminfo.h>
34 # include <sys/param.h> /* needed for OpenBSD 3.0 */
36 # include <sys/sysctl.h>
38 # ifdef HW_MACHINE_ARCH
39 /* E.g., FreeBSD 4.5, NetBSD 1.5.2 */
40 # define UNAME_HARDWARE_PLATFORM HW_MODEL
41 # define UNAME_PROCESSOR HW_MACHINE_ARCH
43 /* E.g., OpenBSD 3.0 */
44 # define UNAME_PROCESSOR HW_MODEL
50 # include <mach/machine.h>
51 # include <mach-o/arch.h>
59 /* The official name of this program (e.g., no `g' prefix). */
60 #define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch")
62 #define AUTHORS "David MacKenzie"
63 #define ARCH_AUTHORS "David MacKenzie", "Karel Zak"
65 /* Values that are bitwise or'd into `toprint'. */
67 #define PRINT_KERNEL_NAME 1
69 /* Node name on a communications network. */
70 #define PRINT_NODENAME 2
73 #define PRINT_KERNEL_RELEASE 4
76 #define PRINT_KERNEL_VERSION 8
78 /* Machine hardware name. */
79 #define PRINT_MACHINE 16
82 #define PRINT_PROCESSOR 32
84 /* Hardware platform. */
85 #define PRINT_HARDWARE_PLATFORM 64
87 /* Operating system. */
88 #define PRINT_OPERATING_SYSTEM 128
90 /* The name this program was run with, for error messages. */
93 static struct option const uname_long_options[] =
95 {"all", no_argument, NULL, 'a'},
96 {"kernel-name", no_argument, NULL, 's'},
97 {"sysname", no_argument, NULL, 's'}, /* Obsolescent. */
98 {"nodename", no_argument, NULL, 'n'},
99 {"kernel-release", no_argument, NULL, 'r'},
100 {"release", no_argument, NULL, 'r'}, /* Obsolescent. */
101 {"kernel-version", no_argument, NULL, 'v'},
102 {"machine", no_argument, NULL, 'm'},
103 {"processor", no_argument, NULL, 'p'},
104 {"hardware-platform", no_argument, NULL, 'i'},
105 {"operating-system", no_argument, NULL, 'o'},
106 {GETOPT_HELP_OPTION_DECL},
107 {GETOPT_VERSION_OPTION_DECL},
111 static struct option const arch_long_options[] =
113 {GETOPT_HELP_OPTION_DECL},
114 {GETOPT_VERSION_OPTION_DECL},
121 if (status != EXIT_SUCCESS)
122 fprintf (stderr, _("Try `%s --help' for more information.\n"),
126 printf (_("Usage: %s [OPTION]...\n"), program_name);
128 if (uname_mode == UNAME_UNAME)
131 Print certain system information. With no OPTION, same as -s.\n\
133 -a, --all print all information, in the following order,\n\
134 except omit -p and -i if unknown:\n\
135 -s, --kernel-name print the kernel name\n\
136 -n, --nodename print the network node hostname\n\
137 -r, --kernel-release print the kernel release\n\
140 -v, --kernel-version print the kernel version\n\
141 -m, --machine print the machine hardware name\n\
142 -p, --processor print the processor type or \"unknown\"\n\
143 -i, --hardware-platform print the hardware platform or \"unknown\"\n\
144 -o, --operating-system print the operating system\n\
150 Print machine architecture.\n\
155 fputs (HELP_OPTION_DESCRIPTION, stdout);
156 fputs (VERSION_OPTION_DESCRIPTION, stdout);
157 emit_bug_reporting_address ();
162 /* Print ELEMENT, preceded by a space if something has already been
166 print_element (char const *element)
172 fputs (element, stdout);
176 /* Set all the option flags according to the switches specified.
177 Return the mask indicating which elements to print. */
180 decode_switches (int argc, char **argv)
183 unsigned int toprint = 0;
185 if (uname_mode == UNAME_ARCH)
187 while ((c = getopt_long (argc, argv, "",
188 arch_long_options, NULL)) != -1)
192 case_GETOPT_HELP_CHAR;
194 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS);
197 usage (EXIT_FAILURE);
200 toprint = PRINT_MACHINE;
204 while ((c = getopt_long (argc, argv, "asnrvmpio",
205 uname_long_options, NULL)) != -1)
214 toprint |= PRINT_KERNEL_NAME;
218 toprint |= PRINT_NODENAME;
222 toprint |= PRINT_KERNEL_RELEASE;
226 toprint |= PRINT_KERNEL_VERSION;
230 toprint |= PRINT_MACHINE;
234 toprint |= PRINT_PROCESSOR;
238 toprint |= PRINT_HARDWARE_PLATFORM;
242 toprint |= PRINT_OPERATING_SYSTEM;
245 case_GETOPT_HELP_CHAR;
247 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
250 usage (EXIT_FAILURE);
257 error (0, 0, _("extra operand %s"), quote (argv[optind]));
258 usage (EXIT_FAILURE);
265 main (int argc, char **argv)
267 static char const unknown[] = "unknown";
269 /* Mask indicating which elements to print. */
270 unsigned int toprint = 0;
272 initialize_main (&argc, &argv);
273 program_name = argv[0];
274 setlocale (LC_ALL, "");
275 bindtextdomain (PACKAGE, LOCALEDIR);
276 textdomain (PACKAGE);
278 atexit (close_stdout);
280 toprint = decode_switches (argc, argv);
283 toprint = PRINT_KERNEL_NAME;
286 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
287 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
291 if (uname (&name) == -1)
292 error (EXIT_FAILURE, errno, _("cannot get system name"));
294 if (toprint & PRINT_KERNEL_NAME)
295 print_element (name.sysname);
296 if (toprint & PRINT_NODENAME)
297 print_element (name.nodename);
298 if (toprint & PRINT_KERNEL_RELEASE)
299 print_element (name.release);
300 if (toprint & PRINT_KERNEL_VERSION)
301 print_element (name.version);
302 if (toprint & PRINT_MACHINE)
303 print_element (name.machine);
306 if (toprint & PRINT_PROCESSOR)
308 char const *element = unknown;
309 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
311 static char processor[257];
312 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
316 #ifdef UNAME_PROCESSOR
317 if (element == unknown)
319 static char processor[257];
320 size_t s = sizeof processor;
321 static int mib[] = { CTL_HW, UNAME_PROCESSOR };
322 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
326 /* This kludge works around a bug in Mac OS X. */
327 if (element == unknown)
330 size_t s = sizeof cputype;
331 NXArchInfo const *ai;
332 if (sysctlbyname ("hw.cputype", &cputype, &s, NULL, 0) == 0
333 && (ai = NXGetArchInfoFromCpuType (cputype,
334 CPU_SUBTYPE_MULTIPLE))
338 /* Hack "safely" around the ppc vs. powerpc return value. */
339 if (cputype == CPU_TYPE_POWERPC
340 && strncmp (element, "ppc", 3) == 0)
346 if (! (toprint == UINT_MAX && element == unknown))
347 print_element (element);
350 if (toprint & PRINT_HARDWARE_PLATFORM)
352 char const *element = unknown;
353 #if HAVE_SYSINFO && defined SI_PLATFORM
355 static char hardware_platform[257];
356 if (0 <= sysinfo (SI_PLATFORM,
357 hardware_platform, sizeof hardware_platform))
358 element = hardware_platform;
361 #ifdef UNAME_HARDWARE_PLATFORM
362 if (element == unknown)
364 static char hardware_platform[257];
365 size_t s = sizeof hardware_platform;
366 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
367 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
368 element = hardware_platform;
371 if (! (toprint == UINT_MAX && element == unknown))
372 print_element (element);
375 if (toprint & PRINT_OPERATING_SYSTEM)
376 print_element (HOST_OPERATING_SYSTEM);