blob: 45365b370d09ae8404f708b3d7585e074be5c26d [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001#include "internal.h"
2#include <stdio.h>
3#include <string.h>
4#include <errno.h>
5
6static int been_there_done_that = 0;
7
8static 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 Andersen1b61f411999-10-13 18:56:42 +000019#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 Andersencc8ed391999-10-05 16:24:54 +000023#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 Andersenf811e071999-10-09 00:25:00 +000031 {"cp", cp_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000032#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
46 {"dutmp", cat_more_main},
47#endif
48#ifdef BB_FALSE //bin
49 {"false", false_main},
50#endif
51#ifdef BB_FDFLUSH //bin
Eric Andersenf6be9441999-10-13 21:12:06 +000052 {"fdflush", fdflush_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000053#endif
54#ifdef BB_FIND //usr/bin
55 {"find", find_main},
56#endif
57#ifdef BB_GREP //bin
58 {"grep", grep_main},
59#endif
60#ifdef BB_HALT //sbin
61 {"halt", halt_main},
62#endif
63#ifdef BB_INIT //sbin
64 {"init", init_main},
65#endif
66#ifdef BB_KILL //bin
67 {"kill", kill_main},
68#endif
69#ifdef BB_LENGTH //usr/bin
70 {"length", length_main},
71#endif
72#ifdef BB_LN //bin
Eric Andersenf6be9441999-10-13 21:12:06 +000073 {"ln", ln_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000074#endif
75#ifdef BB_LOADKMAP //sbin
76 {"loadkmap", loadkmap_main},
77#endif
78#ifdef BB_LOSETUP //sbin
79 {"losetup", losetup_main},
80#endif
81#ifdef BB_LS //bin
82 {"ls", ls_main},
83#endif
84#ifdef BB_MAKEDEVS //sbin
85 {"makedevs", makedevs_main},
86#endif
87#ifdef BB_MATH //usr/bin
88 {"math", math_main},
89#endif
90#ifdef BB_MKDIR //bin
Eric Andersenf6be9441999-10-13 21:12:06 +000091 {"mkdir", mkdir_main},
Eric Andersencc8ed391999-10-05 16:24:54 +000092#endif
93#ifdef BB_MKNOD //bin
94 {"mknod", mknod_main},
95#endif
96#ifdef BB_MKSWAP //sbin
97 {"mkswap", mkswap_main},
98#endif
99#ifdef BB_MNC //usr/bin
100 {"mnc", mnc_main},
101#endif
102#ifdef BB_MORE //bin
Eric Andersen4bea32a1999-10-06 00:30:51 +0000103 {"more", more_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000104#endif
105#ifdef BB_MOUNT //bin
106 {"mount", mount_main},
107#endif
108#ifdef BB_MT //bin
109 {"mt", mt_main},
110#endif
111#ifdef BB_MV //bin
Eric Andersen596e5461999-10-07 08:30:23 +0000112 {"mv", mv_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000113#endif
114#ifdef BB_PRINTF //usr/bin
115 {"printf", printf_main},
116#endif
117#ifdef BB_PWD //bin
118 {"pwd", pwd_main},
119#endif
120#ifdef BB_REBOOT //sbin
121 {"reboot", reboot_main},
122#endif
123#ifdef BB_RM //bin
124 {"rm", rm_main},
125#endif
126#ifdef BB_RMDIR //bin
Eric Andersenf6be9441999-10-13 21:12:06 +0000127 {"rmdir", rmdir_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000128#endif
129#ifdef BB_SLEEP //bin
130 {"sleep", sleep_main},
131#endif
132#ifdef BB_TAR //bin
133 {"tar", tar_main},
134#endif
135#ifdef BB_SWAPOFF //sbin
136 {"swapoff", monadic_main},
137#endif
138#ifdef BB_SWAPON //sbin
139 {"swapon", monadic_main},
140#endif
141#ifdef BB_SYNC //bin
142 {"sync", sync_main},
143#endif
144#ifdef BB_TOUCH //usr/bin
Eric Andersen596e5461999-10-07 08:30:23 +0000145 {"touch", touch_main},
Eric Andersencc8ed391999-10-05 16:24:54 +0000146#endif
147#ifdef BB_TRUE //bin
148 {"true", true_main},
149#endif
150#ifdef BB_UMOUNT //bin
151 {"umount", umount_main},
152#endif
153#ifdef BB_UPDATE //sbin
154 {"update", update_main},
155#endif
156#ifdef BB_ZCAT //bin
157 {"zcat", zcat_main},
158 {"gunzip", zcat_main},
159#endif
160#ifdef BB_GZIP //bin
161 {"gzip", gzip_main},
162#endif
163 {0}
164};
165
166int main(int argc, char **argv)
167{
168 char *s = argv[0];
169 char *name = argv[0];
170 const struct Applet *a = applets;
171
172 while (*s != '\0') {
173 if (*s++ == '/')
174 name = s;
175 }
176
177 while (a->name != 0) {
178 if (strcmp(name, a->name) == 0) {
179 int status;
180
181 status = ((*(a->main)) (argc, argv));
182 if (status < 0) {
183 fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
184 }
185 fprintf(stderr, "\n");
186 exit(status);
187 }
188 a++;
189 }
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000190 exit (busybox_main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000191}
192
193
194int busybox_main(int argc, char **argv)
195{
Eric Andersen17d49ef1999-10-06 20:25:32 +0000196 int col=0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000197 argc--;
198 argv++;
199
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000200 if (been_there_done_that == 1 || argc < 1) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000201 const struct Applet *a = applets;
202 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n",
203 BB_VER, BB_BT);
Eric Andersen596e5461999-10-07 08:30:23 +0000204 fprintf(stderr, "\nUsage:\t[function] [arguments]...\n");
205 fprintf(stderr, "\tbusybox [function] [arguments]...\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000206 fprintf(stderr,
207 "\n\tMost people will create a symlink to busybox for each\n"
208 "\tfunction name, and busybox will act like whatever you invoke it as.\n");
209 fprintf(stderr, "\nCurrently defined functions:\n");
210
Eric Andersencc8ed391999-10-05 16:24:54 +0000211 while (a->name != 0) {
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000212 col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000213 if (col>60) {
214 fprintf(stderr, ",\n");
215 col=0;
216 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000217 }
218 fprintf(stderr, "\n\n");
219 exit(-1);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000220 } else {
221 /* If we've already been here once, exit now */
222 been_there_done_that = 1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000223 return (main(argc, argv));
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000224 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000225}