1 /* uname -- print system information
3 Copyright (C) 1989-2016 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 <http://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>
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
48 # include <mach/machine.h>
49 # 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
)
121 printf (_("Usage: %s [OPTION]...\n"), program_name
);
123 if (uname_mode
== UNAME_UNAME
)
126 Print certain system information. With no OPTION, same as -s.\n\
128 -a, --all print all information, in the following order,\n\
129 except omit -p and -i if unknown:\n\
130 -s, --kernel-name print the kernel name\n\
131 -n, --nodename print the network node hostname\n\
132 -r, --kernel-release print the kernel release\n\
135 -v, --kernel-version print the kernel version\n\
136 -m, --machine print the machine hardware name\n\
137 -p, --processor print the processor type (non-portable)\n\
138 -i, --hardware-platform print the hardware platform (non-portable)\n\
139 -o, --operating-system print the operating system\n\
145 Print machine architecture.\n\
150 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
151 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
152 emit_ancillary_info (PROGRAM_NAME
);
157 /* Print ELEMENT, preceded by a space if something has already been
161 print_element (char const *element
)
167 fputs (element
, stdout
);
171 /* Set all the option flags according to the switches specified.
172 Return the mask indicating which elements to print. */
175 decode_switches (int argc
, char **argv
)
178 unsigned int toprint
= 0;
180 if (uname_mode
== UNAME_ARCH
)
182 while ((c
= getopt_long (argc
, argv
, "",
183 arch_long_options
, NULL
)) != -1)
187 case_GETOPT_HELP_CHAR
;
189 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, ARCH_AUTHORS
);
192 usage (EXIT_FAILURE
);
195 toprint
= PRINT_MACHINE
;
199 while ((c
= getopt_long (argc
, argv
, "asnrvmpio",
200 uname_long_options
, NULL
)) != -1)
209 toprint
|= PRINT_KERNEL_NAME
;
213 toprint
|= PRINT_NODENAME
;
217 toprint
|= PRINT_KERNEL_RELEASE
;
221 toprint
|= PRINT_KERNEL_VERSION
;
225 toprint
|= PRINT_MACHINE
;
229 toprint
|= PRINT_PROCESSOR
;
233 toprint
|= PRINT_HARDWARE_PLATFORM
;
237 toprint
|= PRINT_OPERATING_SYSTEM
;
240 case_GETOPT_HELP_CHAR
;
242 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
245 usage (EXIT_FAILURE
);
252 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
253 usage (EXIT_FAILURE
);
260 main (int argc
, char **argv
)
262 static char const unknown
[] = "unknown";
264 /* Mask indicating which elements to print. */
265 unsigned int toprint
= 0;
267 initialize_main (&argc
, &argv
);
268 set_program_name (argv
[0]);
269 setlocale (LC_ALL
, "");
270 bindtextdomain (PACKAGE
, LOCALEDIR
);
271 textdomain (PACKAGE
);
273 atexit (close_stdout
);
275 toprint
= decode_switches (argc
, argv
);
278 toprint
= PRINT_KERNEL_NAME
;
281 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
282 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
286 if (uname (&name
) == -1)
287 die (EXIT_FAILURE
, errno
, _("cannot get system name"));
289 if (toprint
& PRINT_KERNEL_NAME
)
290 print_element (name
.sysname
);
291 if (toprint
& PRINT_NODENAME
)
292 print_element (name
.nodename
);
293 if (toprint
& PRINT_KERNEL_RELEASE
)
294 print_element (name
.release
);
295 if (toprint
& PRINT_KERNEL_VERSION
)
296 print_element (name
.version
);
297 if (toprint
& PRINT_MACHINE
)
298 print_element (name
.machine
);
301 if (toprint
& PRINT_PROCESSOR
)
303 char const *element
= unknown
;
304 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
306 static char processor
[257];
307 if (0 <= sysinfo (SI_ARCHITECTURE
, processor
, sizeof processor
))
311 #ifdef UNAME_PROCESSOR
312 if (element
== unknown
)
314 static char processor
[257];
315 size_t s
= sizeof processor
;
316 static int mib
[] = { CTL_HW
, UNAME_PROCESSOR
};
317 if (sysctl (mib
, 2, processor
, &s
, 0, 0) >= 0)
321 /* This kludge works around a bug in Mac OS X. */
322 if (element
== unknown
)
325 size_t cs
= sizeof cputype
;
326 NXArchInfo
const *ai
;
327 if (sysctlbyname ("hw.cputype", &cputype
, &cs
, NULL
, 0) == 0
328 && (ai
= NXGetArchInfoFromCpuType (cputype
,
329 CPU_SUBTYPE_MULTIPLE
))
333 /* Hack "safely" around the ppc vs. powerpc return value. */
334 if (cputype
== CPU_TYPE_POWERPC
335 && STRNCMP_LIT (element
, "ppc") == 0)
341 if (! (toprint
== UINT_MAX
&& element
== unknown
))
342 print_element (element
);
345 if (toprint
& PRINT_HARDWARE_PLATFORM
)
347 char const *element
= unknown
;
348 #if HAVE_SYSINFO && defined SI_PLATFORM
350 static char hardware_platform
[257];
351 if (0 <= sysinfo (SI_PLATFORM
,
352 hardware_platform
, sizeof hardware_platform
))
353 element
= hardware_platform
;
356 #ifdef UNAME_HARDWARE_PLATFORM
357 if (element
== unknown
)
359 static char hardware_platform
[257];
360 size_t s
= sizeof hardware_platform
;
361 static int mib
[] = { CTL_HW
, UNAME_HARDWARE_PLATFORM
};
362 if (sysctl (mib
, 2, hardware_platform
, &s
, 0, 0) >= 0)
363 element
= hardware_platform
;
366 if (! (toprint
== UINT_MAX
&& element
== unknown
))
367 print_element (element
);
370 if (toprint
& PRINT_OPERATING_SYSTEM
)
371 print_element (HOST_OPERATING_SYSTEM
);