Mercurial > hg > toybox
annotate main.c @ 528:878b94b32866
Comment and whitespace tweaks.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 06 Mar 2012 20:48:27 -0600 |
parents | f558dce66095 |
children | 85fc06bd63c4 |
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 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
20 #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
|
21 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
22 // 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
|
23 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
28 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
|
29 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
30 int top, bottom, middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
31 |
402 | 32 // 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
|
33 // 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
|
34 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
35 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
|
36 bottom = 1; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
37 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
38 // Binary search to find this applet. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
39 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
40 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
|
41 for (;;) { |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
42 int result; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
138
diff
changeset
|
43 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
44 middle = (top+bottom)/2; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 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
|
49 else bottom = ++middle; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
50 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
51 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
52 |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
53 // 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
|
54 // because the compiler can't figure out whether or not to optimize it away |
402 | 55 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize |
56 // 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
|
57 |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
58 #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
|
59 #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
|
60 #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
|
61 #define OLDTOY(name, oldname, opts, flags) opts || |
125 | 62 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
|
63 #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
|
64 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
|
65 |
402 | 66 // Setup toybox global state for this command. |
67 | |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
68 void toy_init(struct toy_list *which, char *argv[]) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
69 { |
370 | 70 // Drop permissions for non-suid commands. |
71 | |
72 if (CFG_TOYBOX_SUID) { | |
73 uid_t uid = getuid(), euid = geteuid(); | |
74 | |
75 if (!(which->flags & TOYFLAG_STAYROOT)) { | |
76 if (uid != euid) xsetuid(euid=uid); | |
77 } else if (CFG_TOYBOX_DEBUG && uid) | |
78 error_exit("Not installed suid root"); | |
79 | |
80 if ((which->flags & TOYFLAG_NEEDROOT) && euid) | |
81 error_exit("Not root"); | |
82 } | |
83 | |
373
5e68c7cab1a4
Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
84 // 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
|
85 |
5e68c7cab1a4
Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
86 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
|
87 memset(&toys, 0, sizeof(struct toy_context)); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
88 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
89 toys.which = which; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
90 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
|
91 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
|
92 else toys.optargs = argv+1; |
257 | 93 if (which->flags & TOYFLAG_UMASK) toys.old_umask = umask(0); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
94 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
95 |
402 | 96 // Like exec() but runs an internal toybox command instead of another file. |
97 // 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
|
98 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
|
99 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
100 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
|
101 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
102 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
|
103 if (!which) return; |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
104 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
|
105 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
|
106 exit(toys.exitval); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
107 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
108 |
402 | 109 // Multiplexer command, first argument is command to run, rest are args to that. |
110 // If first argument starts with - output list of command install paths. | |
111 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
112 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
|
113 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
114 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
|
115 int i, len = 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
116 |
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]) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
118 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
|
119 toy_exec(toys.argv+1); |
241
4121825716b8
Fix crash when running unknown command via symlink.
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
120 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
|
121 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
|
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 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
125 // Output list of applets. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
126 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
|
127 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
|
128 if (fl & TOYMASK_LOCATION) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
129 if (toys.argv[1]) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
130 int j; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
131 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
|
132 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
|
133 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
134 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
|
135 if (len>65) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
136 putchar('\n'); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
137 len=0; |
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 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
140 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
141 putchar('\n'); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
142 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
143 |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
144 int main(int argc, char *argv[]) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
145 { |
65 | 146 // Artificial scope to eat less stack for things we call |
147 { | |
148 char *name; | |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
149 |
402 | 150 // 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
|
151 name = strrchr(argv[0], '/'); |
65 | 152 if (!name) name=argv[0]; |
153 else name++; | |
154 argv[0] = name; | |
155 } | |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
156 |
402 | 157 // Call the multiplexer, adjusting this argv[] to be its' argv[1]. |
158 // (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
|
159 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
|
160 toybox_main(); |
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
161 return 0; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
162 } |