Define ENOTSUP, not ENOSYS. Needed on OpenBSD 3.9.
[coreutils/ericb.git] / src / uname.c
blob5d04e6f882b1a2a365581500e1db8d00bcccda00
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> */
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 "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 /* The name this program was run with, for error messages. */
90 char *program_name;
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},
107 {NULL, 0, NULL, 0}
110 static struct option const arch_long_options[] =
112 {GETOPT_HELP_OPTION_DECL},
113 {GETOPT_VERSION_OPTION_DECL},
114 {NULL, 0, NULL, 0}
117 void
118 usage (int status)
120 if (status != EXIT_SUCCESS)
121 fprintf (stderr, _("Try `%s --help' for more information.\n"),
122 program_name);
123 else
125 printf (_("Usage: %s [OPTION]...\n"), program_name);
127 if (uname_mode == UNAME_UNAME)
129 fputs (_("\
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\
137 "), stdout);
138 fputs (_("\
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\
144 "), stdout);
146 else
148 fputs (_("\
149 Print machine architecture.\n\
151 "), stdout);
154 fputs (HELP_OPTION_DESCRIPTION, stdout);
155 fputs (VERSION_OPTION_DESCRIPTION, stdout);
156 emit_bug_reporting_address ();
158 exit (status);
161 /* Print ELEMENT, preceded by a space if something has already been
162 printed. */
164 static void
165 print_element (char const *element)
167 static bool printed;
168 if (printed)
169 putchar (' ');
170 printed = true;
171 fputs (element, stdout);
175 /* Set all the option flags according to the switches specified.
176 Return the mask indicating which elements to print. */
178 static int
179 decode_switches (int argc, char **argv)
181 int c;
182 unsigned int toprint = 0;
184 if (uname_mode == UNAME_ARCH)
186 while ((c = getopt_long (argc, argv, "",
187 arch_long_options, NULL)) != -1)
189 switch (c)
191 case_GETOPT_HELP_CHAR;
193 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS);
195 default:
196 usage (EXIT_FAILURE);
199 toprint = PRINT_MACHINE;
201 else
203 while ((c = getopt_long (argc, argv, "asnrvmpio",
204 uname_long_options, NULL)) != -1)
206 switch (c)
208 case 'a':
209 toprint = UINT_MAX;
210 break;
212 case 's':
213 toprint |= PRINT_KERNEL_NAME;
214 break;
216 case 'n':
217 toprint |= PRINT_NODENAME;
218 break;
220 case 'r':
221 toprint |= PRINT_KERNEL_RELEASE;
222 break;
224 case 'v':
225 toprint |= PRINT_KERNEL_VERSION;
226 break;
228 case 'm':
229 toprint |= PRINT_MACHINE;
230 break;
232 case 'p':
233 toprint |= PRINT_PROCESSOR;
234 break;
236 case 'i':
237 toprint |= PRINT_HARDWARE_PLATFORM;
238 break;
240 case 'o':
241 toprint |= PRINT_OPERATING_SYSTEM;
242 break;
244 case_GETOPT_HELP_CHAR;
246 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
248 default:
249 usage (EXIT_FAILURE);
254 if (argc != optind)
256 error (0, 0, _("extra operand %s"), quote (argv[optind]));
257 usage (EXIT_FAILURE);
260 return toprint;
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);
281 if (toprint == 0)
282 toprint = PRINT_KERNEL_NAME;
284 if (toprint
285 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
286 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
288 struct utsname name;
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))
312 element = processor;
314 #endif
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)
322 element = processor;
324 # ifdef __APPLE__
325 /* This kludge works around a bug in Mac OS X. */
326 if (element == unknown)
328 cpu_type_t cputype;
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))
334 != NULL)
335 element = ai->name;
337 /* Hack "safely" around the ppc vs. powerpc return value. */
338 if (cputype == CPU_TYPE_POWERPC
339 && strncmp (element, "ppc", 3) == 0)
340 element = "powerpc";
342 # endif
344 #endif
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;
359 #endif
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;
369 #endif
370 if (! (toprint == UINT_MAX && element == unknown))
371 print_element (element);
374 if (toprint & PRINT_OPERATING_SYSTEM)
375 print_element (HOST_OPERATING_SYSTEM);
377 putchar ('\n');
379 exit (EXIT_SUCCESS);