Fixed NFS so it supports 2.4.x kernels and NFSv3. Should close bug #1009.
[oweals/busybox.git] / 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 static int been_there_done_that = 0;
8
9 /* It has been alledged that doing such things can
10  * help reduce binary size when staticly linking,
11  * of course with glibc, this is unlikely as long
12  * as we use things like printf -- perhaps a printf
13  * replacement may be in order 
14  */
15 #if 0
16 void exit(int status) __attribute__ ((noreturn));
17 void exit(int status)
18 {
19         _exit(status);
20 };
21 void abort(void) __attribute__ ((__noreturn__));
22 void abort(void)
23 {
24         _exit(0);
25 };
26 int atexit(void (*__func) (void))
27 {
28         _exit(0);
29 };
30 void *__libc_stack_end;
31 #endif
32
33 const struct BB_applet applets[] = {
34
35 #ifdef BB_AR
36         {"ar", ar_main, _BB_DIR_USR_BIN},
37 #endif
38 #ifdef BB_BASENAME
39         {"basename", basename_main, _BB_DIR_USR_BIN},
40 #endif
41         {"busybox", busybox_main, _BB_DIR_BIN},
42 #ifdef BB_CAT
43         {"cat", cat_main, _BB_DIR_BIN},
44 #endif
45 #ifdef BB_CHMOD_CHOWN_CHGRP
46         {"chgrp", chmod_chown_chgrp_main, _BB_DIR_BIN},
47 #endif
48 #ifdef BB_CHMOD_CHOWN_CHGRP
49         {"chmod", chmod_chown_chgrp_main, _BB_DIR_BIN},
50 #endif
51 #ifdef BB_CHMOD_CHOWN_CHGRP
52         {"chown", chmod_chown_chgrp_main, _BB_DIR_BIN},
53 #endif
54 #ifdef BB_CHROOT
55         {"chroot", chroot_main, _BB_DIR_SBIN},
56 #endif
57 #ifdef BB_CLEAR
58         {"clear", clear_main, _BB_DIR_USR_BIN},
59 #endif
60 #ifdef BB_CHVT
61         {"chvt", chvt_main, _BB_DIR_USR_BIN},
62 #endif
63 #ifdef BB_CP_MV
64         {"cp", cp_mv_main, _BB_DIR_BIN},
65 #endif
66 #ifdef BB_CUT
67         {"cut", cut_main, _BB_DIR_USR_BIN},
68 #endif
69 #ifdef BB_DATE
70         {"date", date_main, _BB_DIR_BIN},
71 #endif
72 #ifdef BB_DC
73         {"dc", dc_main, _BB_DIR_USR_BIN},
74 #endif
75 #ifdef BB_DD
76         {"dd", dd_main, _BB_DIR_BIN},
77 #endif
78 #ifdef BB_DF
79         {"df", df_main, _BB_DIR_BIN},
80 #endif
81 #ifdef BB_DIRNAME
82         {"dirname", dirname_main, _BB_DIR_USR_BIN},
83 #endif
84 #ifdef BB_DMESG
85         {"dmesg", dmesg_main, _BB_DIR_BIN},
86 #endif
87 #ifdef BB_DU
88         {"du", du_main, _BB_DIR_BIN},
89 #endif
90 #ifdef BB_DUTMP
91         {"dutmp", dutmp_main, _BB_DIR_USR_SBIN},
92 #endif
93 #ifdef BB_ECHO
94         {"echo", echo_main, _BB_DIR_BIN},
95 #endif
96 #ifdef BB_TRUE_FALSE
97         {"false", false_main, _BB_DIR_BIN},
98 #endif
99 #ifdef BB_FBSET
100         {"fbset", fbset_main, _BB_DIR_USR_SBIN},
101 #endif
102 #ifdef BB_FDFLUSH
103         {"fdflush", fdflush_main, _BB_DIR_BIN},
104 #endif
105 #ifdef BB_FIND
106         {"find", find_main, _BB_DIR_USR_BIN},
107 #endif
108 #ifdef BB_FREE
109         {"free", free_main, _BB_DIR_USR_BIN},
110 #endif
111 #ifdef BB_FREERAMDISK
112         {"freeramdisk", freeramdisk_main, _BB_DIR_SBIN},
113 #endif
114 #ifdef BB_DEALLOCVT
115         {"deallocvt", deallocvt_main, _BB_DIR_USR_BIN},
116 #endif
117 #ifdef BB_FSCK_MINIX
118         {"fsck.minix", fsck_minix_main, _BB_DIR_SBIN},
119 #endif
120 #ifdef BB_GREP
121         {"grep", grep_main, _BB_DIR_BIN},
122 #endif
123 #ifdef BB_GUNZIP
124         {"gunzip", gunzip_main, _BB_DIR_BIN},
125 #endif
126 #ifdef BB_GZIP
127         {"gzip", gzip_main, _BB_DIR_BIN},
128 #endif
129 #ifdef BB_HALT
130         {"halt", halt_main, _BB_DIR_SBIN},
131 #endif
132 #ifdef BB_HEAD
133         {"head", head_main, _BB_DIR_BIN},
134 #endif
135 #ifdef BB_HOSTID
136         {"hostid", hostid_main, _BB_DIR_USR_BIN},
137 #endif
138 #ifdef BB_HOSTNAME
139         {"hostname", hostname_main, _BB_DIR_BIN},
140 #endif
141 #ifdef BB_ID
142         {"id", id_main, _BB_DIR_USR_BIN},
143 #endif
144 #ifdef BB_INIT
145         {"init", init_main, _BB_DIR_SBIN},
146 #endif
147 #ifdef BB_INSMOD
148         {"insmod", insmod_main, _BB_DIR_SBIN},
149 #endif
150 #ifdef BB_KILL
151         {"kill", kill_main, _BB_DIR_BIN},
152 #endif
153 #ifdef BB_KILLALL
154         {"killall", kill_main, _BB_DIR_USR_BIN},
155 #endif
156 #ifdef BB_LENGTH
157         {"length", length_main, _BB_DIR_USR_BIN},
158 #endif
159 #ifdef BB_LINUXRC
160         {"linuxrc", init_main, _BB_DIR_ROOT},
161 #endif
162 #ifdef BB_LN
163         {"ln", ln_main, _BB_DIR_BIN},
164 #endif
165 #ifdef BB_LOADACM
166         {"loadacm", loadacm_main, _BB_DIR_USR_BIN},
167 #endif
168 #ifdef BB_LOADFONT
169         {"loadfont", loadfont_main, _BB_DIR_USR_BIN},
170 #endif
171 #ifdef BB_LOADKMAP
172         {"loadkmap", loadkmap_main, _BB_DIR_SBIN},
173 #endif
174 #ifdef BB_LOGGER
175         {"logger", logger_main, _BB_DIR_USR_BIN},
176 #endif
177 #ifdef BB_LOGNAME
178         {"logname", logname_main, _BB_DIR_USR_BIN},
179 #endif
180 #ifdef BB_LS
181         {"ls", ls_main, _BB_DIR_BIN},
182 #endif
183 #ifdef BB_LSMOD
184         {"lsmod", lsmod_main, _BB_DIR_SBIN},
185 #endif
186 #ifdef BB_MAKEDEVS
187         {"makedevs", makedevs_main, _BB_DIR_SBIN},
188 #endif
189 #ifdef BB_MD5SUM
190         {"md5sum", md5sum_main, _BB_DIR_USR_BIN},
191 #endif
192 #ifdef BB_MKDIR
193         {"mkdir", mkdir_main, _BB_DIR_BIN},
194 #endif
195 #ifdef BB_MKFIFO
196         {"mkfifo", mkfifo_main, _BB_DIR_USR_BIN},
197 #endif
198 #ifdef BB_MKFS_MINIX
199         {"mkfs.minix", mkfs_minix_main, _BB_DIR_SBIN},
200 #endif
201 #ifdef BB_MKNOD
202         {"mknod", mknod_main, _BB_DIR_BIN},
203 #endif
204 #ifdef BB_MKSWAP
205         {"mkswap", mkswap_main, _BB_DIR_SBIN},
206 #endif
207 #ifdef BB_MKTEMP
208         {"mktemp", mktemp_main, _BB_DIR_BIN},
209 #endif
210 #ifdef BB_NC
211         {"nc", nc_main, _BB_DIR_USR_BIN},
212 #endif
213 #ifdef BB_MORE
214         {"more", more_main, _BB_DIR_BIN},
215 #endif
216 #ifdef BB_MOUNT
217         {"mount", mount_main, _BB_DIR_BIN},
218 #endif
219 #ifdef BB_MT
220         {"mt", mt_main, _BB_DIR_BIN},
221 #endif
222 #ifdef BB_CP_MV
223         {"mv", cp_mv_main, _BB_DIR_BIN},
224 #endif
225 #ifdef BB_NSLOOKUP
226         {"nslookup", nslookup_main, _BB_DIR_USR_BIN},
227 #endif
228 #ifdef BB_PING
229         {"ping", ping_main, _BB_DIR_BIN},
230 #endif
231 #ifdef BB_POWEROFF
232         {"poweroff", poweroff_main, _BB_DIR_SBIN},
233 #endif
234 #ifdef BB_PRINTF
235         {"printf", printf_main, _BB_DIR_USR_BIN},
236 #endif
237 #ifdef BB_PS
238         {"ps", ps_main, _BB_DIR_BIN},
239 #endif
240 #ifdef BB_PWD
241         {"pwd", pwd_main, _BB_DIR_BIN},
242 #endif
243 #ifdef BB_REBOOT
244         {"reboot", reboot_main, _BB_DIR_SBIN},
245 #endif
246 #ifdef BB_RM
247         {"rm", rm_main, _BB_DIR_BIN},
248 #endif
249 #ifdef BB_RMDIR
250         {"rmdir", rmdir_main, _BB_DIR_BIN},
251 #endif
252 #ifdef BB_RMMOD
253         {"rmmod", rmmod_main, _BB_DIR_SBIN},
254 #endif
255 #ifdef BB_SED
256         {"sed", sed_main, _BB_DIR_BIN},
257 #endif
258 #ifdef BB_SETKEYCODES
259         {"setkeycodes", setkeycodes_main, _BB_DIR_USR_BIN},
260 #endif
261 #ifdef BB_SH
262         {"sh", shell_main, _BB_DIR_BIN},
263 #endif
264 #ifdef BB_SLEEP
265         {"sleep", sleep_main, _BB_DIR_BIN},
266 #endif
267 #ifdef BB_SORT
268         {"sort", sort_main, _BB_DIR_BIN},
269 #endif
270 #ifdef BB_SYNC
271         {"sync", sync_main, _BB_DIR_BIN},
272 #endif
273 #ifdef BB_SYSLOGD
274         {"syslogd", syslogd_main, _BB_DIR_SBIN},
275 #endif
276 #ifdef BB_SWAPONOFF
277         {"swapon", swap_on_off_main, _BB_DIR_SBIN},
278 #endif
279 #ifdef BB_SWAPONOFF
280         {"swapoff", swap_on_off_main, _BB_DIR_SBIN},
281 #endif
282 #ifdef BB_TAIL
283         {"tail", tail_main, _BB_DIR_USR_BIN},
284 #endif
285 #ifdef BB_TAR
286         {"tar", tar_main, _BB_DIR_BIN},
287 #endif
288 #ifdef BB_TELNET
289         {"telnet", telnet_main, _BB_DIR_USR_BIN},
290 #endif
291 #ifdef BB_TEST
292         {"test", test_main, _BB_DIR_USR_BIN},
293 #endif
294 #ifdef BB_TEE
295         {"tee", tee_main, _BB_DIR_BIN},
296 #endif
297 #ifdef BB_TOUCH
298         {"touch", touch_main, _BB_DIR_USR_BIN},
299 #endif
300 #ifdef BB_TR
301         {"tr", tr_main, _BB_DIR_USR_BIN},
302 #endif
303 #ifdef BB_TRUE_FALSE
304         {"true", true_main, _BB_DIR_BIN},
305 #endif
306 #ifdef BB_TTY
307         {"tty", tty_main, _BB_DIR_USR_BIN},
308 #endif
309 #ifdef BB_UMOUNT
310         {"umount", umount_main, _BB_DIR_BIN},
311 #endif
312 #ifdef BB_UNAME
313         {"uname", uname_main, _BB_DIR_BIN},
314 #endif
315 #ifdef BB_UNIQ
316         {"uniq", uniq_main, _BB_DIR_USR_BIN},
317 #endif
318 #ifdef BB_UPDATE
319         {"update", update_main, _BB_DIR_SBIN},
320 #endif
321 #ifdef BB_UPTIME
322         {"uptime", uptime_main, _BB_DIR_USR_BIN},
323 #endif
324 #ifdef BB_UUENCODE
325         {"uuencode", uuencode_main, _BB_DIR_USR_BIN},
326 #endif
327 #ifdef BB_UUDECODE
328         {"uudecode", uudecode_main, _BB_DIR_USR_BIN},
329 #endif
330 #ifdef BB_USLEEP
331         {"usleep", usleep_main, _BB_DIR_BIN},
332 #endif
333 #ifdef BB_WC
334         {"wc", wc_main, _BB_DIR_USR_BIN},
335 #endif
336 #ifdef BB_WHICH
337         {"which", which_main, _BB_DIR_USR_BIN},
338 #endif
339 #ifdef BB_WHOAMI
340         {"whoami", whoami_main, _BB_DIR_USR_BIN},
341 #endif
342 #ifdef BB_YES
343         {"yes", yes_main, _BB_DIR_USR_BIN},
344 #endif
345 #ifdef BB_GUNZIP
346         {"zcat", gunzip_main, _BB_DIR_BIN},
347 #endif
348 #ifdef BB_TEST
349         {"[", test_main, _BB_DIR_USR_BIN},
350 #endif
351         {0}
352 };
353
354
355 #ifdef BB_FEATURE_INSTALLER
356 /* 
357  * directory table
358  *              this should be consistent w/ the enum, internal.h::Location,
359  *              or else...
360  */
361 static char* install_dir[] = {
362         "/",
363         "/bin",
364         "/sbin",
365         "/usr/bin",
366         "/usr/sbin",
367 };
368
369 /* abstract link() */
370 typedef int (*__link_f)(const char *, const char *);
371
372 /* 
373  * Where in the filesystem is this busybox?
374  * [return]
375  *              malloc'd string w/ full pathname of busybox's location
376  *              NULL on failure
377  */
378 static char *busybox_fullpath()
379 {
380         pid_t pid;
381         char path[256];
382         char proc[256];
383         int len;
384
385         pid = getpid();
386         sprintf(proc, "/proc/%d/exe", pid);
387         len = readlink(proc, path, 256);
388         if (len != -1) {
389                 path[len] = 0;
390         } else {
391                 fprintf(stderr, "busybox : %s : %s\n", proc, strerror(errno));
392                 return NULL;
393         }
394         return strdup(path);
395 }
396
397 /* create (sym)links for each applet */
398 static int install_links(const char *busybox, int use_symbolic_links)
399 {
400         __link_f Link = link;
401
402         char command[256];
403         int i;
404         int rc = 0;
405
406         if (use_symbolic_links) Link = symlink;
407
408         for (i = 0; applets[i].name != NULL; i++) {
409                 sprintf (
410                         command, 
411                         "%s/%s", 
412                         install_dir[applets[i].location], 
413                         applets[i].name
414                 );
415 #if 1
416                 rc |= Link(busybox, command);
417 #else
418                 puts(command);
419 #endif
420                 if (rc) {
421                         fprintf(stderr,"busybox : %s : %s\n", command, strerror(errno));
422                 }
423         }
424         return rc;
425 }
426
427 #if 0
428 int uninstall_links() ?
429 #endif
430 #endif /* BB_FEATURE_INSTALLER */
431
432
433 int main(int argc, char **argv)
434 {
435         char                            *s;
436         char                            *name;
437         const struct BB_applet  *a              = applets;
438
439 #ifdef BB_FEATURE_INSTALLER     
440         /* 
441          * This style of argument parsing doesn't scale well 
442          * in the event that busybox starts wanting more --options.
443          * If someone has a cleaner approach, by all means implement it.
444          */
445         if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
446                 int use_symbolic_links = 0;
447                 int rc = 0;
448                 char *busybox;
449
450                 /* to use symlinks, or not to use symlinks... */
451                 if (argc > 2) {
452                         if ((strcmp(argv[2], "-s") == 0)) { 
453                                 use_symbolic_links = 1; 
454                         }
455                 }
456
457                 /* link */
458                 busybox = busybox_fullpath();
459                 if (busybox) {
460                         install_links(busybox, use_symbolic_links);
461                         free(busybox);
462                 } else {
463                         rc = 1;
464                 }
465                 return rc;
466         }
467 #endif /* BB_FEATURE_INSTALLER */
468
469         for (s = name = argv[0]; *s != '\0';) {
470                 if (*s++ == '/')
471                         name = s;
472         }
473
474         *argv = name;
475
476 #ifdef BB_SH
477         /* Add in a special case hack -- whenever **argv == '-'
478          * (i.e. '-su' or '-sh') always invoke the shell */
479         if (**argv == '-')
480                 exit(((*(shell_main)) (argc, argv)));
481 #endif
482
483         while (a->name != 0) {
484                 if (strcmp(name, a->name) == 0) {
485                         exit(((*(a->main)) (argc, argv)));
486                 }
487                 a++;
488         }
489         return(busybox_main(argc, argv));
490 }
491
492
493 int busybox_main(int argc, char **argv)
494 {
495         int col = 0;
496
497         argc--;
498         argv++;
499
500         if (been_there_done_that == 1 || argc < 1) {
501                 const struct BB_applet *a = applets;
502
503                 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n"
504                                 "Usage: busybox [function] [arguments]...\n"
505                                 "   or: [function] [arguments]...\n\n"
506                                 "\tBusyBox is a multi-call binary that combines many common Unix\n"
507                                 "\tutilities into a single executable.  Most people will create a\n"
508                                 "\tlink to busybox for each function they wish to use, and BusyBox\n"
509                                 "\twill act like whatever it was invoked as.\n" 
510                                 "\nCurrently defined functions:\n", BB_VER, BB_BT);
511
512                 while (a->name != 0) {
513                         col +=
514                                 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
515                                                 (a++)->name);
516                         if (col > 60 && a->name != 0) {
517                                 fprintf(stderr, ",\n");
518                                 col = 0;
519                         }
520                 }
521                 fprintf(stderr, "\n\n");
522                 exit(-1);
523         }
524         /* If we've already been here once, exit now */
525         been_there_done_that = 1;
526         return (main(argc, argv));
527 }
528
529 /*
530 Local Variables:
531 c-file-style: "linux"
532 c-basic-offset: 4
533 tab-width: 4
534 End:
535 */