Next snapshot. Tries to grab something out of lib in order to build, I have
[platform/upstream/toybox.git] / main.c
1 /* vi: set ts=4 :*/
2 /* Toybox infrastructure.
3  *
4  * Copyright 2006 Rob Landley <rob@landley.net>
5  *
6  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
7  */
8
9 #include "toys.h"
10
11 // The monster fun applet list.
12
13 struct toy_list toy_list[] = {
14         {"toybox", toybox_main},
15         {"df", df_main},
16         {"toysh", toysh_main}
17 };
18
19 // global context for this applet.
20
21 struct toy_context toys;
22
23
24
25 /*
26 name
27 main()
28 struct
29 usage (short long example info)
30 path (/usr/sbin)
31 */
32
33 int toybox_main(void)
34 {
35         printf("toybox\n");
36         return 0;
37 }
38
39 int toysh_main(void)
40 {
41         printf("toysh\n");
42 }
43
44 struct toy_list *find_toy_by_name(char *name)
45 {
46         int top, bottom, middle;
47
48         // If the name starts with "toybox", accept that as a match.  Otherwise
49         // skip the first entry, which is out of order.
50
51         if (!strncmp(name,"toybox",6)) return toy_list;
52         bottom=1;
53
54         // Binary search to find this applet.
55
56         top=(sizeof(toy_list)/sizeof(struct toy_list))-1;
57         for(;;) {
58                 int result;
59                 
60                 middle=(top+bottom)/2;
61                 if(middle<bottom || middle>top) return NULL;
62                 result = strcmp(name,toy_list[middle].name);
63                 if(!result) return toy_list+middle;
64                 if(result<0) top=--middle;
65                 else bottom=++middle;
66         }
67 }
68
69 int main(int argc, char *argv[])
70 {
71         char *name;
72
73         // Record command line arguments.
74         toys.argc = argc;
75         toys.argv = argv;
76
77         // Figure out which applet got called.
78         name = rindex(argv[0],'/');
79         if (!name) name = argv[0];
80         else name++;
81         toys.which = find_toy_by_name(name);
82
83         if (!toys.which) {
84                 dprintf(2,"No behavior for %s\n",name);
85                 return 1;
86         }
87         return toys.which->toy_main();
88 }