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