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