2 /* Toybox infrastructure.
4 * Copyright 2006 Rob Landley <rob@landley.net>
6 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
11 // The monster fun applet list.
13 struct toy_list toy_list[] = {
14 // This one is out of order on purpose.
15 {"toybox", toybox_main, 0},
16 // The rest of these are alphabetical, for binary search.
17 {"cd", cd_main, TOYFLAG_NOFORK},
18 {"df", df_main, TOYFLAG_USR|TOYFLAG_SBIN},
19 {"exit", exit_main, TOYFLAG_NOFORK},
20 {"toysh", toysh_main, TOYFLAG_BIN}
23 #define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list))
25 // global context for this applet.
27 struct toy_context toys;
29 struct toy_list *toy_find(char *name)
31 int top, bottom, middle;
33 // If the name starts with "toybox", accept that as a match. Otherwise
34 // skip the first entry, which is out of order.
36 if (!strncmp(name,"toybox",6)) return toy_list;
39 // Binary search to find this applet.
45 middle = (top+bottom)/2;
46 if (middle<bottom || middle>top) return NULL;
47 result = strcmp(name,toy_list[middle].name);
48 if (!result) return toy_list+middle;
49 if (result<0) top=--middle;
50 else bottom = ++middle;
55 void toy_exec(char *argv[])
57 struct toy_list *which;
59 which = toy_find(argv[0]);
62 // Free old toys contents here?
66 for (toys.argc = 0; argv[toys.argc]; toys.argc++);
69 exit(toys.which->toy_main());
74 static char *toy_paths[]={"usr/","bin/","sbin/",0};
78 if (toys.argv[1][0]!='-') {
79 toy_exec(toys.argv+1);
80 error_exit("No behavior for %s\n",toys.argv[1]);
84 // Output list of applets.
85 for (i=1; i<TOY_LIST_LEN; i++) {
86 int fl = toy_list[i].flags;
87 if (fl & TOYMASK_LOCATION) {
90 for (j=0; toy_paths[j]; j++)
91 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
93 len += printf("%s ",toy_list[i].name);
104 int main(int argc, char *argv[])
108 // Figure out which applet to call.
109 name = rindex(argv[0], '/');
110 if (!name) name=argv[0];
115 return toybox_main();