#undef getcwd before including system headers.
[coreutils.git] / src / uname.c
blobad487e334a7b2e2c7f8215494606dd08c5e4c012
1 /* uname -- print system information
3 Copyright 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002 Free
4 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_SYSCTL && HAVE_SYS_SYSCTL_H
33 # include <sys/param.h> /* needed for OpenBSD 3.0 */
34 # include <sys/sysctl.h>
35 # ifdef HW_MODEL
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
40 # else
41 /* E.g., OpenBSD 3.0 */
42 # define UNAME_PROCESSOR HW_MODEL
43 # endif
44 # endif
45 #endif
47 #include "system.h"
48 #include "error.h"
49 #include "closeout.h"
51 /* The official name of this program (e.g., no `g' prefix). */
52 #define PROGRAM_NAME "uname"
54 #define AUTHORS "David MacKenzie"
56 /* Values that are bitwise or'd into `toprint'. */
57 /* Kernel name. */
58 #define PRINT_KERNEL_NAME 1
60 /* Node name on a communications network. */
61 #define PRINT_NODENAME 2
63 /* Kernel release. */
64 #define PRINT_KERNEL_RELEASE 4
66 /* Kernel version. */
67 #define PRINT_KERNEL_VERSION 8
69 /* Machine hardware name. */
70 #define PRINT_MACHINE 16
72 /* Processor type. */
73 #define PRINT_PROCESSOR 32
75 /* Hardware platform. */
76 #define PRINT_HARDWARE_PLATFORM 64
78 /* Operating system. */
79 #define PRINT_OPERATING_SYSTEM 128
81 /* The name this program was run with, for error messages. */
82 char *program_name;
84 static struct option const long_options[] =
86 {"all", no_argument, NULL, 'a'},
87 {"kernel-name", no_argument, NULL, 's'},
88 {"sysname", no_argument, NULL, 's'}, /* Obsolescent. */
89 {"nodename", no_argument, NULL, 'n'},
90 {"kernel-release", no_argument, NULL, 'r'},
91 {"release", no_argument, NULL, 'r'}, /* Obsolescent. */
92 {"kernel-version", no_argument, NULL, 'v'},
93 {"machine", no_argument, NULL, 'm'},
94 {"processor", no_argument, NULL, 'p'},
95 {"hardware-platform", no_argument, NULL, 'i'},
96 {"operating-system", no_argument, NULL, 'o'},
97 {GETOPT_HELP_OPTION_DECL},
98 {GETOPT_VERSION_OPTION_DECL},
99 {NULL, 0, NULL, 0}
102 void
103 usage (int status)
105 if (status != 0)
106 fprintf (stderr, _("Try `%s --help' for more information.\n"),
107 program_name);
108 else
110 printf (_("Usage: %s [OPTION]...\n"), program_name);
111 fputs (_("\
112 Print certain system information. With no OPTION, same as -s.\n\
114 -a, --all print all information, in the following order:\n\
115 -s, --kernel-name print the kernel name\n\
116 -n, --nodename print the network node hostname\n\
117 -r, --kernel-release print the kernel release\n\
118 "), stdout);
119 fputs (_("\
120 -v, --kernel-version print the kernel version\n\
121 -m, --machine print the machine hardware name\n\
122 -p, --processor print the processor type\n\
123 -i, --hardware-platform print the hardware platform\n\
124 -o, --operating-system print the operating system\n\
125 "), stdout);
126 fputs (HELP_OPTION_DESCRIPTION, stdout);
127 fputs (VERSION_OPTION_DESCRIPTION, stdout);
128 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
130 exit (status);
133 /* Print ELEMENT, preceded by a space if something has already been
134 printed. */
136 static void
137 print_element (char const *element)
139 static int printed;
140 if (printed++)
141 putchar (' ');
142 fputs (element, stdout);
146 main (int argc, char **argv)
148 int c;
149 static char const unknown[] = "unknown";
151 /* Mask indicating which elements to print. */
152 unsigned toprint = 0;
154 program_name = argv[0];
155 setlocale (LC_ALL, "");
156 bindtextdomain (PACKAGE, LOCALEDIR);
157 textdomain (PACKAGE);
159 atexit (close_stdout);
161 while ((c = getopt_long (argc, argv, "asnrvmpio", long_options, NULL)) != -1)
163 switch (c)
165 case 0:
166 break;
168 case 'a':
169 toprint = -1;
170 break;
172 case 's':
173 toprint |= PRINT_KERNEL_NAME;
174 break;
176 case 'n':
177 toprint |= PRINT_NODENAME;
178 break;
180 case 'r':
181 toprint |= PRINT_KERNEL_RELEASE;
182 break;
184 case 'v':
185 toprint |= PRINT_KERNEL_VERSION;
186 break;
188 case 'm':
189 toprint |= PRINT_MACHINE;
190 break;
192 case 'p':
193 toprint |= PRINT_PROCESSOR;
194 break;
196 case 'i':
197 toprint |= PRINT_HARDWARE_PLATFORM;
198 break;
200 case 'o':
201 toprint |= PRINT_OPERATING_SYSTEM;
202 break;
204 case_GETOPT_HELP_CHAR;
206 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
208 default:
209 usage (EXIT_FAILURE);
213 if (optind != argc)
214 usage (EXIT_FAILURE);
216 if (toprint == 0)
217 toprint = PRINT_KERNEL_NAME;
219 if (toprint
220 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
221 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
223 struct utsname name;
225 if (uname (&name) == -1)
226 error (EXIT_FAILURE, errno, _("cannot get system name"));
228 if (toprint & PRINT_KERNEL_NAME)
229 print_element (name.sysname);
230 if (toprint & PRINT_NODENAME)
231 print_element (name.nodename);
232 if (toprint & PRINT_KERNEL_RELEASE)
233 print_element (name.release);
234 if (toprint & PRINT_KERNEL_VERSION)
235 print_element (name.version);
236 if (toprint & PRINT_MACHINE)
237 print_element (name.machine);
240 if (toprint & PRINT_PROCESSOR)
242 char const *element = unknown;
243 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
245 static char processor[257];
246 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
247 element = processor;
249 #endif
250 #ifdef UNAME_PROCESSOR
251 if (element == unknown)
253 static char processor[257];
254 size_t s = sizeof processor;
255 static int mib[] = { CTL_HW, UNAME_PROCESSOR };
256 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
257 element = processor;
259 #endif
260 print_element (element);
263 if (toprint & PRINT_HARDWARE_PLATFORM)
265 char const *element = unknown;
266 #if HAVE_SYSINFO && defined SI_PLATFORM
268 static char hardware_platform[257];
269 if (0 <= sysinfo (SI_PLATFORM,
270 hardware_platform, sizeof hardware_platform))
271 element = hardware_platform;
273 #endif
274 #ifdef UNAME_HARDWARE_PLATFORM
275 if (element == unknown)
277 static char hardware_platform[257];
278 size_t s = sizeof hardware_platform;
279 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
280 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
281 element = hardware_platform;
283 #endif
284 print_element (element);
287 if (toprint & PRINT_OPERATING_SYSTEM)
288 print_element (HOST_OPERATING_SYSTEM);
290 putchar ('\n');
292 exit (EXIT_SUCCESS);