Mercurial > hg > toybox
comparison toys/main.c @ 0:e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
author | landley@driftwood |
---|---|
date | Wed, 27 Sep 2006 00:45:05 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e651a31d5416 |
---|---|
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 <stdio.h> | |
10 #include <strings.h> | |
11 | |
12 /* | |
13 name | |
14 main() | |
15 struct | |
16 usage (short long example info) | |
17 path (/usr/sbin) | |
18 */ | |
19 | |
20 int toybox_main(void) | |
21 { | |
22 printf("toybox\n"); | |
23 return 0; | |
24 } | |
25 | |
26 int toysh_main(void) | |
27 { | |
28 printf("toysh\n"); | |
29 } | |
30 | |
31 // The monster fun applet list. | |
32 | |
33 struct toy_list { | |
34 char *name; | |
35 int (*toy_main)(void); | |
36 } toy_list[] = { | |
37 {"toybox", toybox_main}, | |
38 {"toysh", toysh_main} | |
39 }; | |
40 | |
41 // Global context for this applet. | |
42 | |
43 struct toy_context { | |
44 struct toy_list *which; | |
45 int argc; | |
46 char **argv; | |
47 char buf[4096]; | |
48 // toy_union toydata; | |
49 } toys; | |
50 | |
51 struct toy_list *find_toy_by_name(char *name) | |
52 { | |
53 int top, bottom, middle; | |
54 | |
55 // If the name starts with "toybox", accept that as a match. Otherwise | |
56 // skip the first entry, which is out of order. | |
57 | |
58 if (!strncmp(name,"toybox",6)) return toy_list; | |
59 bottom=1; | |
60 | |
61 // Binary search to find this applet. | |
62 | |
63 top=(sizeof(toy_list)/sizeof(struct toy_list))-1; | |
64 for(;;) { | |
65 int result; | |
66 | |
67 middle=(top+bottom)/2; | |
68 if(middle<bottom || middle>top) return NULL; | |
69 result = strcmp(name,toy_list[middle].name); | |
70 if(!result) return toy_list+middle; | |
71 if(result<0) top=--middle; | |
72 else bottom=++middle; | |
73 } | |
74 } | |
75 | |
76 int main(int argc, char *argv[]) | |
77 { | |
78 char *name; | |
79 | |
80 // Record command line arguments. | |
81 toys.argc = argc; | |
82 toys.argv = argv; | |
83 | |
84 // Figure out which applet got called. | |
85 name = rindex(argv[0],'/'); | |
86 if (!name) name = argv[0]; | |
87 else name++; | |
88 toys.which = find_toy_by_name(name); | |
89 | |
90 if (!toys.which) { | |
91 dprintf(2,"No behavior for %s\n",name); | |
92 return 1; | |
93 } | |
94 return toys.which->toy_main(); | |
95 } |