Avoid spurious failure on x86 solaris2.9 when using c89.
[coreutils.git] / src / uname.c
blobffdc44a3e8ab70e047b5f81f640e4da10bb16d12
1 /* uname -- print system information
3 Copyright 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002,
4 2003, 2004 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 2, or (at your option)
9 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, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
22 #include <config.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/utsname.h>
26 #include <getopt.h>
28 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
29 # include <sys/systeminfo.h>
30 #endif
32 #if HAVE_SYS_SYSCTL_H
33 # if HAVE_SYS_PARAM_H
34 # include <sys/param.h> /* needed for OpenBSD 3.0 */
35 # endif
36 # include <sys/sysctl.h>
37 # ifdef HW_MODEL
38 # ifdef HW_MACHINE_ARCH
39 /* E.g., FreeBSD 4.5, NetBSD 1.5.2 */
40 # define UNAME_HARDWARE_PLATFORM HW_MODEL
41 # define UNAME_PROCESSOR HW_MACHINE_ARCH
42 # else
43 /* E.g., OpenBSD 3.0 */
44 # define UNAME_PROCESSOR HW_MODEL
45 # endif
46 # endif
47 #endif
49 #ifdef __APPLE__
50 #include <mach/machine.h>
51 #include <mach-o/arch.h>
52 #endif
54 #include "system.h"
55 #include "error.h"
56 #include "quote.h"
58 /* The official name of this program (e.g., no `g' prefix). */
59 #define PROGRAM_NAME "uname"
61 #define AUTHORS "David MacKenzie"
63 /* Values that are bitwise or'd into `toprint'. */
64 /* Kernel name. */
65 #define PRINT_KERNEL_NAME 1
67 /* Node name on a communications network. */
68 #define PRINT_NODENAME 2
70 /* Kernel release. */
71 #define PRINT_KERNEL_RELEASE 4
73 /* Kernel version. */
74 #define PRINT_KERNEL_VERSION 8
76 /* Machine hardware name. */
77 #define PRINT_MACHINE 16
79 /* Processor type. */
80 #define PRINT_PROCESSOR 32
82 /* Hardware platform. */
83 #define PRINT_HARDWARE_PLATFORM 64
85 /* Operating system. */
86 #define PRINT_OPERATING_SYSTEM 128
88 /* The name this program was run with, for error messages. */
89 char *program_name;
91 static struct option const long_options[] =
93 {"all", no_argument, NULL, 'a'},
94 {"kernel-name", no_argument, NULL, 's'},
95 {"sysname", no_argument, NULL, 's'}, /* Obsolescent. */
96 {"nodename", no_argument, NULL, 'n'},
97 {"kernel-release", no_argument, NULL, 'r'},
98 {"release", no_argument, NULL, 'r'}, /* Obsolescent. */
99 {"kernel-version", no_argument, NULL, 'v'},
100 {"machine", no_argument, NULL, 'm'},
101 {"processor", no_argument, NULL, 'p'},
102 {"hardware-platform", no_argument, NULL, 'i'},
103 {"operating-system", no_argument, NULL, 'o'},
104 {GETOPT_HELP_OPTION_DECL},
105 {GETOPT_VERSION_OPTION_DECL},
106 {NULL, 0, NULL, 0}
109 void
110 usage (int status)
112 if (status != EXIT_SUCCESS)
113 fprintf (stderr, _("Try `%s --help' for more information.\n"),
114 program_name);
115 else
117 printf (_("Usage: %s [OPTION]...\n"), program_name);
118 fputs (_("\
119 Print certain system information. With no OPTION, same as -s.\n\
121 -a, --all print all information, in the following order:\n\
122 -s, --kernel-name print the kernel name\n\
123 -n, --nodename print the network node hostname\n\
124 -r, --kernel-release print the kernel release\n\
125 "), stdout);
126 fputs (_("\
127 -v, --kernel-version print the kernel version\n\
128 -m, --machine print the machine hardware name\n\
129 -p, --processor print the processor type\n\
130 -i, --hardware-platform print the hardware platform\n\
131 -o, --operating-system print the operating system\n\
132 "), stdout);
133 fputs (HELP_OPTION_DESCRIPTION, stdout);
134 fputs (VERSION_OPTION_DESCRIPTION, stdout);
135 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
137 exit (status);
140 /* Print ELEMENT, preceded by a space if something has already been
141 printed. */
143 static void
144 print_element (char const *element)
146 static bool printed;
147 if (printed)
148 putchar (' ');
149 printed = true;
150 fputs (element, stdout);
154 main (int argc, char **argv)
156 int c;
157 static char const unknown[] = "unknown";
159 /* Mask indicating which elements to print. */
160 unsigned int toprint = 0;
162 initialize_main (&argc, &argv);
163 program_name = argv[0];
164 setlocale (LC_ALL, "");
165 bindtextdomain (PACKAGE, LOCALEDIR);
166 textdomain (PACKAGE);
168 atexit (close_stdout);
170 while ((c = getopt_long (argc, argv, "asnrvmpio", long_options, NULL)) != -1)
172 switch (c)
174 case 'a':
175 toprint = -1;
176 break;
178 case 's':
179 toprint |= PRINT_KERNEL_NAME;
180 break;
182 case 'n':
183 toprint |= PRINT_NODENAME;
184 break;
186 case 'r':
187 toprint |= PRINT_KERNEL_RELEASE;
188 break;
190 case 'v':
191 toprint |= PRINT_KERNEL_VERSION;
192 break;
194 case 'm':
195 toprint |= PRINT_MACHINE;
196 break;
198 case 'p':
199 toprint |= PRINT_PROCESSOR;
200 break;
202 case 'i':
203 toprint |= PRINT_HARDWARE_PLATFORM;
204 break;
206 case 'o':
207 toprint |= PRINT_OPERATING_SYSTEM;
208 break;
210 case_GETOPT_HELP_CHAR;
212 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
214 default:
215 usage (EXIT_FAILURE);
219 if (argc != optind)
221 error (0, 0, _("extra operand %s"), quote (argv[optind]));
222 usage (EXIT_FAILURE);
225 if (toprint == 0)
226 toprint = PRINT_KERNEL_NAME;
228 if (toprint
229 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
230 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
232 struct utsname name;
234 if (uname (&name) == -1)
235 error (EXIT_FAILURE, errno, _("cannot get system name"));
237 if (toprint & PRINT_KERNEL_NAME)
238 print_element (name.sysname);
239 if (toprint & PRINT_NODENAME)
240 print_element (name.nodename);
241 if (toprint & PRINT_KERNEL_RELEASE)
242 print_element (name.release);
243 if (toprint & PRINT_KERNEL_VERSION)
244 print_element (name.version);
245 if (toprint & PRINT_MACHINE)
246 print_element (name.machine);
249 if (toprint & PRINT_PROCESSOR)
251 char const *element = unknown;
252 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
254 static char processor[257];
255 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
256 element = processor;
258 #endif
259 #ifdef UNAME_PROCESSOR
260 if (element == unknown)
262 static char processor[257];
263 size_t s = sizeof processor;
264 static int mib[] = { CTL_HW, UNAME_PROCESSOR };
265 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
266 element = processor;
268 # ifdef __APPLE__
269 /* This kludge works around a bug in Mac OS X. */
270 if (element == unknown)
272 cpu_type_t cputype;
273 size_t s = sizeof cputype;
274 NXArchInfo const *ai;
275 if (sysctlbyname ("hw.cputype", &cputype, &s, NULL, 0) == 0
276 && (ai = NXGetArchInfoFromCpuType (cputype,
277 CPU_SUBTYPE_MULTIPLE))
278 != NULL)
279 element = ai->name;
281 /* Hack "safely" around the ppc vs. powerpc return value. */
282 if (cputype == CPU_TYPE_POWERPC
283 && strncmp (element, "ppc", 3) == 0)
284 element = "powerpc";
286 # endif
288 #endif
289 print_element (element);
292 if (toprint & PRINT_HARDWARE_PLATFORM)
294 char const *element = unknown;
295 #if HAVE_SYSINFO && defined SI_PLATFORM
297 static char hardware_platform[257];
298 if (0 <= sysinfo (SI_PLATFORM,
299 hardware_platform, sizeof hardware_platform))
300 element = hardware_platform;
302 #endif
303 #ifdef UNAME_HARDWARE_PLATFORM
304 if (element == unknown)
306 static char hardware_platform[257];
307 size_t s = sizeof hardware_platform;
308 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
309 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
310 element = hardware_platform;
312 #endif
313 print_element (element);
316 if (toprint & PRINT_OPERATING_SYSTEM)
317 print_element (HOST_OPERATING_SYSTEM);
319 putchar ('\n');
321 exit (EXIT_SUCCESS);