blob: 396a354cda421eb8dd280aac998ba656c9de42ff [file] [log] [blame]
landley13bab2f2006-09-27 00:45:05 -04001/* 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
landleyc5621502006-09-28 17:18:51 -04009#include "toys.h"
10
11// The monster fun applet list.
12
13struct 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
21struct toy_context toys;
22
23
landley13bab2f2006-09-27 00:45:05 -040024
25/*
26name
27main()
28struct
29usage (short long example info)
30path (/usr/sbin)
31*/
32
33int toybox_main(void)
34{
35 printf("toybox\n");
36 return 0;
37}
38
39int toysh_main(void)
40{
41 printf("toysh\n");
42}
43
landley13bab2f2006-09-27 00:45:05 -040044struct 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
69int 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}