1 /* uname -- print system information
3 Copyright 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002, 2003 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)
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> */
24 #include <sys/types.h>
25 #include <sys/utsname.h>
28 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
29 # include <sys/systeminfo.h>
32 #if HAVE_SYSCTL && HAVE_SYS_SYSCTL_H
33 # include <sys/param.h> /* needed for OpenBSD 3.0 */
34 # include <sys/sysctl.h>
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
41 /* E.g., OpenBSD 3.0 */
42 # define UNAME_PROCESSOR HW_MODEL
50 /* The official name of this program (e.g., no `g' prefix). */
51 #define PROGRAM_NAME "uname"
53 #define AUTHORS "David MacKenzie"
55 /* Values that are bitwise or'd into `toprint'. */
57 #define PRINT_KERNEL_NAME 1
59 /* Node name on a communications network. */
60 #define PRINT_NODENAME 2
63 #define PRINT_KERNEL_RELEASE 4
66 #define PRINT_KERNEL_VERSION 8
68 /* Machine hardware name. */
69 #define PRINT_MACHINE 16
72 #define PRINT_PROCESSOR 32
74 /* Hardware platform. */
75 #define PRINT_HARDWARE_PLATFORM 64
77 /* Operating system. */
78 #define PRINT_OPERATING_SYSTEM 128
80 /* The name this program was run with, for error messages. */
83 static struct option const long_options[] =
85 {"all", no_argument, NULL, 'a'},
86 {"kernel-name", no_argument, NULL, 's'},
87 {"sysname", no_argument, NULL, 's'}, /* Obsolescent. */
88 {"nodename", no_argument, NULL, 'n'},
89 {"kernel-release", no_argument, NULL, 'r'},
90 {"release", no_argument, NULL, 'r'}, /* Obsolescent. */
91 {"kernel-version", no_argument, NULL, 'v'},
92 {"machine", no_argument, NULL, 'm'},
93 {"processor", no_argument, NULL, 'p'},
94 {"hardware-platform", no_argument, NULL, 'i'},
95 {"operating-system", no_argument, NULL, 'o'},
96 {GETOPT_HELP_OPTION_DECL},
97 {GETOPT_VERSION_OPTION_DECL},
105 fprintf (stderr, _("Try `%s --help' for more information.\n"),
109 printf (_("Usage: %s [OPTION]...\n"), program_name);
111 Print certain system information. With no OPTION, same as -s.\n\
113 -a, --all print all information, in the following order:\n\
114 -s, --kernel-name print the kernel name\n\
115 -n, --nodename print the network node hostname\n\
116 -r, --kernel-release print the kernel release\n\
119 -v, --kernel-version print the kernel version\n\
120 -m, --machine print the machine hardware name\n\
121 -p, --processor print the processor type\n\
122 -i, --hardware-platform print the hardware platform\n\
123 -o, --operating-system print the operating system\n\
125 fputs (HELP_OPTION_DESCRIPTION, stdout);
126 fputs (VERSION_OPTION_DESCRIPTION, stdout);
127 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
132 /* Print ELEMENT, preceded by a space if something has already been
136 print_element (char const *element)
141 fputs (element, stdout);
145 main (int argc, char **argv)
148 static char const unknown[] = "unknown";
150 /* Mask indicating which elements to print. */
151 unsigned toprint = 0;
153 initialize_main (&argc, &argv);
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)
173 toprint |= PRINT_KERNEL_NAME;
177 toprint |= PRINT_NODENAME;
181 toprint |= PRINT_KERNEL_RELEASE;
185 toprint |= PRINT_KERNEL_VERSION;
189 toprint |= PRINT_MACHINE;
193 toprint |= PRINT_PROCESSOR;
197 toprint |= PRINT_HARDWARE_PLATFORM;
201 toprint |= PRINT_OPERATING_SYSTEM;
204 case_GETOPT_HELP_CHAR;
206 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
209 usage (EXIT_FAILURE);
215 error (0, 0, _("too many arguments"));
216 usage (EXIT_FAILURE);
220 toprint = PRINT_KERNEL_NAME;
223 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
224 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
228 if (uname (&name) == -1)
229 error (EXIT_FAILURE, errno, _("cannot get system name"));
231 if (toprint & PRINT_KERNEL_NAME)
232 print_element (name.sysname);
233 if (toprint & PRINT_NODENAME)
234 print_element (name.nodename);
235 if (toprint & PRINT_KERNEL_RELEASE)
236 print_element (name.release);
237 if (toprint & PRINT_KERNEL_VERSION)
238 print_element (name.version);
239 if (toprint & PRINT_MACHINE)
240 print_element (name.machine);
243 if (toprint & PRINT_PROCESSOR)
245 char const *element = unknown;
246 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
248 static char processor[257];
249 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
253 #ifdef UNAME_PROCESSOR
254 if (element == unknown)
256 static char processor[257];
257 size_t s = sizeof processor;
258 static int mib[] = { CTL_HW, UNAME_PROCESSOR };
259 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)
263 print_element (element);
266 if (toprint & PRINT_HARDWARE_PLATFORM)
268 char const *element = unknown;
269 #if HAVE_SYSINFO && defined SI_PLATFORM
271 static char hardware_platform[257];
272 if (0 <= sysinfo (SI_PLATFORM,
273 hardware_platform, sizeof hardware_platform))
274 element = hardware_platform;
277 #ifdef UNAME_HARDWARE_PLATFORM
278 if (element == unknown)
280 static char hardware_platform[257];
281 size_t s = sizeof hardware_platform;
282 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
283 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
284 element = hardware_platform;
287 print_element (element);
290 if (toprint & PRINT_OPERATING_SYSTEM)
291 print_element (HOST_OPERATING_SYSTEM);