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