1 /* vi: set sw=4 ts=4: */
7 #define bb_need_full_version
8 #define BB_DECLARE_EXTERN
11 static int been_there_done_that = 0;
13 const struct BB_applet applets[] = {
16 {"ar", ar_main, _BB_DIR_USR_BIN, ar_usage},
19 {"basename", basename_main, _BB_DIR_USR_BIN, basename_usage},
21 {"busybox", busybox_main, _BB_DIR_BIN, NULL},
23 {"cat", cat_main, _BB_DIR_BIN, cat_usage},
25 #ifdef BB_CHMOD_CHOWN_CHGRP
26 {"chgrp", chmod_chown_chgrp_main, _BB_DIR_BIN, chgrp_usage},
28 #ifdef BB_CHMOD_CHOWN_CHGRP
29 {"chmod", chmod_chown_chgrp_main, _BB_DIR_BIN, chmod_usage},
31 #ifdef BB_CHMOD_CHOWN_CHGRP
32 {"chown", chmod_chown_chgrp_main, _BB_DIR_BIN, chown_usage},
35 {"chroot", chroot_main, _BB_DIR_SBIN, chroot_usage},
38 {"clear", clear_main, _BB_DIR_USR_BIN, clear_usage},
41 {"chvt", chvt_main, _BB_DIR_USR_BIN, chvt_usage},
44 {"cp", cp_mv_main, _BB_DIR_BIN, cp_usage},
47 {"cut", cut_main, _BB_DIR_USR_BIN, cut_usage},
50 {"date", date_main, _BB_DIR_BIN, date_usage},
53 {"dc", dc_main, _BB_DIR_USR_BIN, dc_usage},
56 {"dd", dd_main, _BB_DIR_BIN, dd_usage},
59 {"df", df_main, _BB_DIR_BIN, df_usage},
62 {"dirname", dirname_main, _BB_DIR_USR_BIN, dirname_usage},
65 {"dmesg", dmesg_main, _BB_DIR_BIN, dmesg_usage},
68 {"du", du_main, _BB_DIR_BIN, du_usage},
71 {"dumpkmap", dumpkmap_main, _BB_DIR_BIN, dumpkmap_usage},
74 {"dutmp", dutmp_main, _BB_DIR_USR_SBIN, dutmp_usage},
77 {"echo", echo_main, _BB_DIR_BIN, echo_usage},
80 {"false", false_main, _BB_DIR_BIN, false_usage},
83 {"fbset", fbset_main, _BB_DIR_USR_SBIN, NULL},
86 {"fdflush", fdflush_main, _BB_DIR_BIN, fdflush_usage},
89 {"find", find_main, _BB_DIR_USR_BIN, find_usage},
92 {"free", free_main, _BB_DIR_USR_BIN, free_usage},
95 {"freeramdisk", freeramdisk_main, _BB_DIR_SBIN, freeramdisk_usage},
98 {"deallocvt", deallocvt_main, _BB_DIR_USR_BIN, deallocvt_usage},
101 {"fsck.minix", fsck_minix_main, _BB_DIR_SBIN, fsck_minix_usage},
104 {"grep", grep_main, _BB_DIR_BIN, grep_usage},
107 {"gunzip", gunzip_main, _BB_DIR_BIN, gunzip_usage},
110 {"gzip", gzip_main, _BB_DIR_BIN, gzip_usage},
113 {"halt", halt_main, _BB_DIR_SBIN, halt_usage},
116 {"head", head_main, _BB_DIR_USR_BIN, head_usage},
119 {"hostid", hostid_main, _BB_DIR_USR_BIN, hostid_usage},
122 {"hostname", hostname_main, _BB_DIR_BIN, hostname_usage},
125 {"id", id_main, _BB_DIR_USR_BIN, id_usage},
128 {"init", init_main, _BB_DIR_SBIN, NULL},
131 {"insmod", insmod_main, _BB_DIR_SBIN, insmod_usage},
134 {"kill", kill_main, _BB_DIR_BIN, kill_usage},
137 {"killall", kill_main, _BB_DIR_USR_BIN, kill_usage},
140 {"length", length_main, _BB_DIR_USR_BIN, length_usage},
143 {"linuxrc", init_main, _BB_DIR_ROOT, init_usage},
146 {"ln", ln_main, _BB_DIR_BIN, ln_usage},
149 {"loadacm", loadacm_main, _BB_DIR_USR_BIN, loadacm_usage},
152 {"loadfont", loadfont_main, _BB_DIR_USR_BIN, loadfont_usage},
155 {"loadkmap", loadkmap_main, _BB_DIR_SBIN, loadkmap_usage},
158 {"logger", logger_main, _BB_DIR_USR_BIN, logger_usage},
161 {"logname", logname_main, _BB_DIR_USR_BIN, logname_usage},
164 {"ls", ls_main, _BB_DIR_BIN, ls_usage},
167 {"lsmod", lsmod_main, _BB_DIR_SBIN, lsmod_usage},
170 {"makedevs", makedevs_main, _BB_DIR_SBIN, makedevs_usage},
173 {"md5sum", md5sum_main, _BB_DIR_USR_BIN, md5sum_usage},
176 {"mkdir", mkdir_main, _BB_DIR_BIN, mkdir_usage},
179 {"mkfifo", mkfifo_main, _BB_DIR_USR_BIN, mkfifo_usage},
182 {"mkfs.minix", mkfs_minix_main, _BB_DIR_SBIN, mkfs_minix_usage},
185 {"mknod", mknod_main, _BB_DIR_BIN, mknod_usage},
188 {"mkswap", mkswap_main, _BB_DIR_SBIN, mkswap_usage},
191 {"mktemp", mktemp_main, _BB_DIR_BIN, mktemp_usage},
194 {"nc", nc_main, _BB_DIR_USR_BIN, nc_usage},
197 {"more", more_main, _BB_DIR_BIN, more_usage},
200 {"mount", mount_main, _BB_DIR_BIN, mount_usage},
203 {"mt", mt_main, _BB_DIR_BIN, mt_usage},
206 {"mv", cp_mv_main, _BB_DIR_BIN, mv_usage},
209 {"nslookup", nslookup_main, _BB_DIR_USR_BIN, nslookup_usage},
212 {"ping", ping_main, _BB_DIR_BIN, ping_usage},
215 {"poweroff", poweroff_main, _BB_DIR_SBIN, poweroff_usage},
218 {"printf", printf_main, _BB_DIR_USR_BIN, printf_usage},
221 {"ps", ps_main, _BB_DIR_BIN, ps_usage},
224 {"pwd", pwd_main, _BB_DIR_BIN, pwd_usage},
227 {"reboot", reboot_main, _BB_DIR_SBIN, reboot_usage},
230 {"renice", renice_main, _BB_DIR_USR_BIN},
233 {"rm", rm_main, _BB_DIR_BIN, rm_usage},
236 {"rmdir", rmdir_main, _BB_DIR_BIN, rmdir_usage},
239 {"rmmod", rmmod_main, _BB_DIR_SBIN, rmmod_usage},
242 {"sed", sed_main, _BB_DIR_BIN, sed_usage},
244 #ifdef BB_SETKEYCODES
245 {"setkeycodes", setkeycodes_main, _BB_DIR_USR_BIN, setkeycodes_usage},
248 {"sh", shell_main, _BB_DIR_BIN, shell_usage},
251 {"sleep", sleep_main, _BB_DIR_BIN, sleep_usage},
254 {"sort", sort_main, _BB_DIR_BIN, sort_usage},
257 {"sync", sync_main, _BB_DIR_BIN, sync_usage},
260 {"syslogd", syslogd_main, _BB_DIR_SBIN, syslogd_usage},
263 {"swapon", swap_on_off_main, _BB_DIR_SBIN, swapon_usage},
266 {"swapoff", swap_on_off_main, _BB_DIR_SBIN, swapoff_usage},
269 {"tail", tail_main, _BB_DIR_USR_BIN, tail_usage},
272 {"tar", tar_main, _BB_DIR_BIN, tar_usage},
275 {"telnet", telnet_main, _BB_DIR_USR_BIN, telnet_usage},
278 {"test", test_main, _BB_DIR_USR_BIN, test_usage},
281 {"tee", tee_main, _BB_DIR_BIN, tee_usage},
284 {"touch", touch_main, _BB_DIR_USR_BIN, touch_usage},
287 {"tr", tr_main, _BB_DIR_USR_BIN, tr_usage},
290 {"true", true_main, _BB_DIR_BIN, true_usage},
293 {"tty", tty_main, _BB_DIR_USR_BIN, tty_usage},
296 {"umount", umount_main, _BB_DIR_BIN, umount_usage},
299 {"uname", uname_main, _BB_DIR_BIN, uname_usage},
302 {"uniq", uniq_main, _BB_DIR_USR_BIN, uniq_usage},
305 {"update", update_main, _BB_DIR_SBIN, update_usage},
308 {"uptime", uptime_main, _BB_DIR_USR_BIN, uptime_usage},
311 {"uuencode", uuencode_main, _BB_DIR_USR_BIN, uuencode_usage},
314 {"uudecode", uudecode_main, _BB_DIR_USR_BIN, uudecode_usage},
317 {"usleep", usleep_main, _BB_DIR_BIN, usleep_usage},
320 {"wc", wc_main, _BB_DIR_USR_BIN, wc_usage},
323 {"which", which_main, _BB_DIR_USR_BIN, which_usage},
326 {"whoami", whoami_main, _BB_DIR_USR_BIN, whoami_usage},
329 {"yes", yes_main, _BB_DIR_USR_BIN, yes_usage},
332 {"zcat", gunzip_main, _BB_DIR_BIN, gunzip_usage},
335 {"[", test_main, _BB_DIR_USR_BIN, test_usage},
342 #ifdef BB_FEATURE_INSTALLER
345 * this should be consistent w/ the enum, internal.h::Location,
348 static char* install_dir[] = {
356 /* abstract link() */
357 typedef int (*__link_f)(const char *, const char *);
360 * Where in the filesystem is this busybox?
362 * malloc'd string w/ full pathname of busybox's location
365 static char *busybox_fullpath()
373 sprintf(proc, "/proc/%d/exe", pid);
374 len = readlink(proc, path, 256);
378 errorMsg("%s: %s\n", proc, strerror(errno));
384 /* create (sym)links for each applet */
385 static int install_links(const char *busybox, int use_symbolic_links)
387 __link_f Link = link;
393 if (use_symbolic_links) Link = symlink;
395 for (i = 0; applets[i].name != NULL; i++) {
399 install_dir[applets[i].location],
403 rc |= Link(busybox, command);
408 errorMsg("%s: %s\n", command, strerror(errno));
414 #endif /* BB_FEATURE_INSTALLER */
417 int main(int argc, char **argv)
420 const struct BB_applet *a = applets;
421 applet_name = "busybox";
423 #ifdef BB_FEATURE_INSTALLER
425 * This style of argument parsing doesn't scale well
426 * in the event that busybox starts wanting more --options.
427 * If someone has a cleaner approach, by all means implement it.
429 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
430 int use_symbolic_links = 0;
434 /* to use symlinks, or not to use symlinks... */
436 if ((strcmp(argv[2], "-s") == 0)) {
437 use_symbolic_links = 1;
442 busybox = busybox_fullpath();
444 install_links(busybox, use_symbolic_links);
451 #endif /* BB_FEATURE_INSTALLER */
453 for (s = applet_name = argv[0]; *s != '\0';) {
461 /* Add in a special case hack -- whenever **argv == '-'
462 * (i.e. '-su' or '-sh') always invoke the shell */
463 if (**argv == '-' && *(*argv+1)!= '-') {
464 exit(((*(shell_main)) (argc, argv)));
468 while (a->name != 0) {
469 if (strcmp(applet_name, a->name) == 0) {
470 if (a->usage && argv[1] && strcmp(argv[1], "--help") == 0)
472 exit(((*(a->main)) (argc, argv)));
476 return(busybox_main(argc, argv));
480 int busybox_main(int argc, char **argv)
487 if (been_there_done_that == 1 || argc < 1) {
488 const struct BB_applet *a = applets;
490 fprintf(stderr, "%s\n\n"
491 "Usage: busybox [function] [arguments]...\n"
492 " or: [function] [arguments]...\n\n"
493 "\tBusyBox is a multi-call binary that combines many common Unix\n"
494 "\tutilities into a single executable. Most people will create a\n"
495 "\tlink to busybox for each function they wish to use, and BusyBox\n"
496 "\twill act like whatever it was invoked as.\n"
497 "\nCurrently defined functions:\n", full_version);
499 while (a->name != 0) {
501 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
503 if (col > 60 && a->name != 0) {
504 fprintf(stderr, ",\n");
508 fprintf(stderr, "\n\n");
511 /* If we've already been here once, exit now */
512 been_there_done_that = 1;
513 return (main(argc, argv));
518 c-file-style: "linux"