1 /* uname -- print system information
3 Copyright (C) 1989, 1992, 1993, 1996, 1997, 1999-2005,
4 2007-2009 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 of the License, or
9 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
23 #include <sys/types.h>
24 #include <sys/utsname.h>
27 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
28 # include <sys/systeminfo.h>
33 # include <sys/param.h> /* needed for OpenBSD 3.0 */
35 # include <sys/sysctl.h>
37 # ifdef HW_MACHINE_ARCH
38 /* E.g., FreeBSD 4.5, NetBSD 1.5.2 */
39 # define UNAME_HARDWARE_PLATFORM HW_MODEL
40 # define UNAME_PROCESSOR HW_MACHINE_ARCH
42 /* E.g., OpenBSD 3.0 */
43 # define UNAME_PROCESSOR HW_MODEL
49 # include <mach/machine.h>
50 # include <mach-o/arch.h>
58 /* The official name of this program (e.g., no `g' prefix). */
59 #define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch")
61 #define AUTHORS proper_name ("David MacKenzie")
62 #define ARCH_AUTHORS "David MacKenzie", "Karel Zak"
64 /* Values that are bitwise or'd into `toprint'. */
66 #define PRINT_KERNEL_NAME 1
68 /* Node name on a communications network. */
69 #define PRINT_NODENAME 2
72 #define PRINT_KERNEL_RELEASE 4
75 #define PRINT_KERNEL_VERSION 8
77 /* Machine hardware name. */
78 #define PRINT_MACHINE 16
81 #define PRINT_PROCESSOR 32
83 /* Hardware platform. */
84 #define PRINT_HARDWARE_PLATFORM 64
86 /* Operating system. */
87 #define PRINT_OPERATING_SYSTEM 128
89 static struct option
const uname_long_options
[] =
91 {"all", no_argument
, NULL
, 'a'},
92 {"kernel-name", no_argument
, NULL
, 's'},
93 {"sysname", no_argument
, NULL
, 's'}, /* Obsolescent. */
94 {"nodename", no_argument
, NULL
, 'n'},
95 {"kernel-release", no_argument
, NULL
, 'r'},
96 {"release", no_argument
, NULL
, 'r'}, /* Obsolescent. */
97 {"kernel-version", no_argument
, NULL
, 'v'},
98 {"machine", no_argument
, NULL
, 'm'},
99 {"processor", no_argument
, NULL
, 'p'},
100 {"hardware-platform", no_argument
, NULL
, 'i'},
101 {"operating-system", no_argument
, NULL
, 'o'},
102 {GETOPT_HELP_OPTION_DECL
},
103 {GETOPT_VERSION_OPTION_DECL
},
107 static struct option
const arch_long_options
[] =
109 {GETOPT_HELP_OPTION_DECL
},
110 {GETOPT_VERSION_OPTION_DECL
},
117 if (status
!= EXIT_SUCCESS
)
118 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
122 printf (_("Usage: %s [OPTION]...\n"), program_name
);
124 if (uname_mode
== UNAME_UNAME
)
127 Print certain system information. With no OPTION, same as -s.\n\
129 -a, --all print all information, in the following order,\n\
130 except omit -p and -i if unknown:\n\
131 -s, --kernel-name print the kernel name\n\
132 -n, --nodename print the network node hostname\n\
133 -r, --kernel-release print the kernel release\n\
136 -v, --kernel-version print the kernel version\n\
137 -m, --machine print the machine hardware name\n\
138 -p, --processor print the processor type or \"unknown\"\n\
139 -i, --hardware-platform print the hardware platform or \"unknown\"\n\
140 -o, --operating-system print the operating system\n\
146 Print machine architecture.\n\
151 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
152 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
153 emit_ancillary_info ();
158 /* Print ELEMENT, preceded by a space if something has already been
162 print_element (char const *element
)
168 fputs (element
, stdout
);
172 /* Set all the option flags according to the switches specified.
173 Return the mask indicating which elements to print. */
176 decode_switches (int argc
, char **argv
)
179 unsigned int toprint
= 0;
181 if (uname_mode
== UNAME_ARCH
)
183 while ((c
= getopt_long (argc
, argv
, "",
184 arch_long_options
, NULL
)) != -1)
188 case_GETOPT_HELP_CHAR
;
190 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, ARCH_AUTHORS
);
193 usage (EXIT_FAILURE
);
196 toprint
= PRINT_MACHINE
;
200 while ((c
= getopt_long (argc
, argv
, "asnrvmpio",
201 uname_long_options
, NULL
)) != -1)
210 toprint
|= PRINT_KERNEL_NAME
;
214 toprint
|= PRINT_NODENAME
;
218 toprint
|= PRINT_KERNEL_RELEASE
;
222 toprint
|= PRINT_KERNEL_VERSION
;
226 toprint
|= PRINT_MACHINE
;
230 toprint
|= PRINT_PROCESSOR
;
234 toprint
|= PRINT_HARDWARE_PLATFORM
;
238 toprint
|= PRINT_OPERATING_SYSTEM
;
241 case_GETOPT_HELP_CHAR
;
243 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
246 usage (EXIT_FAILURE
);
253 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
254 usage (EXIT_FAILURE
);
261 main (int argc
, char **argv
)
263 static char const unknown
[] = "unknown";
265 /* Mask indicating which elements to print. */
266 unsigned int toprint
= 0;
268 initialize_main (&argc
, &argv
);
269 set_program_name (argv
[0]);
270 setlocale (LC_ALL
, "");
271 bindtextdomain (PACKAGE
, LOCALEDIR
);
272 textdomain (PACKAGE
);
274 atexit (close_stdout
);
276 toprint
= decode_switches (argc
, argv
);
279 toprint
= PRINT_KERNEL_NAME
;
282 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
283 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
287 if (uname (&name
) == -1)
288 error (EXIT_FAILURE
, errno
, _("cannot get system name"));
290 if (toprint
& PRINT_KERNEL_NAME
)
291 print_element (name
.sysname
);
292 if (toprint
& PRINT_NODENAME
)
293 print_element (name
.nodename
);
294 if (toprint
& PRINT_KERNEL_RELEASE
)
295 print_element (name
.release
);
296 if (toprint
& PRINT_KERNEL_VERSION
)
297 print_element (name
.version
);
298 if (toprint
& PRINT_MACHINE
)
299 print_element (name
.machine
);
302 if (toprint
& PRINT_PROCESSOR
)
304 char const *element
= unknown
;
305 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
307 static char processor
[257];
308 if (0 <= sysinfo (SI_ARCHITECTURE
, processor
, sizeof processor
))
312 #ifdef UNAME_PROCESSOR
313 if (element
== unknown
)
315 static char processor
[257];
316 size_t s
= sizeof processor
;
317 static int mib
[] = { CTL_HW
, UNAME_PROCESSOR
};
318 if (sysctl (mib
, 2, processor
, &s
, 0, 0) >= 0)
322 /* This kludge works around a bug in Mac OS X. */
323 if (element
== unknown
)
326 size_t s
= sizeof cputype
;
327 NXArchInfo
const *ai
;
328 if (sysctlbyname ("hw.cputype", &cputype
, &s
, NULL
, 0) == 0
329 && (ai
= NXGetArchInfoFromCpuType (cputype
,
330 CPU_SUBTYPE_MULTIPLE
))
334 /* Hack "safely" around the ppc vs. powerpc return value. */
335 if (cputype
== CPU_TYPE_POWERPC
336 && strncmp (element
, "ppc", 3) == 0)
342 if (! (toprint
== UINT_MAX
&& element
== unknown
))
343 print_element (element
);
346 if (toprint
& PRINT_HARDWARE_PLATFORM
)
348 char const *element
= unknown
;
349 #if HAVE_SYSINFO && defined SI_PLATFORM
351 static char hardware_platform
[257];
352 if (0 <= sysinfo (SI_PLATFORM
,
353 hardware_platform
, sizeof hardware_platform
))
354 element
= hardware_platform
;
357 #ifdef UNAME_HARDWARE_PLATFORM
358 if (element
== unknown
)
360 static char hardware_platform
[257];
361 size_t s
= sizeof hardware_platform
;
362 static int mib
[] = { CTL_HW
, UNAME_HARDWARE_PLATFORM
};
363 if (sysctl (mib
, 2, hardware_platform
, &s
, 0, 0) >= 0)
364 element
= hardware_platform
;
367 if (! (toprint
== UINT_MAX
&& element
== unknown
))
368 print_element (element
);
371 if (toprint
& PRINT_OPERATING_SYSTEM
)
372 print_element (HOST_OPERATING_SYSTEM
);