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