3af98407d2f8188e51445d78bad4e024901f7e98
[oweals/busybox.git] / applets / busybox.c
1 #include "internal.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5
6 static int been_there_done_that = 0;
7
8 static const struct Applet applets[] = {
9
10 #ifdef BB_BUSYBOX               //bin
11     {"busybox", busybox_main},
12 #endif
13 #ifdef BB_BLOCK_DEVICE          //sbin
14     {"block_device", block_device_main},
15 #endif
16 #ifdef BB_CAT                   //bin
17     {"cat", cat_more_main},
18 #endif
19 #ifdef BB_CHMOD_CHOWN_CHGRP     //bin
20     {"chmod", chmod_chown_chgrp_main},
21     {"chown", chmod_chown_chgrp_main},
22     {"chgrp", chmod_chown_chgrp_main},
23 #endif
24 #ifdef BB_CHROOT                //sbin
25     {"chroot", chroot_main},
26 #endif
27 #ifdef BB_CLEAR                 //usr/bin
28     {"clear", clear_main},
29 #endif
30 #ifdef BB_CP                    //bin
31     {"cp", cp_main},
32 #endif
33 #ifdef BB_DATE                  //bin
34     {"date", date_main},
35 #endif
36 #ifdef BB_DD                    //bin
37     {"dd", dd_main},
38 #endif
39 #ifdef BB_DF                    //bin
40     {"df", df_main},
41 #endif
42 #ifdef BB_DMESG                 //bin
43     {"dmesg", dmesg_main},
44 #endif
45 #ifdef BB_DUTMP                 //usr/sbin
46     {"dutmp", cat_more_main},
47 #endif
48 #ifdef BB_FDFLUSH               //bin
49     {"fdflush", fdflush_main},
50 #endif
51 #ifdef BB_FIND                  //usr/bin
52     {"find", find_main},
53 #endif
54 #ifdef BB_GREP                  //bin
55     {"grep", grep_main},
56 #endif
57 #ifdef BB_HALT                  //sbin
58     {"halt", halt_main},
59 #endif
60 #ifdef BB_INIT                  //sbin
61     {"init", init_main},
62 #endif
63 #ifdef BB_KILL                  //bin
64     {"kill", kill_main},
65 #endif
66 #ifdef BB_LENGTH                //usr/bin
67     {"length", length_main},
68 #endif
69 #ifdef BB_LN                    //bin
70     {"ln", ln_main},
71 #endif
72 #ifdef BB_LOADFONT              //usr/bin
73     {"loadfont", loadfont_main},
74 #endif
75 #ifdef BB_LOADKMAP              //sbin
76     {"loadkmap", loadkmap_main},
77 #endif
78 #ifdef BB_LS                    //bin
79     {"ls", ls_main},
80 #endif
81 #ifdef BB_MAKEDEVS              //sbin
82     {"makedevs", makedevs_main},
83 #endif
84 #ifdef BB_MATH                  //usr/bin
85     {"math", math_main},
86 #endif
87 #ifdef BB_MKDIR                 //bin
88     {"mkdir", mkdir_main},
89 #endif
90 #ifdef BB_MKNOD                 //bin
91     {"mknod", mknod_main},
92 #endif
93 #ifdef BB_MKSWAP                //sbin
94     {"mkswap", mkswap_main},
95 #endif
96 #ifdef BB_MNC                   //usr/bin
97     {"mnc", mnc_main},
98 #endif
99 #ifdef BB_MORE                  //bin
100     {"more", more_main},
101 #endif
102 #ifdef BB_MOUNT                 //bin
103     {"mount", mount_main},
104 #endif
105 #ifdef BB_MT                    //bin
106     {"mt", mt_main},
107 #endif
108 #ifdef BB_MV                    //bin
109     {"mv", mv_main},
110 #endif
111 #ifdef BB_PRINTF                //usr/bin
112     {"printf", printf_main},
113 #endif
114 #ifdef BB_PWD                   //bin
115     {"pwd", pwd_main},
116 #endif
117 #ifdef BB_REBOOT                //sbin
118     {"reboot", reboot_main},
119 #endif
120 #ifdef BB_RM                    //bin
121     {"rm", rm_main},
122 #endif
123 #ifdef BB_RMDIR                 //bin
124     {"rmdir", rmdir_main},
125 #endif
126 #ifdef BB_SLEEP                 //bin
127     {"sleep", sleep_main},
128 #endif
129 #ifdef BB_TAR                   //bin
130     {"tar", tar_main},
131 #endif
132 #ifdef BB_SWAPONOFF             //sbin
133     {"swapon", swap_on_off_main},
134     {"swapoff", swap_on_off_main},
135 #endif
136 #ifdef BB_SYNC                  //bin
137     {"sync", sync_main},
138 #endif
139 #ifdef BB_TOUCH                 //usr/bin
140     {"touch", touch_main},
141 #endif
142 #ifdef BB_TRUE_FALSE            //bin
143     {"true", true_main},
144     {"false", false_main},
145 #endif
146 #ifdef BB_UMOUNT                //bin
147     {"umount",  umount_main},
148 #endif
149 #ifdef BB_UPDATE                //sbin
150     {"update", update_main},
151 #endif
152 #ifdef BB_ZCAT                  //bin
153     {"zcat", zcat_main},
154     {"gunzip", zcat_main},
155 #endif
156 #ifdef BB_GZIP                  //bin
157     {"gzip", gzip_main},
158 #endif
159     {0}
160 };
161
162 int main(int argc, char **argv)
163 {
164     char *s = argv[0];
165     char *name = argv[0];
166     const struct Applet *a = applets;
167
168     while (*s != '\0') {
169         if (*s++ == '/')
170             name = s;
171     }
172
173     while (a->name != 0) {
174         if (strcmp(name, a->name) == 0) {
175             int status;
176
177             status = ((*(a->main)) (argc, argv));
178             if (status < 0) {
179                 fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
180             }
181             fprintf(stderr, "\n");
182             exit(status);
183         }
184         a++;
185     }
186     exit (busybox_main(argc, argv));
187 }
188
189
190 int busybox_main(int argc, char **argv)
191 {
192     int col=0;
193     argc--;
194     argv++;
195
196     if (been_there_done_that == 1 || argc < 1) {
197         const struct Applet *a = applets;
198         fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n",
199                 BB_VER, BB_BT);
200         fprintf(stderr, "\nUsage:\t[function] [arguments]...\n");
201         fprintf(stderr, "\tbusybox [function] [arguments]...\n");
202         fprintf(stderr,
203                 "\n\tMost people will create a symlink to busybox for each\n"
204                 "\tfunction name, and busybox will act like whatever you invoke it as.\n");
205         fprintf(stderr, "\nCurrently defined functions:\n");
206
207         while (a->name != 0) {
208             col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name);
209             if (col>60) {
210                 fprintf(stderr, ",\n");
211                 col=0;
212             }
213         }
214         fprintf(stderr, "\n\n");
215         exit(-1);
216     } else {
217         /* If we've already been here once, exit now */
218         been_there_done_that = 1;
219         return (main(argc, argv));
220     }
221 }