Mercurial > hg > toybox
annotate main.c @ 647:3258d9233753
Replace TOY_LIST_LEN with more generic ARRAY_LEN()
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 14 Aug 2012 01:42:06 -0500 |
parents | 85fc06bd63c4 |
children | 786841fdb1e0 |
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 |
88
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
11 #undef NEWTOY |
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
12 #undef OLDTOY |
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
13 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags}, |
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
14 #define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags}, |
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
15 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
16 struct toy_list toy_list[] = { |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
17 #include "generated/newtoys.h" |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
18 }; |
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 // 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
|
21 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
22 struct toy_context toys; |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
23 union global_union this; |
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
|
24 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
|
25 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
26 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
|
27 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
28 int top, bottom, middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
29 |
402 | 30 // If the name starts with "toybox" accept that as a match. Otherwise |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
31 // 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
|
32 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
33 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
|
34 bottom = 1; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
35 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
36 // Binary search to find this applet. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
37 |
647
3258d9233753
Replace TOY_LIST_LEN with more generic ARRAY_LEN()
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
38 top = ARRAY_LEN(toy_list)-1; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
39 for (;;) { |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
40 int result; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
138
diff
changeset
|
41 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
42 middle = (top+bottom)/2; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 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
|
47 else bottom = ++middle; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
48 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
49 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
50 |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
51 // Figure out whether or not anything is using the option parsing logic, |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
52 // because the compiler can't figure out whether or not to optimize it away |
402 | 53 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize |
54 // stuff out via dead code elimination. | |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
55 |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
56 #undef NEWTOY |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
57 #undef OLDTOY |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
58 #define NEWTOY(name, opts, flags) opts || |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
59 #define OLDTOY(name, oldname, opts, flags) opts || |
125 | 60 static const int NEED_OPTIONS = |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
61 #include "generated/newtoys.h" |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
62 0; // Ends the opts || opts || opts... |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
63 |
402 | 64 // Setup toybox global state for this command. |
65 | |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
66 void toy_init(struct toy_list *which, char *argv[]) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
67 { |
370 | 68 // Drop permissions for non-suid commands. |
69 | |
70 if (CFG_TOYBOX_SUID) { | |
71 uid_t uid = getuid(), euid = geteuid(); | |
72 | |
73 if (!(which->flags & TOYFLAG_STAYROOT)) { | |
74 if (uid != euid) xsetuid(euid=uid); | |
75 } else if (CFG_TOYBOX_DEBUG && uid) | |
76 error_exit("Not installed suid root"); | |
77 | |
78 if ((which->flags & TOYFLAG_NEEDROOT) && euid) | |
79 error_exit("Not root"); | |
80 } | |
81 | |
373
5e68c7cab1a4
Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
82 // Free old toys contents (to be reentrant) |
5e68c7cab1a4
Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
83 |
5e68c7cab1a4
Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
84 if (toys.optargs != toys.argv+1) free(toys.optargs); |
480
f558dce66095
Nathan McSween convinced me compilers that inline memset() can optimize the bzero case pretty well.
Rob Landley <rob@landley.net>
parents:
441
diff
changeset
|
85 memset(&toys, 0, sizeof(struct toy_context)); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
86 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
87 toys.which = which; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
88 toys.argv = argv; |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
89 if (NEED_OPTIONS && which->options) get_optflags(); |
121
933766b0bd4b
Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents:
93
diff
changeset
|
90 else toys.optargs = argv+1; |
647
3258d9233753
Replace TOY_LIST_LEN with more generic ARRAY_LEN()
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
91 toys.old_umask = umask(0); |
3258d9233753
Replace TOY_LIST_LEN with more generic ARRAY_LEN()
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
92 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
93 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
94 |
402 | 95 // Like exec() but runs an internal toybox command instead of another file. |
96 // Only returns if it can't find the command, otherwise exit() when done. | |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
97 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
|
98 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
99 struct toy_list *which; |
121
933766b0bd4b
Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents:
93
diff
changeset
|
100 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
101 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
|
102 if (!which) return; |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
103 toy_init(which, argv); |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
104 toys.which->toy_main(); |
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
105 exit(toys.exitval); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
106 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
107 |
402 | 108 // Multiplexer command, first argument is command to run, rest are args to that. |
109 // If first argument starts with - output list of command install paths. | |
110 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
111 void toybox_main(void) |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
112 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
113 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
|
114 int i, len = 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
115 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
116 if (toys.argv[1]) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
117 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
|
118 toy_exec(toys.argv+1); |
241
4121825716b8
Fix crash when running unknown command via symlink.
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
119 toys.which = toy_list; |
182
ed26d3529575
Patch from Charlie Shepherd: remove extra \n from error_exit() arguments.
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
120 error_exit("Unknown command %s",toys.argv[1]); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
121 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
122 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
123 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
124 // Output list of applets. |
647
3258d9233753
Replace TOY_LIST_LEN with more generic ARRAY_LEN()
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
125 for (i=1; i<ARRAY_LEN(toy_list); i++) { |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
126 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
|
127 if (fl & TOYMASK_LOCATION) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
128 if (toys.argv[1]) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
129 int j; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
130 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
|
131 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
|
132 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
133 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
|
134 if (len>65) { |
640
85fc06bd63c4
Workaround longstanding glibc/ld bug, ala http://sources.redhat.com/bugzilla/show_bug.cgi?id=3400, which prevents "./toybox | wc" from producing any output when toybox was statically linked.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
135 xputc('\n'); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
136 len=0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
137 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
138 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
139 } |
640
85fc06bd63c4
Workaround longstanding glibc/ld bug, ala http://sources.redhat.com/bugzilla/show_bug.cgi?id=3400, which prevents "./toybox | wc" from producing any output when toybox was statically linked.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
140 xputc('\n'); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
141 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
142 |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
143 int main(int argc, char *argv[]) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
144 { |
65 | 145 // Artificial scope to eat less stack for things we call |
146 { | |
147 char *name; | |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
148 |
402 | 149 // Trim path off of command name |
441
fdb05e02b485
Switch from deprecated function to gratuitously renamed identical non-depricated function. *shrug*
Rob Landley <rob@landley.net>
parents:
402
diff
changeset
|
150 name = strrchr(argv[0], '/'); |
65 | 151 if (!name) name=argv[0]; |
152 else name++; | |
153 argv[0] = name; | |
154 } | |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
155 |
402 | 156 // Call the multiplexer, adjusting this argv[] to be its' argv[1]. |
157 // (It will adjust it back before calling toy_exec().) | |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
158 toys.argv = argv-1; |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
159 toybox_main(); |
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
160 return 0; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
161 } |