blob: 36cf0a05cc5ac16f00181c3d536e6764265971ee [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002#include "internal.h"
3#include <stdio.h>
4#include <string.h>
5#include <errno.h>
6
Erik Andersen31638212000-01-15 22:28:50 +00007#ifndef BB_INIT
8#undef BB_FEATURE_LINUXRC
9#endif
10
Eric Andersencc8ed391999-10-05 16:24:54 +000011static int been_there_done_that = 0;
12
Erik Andersen31638212000-01-15 22:28:50 +000013/* It has been alledged that doing such things can
14 * help reduce binary size when staticly linking,
15 * of course with glibc, this is unlikely as long
16 * as we use things like printf -- perhaps a printf
17 * replacement may be in order
18 */
Erik Andersen05df2392000-01-13 04:43:48 +000019#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000020void exit(int status) __attribute__ ((noreturn));
21void exit(int status)
22{
23 _exit(status);
24};
25void abort(void) __attribute__ ((__noreturn__));
26void abort(void)
27{
28 _exit(0);
29};
30int atexit(void (*__func) (void))
31{
32 _exit(0);
33};
Erik Andersen05df2392000-01-13 04:43:48 +000034void *__libc_stack_end;
35#endif
36
37
Eric Andersencc8ed391999-10-05 16:24:54 +000038static const struct Applet applets[] = {
39
Erik Andersene2729152000-02-18 21:34:17 +000040#ifdef BB_BASENAME //usr/bin/basename
41 {"basename", basename_main},
42#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000043#ifdef BB_BUSYBOX //bin
44 {"busybox", busybox_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000045#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000046#ifdef BB_BLOCK_DEVICE //sbin
47 {"block_device", block_device_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000048#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000049#ifdef BB_CAT //bin
50 {"cat", cat_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000051#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000052#ifdef BB_CHMOD_CHOWN_CHGRP //bin
53 {"chmod", chmod_chown_chgrp_main},
54 {"chown", chmod_chown_chgrp_main},
55 {"chgrp", chmod_chown_chgrp_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000056#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000057#ifdef BB_CHROOT //sbin
58 {"chroot", chroot_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000059#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000060#ifdef BB_CLEAR //usr/bin
61 {"clear", clear_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000062#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000063#ifdef BB_CHVT //usr/bin
64 {"chvt", chvt_main},
Eric Andersenabc0f4f1999-12-08 23:19:36 +000065#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000066#ifdef BB_CP_MV //bin
67 {"cp", cp_mv_main},
68 {"mv", cp_mv_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000069#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000070#ifdef BB_DATE //bin
71 {"date", date_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000072#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000073#ifdef BB_DD //bin
74 {"dd", dd_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000075#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000076#ifdef BB_DF //bin
77 {"df", df_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000078#endif
Erik Andersenfb002d02000-03-05 08:07:00 +000079#ifdef BB_DIRNAME //usr/bin
80 {"dirname", dirname_main},
81#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000082#ifdef BB_DMESG //bin
83 {"dmesg", dmesg_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000084#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000085#ifdef BB_DU //bin
86 {"du", du_main},
John Beppu7a986061999-12-09 18:29:29 +000087#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000088#ifdef BB_DUTMP //usr/sbin
89 {"dutmp", dutmp_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000090#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000091#ifdef BB_FBSET //usr/sbin
92 {"fbset", fbset_main},
Erik Andersen1c5b2581999-12-16 20:59:36 +000093#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000094#ifdef BB_FDFLUSH //bin
95 {"fdflush", fdflush_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000096#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000097#ifdef BB_FIND //usr/bin
98 {"find", find_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000099#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100#ifdef BB_FREE //usr/bin
101 {"free", free_main},
Eric Andersen0460ff21999-10-25 23:32:44 +0000102#endif
Erik Andersen64371702000-03-04 22:23:27 +0000103#ifdef BB_FREERAMDISK //sbin
104 {"freeramdisk", freeramdisk_main},
105#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106#ifdef BB_DEALLOCVT //usr/bin
107 {"deallocvt", deallocvt_main},
Eric Andersen0460ff21999-10-25 23:32:44 +0000108#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109#ifdef BB_FSCK_MINIX //sbin
110 {"fsck.minix", fsck_minix_main},
Eric Andersene494fdd1999-10-19 20:23:03 +0000111#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112#ifdef BB_MKFS_MINIX //sbin
113 {"mkfs.minix", mkfs_minix_main},
Eric Andersene494fdd1999-10-19 20:23:03 +0000114#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115#ifdef BB_GREP //bin
116 {"grep", grep_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000117#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118#ifdef BB_HALT //sbin
119 {"halt", halt_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000120#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121#ifdef BB_HEAD //bin
122 {"head", head_main},
John Beppu3157b1f1999-12-10 07:42:50 +0000123#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124#ifdef BB_HOSTID //usr/bin
125 {"hostid", hostid_main},
Erik Andersen31638212000-01-15 22:28:50 +0000126#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127#ifdef BB_HOSTNAME //bin
128 {"hostname", hostname_main},
Eric Andersen485b9551999-12-07 23:14:59 +0000129#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130#ifdef BB_INIT //sbin
131 {"init", init_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000132#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133#ifdef BB_INSMOD //sbin
134 {"insmod", insmod_main},
Erik Andersen6da0ae81999-12-17 01:08:27 +0000135#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136#ifdef BB_FEATURE_LINUXRC //
137 {"linuxrc", init_main},
Eric Andersen6a76e651999-11-19 05:31:45 +0000138#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139#ifdef BB_KILL //bin
140 {"kill", kill_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000141#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142#ifdef BB_LENGTH //usr/bin
143 {"length", length_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000144#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145#ifdef BB_LN //bin
146 {"ln", ln_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000147#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148#ifdef BB_LOADACM //usr/bin
149 {"loadacm", loadacm_main},
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000150#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151#ifdef BB_LOADFONT //usr/bin
152 {"loadfont", loadfont_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000153#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154#ifdef BB_LOADKMAP //sbin
155 {"loadkmap", loadkmap_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000156#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157#ifdef BB_LS //bin
158 {"ls", ls_main},
Erik Andersen6f23cec1999-12-15 22:14:12 +0000159#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160#ifdef BB_LSMOD //sbin
161 {"lsmod", lsmod_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000162#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163#ifdef BB_MAKEDEVS //sbin
164 {"makedevs", makedevs_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000165#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166#ifdef BB_MATH //usr/bin
167 {"math", math_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000168#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169#ifdef BB_MKDIR //bin
170 {"mkdir", mkdir_main},
171#endif
172#ifdef BB_MKFIFO //usr/bin
173 {"mkfifo", mkfifo_main},
Eric Andersen19db07b1999-12-11 08:41:28 +0000174#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175#ifdef BB_MKNOD //bin
176 {"mknod", mknod_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000177#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178#ifdef BB_MKSWAP //sbin
179 {"mkswap", mkswap_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000180#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181#ifdef BB_MNC //usr/bin
182 {"mnc", mnc_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000183#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184#ifdef BB_MORE //bin
185 {"more", more_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000186#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187#ifdef BB_MOUNT //bin
188 {"mount", mount_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000189#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190#ifdef BB_MT //bin
191 {"mt", mt_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000192#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193#ifdef BB_NSLOOKUP //usr/bin
194 {"nslookup", nslookup_main},
John Beppub332e772000-01-29 12:59:01 +0000195#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196#ifdef BB_PING //bin
197 {"ping", ping_main},
Eric Andersen485b9551999-12-07 23:14:59 +0000198#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199#ifdef BB_POWEROFF //sbin
200 {"poweroff", poweroff_main},
Eric Andersen2cb55071999-12-10 08:25:07 +0000201#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202#ifdef BB_PRINTF //usr/bin
203 {"printf", printf_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000204#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205#ifdef BB_PS //bin
206 {"ps", ps_main},
Eric Andersenef8b6c71999-10-20 08:05:35 +0000207#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208#ifdef BB_PWD //bin
209 {"pwd", pwd_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000210#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211#ifdef BB_REBOOT //sbin
212 {"reboot", reboot_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000213#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214#ifdef BB_RM //bin
215 {"rm", rm_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000216#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217#ifdef BB_RMDIR //bin
218 {"rmdir", rmdir_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000219#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220#ifdef BB_RMMOD //sbin
221 {"rmmod", rmmod_main},
Erik Andersen3d7e3411999-12-16 23:04:20 +0000222#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223#ifdef BB_SFDISK //sbin
224 {"fdisk", sfdisk_main},
225 {"sfdisk", sfdisk_main},
Eric Andersene77ae3a1999-10-19 20:03:34 +0000226#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227#ifdef BB_SED //bin
228 {"sed", sed_main},
Eric Andersenc1525e81999-10-29 00:07:31 +0000229#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230#ifdef BB_SLEEP //bin
231 {"sleep", sleep_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000232#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233#ifdef BB_SORT //bin
234 {"sort", sort_main},
John Beppu019513a1999-12-22 17:57:31 +0000235#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236#ifdef BB_SYNC //bin
237 {"sync", sync_main},
Eric Andersen3843e961999-11-25 07:30:46 +0000238#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239#ifdef BB_SYSLOGD //sbin
240 {"syslogd", syslogd_main},
Eric Andersen3843e961999-11-25 07:30:46 +0000241#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242#ifdef BB_LOGGER //usr/bin
243 {"logger", logger_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000244#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245#ifdef BB_LOGNAME //usr/bin
246 {"logname", logname_main},
Erik Andersen31638212000-01-15 22:28:50 +0000247#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248#ifdef BB_SWAPONOFF //sbin
249 {"swapon", swap_on_off_main},
250 {"swapoff", swap_on_off_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000251#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252#ifdef BB_TAIL //usr/bin
253 {"tail", tail_main},
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000254#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255#ifdef BB_TAR //bin
256 {"tar", tar_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000257#endif
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000258#ifdef BB_TELNET //usr/bin
259 {"telnet", telnet_main},
260#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261#ifdef BB_TEE //bin
262 {"tee", tee_main},
John Beppu059f1521999-12-10 05:27:16 +0000263#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264#ifdef BB_TOUCH //usr/bin
265 {"touch", touch_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000266#endif
Erik Andersenfb002d02000-03-05 08:07:00 +0000267#ifdef BB_TR //usr/bin
268 {"tr", tr_main},
269#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270#ifdef BB_TRUE_FALSE //bin
271 {"true", true_main},
272 {"false", false_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000273#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274#ifdef BB_TTY //usr/bin
275 {"tty", tty_main},
Erik Andersen05df2392000-01-13 04:43:48 +0000276#endif
Erik Andersen66be5e72000-03-05 07:52:05 +0000277#ifdef BB_UMOUNT //bin
278 {"umount", umount_main},
279#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280#ifdef BB_UNAME //bin
281 {"uname", uname_main},
Eric Andersen0460ff21999-10-25 23:32:44 +0000282#endif
Erik Andersen66be5e72000-03-05 07:52:05 +0000283#ifdef BB_UPTIME //usr/bin
284 {"uptime", uptime_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000285#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286#ifdef BB_UNIQ //bin
287 {"uniq", uniq_main},
John Beppuabb47722000-01-06 00:48:21 +0000288#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289#ifdef BB_UPDATE //sbin
290 {"update", update_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000291#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292#ifdef BB_WC //usr/bin
293 {"wc", wc_main},
Erik Andersen31638212000-01-15 22:28:50 +0000294#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295#ifdef BB_WHOAMI //usr/bin
296 {"whoami", whoami_main},
Erik Andersen31638212000-01-15 22:28:50 +0000297#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298#ifdef BB_YES //usr/bin
299 {"yes", yes_main},
Erik Andersen31638212000-01-15 22:28:50 +0000300#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301#ifdef BB_GUNZIP //bin
302 {"zcat", gunzip_main},
303 {"gunzip", gunzip_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000304#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305#ifdef BB_GZIP //bin
306 {"gzip", gzip_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000307#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 {0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000309};
310
Erik Andersen05df2392000-01-13 04:43:48 +0000311
312
Eric Andersencc8ed391999-10-05 16:24:54 +0000313int main(int argc, char **argv)
314{
Erik Andersen029011b2000-03-04 21:19:32 +0000315 char *s = argv[0];
316 char *name = argv[0];
317 const struct Applet *a = applets;
Eric Andersencc8ed391999-10-05 16:24:54 +0000318
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 while (*s != '\0') {
320 if (*s++ == '/')
321 name = s;
Eric Andersencc8ed391999-10-05 16:24:54 +0000322 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323
Erik Andersen029011b2000-03-04 21:19:32 +0000324 *argv = name;
325
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 while (a->name != 0) {
327 if (strcmp(name, a->name) == 0) {
328 int status;
329
330 status = ((*(a->main)) (argc, argv));
331 if (status < 0) {
332 fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
333 }
334 fprintf(stderr, "\n");
335 exit(status);
336 }
337 a++;
338 }
339 exit(busybox_main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000340}
341
342
343int busybox_main(int argc, char **argv)
344{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 int col = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000346
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 argc--;
348 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000349
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 if (been_there_done_that == 1 || argc < 1) {
351 const struct Applet *a = applets;
352
353 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
354 BB_VER, BB_BT);
355 fprintf(stderr, "Usage: busybox [function] [arguments]...\n");
356 fprintf(stderr, " or: [function] [arguments]...\n\n");
357 fprintf(stderr,
Erik Andersen029011b2000-03-04 21:19:32 +0000358 "\tMost people will create a link to busybox for each\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 "\tfunction name, and busybox will act like whatever you invoke it as.\n");
360 fprintf(stderr, "\nCurrently defined functions:\n");
361
362 while (a->name != 0) {
363 col +=
364 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
365 (a++)->name);
366 if (col > 60 && a->name != 0) {
367 fprintf(stderr, ",\n");
368 col = 0;
369 }
370 }
371 fprintf(stderr, "\n\n");
372 exit(-1);
373 } else {
374 /* If we've already been here once, exit now */
375 been_there_done_that = 1;
376 return (main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000377 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000378}
Erik Andersen029011b2000-03-04 21:19:32 +0000379
380/*
381Local Variables:
382c-file-style: "linux"
383c-basic-offset: 4
384tab-width: 4
385End:
386*/