Implemented du -s
[oweals/busybox.git] / busybox.c
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_main},
18 #endif
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},
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_CHVT                  //usr/bin
31     {"chvt", chvt_main},
32 #endif
33 #ifdef BB_CP                    //bin
34     {"cp", cp_main},
35 #endif
36 #ifdef BB_DATE                  //bin
37     {"date", date_main},
38 #endif
39 #ifdef BB_DD                    //bin
40     {"dd", dd_main},
41 #endif
42 #ifdef BB_DF                    //bin
43     {"df", df_main},
44 #endif
45 #ifdef BB_DMESG                 //bin
46     {"dmesg", dmesg_main},
47 #endif
48 #ifdef BB_DU                    //bin
49     {"du", du_main},
50 #endif
51 #ifdef BB_DUTMP                 //usr/sbin
52     {"dutmp", dutmp_main},
53 #endif
54 #ifdef BB_FDFLUSH               //bin
55     {"fdflush", fdflush_main},
56 #endif
57 #ifdef BB_FIND                  //usr/bin
58     {"find", find_main},
59 #endif
60 #ifdef BB_FREE                  //usr/bin
61     {"free", free_main},
62 #endif
63 #ifdef BB_DEALLOCVT                     //usr/bin
64     {"deallocvt", deallocvt_main},
65 #endif
66 #ifdef BB_FSCK_MINIX            //sbin
67     {"fsck.minix", fsck_minix_main},
68 #endif
69 #ifdef BB_MKFS_MINIX            //sbin
70     {"mkfs.minix", mkfs_minix_main},
71 #endif
72 #ifdef BB_GREP                  //bin
73     {"grep", grep_main},
74 #endif
75 #ifdef BB_HALT                  //sbin
76     {"halt", halt_main},
77 #endif
78 #ifdef BB_HEAD                  //bin
79     {"head", head_main},
80 #endif
81 #ifdef BB_HOSTNAME              //bin
82     {"hostname", hostname_main},
83 #endif
84 #ifdef BB_INIT                  //sbin
85     {"init", init_main},
86 #endif
87 #ifdef BB_FEATURE_LINUXRC       //
88     {"linuxrc", init_main},
89 #endif
90 #ifdef BB_KILL                  //bin
91     {"kill", kill_main},
92 #endif
93 #ifdef BB_LENGTH                //usr/bin
94     {"length", length_main},
95 #endif
96 #ifdef BB_LN                    //bin
97     {"ln", ln_main},
98 #endif
99 #ifdef BB_LOADFONT              //usr/bin
100     {"loadfont", loadfont_main},
101 #endif
102 #ifdef BB_LOADKMAP              //sbin
103     {"loadkmap", loadkmap_main},
104 #endif
105 #ifdef BB_LS                    //bin
106     {"ls", ls_main},
107 #endif
108 #ifdef BB_MAKEDEVS              //sbin
109     {"makedevs", makedevs_main},
110 #endif
111 #ifdef BB_MATH                  //usr/bin
112     {"math", math_main},
113 #endif
114 #ifdef BB_MKDIR                 //bin
115     {"mkdir", mkdir_main},
116 #endif
117 #ifdef BB_MKFIFO                //usr/bin
118     {"mkfifo", mkfifo_main},
119 #endif
120 #ifdef BB_MKNOD                 //bin
121     {"mknod", mknod_main},
122 #endif
123 #ifdef BB_MKSWAP                //sbin
124     {"mkswap", mkswap_main},
125 #endif
126 #ifdef BB_MNC                   //usr/bin
127     {"mnc", mnc_main},
128 #endif
129 #ifdef BB_MORE                  //bin
130     {"more", more_main},
131 #endif
132 #ifdef BB_MOUNT                 //bin
133     {"mount", mount_main},
134 #endif
135 #ifdef BB_MT                    //bin
136     {"mt", mt_main},
137 #endif
138 #ifdef BB_MV                    //bin
139     {"mv", mv_main},
140 #endif
141 #ifdef BB_PING                  //bin
142     {"ping", ping_main},
143 #endif
144 #ifdef BB_POWEROFF              //sbin
145     {"poweroff", poweroff_main},
146 #endif
147 #ifdef BB_PRINTF                //usr/bin
148     {"printf", printf_main},
149 #endif
150 #ifdef BB_PS                    //bin
151     {"ps", ps_main},
152 #endif
153 #ifdef BB_PWD                   //bin
154     {"pwd", pwd_main},
155 #endif
156 #ifdef BB_REBOOT                //sbin
157     {"reboot", reboot_main},
158 #endif
159 #ifdef BB_RM                    //bin
160     {"rm", rm_main},
161 #endif
162 #ifdef BB_RMDIR                 //bin
163     {"rmdir", rmdir_main},
164 #endif
165 #ifdef BB_SFDISK                //sbin
166     {"fdisk", sfdisk_main},
167     {"sfdisk", sfdisk_main},
168 #endif
169 #ifdef BB_SED                   //bin
170     {"sed", sed_main},
171 #endif
172 #ifdef BB_SLEEP                 //bin
173     {"sleep", sleep_main},
174 #endif
175 #ifdef BB_SYNC                  //bin
176     {"sync", sync_main},
177 #endif
178 #ifdef BB_SYSLOGD               //sbin
179     {"syslogd", syslogd_main},
180 #endif
181 #ifdef BB_LOGGER                //usr/bin
182     {"logger", logger_main},
183 #endif
184 #ifdef BB_SWAPONOFF             //sbin
185     {"swapon", swap_on_off_main},
186     {"swapoff", swap_on_off_main},
187 #endif
188 #ifdef BB_TAIL                  //usr/bin
189     {"tail", tail_main},
190 #endif
191 #ifdef BB_TAR                   //bin
192     {"tar", tar_main},
193 #endif
194 #ifdef BB_TEE                   //bin
195     {"tee", tee_main},
196 #endif
197 #ifdef BB_TOUCH                 //usr/bin
198     {"touch", touch_main},
199 #endif
200 #ifdef BB_TRUE_FALSE            //bin
201     {"true", true_main},
202     {"false", false_main},
203 #endif
204 #ifdef BB_UNAME                 //bin
205     {"uname",  uname_main},
206 #endif
207 #ifdef BB_UMOUNT                //bin
208     {"umount",  umount_main},
209 #endif
210 #ifdef BB_UPDATE                //sbin
211     {"update", update_main},
212 #endif
213 #ifdef BB_GUNZIP                //bin
214     {"zcat", gunzip_main},
215     {"gunzip", gunzip_main},
216 #endif
217 #ifdef BB_GZIP                  //bin
218     {"gzip", gzip_main},
219 #endif
220     {0}
221 };
222
223 int main(int argc, char **argv)
224 {
225     char *s = argv[0];
226     char *name = argv[0];
227     const struct Applet *a = applets;
228
229     while (*s != '\0') {
230         if (*s++ == '/')
231             name = s;
232     }
233
234     while (a->name != 0) {
235         if (strcmp(name, a->name) == 0) {
236             int status;
237
238             status = ((*(a->main)) (argc, argv));
239             if (status < 0) {
240                 fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
241             }
242             fprintf(stderr, "\n");
243             exit(status);
244         }
245         a++;
246     }
247     exit (busybox_main(argc, argv));
248 }
249
250
251 int busybox_main(int argc, char **argv)
252 {
253     int col=0;
254     argc--;
255     argv++;
256
257     if (been_there_done_that == 1 || argc < 1) {
258         const struct Applet *a = applets;
259         fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
260                 BB_VER, BB_BT);
261         fprintf(stderr, "Usage: busybox [function] [arguments]...\n");
262         fprintf(stderr, "   or: [function] [arguments]...\n\n");
263         fprintf(stderr,
264                 "\tMost people will create a symlink to busybox for each\n"
265                 "\tfunction name, and busybox will act like whatever you invoke it as.\n");
266         fprintf(stderr, "\nCurrently defined functions:\n");
267
268         while (a->name != 0) {
269             col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name);
270             if (col>60) {
271                 fprintf(stderr, ",\n");
272                 col=0;
273             }
274         }
275         fprintf(stderr, "\n\n");
276         exit(-1);
277     } else {
278         /* If we've already been here once, exit now */
279         been_there_done_that = 1;
280         return (main(argc, argv));
281     }
282 }