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 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 "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 /* The name this program was run with, for error messages. */
92 static struct option
const uname_long_options
[] =
94 {"all", no_argument
, NULL
, 'a'},
95 {"kernel-name", no_argument
, NULL
, 's'},
96 {"sysname", no_argument
, NULL
, 's'}, /* Obsolescent. */
97 {"nodename", no_argument
, NULL
, 'n'},
98 {"kernel-release", no_argument
, NULL
, 'r'},
99 {"release", no_argument
, NULL
, 'r'}, /* Obsolescent. */
100 {"kernel-version", no_argument
, NULL
, 'v'},
101 {"machine", no_argument
, NULL
, 'm'},
102 {"processor", no_argument
, NULL
, 'p'},
103 {"hardware-platform", no_argument
, NULL
, 'i'},
104 {"operating-system", no_argument
, NULL
, 'o'},
105 {GETOPT_HELP_OPTION_DECL
},
106 {GETOPT_VERSION_OPTION_DECL
},
110 static struct option
const arch_long_options
[] =
112 {GETOPT_HELP_OPTION_DECL
},
113 {GETOPT_VERSION_OPTION_DECL
},
120 if (status
!= EXIT_SUCCESS
)
121 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
125 printf (_("Usage: %s [OPTION]...\n"), program_name
);
127 if (uname_mode
== UNAME_UNAME
)
130 Print certain system information. With no OPTION, same as -s.\n\
132 -a, --all print all information, in the following order,\n\
133 except omit -p and -i if unknown:\n\
134 -s, --kernel-name print the kernel name\n\
135 -n, --nodename print the network node hostname\n\
136 -r, --kernel-release print the kernel release\n\
139 -v, --kernel-version print the kernel version\n\
140 -m, --machine print the machine hardware name\n\
141 -p, --processor print the processor type or \"unknown\"\n\
142 -i, --hardware-platform print the hardware platform or \"unknown\"\n\
143 -o, --operating-system print the operating system\n\
149 Print machine architecture.\n\
154 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
155 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
156 emit_bug_reporting_address ();
161 /* Print ELEMENT, preceded by a space if something has already been
165 print_element (char const *element
)
171 fputs (element
, stdout
);
175 /* Set all the option flags according to the switches specified.
176 Return the mask indicating which elements to print. */
179 decode_switches (int argc
, char **argv
)
182 unsigned int toprint
= 0;
184 if (uname_mode
== UNAME_ARCH
)
186 while ((c
= getopt_long (argc
, argv
, "",
187 arch_long_options
, NULL
)) != -1)
191 case_GETOPT_HELP_CHAR
;
193 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, ARCH_AUTHORS
);
196 usage (EXIT_FAILURE
);
199 toprint
= PRINT_MACHINE
;
203 while ((c
= getopt_long (argc
, argv
, "asnrvmpio",
204 uname_long_options
, NULL
)) != -1)
213 toprint
|= PRINT_KERNEL_NAME
;
217 toprint
|= PRINT_NODENAME
;
221 toprint
|= PRINT_KERNEL_RELEASE
;
225 toprint
|= PRINT_KERNEL_VERSION
;
229 toprint
|= PRINT_MACHINE
;
233 toprint
|= PRINT_PROCESSOR
;
237 toprint
|= PRINT_HARDWARE_PLATFORM
;
241 toprint
|= PRINT_OPERATING_SYSTEM
;
244 case_GETOPT_HELP_CHAR
;
246 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
249 usage (EXIT_FAILURE
);
256 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
257 usage (EXIT_FAILURE
);
264 main (int argc
, char **argv
)
266 static char const unknown
[] = "unknown";
268 /* Mask indicating which elements to print. */
269 unsigned int toprint
= 0;
271 initialize_main (&argc
, &argv
);
272 program_name
= argv
[0];
273 setlocale (LC_ALL
, "");
274 bindtextdomain (PACKAGE
, LOCALEDIR
);
275 textdomain (PACKAGE
);
277 atexit (close_stdout
);
279 toprint
= decode_switches (argc
, argv
);
282 toprint
= PRINT_KERNEL_NAME
;
285 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
286 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
290 if (uname (&name
) == -1)
291 error (EXIT_FAILURE
, errno
, _("cannot get system name"));
293 if (toprint
& PRINT_KERNEL_NAME
)
294 print_element (name
.sysname
);
295 if (toprint
& PRINT_NODENAME
)
296 print_element (name
.nodename
);
297 if (toprint
& PRINT_KERNEL_RELEASE
)
298 print_element (name
.release
);
299 if (toprint
& PRINT_KERNEL_VERSION
)
300 print_element (name
.version
);
301 if (toprint
& PRINT_MACHINE
)
302 print_element (name
.machine
);
305 if (toprint
& PRINT_PROCESSOR
)
307 char const *element
= unknown
;
308 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
310 static char processor
[257];
311 if (0 <= sysinfo (SI_ARCHITECTURE
, processor
, sizeof processor
))
315 #ifdef UNAME_PROCESSOR
316 if (element
== unknown
)
318 static char processor
[257];
319 size_t s
= sizeof processor
;
320 static int mib
[] = { CTL_HW
, UNAME_PROCESSOR
};
321 if (sysctl (mib
, 2, processor
, &s
, 0, 0) >= 0)
325 /* This kludge works around a bug in Mac OS X. */
326 if (element
== unknown
)
329 size_t s
= sizeof cputype
;
330 NXArchInfo
const *ai
;
331 if (sysctlbyname ("hw.cputype", &cputype
, &s
, NULL
, 0) == 0
332 && (ai
= NXGetArchInfoFromCpuType (cputype
,
333 CPU_SUBTYPE_MULTIPLE
))
337 /* Hack "safely" around the ppc vs. powerpc return value. */
338 if (cputype
== CPU_TYPE_POWERPC
339 && strncmp (element
, "ppc", 3) == 0)
345 if (! (toprint
== UINT_MAX
&& element
== unknown
))
346 print_element (element
);
349 if (toprint
& PRINT_HARDWARE_PLATFORM
)
351 char const *element
= unknown
;
352 #if HAVE_SYSINFO && defined SI_PLATFORM
354 static char hardware_platform
[257];
355 if (0 <= sysinfo (SI_PLATFORM
,
356 hardware_platform
, sizeof hardware_platform
))
357 element
= hardware_platform
;
360 #ifdef UNAME_HARDWARE_PLATFORM
361 if (element
== unknown
)
363 static char hardware_platform
[257];
364 size_t s
= sizeof hardware_platform
;
365 static int mib
[] = { CTL_HW
, UNAME_HARDWARE_PLATFORM
};
366 if (sysctl (mib
, 2, hardware_platform
, &s
, 0, 0) >= 0)
367 element
= hardware_platform
;
370 if (! (toprint
== UINT_MAX
&& element
== unknown
))
371 print_element (element
);
374 if (toprint
& PRINT_OPERATING_SYSTEM
)
375 print_element (HOST_OPERATING_SYSTEM
);