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)
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> */
24 #include <sys/types.h>
25 #include <sys/utsname.h>
28 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H
29 # include <sys/systeminfo.h>
34 # include <sys/param.h> /* needed for OpenBSD 3.0 */
36 # include <sys/sysctl.h>
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
43 /* E.g., OpenBSD 3.0 */
44 # define UNAME_PROCESSOR HW_MODEL
50 # include <mach/machine.h>
51 # include <mach-o/arch.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'. */
65 #define PRINT_KERNEL_NAME 1
67 /* Node name on a communications network. */
68 #define PRINT_NODENAME 2
71 #define PRINT_KERNEL_RELEASE 4
74 #define PRINT_KERNEL_VERSION 8
76 /* Machine hardware name. */
77 #define PRINT_MACHINE 16
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. */
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
},
112 if (status
!= EXIT_SUCCESS
)
113 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
117 printf (_("Usage: %s [OPTION]...\n"), program_name
);
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\
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\
134 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
135 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
136 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
141 /* Print ELEMENT, preceded by a space if something has already been
145 print_element (char const *element
)
151 fputs (element
, stdout
);
155 main (int argc
, char **argv
)
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)
180 toprint
|= PRINT_KERNEL_NAME
;
184 toprint
|= PRINT_NODENAME
;
188 toprint
|= PRINT_KERNEL_RELEASE
;
192 toprint
|= PRINT_KERNEL_VERSION
;
196 toprint
|= PRINT_MACHINE
;
200 toprint
|= PRINT_PROCESSOR
;
204 toprint
|= PRINT_HARDWARE_PLATFORM
;
208 toprint
|= PRINT_OPERATING_SYSTEM
;
211 case_GETOPT_HELP_CHAR
;
213 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
216 usage (EXIT_FAILURE
);
222 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
223 usage (EXIT_FAILURE
);
227 toprint
= PRINT_KERNEL_NAME
;
230 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
231 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
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
))
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)
270 /* This kludge works around a bug in Mac OS X. */
271 if (element
== unknown
)
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
))
282 /* Hack "safely" around the ppc vs. powerpc return value. */
283 if (cputype
== CPU_TYPE_POWERPC
284 && strncmp (element
, "ppc", 3) == 0)
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
;
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
;
315 if (! (toprint
== UINT_MAX
&& element
== unknown
))
316 print_element (element
);
319 if (toprint
& PRINT_OPERATING_SYSTEM
)
320 print_element (HOST_OPERATING_SYSTEM
);