blob: 9822e49bd99a87bfa915267e20b1d337f043763f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc8fdb561999-10-26 05:21:02 +00002/* uname -- print system information
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Copyright (C) 1989-1999 Free Software Foundation, Inc.
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
Eric Andersenc8fdb561999-10-26 05:21:02 +00007
Manuel Novoa III cad53642003-03-19 09:13:01 +00008/* BB_AUDIT SUSv3 compliant */
9/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
10
Eric Andersenc8fdb561999-10-26 05:21:02 +000011/* Option Example
Denis Vlasenko059138f2009-01-19 16:32:23 +000012 * -s, --sysname SunOS
13 * -n, --nodename rocky8
14 * -r, --release 4.0
15 * -v, --version
16 * -m, --machine sun
17 * -a, --all SunOS rocky8 4.0 sun
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 *
Denis Vlasenko059138f2009-01-19 16:32:23 +000019 * The default behavior is equivalent to '-s'.
20 *
21 * David MacKenzie <djm@gnu.ai.mit.edu>
22 *
23 * GNU coreutils 6.10:
24 * Option: struct Example(s):
25 * utsname
26 * field:
27 * -s, --kernel-name sysname Linux
28 * -n, --nodename nodename localhost.localdomain
29 * -r, --kernel-release release 2.6.29
30 * -v, --kernel-version version #1 SMP Sun Jan 11 20:52:37 EST 2009
31 * -m, --machine machine x86_64 i686
32 * -p, --processor (none) x86_64 i686
33 * -i, --hardware-platform (none) x86_64 i386
34 * NB: vanilla coreutils reports "unknown" -p and -i,
35 * x86_64 and i686/i386 shown above are Fedora's inventions.
36 * -o, --operating-system (none) GNU/Linux
37 * -a, --all: all of the above, in the order shown.
38 * If -p or -i is not known, don't show them
39 */
40
41/* Busyboxed by Erik Andersen
42 *
43 * Before 2003: Glenn McGrath and Manuel Novoa III
44 * Further size reductions.
45 * Mar 16, 2003: Manuel Novoa III (mjn3@codepoet.org)
46 * Now does proper error checking on i/o. Plus some further space savings.
47 * Jan 2009:
48 * Fix handling of -a to not print "unknown", add -o and -i support.
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 */
50
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000051#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020052/* After libbb.h, since it needs sys/types.h on some systems */
53#include <sys/utsname.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +000054
Manuel Novoa III 6509f922001-12-05 04:21:30 +000055typedef struct {
56 struct utsname name;
Denis Vlasenko059138f2009-01-19 16:32:23 +000057 char processor[sizeof(((struct utsname*)NULL)->machine)];
58 char platform[sizeof(((struct utsname*)NULL)->machine)];
59 char os[sizeof("GNU/Linux")];
Manuel Novoa III 6509f922001-12-05 04:21:30 +000060} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +000061
Denis Vlasenko059138f2009-01-19 16:32:23 +000062static const char options[] ALIGN1 = "snrvmpioa";
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000063static const unsigned short utsname_offset[] = {
Denis Vlasenko059138f2009-01-19 16:32:23 +000064 offsetof(uname_info_t, name.sysname), /* -s */
65 offsetof(uname_info_t, name.nodename), /* -n */
66 offsetof(uname_info_t, name.release), /* -r */
67 offsetof(uname_info_t, name.version), /* -v */
68 offsetof(uname_info_t, name.machine), /* -m */
69 offsetof(uname_info_t, processor), /* -p */
70 offsetof(uname_info_t, platform), /* -i */
71 offsetof(uname_info_t, os), /* -o */
Manuel Novoa III 6509f922001-12-05 04:21:30 +000072};
Eric Andersenc8fdb561999-10-26 05:21:02 +000073
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000074int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000075int uname_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc8fdb561999-10-26 05:21:02 +000076{
Denys Vlasenkof3b92d32009-06-19 12:10:38 +020077#if ENABLE_LONG_OPTS
Denys Vlasenko7aca89a2009-07-18 03:41:29 +020078 static const char uname_longopts[] ALIGN1 =
Denis Vlasenko447ab182009-03-14 18:41:19 +000079 /* name, has_arg, val */
80 "all\0" No_argument "a"
81 "kernel-name\0" No_argument "s"
82 "nodename\0" No_argument "n"
83 "kernel-release\0" No_argument "r"
84 "release\0" No_argument "r"
85 "kernel-version\0" No_argument "v"
86 "machine\0" No_argument "m"
87 "processor\0" No_argument "p"
88 "hardware-platform\0" No_argument "i"
89 "operating-system\0" No_argument "o"
90 ;
91#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +000092 uname_info_t uname_info;
Eric Andersenc8fdb561999-10-26 05:21:02 +000093#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000095#endif
Denis Vlasenko059138f2009-01-19 16:32:23 +000096 const char *unknown_str = "unknown";
97 const char *fmt;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000098 const unsigned short *delta;
Denis Vlasenko038eec72009-01-19 16:38:30 +000099 unsigned toprint;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000100
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200101 IF_LONG_OPTS(applet_long_options = uname_longopts);
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000102 toprint = getopt32(argv, options);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000104 if (argv[optind]) { /* coreutils-6.9 compat */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_show_usage();
106 }
107
Denis Vlasenko059138f2009-01-19 16:32:23 +0000108 if (toprint & (1 << 8)) { /* -a => all opts on */
109 toprint = (1 << 8) - 1;
110 unknown_str = ""; /* -a does not print unknown fields */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000111 }
112
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000113 if (toprint == 0) { /* no opts => -s (sysname) */
114 toprint = 1;
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000115 }
116
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000117 uname(&uname_info.name); /* never fails */
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000118
119#if defined(__sparc__) && defined(__linux__)
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000120 if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000121 strcpy(uname_info.name.machine, "sparc");
122 }
123#endif
Denis Vlasenko059138f2009-01-19 16:32:23 +0000124 strcpy(uname_info.processor, unknown_str);
125 strcpy(uname_info.platform, unknown_str);
126 strcpy(uname_info.os, "GNU/Linux");
127#if 0
128 /* Fedora does something like this */
129 strcpy(uname_info.processor, uname_info.name.machine);
130 strcpy(uname_info.platform, uname_info.name.machine);
131 if (uname_info.platform[0] == 'i'
132 && uname_info.platform[1]
133 && uname_info.platform[2] == '8'
134 && uname_info.platform[3] == '6'
135 ) {
136 uname_info.platform[1] = '3';
137 }
138#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000139
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000140 delta = utsname_offset;
Denis Vlasenko059138f2009-01-19 16:32:23 +0000141 fmt = " %s" + 1;
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000142 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000143 if (toprint & 1) {
Denis Vlasenko059138f2009-01-19 16:32:23 +0000144 const char *p = (char *)(&uname_info) + *delta;
145 if (p[0]) {
146 printf(fmt, p);
147 fmt = " %s";
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000148 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000150 ++delta;
151 } while (toprint >>= 1);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000152 bb_putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +0000153
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000154 fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
Eric Andersenc8fdb561999-10-26 05:21:02 +0000155}