9 |
/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
10 |
|
|
11 |
/* Option Example |
/* Option Example |
12 |
|
* -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 |
18 |
|
* |
19 |
|
* 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 |
-s, --sysname SunOS |
/* Busyboxed by Erik Andersen |
|
-n, --nodename rocky8 |
|
|
-r, --release 4.0 |
|
|
-v, --version |
|
|
-m, --machine sun |
|
|
-a, --all SunOS rocky8 4.0 sun |
|
|
|
|
|
The default behavior is equivalent to '-s'. |
|
|
|
|
|
David MacKenzie <djm@gnu.ai.mit.edu> */ |
|
|
|
|
|
/* Busyboxed by Erik Andersen */ |
|
|
|
|
|
/* Further size reductions by Glenn McGrath and Manuel Novoa III. */ |
|
|
|
|
|
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
|
42 |
* |
* |
43 |
* Now does proper error checking on i/o. Plus some further space savings. |
* 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. |
49 |
*/ |
*/ |
50 |
|
|
|
#include <sys/utsname.h> |
|
51 |
#include "libbb.h" |
#include "libbb.h" |
52 |
|
/* After libbb.h, since it needs sys/types.h on some systems */ |
53 |
|
#include <sys/utsname.h> |
54 |
|
|
55 |
typedef struct { |
typedef struct { |
56 |
struct utsname name; |
struct utsname name; |
57 |
char processor[8]; /* for "unknown" */ |
char processor[sizeof(((struct utsname*)NULL)->machine)]; |
58 |
|
char platform[sizeof(((struct utsname*)NULL)->machine)]; |
59 |
|
char os[sizeof("GNU/Linux")]; |
60 |
} uname_info_t; |
} uname_info_t; |
61 |
|
|
62 |
static const char options[] ALIGN1 = "snrvmpa"; |
static const char options[] ALIGN1 = "snrvmpioa"; |
63 |
static const unsigned short utsname_offset[] = { |
static const unsigned short utsname_offset[] = { |
64 |
offsetof(uname_info_t, name.sysname), |
offsetof(uname_info_t, name.sysname), /* -s */ |
65 |
offsetof(uname_info_t, name.nodename), |
offsetof(uname_info_t, name.nodename), /* -n */ |
66 |
offsetof(uname_info_t, name.release), |
offsetof(uname_info_t, name.release), /* -r */ |
67 |
offsetof(uname_info_t, name.version), |
offsetof(uname_info_t, name.version), /* -v */ |
68 |
offsetof(uname_info_t, name.machine), |
offsetof(uname_info_t, name.machine), /* -m */ |
69 |
offsetof(uname_info_t, processor) |
offsetof(uname_info_t, processor), /* -p */ |
70 |
|
offsetof(uname_info_t, platform), /* -i */ |
71 |
|
offsetof(uname_info_t, os), /* -o */ |
72 |
}; |
}; |
73 |
|
|
74 |
int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
75 |
int uname_main(int argc UNUSED_PARAM, char **argv) |
int uname_main(int argc UNUSED_PARAM, char **argv) |
76 |
{ |
{ |
77 |
|
#if ENABLE_LONG_OPTS |
78 |
|
static const char uname_longopts[] ALIGN1 = |
79 |
|
/* 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 |
92 |
uname_info_t uname_info; |
uname_info_t uname_info; |
93 |
#if defined(__sparc__) && defined(__linux__) |
#if defined(__sparc__) && defined(__linux__) |
94 |
char *fake_sparc = getenv("FAKE_SPARC"); |
char *fake_sparc = getenv("FAKE_SPARC"); |
95 |
#endif |
#endif |
96 |
|
const char *unknown_str = "unknown"; |
97 |
|
const char *fmt; |
98 |
const unsigned short *delta; |
const unsigned short *delta; |
99 |
char toprint; |
unsigned toprint; |
100 |
|
|
101 |
|
IF_LONG_OPTS(applet_long_options = uname_longopts); |
102 |
toprint = getopt32(argv, options); |
toprint = getopt32(argv, options); |
103 |
|
|
104 |
if (argv[optind]) { /* coreutils-6.9 compat */ |
if (argv[optind]) { /* coreutils-6.9 compat */ |
105 |
bb_show_usage(); |
bb_show_usage(); |
106 |
} |
} |
107 |
|
|
108 |
if (toprint & (1 << 6)) { /* -a => all opts on */ |
if (toprint & (1 << 8)) { /* -a => all opts on */ |
109 |
toprint = 0x3f; |
toprint = (1 << 8) - 1; |
110 |
|
unknown_str = ""; /* -a does not print unknown fields */ |
111 |
} |
} |
112 |
|
|
113 |
if (toprint == 0) { /* no opts => -s (sysname) */ |
if (toprint == 0) { /* no opts => -s (sysname) */ |
121 |
strcpy(uname_info.name.machine, "sparc"); |
strcpy(uname_info.name.machine, "sparc"); |
122 |
} |
} |
123 |
#endif |
#endif |
124 |
|
strcpy(uname_info.processor, unknown_str); |
125 |
strcpy(uname_info.processor, "unknown"); |
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 |
139 |
|
|
140 |
delta = utsname_offset; |
delta = utsname_offset; |
141 |
|
fmt = " %s" + 1; |
142 |
do { |
do { |
143 |
if (toprint & 1) { |
if (toprint & 1) { |
144 |
/* printf would not be safe here */ |
const char *p = (char *)(&uname_info) + *delta; |
145 |
fputs((char *)(&uname_info) + *delta, stdout); |
if (p[0]) { |
146 |
if (toprint > 1) { |
printf(fmt, p); |
147 |
bb_putchar(' '); |
fmt = " %s"; |
148 |
} |
} |
149 |
} |
} |
150 |
++delta; |
++delta; |