diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-07-16 16:43:41 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-07-16 16:43:41 +0200 |
commit | c49638b7d29e1010bfa95acbd013bf3268810bae (patch) | |
tree | 22724034ef1bef65e7cb1439fc60050da8afb1fa | |
parent | 9f904a22ffcaa517a0836a635a4a676b6b93c8fe (diff) | |
download | busybox-w32-c49638b7d29e1010bfa95acbd013bf3268810bae.tar.gz busybox-w32-c49638b7d29e1010bfa95acbd013bf3268810bae.tar.bz2 busybox-w32-c49638b7d29e1010bfa95acbd013bf3268810bae.zip |
arch: new applet (same as unmae -m). ~30 bytes.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/uname.c | 165 |
1 files changed, 96 insertions, 69 deletions
diff --git a/coreutils/uname.c b/coreutils/uname.c index 4d98fde25..9c6a06ebb 100644 --- a/coreutils/uname.c +++ b/coreutils/uname.c | |||
@@ -55,10 +55,20 @@ | |||
55 | //config: help | 55 | //config: help |
56 | //config: Sets the operating system name reported by uname -o. The | 56 | //config: Sets the operating system name reported by uname -o. The |
57 | //config: default is "GNU/Linux". | 57 | //config: default is "GNU/Linux". |
58 | //config: | ||
59 | //can't use "ARCH" for this applet, all hell breaks loose in build system :) | ||
60 | //config:config BBARCH | ||
61 | //config: bool "arch" | ||
62 | //config: default y | ||
63 | //config: help | ||
64 | //config: Same as uname -m. | ||
58 | 65 | ||
59 | //applet:IF_UNAME(APPLET(uname, BB_DIR_BIN, BB_SUID_DROP)) | 66 | //applet:IF_UNAME(APPLET(uname, BB_DIR_BIN, BB_SUID_DROP)) |
67 | // APPLET_ODDNAME:name main location suid_type help | ||
68 | //applet:IF_BBARCH(APPLET_ODDNAME(arch, uname, BB_DIR_BIN, BB_SUID_DROP, arch)) | ||
60 | 69 | ||
61 | //kbuild:lib-$(CONFIG_UNAME) += uname.o | 70 | //kbuild:lib-$(CONFIG_UNAME) += uname.o |
71 | //kbuild:lib-$(CONFIG_BBARCH) += uname.o | ||
62 | 72 | ||
63 | /* BB_AUDIT SUSv3 compliant */ | 73 | /* BB_AUDIT SUSv3 compliant */ |
64 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ | 74 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
@@ -81,6 +91,11 @@ | |||
81 | //usage: "$ uname -a\n" | 91 | //usage: "$ uname -a\n" |
82 | //usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n" | 92 | //usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n" |
83 | 93 | ||
94 | //usage:#define arch_trivial_usage | ||
95 | //usage: "" | ||
96 | //usage:#define arch_full_usage "\n\n" | ||
97 | //usage: "Print system architecture" | ||
98 | |||
84 | #include "libbb.h" | 99 | #include "libbb.h" |
85 | /* After libbb.h, since it needs sys/types.h on some systems */ | 100 | /* After libbb.h, since it needs sys/types.h on some systems */ |
86 | #include <sys/utsname.h> | 101 | #include <sys/utsname.h> |
@@ -92,7 +107,8 @@ typedef struct { | |||
92 | char os[sizeof(CONFIG_UNAME_OSNAME)]; | 107 | char os[sizeof(CONFIG_UNAME_OSNAME)]; |
93 | } uname_info_t; | 108 | } uname_info_t; |
94 | 109 | ||
95 | static const char options[] ALIGN1 = "snrvmpioa"; | 110 | #if ENABLE_UNAME |
111 | #define options "snrvmpioa" | ||
96 | static const unsigned short utsname_offset[] = { | 112 | static const unsigned short utsname_offset[] = { |
97 | offsetof(uname_info_t, name.sysname), /* -s */ | 113 | offsetof(uname_info_t, name.sysname), /* -s */ |
98 | offsetof(uname_info_t, name.nodename), /* -n */ | 114 | offsetof(uname_info_t, name.nodename), /* -n */ |
@@ -103,86 +119,97 @@ static const unsigned short utsname_offset[] = { | |||
103 | offsetof(uname_info_t, platform), /* -i */ | 119 | offsetof(uname_info_t, platform), /* -i */ |
104 | offsetof(uname_info_t, os), /* -o */ | 120 | offsetof(uname_info_t, os), /* -o */ |
105 | }; | 121 | }; |
122 | #endif | ||
106 | 123 | ||
107 | int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 124 | int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
108 | int uname_main(int argc UNUSED_PARAM, char **argv) | 125 | int uname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
109 | { | 126 | { |
110 | #if ENABLE_LONG_OPTS | ||
111 | static const char uname_longopts[] ALIGN1 = | ||
112 | /* name, has_arg, val */ | ||
113 | "all\0" No_argument "a" | ||
114 | "kernel-name\0" No_argument "s" | ||
115 | "nodename\0" No_argument "n" | ||
116 | "kernel-release\0" No_argument "r" | ||
117 | "release\0" No_argument "r" | ||
118 | "kernel-version\0" No_argument "v" | ||
119 | "machine\0" No_argument "m" | ||
120 | "processor\0" No_argument "p" | ||
121 | "hardware-platform\0" No_argument "i" | ||
122 | "operating-system\0" No_argument "o" | ||
123 | ; | ||
124 | #endif | ||
125 | uname_info_t uname_info; | 127 | uname_info_t uname_info; |
126 | #if defined(__sparc__) && defined(__linux__) | 128 | IF_UNAME(const char *unknown_str = "unknown";) |
127 | char *fake_sparc = getenv("FAKE_SPARC"); | ||
128 | #endif | ||
129 | const char *unknown_str = "unknown"; | ||
130 | const char *fmt; | ||
131 | const unsigned short *delta; | ||
132 | unsigned toprint; | 129 | unsigned toprint; |
133 | 130 | ||
134 | IF_LONG_OPTS(applet_long_options = uname_longopts); | 131 | toprint = (1 << 4); /* "arch" = "uname -m" */ |
135 | toprint = getopt32(argv, options); | 132 | |
136 | 133 | #if ENABLE_UNAME | |
137 | if (argv[optind]) { /* coreutils-6.9 compat */ | 134 | if (!ENABLE_BBARCH || applet_name[0] == 'u') { |
138 | bb_show_usage(); | 135 | # if ENABLE_LONG_OPTS |
139 | } | 136 | static const char uname_longopts[] ALIGN1 = |
140 | 137 | /* name, has_arg, val */ | |
141 | if (toprint & (1 << 8)) { /* -a => all opts on */ | 138 | "all\0" No_argument "a" |
142 | toprint = (1 << 8) - 1; | 139 | "kernel-name\0" No_argument "s" |
143 | unknown_str = ""; /* -a does not print unknown fields */ | 140 | "nodename\0" No_argument "n" |
144 | } | 141 | "kernel-release\0" No_argument "r" |
145 | 142 | "release\0" No_argument "r" | |
146 | if (toprint == 0) { /* no opts => -s (sysname) */ | 143 | "kernel-version\0" No_argument "v" |
147 | toprint = 1; | 144 | "machine\0" No_argument "m" |
148 | } | 145 | "processor\0" No_argument "p" |
146 | "hardware-platform\0" No_argument "i" | ||
147 | "operating-system\0" No_argument "o" | ||
148 | ; | ||
149 | # endif | ||
150 | IF_LONG_OPTS(applet_long_options = uname_longopts); | ||
151 | toprint = getopt32(argv, options); | ||
152 | if (argv[optind]) { /* coreutils-6.9 compat */ | ||
153 | bb_show_usage(); | ||
154 | } | ||
155 | if (toprint & (1 << 8)) { /* -a => all opts on */ | ||
156 | toprint = (1 << 8) - 1; | ||
157 | unknown_str = ""; /* -a does not print unknown fields */ | ||
158 | } | ||
159 | if (toprint == 0) { /* no opts => -s (sysname) */ | ||
160 | toprint = 1; | ||
161 | } | ||
162 | } /* if "uname" */ | ||
163 | #endif | ||
149 | 164 | ||
150 | uname(&uname_info.name); /* never fails */ | 165 | uname(&uname_info.name); /* never fails */ |
151 | 166 | ||
152 | #if defined(__sparc__) && defined(__linux__) | 167 | #if defined(__sparc__) && defined(__linux__) |
153 | if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') { | 168 | { |
154 | strcpy(uname_info.name.machine, "sparc"); | 169 | char *fake_sparc = getenv("FAKE_SPARC"); |
155 | } | 170 | if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') { |
156 | #endif | 171 | strcpy(uname_info.name.machine, "sparc"); |
157 | strcpy(uname_info.processor, unknown_str); | 172 | } |
158 | strcpy(uname_info.platform, unknown_str); | ||
159 | strcpy(uname_info.os, CONFIG_UNAME_OSNAME); | ||
160 | #if 0 | ||
161 | /* Fedora does something like this */ | ||
162 | strcpy(uname_info.processor, uname_info.name.machine); | ||
163 | strcpy(uname_info.platform, uname_info.name.machine); | ||
164 | if (uname_info.platform[0] == 'i' | ||
165 | && uname_info.platform[1] | ||
166 | && uname_info.platform[2] == '8' | ||
167 | && uname_info.platform[3] == '6' | ||
168 | ) { | ||
169 | uname_info.platform[1] = '3'; | ||
170 | } | 173 | } |
171 | #endif | 174 | #endif |
172 | 175 | if (ENABLE_BBARCH && (!ENABLE_UNAME || applet_name[0] == 'a')) { | |
173 | delta = utsname_offset; | 176 | puts(uname_info.name.machine); |
174 | fmt = " %s" + 1; | 177 | } else { |
175 | do { | 178 | #if ENABLE_UNAME |
176 | if (toprint & 1) { | 179 | /* "uname" */ |
177 | const char *p = (char *)(&uname_info) + *delta; | 180 | const char *fmt; |
178 | if (p[0]) { | 181 | const unsigned short *delta; |
179 | printf(fmt, p); | 182 | |
180 | fmt = " %s"; | 183 | strcpy(uname_info.processor, unknown_str); |
181 | } | 184 | strcpy(uname_info.platform, unknown_str); |
185 | strcpy(uname_info.os, CONFIG_UNAME_OSNAME); | ||
186 | # if 0 | ||
187 | /* Fedora does something like this */ | ||
188 | strcpy(uname_info.processor, uname_info.name.machine); | ||
189 | strcpy(uname_info.platform, uname_info.name.machine); | ||
190 | if (uname_info.platform[0] == 'i' | ||
191 | && uname_info.platform[1] | ||
192 | && uname_info.platform[2] == '8' | ||
193 | && uname_info.platform[3] == '6' | ||
194 | ) { | ||
195 | uname_info.platform[1] = '3'; | ||
182 | } | 196 | } |
183 | ++delta; | 197 | # endif |
184 | } while (toprint >>= 1); | 198 | delta = utsname_offset; |
185 | bb_putchar('\n'); | 199 | fmt = " %s" + 1; |
200 | do { | ||
201 | if (toprint & 1) { | ||
202 | const char *p = (char *)(&uname_info) + *delta; | ||
203 | if (p[0]) { | ||
204 | printf(fmt, p); | ||
205 | fmt = " %s"; | ||
206 | } | ||
207 | } | ||
208 | ++delta; | ||
209 | } while (toprint >>= 1); | ||
210 | bb_putchar('\n'); | ||
211 | #endif | ||
212 | } | ||
186 | 213 | ||
187 | fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */ | 214 | fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */ |
188 | } | 215 | } |