Mercurial > hg > toybox
annotate main.c @ 73:fb56ba93afa3
Teach pwd to detect write errors.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 20 Jan 2007 22:59:00 -0500 |
parents | 329e2b37d0e1 |
children | 021fe1a818c3 |
rev | line source |
---|---|
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
1 /* vi: set ts=4 :*/ |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
2 /* Toybox infrastructure. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
3 * |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
4 * Copyright 2006 Rob Landley <rob@landley.net> |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
5 */ |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
6 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
7 #include "toys.h" |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
8 |
21
6475d6c46066
Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
9 // Populate toy_list[]. |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
10 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
11 struct toy_list toy_list[] = { |
21
6475d6c46066
Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
12 #define FROM_MAIN |
6475d6c46066
Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
13 #include "toys/toylist.h" |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
14 }; |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
15 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
16 #define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list)) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
17 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
18 // global context for this applet. |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
19 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
20 struct toy_context toys; |
53
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
21 char toybuf[4096]; |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
22 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
23 struct toy_list *toy_find(char *name) |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
24 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
25 int top, bottom, middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
26 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
27 // If the name starts with "toybox", accept that as a match. Otherwise |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
28 // skip the first entry, which is out of order. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
29 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
30 if (!strncmp(name,"toybox",6)) return toy_list; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
31 bottom = 1; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
32 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
33 // Binary search to find this applet. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
34 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
35 top = TOY_LIST_LEN-1; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
36 for (;;) { |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
37 int result; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
38 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
39 middle = (top+bottom)/2; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
40 if (middle<bottom || middle>top) return NULL; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
41 result = strcmp(name,toy_list[middle].name); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
42 if (!result) return toy_list+middle; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
43 if (result<0) top=--middle; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
44 else bottom = ++middle; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
45 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
46 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
47 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
48 void toy_init(struct toy_list *which, char *argv[]) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
49 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
50 // Free old toys contents here? |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
51 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
52 toys.which = which; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
53 toys.argv = argv; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
54 toys.exitval = 1; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
21
diff
changeset
|
55 if (which->options) get_optflags(); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
56 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
57 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
58 // Run a toy. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
59 void toy_exec(char *argv[]) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
60 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
61 struct toy_list *which; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
62 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
63 which = toy_find(argv[0]); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
64 if (!which) return; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
65 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
66 toy_init(which, argv); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
67 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
68 exit(toys.which->toy_main()); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
69 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
70 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
71 int toybox_main(void) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
72 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
73 static char *toy_paths[]={"usr/","bin/","sbin/",0}; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
74 int i, len = 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
75 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
76 if (toys.argv[1]) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
77 if (toys.argv[1][0]!='-') { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
78 toy_exec(toys.argv+1); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
79 error_exit("No behavior for %s\n",toys.argv[1]); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
80 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
81 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
82 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
83 // Output list of applets. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
84 for (i=1; i<TOY_LIST_LEN; i++) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
85 int fl = toy_list[i].flags; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
86 if (fl & TOYMASK_LOCATION) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
87 if (toys.argv[1]) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
88 int j; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
89 for (j=0; toy_paths[j]; j++) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
90 if (fl & (1<<j)) len += printf("%s", toy_paths[j]); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
91 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
92 len += printf("%s ",toy_list[i].name); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
93 if (len>65) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
94 putchar('\n'); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
95 len=0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
96 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
97 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
98 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
99 putchar('\n'); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
100 return 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
101 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
102 |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
103 int main(int argc, char *argv[]) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
104 { |
65 | 105 // Artificial scope to eat less stack for things we call |
106 { | |
107 char *name; | |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
108 |
65 | 109 // Figure out which applet to call. |
110 name = rindex(argv[0], '/'); | |
111 if (!name) name=argv[0]; | |
112 else name++; | |
113 argv[0] = name; | |
114 } | |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
115 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
116 toys.argv = argv-1; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
117 return toybox_main(); |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
118 } |