Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 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 | static const struct Applet applets[] = { |
| 9 | |
| 10 | #ifdef BB_BUSYBOX //bin |
| 11 | {"busybox", busybox_main}, |
| 12 | #endif |
| 13 | #ifdef BB_BLOCK_DEVICE //sbin |
| 14 | {"block_device", block_device_main}, |
| 15 | #endif |
| 16 | #ifdef BB_CAT //bin |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 17 | {"cat", cat_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | #endif |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 19 | #ifdef BB_CHMOD_CHOWN_CHGRP //bin |
| 20 | {"chmod", chmod_chown_chgrp_main}, |
| 21 | {"chown", chmod_chown_chgrp_main}, |
| 22 | {"chgrp", chmod_chown_chgrp_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #endif |
| 24 | #ifdef BB_CHROOT //sbin |
| 25 | {"chroot", chroot_main}, |
| 26 | #endif |
| 27 | #ifdef BB_CLEAR //usr/bin |
| 28 | {"clear", clear_main}, |
| 29 | #endif |
| 30 | #ifdef BB_CP //bin |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 31 | {"cp", cp_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #endif |
| 33 | #ifdef BB_DATE //bin |
| 34 | {"date", date_main}, |
| 35 | #endif |
| 36 | #ifdef BB_DD //bin |
| 37 | {"dd", dd_main}, |
| 38 | #endif |
| 39 | #ifdef BB_DF //bin |
| 40 | {"df", df_main}, |
| 41 | #endif |
| 42 | #ifdef BB_DMESG //bin |
| 43 | {"dmesg", dmesg_main}, |
| 44 | #endif |
| 45 | #ifdef BB_DUTMP //usr/sbin |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 46 | {"dutmp", dutmp_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | #ifdef BB_FDFLUSH //bin |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 49 | {"fdflush", fdflush_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef BB_FIND //usr/bin |
| 52 | {"find", find_main}, |
| 53 | #endif |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame^] | 54 | #ifdef BB_FSCK_MINIX //sbin |
| 55 | {"fsck.minix", fsck_minix_main}, |
| 56 | #endif |
| 57 | #ifdef BB_MKFS_MINIX //sbin |
| 58 | {"mkfs.minix", mkfs_minix_main}, |
| 59 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | #ifdef BB_GREP //bin |
| 61 | {"grep", grep_main}, |
| 62 | #endif |
| 63 | #ifdef BB_HALT //sbin |
| 64 | {"halt", halt_main}, |
| 65 | #endif |
| 66 | #ifdef BB_INIT //sbin |
| 67 | {"init", init_main}, |
| 68 | #endif |
| 69 | #ifdef BB_KILL //bin |
| 70 | {"kill", kill_main}, |
| 71 | #endif |
| 72 | #ifdef BB_LENGTH //usr/bin |
| 73 | {"length", length_main}, |
| 74 | #endif |
| 75 | #ifdef BB_LN //bin |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 76 | {"ln", ln_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 77 | #endif |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 78 | #ifdef BB_LOADFONT //usr/bin |
| 79 | {"loadfont", loadfont_main}, |
| 80 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | #ifdef BB_LOADKMAP //sbin |
| 82 | {"loadkmap", loadkmap_main}, |
| 83 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | #ifdef BB_LS //bin |
| 85 | {"ls", ls_main}, |
| 86 | #endif |
| 87 | #ifdef BB_MAKEDEVS //sbin |
| 88 | {"makedevs", makedevs_main}, |
| 89 | #endif |
| 90 | #ifdef BB_MATH //usr/bin |
| 91 | {"math", math_main}, |
| 92 | #endif |
| 93 | #ifdef BB_MKDIR //bin |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 94 | {"mkdir", mkdir_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | #endif |
| 96 | #ifdef BB_MKNOD //bin |
| 97 | {"mknod", mknod_main}, |
| 98 | #endif |
| 99 | #ifdef BB_MKSWAP //sbin |
| 100 | {"mkswap", mkswap_main}, |
| 101 | #endif |
| 102 | #ifdef BB_MNC //usr/bin |
| 103 | {"mnc", mnc_main}, |
| 104 | #endif |
| 105 | #ifdef BB_MORE //bin |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 106 | {"more", more_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 107 | #endif |
| 108 | #ifdef BB_MOUNT //bin |
| 109 | {"mount", mount_main}, |
| 110 | #endif |
| 111 | #ifdef BB_MT //bin |
| 112 | {"mt", mt_main}, |
| 113 | #endif |
| 114 | #ifdef BB_MV //bin |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 115 | {"mv", mv_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 116 | #endif |
| 117 | #ifdef BB_PRINTF //usr/bin |
| 118 | {"printf", printf_main}, |
| 119 | #endif |
| 120 | #ifdef BB_PWD //bin |
| 121 | {"pwd", pwd_main}, |
| 122 | #endif |
| 123 | #ifdef BB_REBOOT //sbin |
| 124 | {"reboot", reboot_main}, |
| 125 | #endif |
| 126 | #ifdef BB_RM //bin |
| 127 | {"rm", rm_main}, |
| 128 | #endif |
| 129 | #ifdef BB_RMDIR //bin |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 130 | {"rmdir", rmdir_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 131 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 132 | #ifdef BB_SFDISK //sbin |
| 133 | {"fdisk", sfdisk_main}, |
| 134 | {"sfdisk", sfdisk_main}, |
| 135 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 136 | #ifdef BB_SLEEP //bin |
| 137 | {"sleep", sleep_main}, |
| 138 | #endif |
| 139 | #ifdef BB_TAR //bin |
| 140 | {"tar", tar_main}, |
| 141 | #endif |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 142 | #ifdef BB_SWAPONOFF //sbin |
| 143 | {"swapon", swap_on_off_main}, |
| 144 | {"swapoff", swap_on_off_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | #endif |
| 146 | #ifdef BB_SYNC //bin |
| 147 | {"sync", sync_main}, |
| 148 | #endif |
| 149 | #ifdef BB_TOUCH //usr/bin |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 150 | {"touch", touch_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 151 | #endif |
Eric Andersen | a3f0907 | 1999-10-19 16:39:10 +0000 | [diff] [blame] | 152 | #ifdef BB_TRUE_FALSE //bin |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 153 | {"true", true_main}, |
Eric Andersen | a3f0907 | 1999-10-19 16:39:10 +0000 | [diff] [blame] | 154 | {"false", false_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | #endif |
| 156 | #ifdef BB_UMOUNT //bin |
Eric Andersen | 8759006 | 1999-10-18 21:22:59 +0000 | [diff] [blame] | 157 | {"umount", umount_main}, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | #endif |
| 159 | #ifdef BB_UPDATE //sbin |
| 160 | {"update", update_main}, |
| 161 | #endif |
| 162 | #ifdef BB_ZCAT //bin |
| 163 | {"zcat", zcat_main}, |
| 164 | {"gunzip", zcat_main}, |
| 165 | #endif |
| 166 | #ifdef BB_GZIP //bin |
| 167 | {"gzip", gzip_main}, |
| 168 | #endif |
| 169 | {0} |
| 170 | }; |
| 171 | |
| 172 | int main(int argc, char **argv) |
| 173 | { |
| 174 | char *s = argv[0]; |
| 175 | char *name = argv[0]; |
| 176 | const struct Applet *a = applets; |
| 177 | |
| 178 | while (*s != '\0') { |
| 179 | if (*s++ == '/') |
| 180 | name = s; |
| 181 | } |
| 182 | |
| 183 | while (a->name != 0) { |
| 184 | if (strcmp(name, a->name) == 0) { |
| 185 | int status; |
| 186 | |
| 187 | status = ((*(a->main)) (argc, argv)); |
| 188 | if (status < 0) { |
| 189 | fprintf(stderr, "%s: %s\n", a->name, strerror(errno)); |
| 190 | } |
| 191 | fprintf(stderr, "\n"); |
| 192 | exit(status); |
| 193 | } |
| 194 | a++; |
| 195 | } |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 196 | exit (busybox_main(argc, argv)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
| 200 | int busybox_main(int argc, char **argv) |
| 201 | { |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 202 | int col=0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 203 | argc--; |
| 204 | argv++; |
| 205 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 206 | if (been_there_done_that == 1 || argc < 1) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 207 | const struct Applet *a = applets; |
| 208 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n", |
| 209 | BB_VER, BB_BT); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 210 | fprintf(stderr, "\nUsage:\t[function] [arguments]...\n"); |
| 211 | fprintf(stderr, "\tbusybox [function] [arguments]...\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 212 | fprintf(stderr, |
| 213 | "\n\tMost people will create a symlink to busybox for each\n" |
| 214 | "\tfunction name, and busybox will act like whatever you invoke it as.\n"); |
| 215 | fprintf(stderr, "\nCurrently defined functions:\n"); |
| 216 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 217 | while (a->name != 0) { |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 218 | col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 219 | if (col>60) { |
| 220 | fprintf(stderr, ",\n"); |
| 221 | col=0; |
| 222 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 223 | } |
| 224 | fprintf(stderr, "\n\n"); |
| 225 | exit(-1); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 226 | } else { |
| 227 | /* If we've already been here once, exit now */ |
| 228 | been_there_done_that = 1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | return (main(argc, argv)); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 230 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 231 | } |