1 /* uname -- print system information
3 Copyright 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002,
4 2003, 2004 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., 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>
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 -s, --kernel-name print the kernel name\n\
123 -n, --nodename print the network node hostname\n\
124 -r, --kernel-release print the kernel release\n\
127 -v, --kernel-version print the kernel version\n\
128 -m, --machine print the machine hardware name\n\
129 -p, --processor print the processor type\n\
130 -i, --hardware-platform print the hardware platform\n\
131 -o, --operating-system print the operating system\n\
133 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
134 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
135 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
140 /* Print ELEMENT, preceded by a space if something has already been
144 print_element (char const *element
)
150 fputs (element
, stdout
);
154 main (int argc
, char **argv
)
157 static char const unknown
[] = "unknown";
159 /* Mask indicating which elements to print. */
160 unsigned int toprint
= 0;
162 initialize_main (&argc
, &argv
);
163 program_name
= argv
[0];
164 setlocale (LC_ALL
, "");
165 bindtextdomain (PACKAGE
, LOCALEDIR
);
166 textdomain (PACKAGE
);
168 atexit (close_stdout
);
170 while ((c
= getopt_long (argc
, argv
, "asnrvmpio", long_options
, NULL
)) != -1)
179 toprint
|= PRINT_KERNEL_NAME
;
183 toprint
|= PRINT_NODENAME
;
187 toprint
|= PRINT_KERNEL_RELEASE
;
191 toprint
|= PRINT_KERNEL_VERSION
;
195 toprint
|= PRINT_MACHINE
;
199 toprint
|= PRINT_PROCESSOR
;
203 toprint
|= PRINT_HARDWARE_PLATFORM
;
207 toprint
|= PRINT_OPERATING_SYSTEM
;
210 case_GETOPT_HELP_CHAR
;
212 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
215 usage (EXIT_FAILURE
);
221 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
222 usage (EXIT_FAILURE
);
226 toprint
= PRINT_KERNEL_NAME
;
229 & (PRINT_KERNEL_NAME
| PRINT_NODENAME
| PRINT_KERNEL_RELEASE
230 | PRINT_KERNEL_VERSION
| PRINT_MACHINE
))
234 if (uname (&name
) == -1)
235 error (EXIT_FAILURE
, errno
, _("cannot get system name"));
237 if (toprint
& PRINT_KERNEL_NAME
)
238 print_element (name
.sysname
);
239 if (toprint
& PRINT_NODENAME
)
240 print_element (name
.nodename
);
241 if (toprint
& PRINT_KERNEL_RELEASE
)
242 print_element (name
.release
);
243 if (toprint
& PRINT_KERNEL_VERSION
)
244 print_element (name
.version
);
245 if (toprint
& PRINT_MACHINE
)
246 print_element (name
.machine
);
249 if (toprint
& PRINT_PROCESSOR
)
251 char const *element
= unknown
;
252 #if HAVE_SYSINFO && defined SI_ARCHITECTURE
254 static char processor
[257];
255 if (0 <= sysinfo (SI_ARCHITECTURE
, processor
, sizeof processor
))
259 #ifdef UNAME_PROCESSOR
260 if (element
== unknown
)
262 static char processor
[257];
263 size_t s
= sizeof processor
;
264 static int mib
[] = { CTL_HW
, UNAME_PROCESSOR
};
265 if (sysctl (mib
, 2, processor
, &s
, 0, 0) >= 0)
269 /* This kludge works around a bug in Mac OS X. */
270 if (element
== unknown
)
273 size_t s
= sizeof cputype
;
274 NXArchInfo
const *ai
;
275 if (sysctlbyname ("hw.cputype", &cputype
, &s
, NULL
, 0) == 0
276 && (ai
= NXGetArchInfoFromCpuType (cputype
,
277 CPU_SUBTYPE_MULTIPLE
))
281 /* Hack "safely" around the ppc vs. powerpc return value. */
282 if (cputype
== CPU_TYPE_POWERPC
283 && strncmp (element
, "ppc", 3) == 0)
289 print_element (element
);
292 if (toprint
& PRINT_HARDWARE_PLATFORM
)
294 char const *element
= unknown
;
295 #if HAVE_SYSINFO && defined SI_PLATFORM
297 static char hardware_platform
[257];
298 if (0 <= sysinfo (SI_PLATFORM
,
299 hardware_platform
, sizeof hardware_platform
))
300 element
= hardware_platform
;
303 #ifdef UNAME_HARDWARE_PLATFORM
304 if (element
== unknown
)
306 static char hardware_platform
[257];
307 size_t s
= sizeof hardware_platform
;
308 static int mib
[] = { CTL_HW
, UNAME_HARDWARE_PLATFORM
};
309 if (sysctl (mib
, 2, hardware_platform
, &s
, 0, 0) >= 0)
310 element
= hardware_platform
;
313 print_element (element
);
316 if (toprint
& PRINT_OPERATING_SYSTEM
)
317 print_element (HOST_OPERATING_SYSTEM
);