85b42df238f05d690fa9944d757c92c48e580e37
[oweals/busybox.git] / applets / busybox.c
1 /* vi: set sw=4 ts=4: */
2 #include "internal.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <errno.h>
6
7 static int been_there_done_that = 0;
8
9 /* It has been alledged that doing such things can
10  * help reduce binary size when staticly linking,
11  * of course with glibc, this is unlikely as long
12  * as we use things like printf -- perhaps a printf
13  * replacement may be in order 
14  */
15 #if 0
16 void exit(int status) __attribute__ ((noreturn));
17 void exit(int status)
18 {
19         _exit(status);
20 };
21 void abort(void) __attribute__ ((__noreturn__));
22 void abort(void)
23 {
24         _exit(0);
25 };
26 int atexit(void (*__func) (void))
27 {
28         _exit(0);
29 };
30 void *__libc_stack_end;
31 #endif
32
33 static const struct Applet applets[] = {
34
35 #ifdef BB_BASENAME
36         {"basename", basename_main, _BB_DIR_USR_BIN},
37 #endif
38 #ifdef BB_BUSYBOX
39         {"busybox", busybox_main, _BB_DIR_BIN},
40 #endif
41 #ifdef BB_BLOCK_DEVICE
42         {"block_device", block_device_main, _BB_DIR_SBIN},
43 #endif
44 #ifdef BB_CAT
45         {"cat", cat_main, _BB_DIR_BIN},
46 #endif
47 #ifdef BB_CHMOD_CHOWN_CHGRP
48         {"chgrp", chmod_chown_chgrp_main, _BB_DIR_BIN},
49 #endif
50 #ifdef BB_CHMOD_CHOWN_CHGRP
51         {"chmod", chmod_chown_chgrp_main, _BB_DIR_BIN},
52 #endif
53 #ifdef BB_CHMOD_CHOWN_CHGRP
54         {"chown", chmod_chown_chgrp_main, _BB_DIR_BIN},
55 #endif
56 #ifdef BB_CHROOT
57         {"chroot", chroot_main, _BB_DIR_SBIN},
58 #endif
59 #ifdef BB_CLEAR
60         {"clear", clear_main, _BB_DIR_USR_BIN},
61 #endif
62 #ifdef BB_CHVT
63         {"chvt", chvt_main, _BB_DIR_USR_BIN},
64 #endif
65 #ifdef BB_CP_MV
66         {"cp", cp_mv_main, _BB_DIR_BIN},
67 #endif
68 #ifdef BB_CUT
69         {"cut", cut_main, _BB_DIR_USR_BIN},
70 #endif
71 #ifdef BB_DATE
72         {"date", date_main, _BB_DIR_BIN},
73 #endif
74 #ifdef BB_DD
75         {"dd", dd_main, _BB_DIR_BIN},
76 #endif
77 #ifdef BB_DF
78         {"df", df_main, _BB_DIR_BIN},
79 #endif
80 #ifdef BB_DIRNAME
81         {"dirname", dirname_main, _BB_DIR_USR_BIN},
82 #endif
83 #ifdef BB_DMESG
84         {"dmesg", dmesg_main, _BB_DIR_BIN},
85 #endif
86 #ifdef BB_DU
87         {"du", du_main, _BB_DIR_BIN},
88 #endif
89 #ifdef BB_DUTMP
90         {"dutmp", dutmp_main, _BB_DIR_USR_SBIN},
91 #endif
92 #ifdef BB_ECHO
93         {"echo", echo_main, _BB_DIR_BIN},
94 #endif
95 #ifdef BB_TRUE_FALSE
96         {"false", false_main, _BB_DIR_BIN},
97 #endif
98 #ifdef BB_FBSET
99         {"fbset", fbset_main, _BB_DIR_USR_SBIN},
100 #endif
101 #ifdef BB_FDFLUSH
102         {"fdflush", fdflush_main, _BB_DIR_BIN},
103 #endif
104 #ifdef BB_FIND
105         {"find", find_main, _BB_DIR_USR_BIN},
106 #endif
107 #ifdef BB_FREE
108         {"free", free_main, _BB_DIR_USR_BIN},
109 #endif
110 #ifdef BB_FREERAMDISK
111         {"freeramdisk", freeramdisk_main, _BB_DIR_SBIN},
112 #endif
113 #ifdef BB_DEALLOCVT
114         {"deallocvt", deallocvt_main, _BB_DIR_USR_BIN},
115 #endif
116 #ifdef BB_FSCK_MINIX
117         {"fsck.minix", fsck_minix_main, _BB_DIR_SBIN},
118 #endif
119 #ifdef BB_GREP
120         {"grep", grep_main, _BB_DIR_BIN},
121 #endif
122 #ifdef BB_GUNZIP
123         {"gunzip", gunzip_main, _BB_DIR_BIN},
124 #endif
125 #ifdef BB_GZIP
126         {"gzip", gzip_main, _BB_DIR_BIN},
127 #endif
128 #ifdef BB_HALT
129         {"halt", halt_main, _BB_DIR_SBIN},
130 #endif
131 #ifdef BB_HEAD
132         {"head", head_main, _BB_DIR_BIN},
133 #endif
134 #ifdef BB_HOSTID
135         {"hostid", hostid_main, _BB_DIR_USR_BIN},
136 #endif
137 #ifdef BB_HOSTNAME
138         {"hostname", hostname_main, _BB_DIR_BIN},
139 #endif
140 #ifdef BB_ID
141         {"id", id_main, _BB_DIR_USR_BIN},
142 #endif
143 #ifdef BB_INIT
144         {"init", init_main, _BB_DIR_SBIN},
145 #endif
146 #ifdef BB_INSMOD
147         {"insmod", insmod_main, _BB_DIR_SBIN},
148 #endif
149 #ifdef BB_KILL
150         {"kill", kill_main, _BB_DIR_BIN},
151 #endif
152 #ifdef BB_KILLALL
153         {"killall", kill_main, _BB_DIR_USR_BIN},
154 #endif
155 #ifdef BB_LENGTH
156         {"length", length_main, _BB_DIR_USR_BIN},
157 #endif
158 #ifdef BB_LINUXRC
159         {"linuxrc", init_main, _BB_DIR_ROOT},
160 #endif
161 #ifdef BB_LN
162         {"ln", ln_main, _BB_DIR_BIN},
163 #endif
164 #ifdef BB_LOADACM
165         {"loadacm", loadacm_main, _BB_DIR_USR_BIN},
166 #endif
167 #ifdef BB_LOADFONT
168         {"loadfont", loadfont_main, _BB_DIR_USR_BIN},
169 #endif
170 #ifdef BB_LOADKMAP
171         {"loadkmap", loadkmap_main, _BB_DIR_SBIN},
172 #endif
173 #ifdef BB_LOGGER
174         {"logger", logger_main, _BB_DIR_USR_BIN},
175 #endif
176 #ifdef BB_LOGNAME
177         {"logname", logname_main, _BB_DIR_USR_BIN},
178 #endif
179 #ifdef BB_LS
180         {"ls", ls_main, _BB_DIR_BIN},
181 #endif
182 #ifdef BB_LSMOD
183         {"lsmod", lsmod_main, _BB_DIR_SBIN},
184 #endif
185 #ifdef BB_MAKEDEVS
186         {"makedevs", makedevs_main, _BB_DIR_SBIN},
187 #endif
188 #ifdef BB_MATH
189         {"math", math_main, _BB_DIR_USR_BIN},
190 #endif
191 #ifdef BB_MKDIR
192         {"mkdir", mkdir_main, _BB_DIR_BIN},
193 #endif
194 #ifdef BB_MKFIFO
195         {"mkfifo", mkfifo_main, _BB_DIR_USR_BIN},
196 #endif
197 #ifdef BB_MKFS_MINIX
198         {"mkfs.minix", mkfs_minix_main, _BB_DIR_SBIN},
199 #endif
200 #ifdef BB_MKNOD
201         {"mknod", mknod_main, _BB_DIR_BIN},
202 #endif
203 #ifdef BB_MKSWAP
204         {"mkswap", mkswap_main, _BB_DIR_SBIN},
205 #endif
206 #ifdef BB_MKTEMP
207         {"mktemp", mktemp_main, _BB_DIR_BIN},
208 #endif
209 #ifdef BB_NC
210         {"nc", nc_main, _BB_DIR_USR_BIN},
211 #endif
212 #ifdef BB_MORE
213         {"more", more_main, _BB_DIR_BIN},
214 #endif
215 #ifdef BB_MOUNT
216         {"mount", mount_main, _BB_DIR_BIN},
217 #endif
218 #ifdef BB_MT
219         {"mt", mt_main, _BB_DIR_BIN},
220 #endif
221 #ifdef BB_CP_MV
222         {"mv", cp_mv_main, _BB_DIR_BIN},
223 #endif
224 #ifdef BB_NSLOOKUP
225         {"nslookup", nslookup_main, _BB_DIR_USR_BIN},
226 #endif
227 #ifdef BB_PING
228         {"ping", ping_main, _BB_DIR_BIN},
229 #endif
230 #ifdef BB_POWEROFF
231         {"poweroff", poweroff_main, _BB_DIR_SBIN},
232 #endif
233 #ifdef BB_PRINTF
234         {"printf", printf_main, _BB_DIR_USR_BIN},
235 #endif
236 #ifdef BB_PS
237         {"ps", ps_main, _BB_DIR_BIN},
238 #endif
239 #ifdef BB_PWD
240         {"pwd", pwd_main, _BB_DIR_BIN},
241 #endif
242 #ifdef BB_REBOOT
243         {"reboot", reboot_main, _BB_DIR_SBIN},
244 #endif
245 #ifdef BB_RM
246         {"rm", rm_main, _BB_DIR_BIN},
247 #endif
248 #ifdef BB_RMDIR
249         {"rmdir", rmdir_main, _BB_DIR_BIN},
250 #endif
251 #ifdef BB_RMMOD
252         {"rmmod", rmmod_main, _BB_DIR_SBIN},
253 #endif
254 #ifdef BB_SED
255         {"sed", sed_main, _BB_DIR_BIN},
256 #endif
257 #ifdef BB_SETKEYCODES
258         {"setkeycodes", setkeycodes_main, _BB_DIR_USR_BIN},
259 #endif
260 #ifdef BB_SFDISK
261         {"sfdisk", sfdisk_main, _BB_DIR_SBIN},
262 #endif
263 #ifdef BB_SH
264         {"sh", shell_main, _BB_DIR_BIN},
265 #endif
266 #ifdef BB_SLEEP
267         {"sleep", sleep_main, _BB_DIR_BIN},
268 #endif
269 #ifdef BB_SORT
270         {"sort", sort_main, _BB_DIR_BIN},
271 #endif
272 #ifdef BB_SYNC
273         {"sync", sync_main, _BB_DIR_BIN},
274 #endif
275 #ifdef BB_SYSLOGD
276         {"syslogd", syslogd_main, _BB_DIR_SBIN},
277 #endif
278 #ifdef BB_SWAPONOFF
279         {"swapon", swap_on_off_main, _BB_DIR_SBIN},
280 #endif
281 #ifdef BB_SWAPONOFF
282         {"swapoff", swap_on_off_main, _BB_DIR_SBIN},
283 #endif
284 #ifdef BB_TAIL
285         {"tail", tail_main, _BB_DIR_USR_BIN},
286 #endif
287 #ifdef BB_TAR
288         {"tar", tar_main, _BB_DIR_BIN},
289 #endif
290 #ifdef BB_TELNET
291         {"telnet", telnet_main, _BB_DIR_USR_BIN},
292 #endif
293 #ifdef BB_TEST
294         {"test", test_main, _BB_DIR_USR_BIN},
295 #endif
296 #ifdef BB_TEE
297         {"tee", tee_main, _BB_DIR_BIN},
298 #endif
299 #ifdef BB_TOUCH
300         {"touch", touch_main, _BB_DIR_USR_BIN},
301 #endif
302 #ifdef BB_TR
303         {"tr", tr_main, _BB_DIR_USR_BIN},
304 #endif
305 #ifdef BB_TRUE_FALSE
306         {"true", true_main, _BB_DIR_BIN},
307 #endif
308 #ifdef BB_TTY
309         {"tty", tty_main, _BB_DIR_USR_BIN},
310 #endif
311 #ifdef BB_UMOUNT
312         {"umount", umount_main, _BB_DIR_BIN},
313 #endif
314 #ifdef BB_UNAME
315         {"uname", uname_main, _BB_DIR_BIN},
316 #endif
317 #ifdef BB_UNIQ
318         {"uniq", uniq_main, _BB_DIR_USR_BIN},
319 #endif
320 #ifdef BB_UPDATE
321         {"update", update_main, _BB_DIR_SBIN},
322 #endif
323 #ifdef BB_UPTIME
324         {"uptime", uptime_main, _BB_DIR_USR_BIN},
325 #endif
326 #ifdef BB_USLEEP
327         {"usleep", usleep_main, _BB_DIR_BIN},
328 #endif
329 #ifdef BB_WC
330         {"wc", wc_main, _BB_DIR_USR_BIN},
331 #endif
332 #ifdef BB_WHOAMI
333         {"whoami", whoami_main, _BB_DIR_USR_BIN},
334 #endif
335 #ifdef BB_YES
336         {"yes", yes_main, _BB_DIR_USR_BIN},
337 #endif
338 #ifdef BB_GUNZIP
339         {"zcat", gunzip_main, _BB_DIR_BIN},
340 #endif
341 #ifdef BB_TEST
342         {"[", test_main, _BB_DIR_USR_BIN},
343 #endif
344         {0}
345 };
346
347
348
349 int main(int argc, char **argv)
350 {
351         char                            *s;
352         char                            *name;
353         const struct Applet     *a              = applets;
354
355         for (s = name = argv[0]; *s != '\0';) {
356                 if (*s++ == '/')
357                         name = s;
358         }
359
360         *argv = name;
361
362         while (a->name != 0) {
363                 if (strcmp(name, a->name) == 0) {
364                         int status;
365
366                         status = ((*(a->main)) (argc, argv));
367                         if (status < 0) {
368                                 fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
369                         }
370                         fprintf(stderr, "\n");
371                         exit(status);
372                 }
373                 a++;
374         }
375         exit(busybox_main(argc, argv));
376 }
377
378
379 int busybox_main(int argc, char **argv)
380 {
381         int col = 0;
382
383         argc--;
384         argv++;
385
386         if (been_there_done_that == 1 || argc < 1) {
387                 const struct Applet *a = applets;
388
389                 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
390                                 BB_VER, BB_BT);
391                 fprintf(stderr, "Usage: busybox [function] [arguments]...\n");
392                 fprintf(stderr, "   or: [function] [arguments]...\n\n");
393                 fprintf(stderr,
394                                 "\tBusyBox is a multi-call binary that combines many common Unix\n"
395                                 "\tutilities into a single executable.  Most people will create a\n"
396                                 "\tlink to busybox for each function they wish to use, and BusyBox\n"
397                                 "\twill act like whatever it was invoked as.\n");
398                 fprintf(stderr, "\nCurrently defined functions:\n");
399
400                 while (a->name != 0) {
401                         col +=
402                                 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
403                                                 (a++)->name);
404                         if (col > 60 && a->name != 0) {
405                                 fprintf(stderr, ",\n");
406                                 col = 0;
407                         }
408                 }
409                 fprintf(stderr, "\n\n");
410                 exit(-1);
411         } else {
412                 /* If we've already been here once, exit now */
413                 been_there_done_that = 1;
414                 return (main(argc, argv));
415         }
416 }
417
418 /*
419 Local Variables:
420 c-file-style: "linux"
421 c-basic-offset: 4
422 tab-width: 4
423 End:
424 */