* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / uname.c
blob0715e07cd62e4379256dd306f24736141ada2b9d
1 /* uname -- print system information
3 Copyright (C) 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 except omit -p and -i if unknown:\n\
123 -s, --kernel-name print the kernel name\n\
124 -n, --nodename print the network node hostname\n\
125 -r, --kernel-release print the kernel release\n\
126 "), stdout);
127 fputs (_("\
128 -v, --kernel-version print the kernel version\n\
129 -m, --machine print the machine hardware name\n\
130 -p, --processor print the processor type or \"unknown\"\n\
131 -i, --hardware-platform print the hardware platform or \"unknown\"\n\
132 -o, --operating-system print the operating system\n\
133 "), stdout);
134 fputs (HELP_OPTION_DESCRIPTION, stdout);
135 fputs (VERSION_OPTION_DESCRIPTION, stdout);
136 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
138 exit (status);
141 /* Print ELEMENT, preceded by a space if something has already been
142 printed. */
144 static void
145 print_element (char const *element)
147 static bool printed;
148 if (printed)
149 putchar (' ');
150 printed = true;
151 fputs (element, stdout);
155 main (int argc, char **argv)
157 int c;
158 static char const unknown[] = "unknown";
160 /* Mask indicating which elements to print. */
161 unsigned int toprint = 0;
163 initialize_main (&argc, &argv);
164 program_name = argv[0];
165 setlocale (LC_ALL, "");
166 bindtextdomain (PACKAGE, LOCALEDIR);
167 textdomain (PACKAGE);
169 atexit (close_stdout);
171 while ((c = getopt_long (argc, argv, "asnrvmpio", long_options, NULL)) != -1)
173 switch (c)
175 case 'a':
176 toprint = UINT_MAX;
177 break;
179 case 's':
180 toprint |= PRINT_KERNEL_NAME;
181 break;
183 case 'n':
184 toprint |= PRINT_NODENAME;
185 break;
187 case 'r':
188 toprint |= PRINT_KERNEL_RELEASE;
189 break;
191 case 'v':
192 toprint |= PRINT_KERNEL_VERSION;
193 break;
195 case 'm':
196 toprint |= PRINT_MACHINE;
197 break;
199 case 'p':
200 toprint |= PRINT_PROCESSOR;
201 break;
203 case 'i':
204 toprint |= PRINT_HARDWARE_PLATFORM;
205 break;
207 case 'o':
208 toprint |= PRINT_OPERATING_SYSTEM;
209 break;
211 case_GETOPT_HELP_CHAR;
213 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
215 default:
216 usage (EXIT_FAILURE);
220 if (argc != optind)
222 error (0, 0, _("extra operand %s"), quote (argv[optind]));
223 usage (EXIT_FAILURE);
226 if (toprint == 0)
227 toprint = PRINT_KERNEL_NAME;
229 if (toprint
230 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
231 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
233 struct utsname name;
235 if (uname (&name) == -1)
236 error (EXIT_FAILURE, errno, _("cannot get system name"));
238 if (toprint & PRINT_KERNEL_NAME)
239 print_element (name.sysname);
240 if (toprint & PRINT_NODENAME)
241 print_element (name.nodename);
242 if (toprint & PRINT_KERNEL_RELEASE)
243 print_element (name.release);
244 if (toprint & PRINT_KERNEL_VERSION)
245 print_element (name.version);
246 if (toprint & PRINT_MACHINE)
247 print_element (name.machine);
250 if (toprint & PRINT_PROCESSOR)
252 char const *element = unknown;
253 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
255 static char processor[257];
256 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
257 element = processor;
259 #endif
260 #ifdef UNAME_PROCESSOR
261 if (element == unknown)
263 static char processor[257];
264 size_t s = sizeof processor;
265 static int mib[] = { CTL_HW, UNAME_PROCESSOR };
266 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
267 element = processor;
269 # ifdef __APPLE__
270 /* This kludge works around a bug in Mac OS X. */
271 if (element == unknown)
273 cpu_type_t cputype;
274 size_t s = sizeof cputype;
275 NXArchInfo const *ai;
276 if (sysctlbyname ("hw.cputype", &cputype, &s, NULL, 0) == 0
277 && (ai = NXGetArchInfoFromCpuType (cputype,
278 CPU_SUBTYPE_MULTIPLE))
279 != NULL)
280 element = ai->name;
282 /* Hack "safely" around the ppc vs. powerpc return value. */
283 if (cputype == CPU_TYPE_POWERPC
284 && strncmp (element, "ppc", 3) == 0)
285 element = "powerpc";
287 # endif
289 #endif
290 if (! (toprint == UINT_MAX && element == unknown))
291 print_element (element);
294 if (toprint & PRINT_HARDWARE_PLATFORM)
296 char const *element = unknown;
297 #if HAVE_SYSINFO && defined SI_PLATFORM
299 static char hardware_platform[257];
300 if (0 <= sysinfo (SI_PLATFORM,
301 hardware_platform, sizeof hardware_platform))
302 element = hardware_platform;
304 #endif
305 #ifdef UNAME_HARDWARE_PLATFORM
306 if (element == unknown)
308 static char hardware_platform[257];
309 size_t s = sizeof hardware_platform;
310 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
311 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
312 element = hardware_platform;
314 #endif
315 if (! (toprint == UINT_MAX && element == unknown))
316 print_element (element);
319 if (toprint & PRINT_OPERATING_SYSTEM)
320 print_element (HOST_OPERATING_SYSTEM);
322 putchar ('\n');
324 exit (EXIT_SUCCESS);