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)
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
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'. */
58 #define PRINT_KERNEL_NAME 1
60 /* Node name on a communications network. */
61 #define PRINT_NODENAME 2
64 #define PRINT_KERNEL_RELEASE 4
67 #define PRINT_KERNEL_VERSION 8
69 /* Machine hardware name. */
70 #define PRINT_MACHINE 16
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. */
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
},
106 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
110 printf (_("Usage: %s [OPTION]...\n"), program_name
);
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\
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\
126 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
127 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
128 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
133 /* Print ELEMENT, preceded by a space if something has already been
137 print_element (char const *element
)
142 fputs (element
, stdout
);
146 main (int argc
, char **argv
)
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)
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
);
217 toprint
= PRINT_KERNEL_NAME
;
220 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
221 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
225 if (uname (&name
) == -1)
226 error (1, 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
))
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)
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
;
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
;
284 print_element (element
);
287 if (toprint
& PRINT_OPERATING_SYSTEM
)
288 print_element (HOST_OPERATING_SYSTEM
);