a00f90be027ea40760a0a0a164d32fac321e3a9c
[oweals/busybox.git] / 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 #if 0
9 void exit (int status) __attribute__ ((noreturn));
10 void exit (int status) { _exit(status); };
11 void abort (void) __attribute__ ((__noreturn__));
12 void abort (void) { _exit(0); };
13 int atexit (void (*__func) (void)) { _exit(0); };
14 void *__libc_stack_end;
15 #endif
16
17
18 static const struct Applet applets[] = {
19
20 #ifdef BB_BUSYBOX               //bin
21     {"busybox", busybox_main},
22 #endif
23 #ifdef BB_BLOCK_DEVICE          //sbin
24     {"block_device", block_device_main},
25 #endif
26 #ifdef BB_CAT                   //bin
27     {"cat", cat_main},
28 #endif
29 #ifdef BB_CHMOD_CHOWN_CHGRP     //bin
30     {"chmod", chmod_chown_chgrp_main},
31     {"chown", chmod_chown_chgrp_main},
32     {"chgrp", chmod_chown_chgrp_main},
33 #endif
34 #ifdef BB_CHROOT                //sbin
35     {"chroot", chroot_main},
36 #endif
37 #ifdef BB_CLEAR                 //usr/bin
38     {"clear", clear_main},
39 #endif
40 #ifdef BB_CHVT                  //usr/bin
41     {"chvt", chvt_main},
42 #endif
43 #ifdef BB_CP                    //bin
44     {"cp", cp_main},
45 #endif
46 #ifdef BB_DATE                  //bin
47     {"date", date_main},
48 #endif
49 #ifdef BB_DD                    //bin
50     {"dd", dd_main},
51 #endif
52 #ifdef BB_DF                    //bin
53     {"df", df_main},
54 #endif
55 #ifdef BB_DMESG                 //bin
56     {"dmesg", dmesg_main},
57 #endif
58 #ifdef BB_DU                    //bin
59     {"du", du_main},
60 #endif
61 #ifdef BB_DUTMP                 //usr/sbin
62     {"dutmp", dutmp_main},
63 #endif
64 #ifdef BB_FBSET                 //usr/sbin
65     {"fbset", fbset_main},
66 #endif
67 #ifdef BB_FDFLUSH               //bin
68     {"fdflush", fdflush_main},
69 #endif
70 #ifdef BB_FIND                  //usr/bin
71     {"find", find_main},
72 #endif
73 #ifdef BB_FREE                  //usr/bin
74     {"free", free_main},
75 #endif
76 #ifdef BB_DEALLOCVT                     //usr/bin
77     {"deallocvt", deallocvt_main},
78 #endif
79 #ifdef BB_FSCK_MINIX            //sbin
80     {"fsck.minix", fsck_minix_main},
81 #endif
82 #ifdef BB_MKFS_MINIX            //sbin
83     {"mkfs.minix", mkfs_minix_main},
84 #endif
85 #ifdef BB_GREP                  //bin
86     {"grep", grep_main},
87 #endif
88 #ifdef BB_HALT                  //sbin
89     {"halt", halt_main},
90 #endif
91 #ifdef BB_HEAD                  //bin
92     {"head", head_main},
93 #endif
94 #ifdef BB_HOSTNAME              //bin
95     {"hostname", hostname_main},
96 #endif
97 #ifdef BB_INIT                  //sbin
98     {"init", init_main},
99 #endif
100 #ifdef BB_INSMOD                //sbin
101     {"insmod", insmod_main},
102 #endif
103 #ifdef BB_FEATURE_LINUXRC       //
104     {"linuxrc", init_main},
105 #endif
106 #ifdef BB_KILL                  //bin
107     {"kill", kill_main},
108 #endif
109 #ifdef BB_LENGTH                //usr/bin
110     {"length", length_main},
111 #endif
112 #ifdef BB_LN                    //bin
113     {"ln", ln_main},
114 #endif
115 #ifdef BB_LOADACM               //usr/bin
116     {"loadacm", loadacm_main},
117 #endif    
118 #ifdef BB_LOADFONT              //usr/bin
119     {"loadfont", loadfont_main},
120 #endif
121 #ifdef BB_LOADKMAP              //sbin
122     {"loadkmap", loadkmap_main},
123 #endif
124 #ifdef BB_LS                    //bin
125     {"ls", ls_main},
126 #endif
127 #ifdef BB_LSMOD                 //sbin
128     {"lsmod", lsmod_main},
129 #endif
130 #ifdef BB_MAKEDEVS              //sbin
131     {"makedevs", makedevs_main},
132 #endif
133 #ifdef BB_MATH                  //usr/bin
134     {"math", math_main},
135 #endif
136 #ifdef BB_MKDIR                 //bin
137     {"mkdir", mkdir_main},
138 #endif
139 #ifdef BB_MKFIFO                //usr/bin
140     {"mkfifo", mkfifo_main},
141 #endif
142 #ifdef BB_MKNOD                 //bin
143     {"mknod", mknod_main},
144 #endif
145 #ifdef BB_MKSWAP                //sbin
146     {"mkswap", mkswap_main},
147 #endif
148 #ifdef BB_MNC                   //usr/bin
149     {"mnc", mnc_main},
150 #endif
151 #ifdef BB_MORE                  //bin
152     {"more", more_main},
153 #endif
154 #ifdef BB_MOUNT                 //bin
155     {"mount", mount_main},
156 #endif
157 #ifdef BB_MT                    //bin
158     {"mt", mt_main},
159 #endif
160 #ifdef BB_MV                    //bin
161     {"mv", mv_main},
162 #endif
163 #ifdef BB_PING                  //bin
164     {"ping", ping_main},
165 #endif
166 #ifdef BB_POWEROFF              //sbin
167     {"poweroff", poweroff_main},
168 #endif
169 #ifdef BB_PRINTF                //usr/bin
170     {"printf", printf_main},
171 #endif
172 #ifdef BB_PS                    //bin
173     {"ps", ps_main},
174 #endif
175 #ifdef BB_PWD                   //bin
176     {"pwd", pwd_main},
177 #endif
178 #ifdef BB_REBOOT                //sbin
179     {"reboot", reboot_main},
180 #endif
181 #ifdef BB_RM                    //bin
182     {"rm", rm_main},
183 #endif
184 #ifdef BB_RMDIR                 //bin
185     {"rmdir", rmdir_main},
186 #endif
187 #ifdef BB_RMMOD                 //sbin
188     {"rmmod", rmmod_main},
189 #endif
190 #ifdef BB_SFDISK                //sbin
191     {"fdisk", sfdisk_main},
192     {"sfdisk", sfdisk_main},
193 #endif
194 #ifdef BB_SED                   //bin
195     {"sed", sed_main},
196 #endif
197 #ifdef BB_SLEEP                 //bin
198     {"sleep", sleep_main},
199 #endif
200 #ifdef BB_SORT                  //bin
201     {"sort", sort_main},
202 #endif
203 #ifdef BB_SYNC                  //bin
204     {"sync", sync_main},
205 #endif
206 #ifdef BB_SYSLOGD               //sbin
207     {"syslogd", syslogd_main},
208 #endif
209 #ifdef BB_LOGGER                //usr/bin
210     {"logger", logger_main},
211 #endif
212 #ifdef BB_SWAPONOFF             //sbin
213     {"swapon", swap_on_off_main},
214     {"swapoff", swap_on_off_main},
215 #endif
216 #ifdef BB_TAIL                  //usr/bin
217     {"tail", tail_main},
218 #endif
219 #ifdef BB_TAR                   //bin
220     {"tar", tar_main},
221 #endif
222 #ifdef BB_TEE                   //bin
223     {"tee", tee_main},
224 #endif
225 #ifdef BB_TOUCH                 //usr/bin
226     {"touch", touch_main},
227 #endif
228 #ifdef BB_TRUE_FALSE            //bin
229     {"true", true_main},
230     {"false", false_main},
231 #endif
232 #ifdef BB_WC                    //usr/bin
233     {"wc",  wc_main},
234 #endif
235 #ifdef BB_UNAME                 //bin
236     {"uname",  uname_main},
237 #endif
238 #ifdef BB_UMOUNT                //bin
239     {"umount",  umount_main},
240 #endif
241 #ifdef BB_UNIQ                  //bin
242     {"uniq", uniq_main},
243 #endif
244 #ifdef BB_UPDATE                //sbin
245     {"update", update_main},
246 #endif
247 #ifdef BB_GUNZIP                //bin
248     {"zcat", gunzip_main},
249     {"gunzip", gunzip_main},
250 #endif
251 #ifdef BB_GZIP                  //bin
252     {"gzip", gzip_main},
253 #endif
254     {0}
255 };
256
257
258
259 int main(int argc, char **argv)
260 {
261     char *s = argv[0];
262     char *name = argv[0];
263     const struct Applet *a = applets;
264
265     while (*s != '\0') {
266         if (*s++ == '/')
267             name = s;
268     }
269
270     while (a->name != 0) {
271         if (strcmp(name, a->name) == 0) {
272             int status;
273
274             status = ((*(a->main)) (argc, argv));
275             if (status < 0) {
276                 fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
277             }
278             fprintf(stderr, "\n");
279             exit(status);
280         }
281         a++;
282     }
283     exit (busybox_main(argc, argv));
284 }
285
286
287 int busybox_main(int argc, char **argv)
288 {
289     int col=0;
290     argc--;
291     argv++;
292
293     if (been_there_done_that == 1 || argc < 1) {
294         const struct Applet *a = applets;
295         fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
296                 BB_VER, BB_BT);
297         fprintf(stderr, "Usage: busybox [function] [arguments]...\n");
298         fprintf(stderr, "   or: [function] [arguments]...\n\n");
299         fprintf(stderr,
300                 "\tMost people will create a symlink to busybox for each\n"
301                 "\tfunction name, and busybox will act like whatever you invoke it as.\n");
302         fprintf(stderr, "\nCurrently defined functions:\n");
303
304         while (a->name != 0) {
305             col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name);
306             if (col>60) {
307                 fprintf(stderr, ",\n");
308                 col=0;
309             }
310         }
311         fprintf(stderr, "\n\n");
312         exit(-1);
313     } else {
314         /* If we've already been here once, exit now */
315         been_there_done_that = 1;
316         return (main(argc, argv));
317     }
318 }