build: update gnulib submodule to latest (quoting change)
[coreutils/ericb.git] / src / uname.c
blobcb290e822643f3273de8cdd124922708f61fae49
1 /* uname -- print system information
3 Copyright (C) 1989, 1992-1993, 1996-1997, 1999-2005, 2007-2012 Free Software
4 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> */
21 #include <config.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/utsname.h>
25 #include <getopt.h>
27 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
28 # include <sys/systeminfo.h>
29 #endif
31 #if HAVE_SYS_SYSCTL_H
32 # if HAVE_SYS_PARAM_H
33 # include <sys/param.h> /* needed for OpenBSD 3.0 */
34 # endif
35 # include <sys/sysctl.h>
36 # ifdef HW_MODEL
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
41 # else
42 /* E.g., OpenBSD 3.0 */
43 # define UNAME_PROCESSOR HW_MODEL
44 # endif
45 # endif
46 #endif
48 #ifdef __APPLE__
49 # include <mach/machine.h>
50 # include <mach-o/arch.h>
51 #endif
53 #include "system.h"
54 #include "error.h"
55 #include "quote.h"
56 #include "uname.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'. */
65 /* Kernel name. */
66 #define PRINT_KERNEL_NAME 1
68 /* Node name on a communications network. */
69 #define PRINT_NODENAME 2
71 /* Kernel release. */
72 #define PRINT_KERNEL_RELEASE 4
74 /* Kernel version. */
75 #define PRINT_KERNEL_VERSION 8
77 /* Machine hardware name. */
78 #define PRINT_MACHINE 16
80 /* Processor type. */
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},
104 {NULL, 0, NULL, 0}
107 static struct option const arch_long_options[] =
109 {GETOPT_HELP_OPTION_DECL},
110 {GETOPT_VERSION_OPTION_DECL},
111 {NULL, 0, NULL, 0}
114 void
115 usage (int status)
117 if (status != EXIT_SUCCESS)
118 emit_try_help ();
119 else
121 printf (_("Usage: %s [OPTION]...\n"), program_name);
123 if (uname_mode == UNAME_UNAME)
125 fputs (_("\
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\
133 "), stdout);
134 fputs (_("\
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 or \"unknown\"\n\
138 -i, --hardware-platform print the hardware platform or \"unknown\"\n\
139 -o, --operating-system print the operating system\n\
140 "), stdout);
142 else
144 fputs (_("\
145 Print machine architecture.\n\
147 "), stdout);
150 fputs (HELP_OPTION_DESCRIPTION, stdout);
151 fputs (VERSION_OPTION_DESCRIPTION, stdout);
152 emit_ancillary_info ();
154 exit (status);
157 /* Print ELEMENT, preceded by a space if something has already been
158 printed. */
160 static void
161 print_element (char const *element)
163 static bool printed;
164 if (printed)
165 putchar (' ');
166 printed = true;
167 fputs (element, stdout);
171 /* Set all the option flags according to the switches specified.
172 Return the mask indicating which elements to print. */
174 static int
175 decode_switches (int argc, char **argv)
177 int c;
178 unsigned int toprint = 0;
180 if (uname_mode == UNAME_ARCH)
182 while ((c = getopt_long (argc, argv, "",
183 arch_long_options, NULL)) != -1)
185 switch (c)
187 case_GETOPT_HELP_CHAR;
189 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS);
191 default:
192 usage (EXIT_FAILURE);
195 toprint = PRINT_MACHINE;
197 else
199 while ((c = getopt_long (argc, argv, "asnrvmpio",
200 uname_long_options, NULL)) != -1)
202 switch (c)
204 case 'a':
205 toprint = UINT_MAX;
206 break;
208 case 's':
209 toprint |= PRINT_KERNEL_NAME;
210 break;
212 case 'n':
213 toprint |= PRINT_NODENAME;
214 break;
216 case 'r':
217 toprint |= PRINT_KERNEL_RELEASE;
218 break;
220 case 'v':
221 toprint |= PRINT_KERNEL_VERSION;
222 break;
224 case 'm':
225 toprint |= PRINT_MACHINE;
226 break;
228 case 'p':
229 toprint |= PRINT_PROCESSOR;
230 break;
232 case 'i':
233 toprint |= PRINT_HARDWARE_PLATFORM;
234 break;
236 case 'o':
237 toprint |= PRINT_OPERATING_SYSTEM;
238 break;
240 case_GETOPT_HELP_CHAR;
242 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
244 default:
245 usage (EXIT_FAILURE);
250 if (argc != optind)
252 error (0, 0, _("extra operand %s"), quote (argv[optind]));
253 usage (EXIT_FAILURE);
256 return toprint;
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);
277 if (toprint == 0)
278 toprint = PRINT_KERNEL_NAME;
280 if (toprint
281 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
282 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
284 struct utsname name;
286 if (uname (&name) == -1)
287 error (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))
308 element = processor;
310 #endif
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)
318 element = processor;
320 # ifdef __APPLE__
321 /* This kludge works around a bug in Mac OS X. */
322 if (element == unknown)
324 cpu_type_t cputype;
325 size_t s = sizeof cputype;
326 NXArchInfo const *ai;
327 if (sysctlbyname ("hw.cputype", &cputype, &s, NULL, 0) == 0
328 && (ai = NXGetArchInfoFromCpuType (cputype,
329 CPU_SUBTYPE_MULTIPLE))
330 != NULL)
331 element = ai->name;
333 /* Hack "safely" around the ppc vs. powerpc return value. */
334 if (cputype == CPU_TYPE_POWERPC
335 && STRNCMP_LIT (element, "ppc") == 0)
336 element = "powerpc";
338 # endif
340 #endif
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;
355 #endif
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;
365 #endif
366 if (! (toprint == UINT_MAX && element == unknown))
367 print_element (element);
370 if (toprint & PRINT_OPERATING_SYSTEM)
371 print_element (HOST_OPERATING_SYSTEM);
373 putchar ('\n');
375 exit (EXIT_SUCCESS);