Mercurial > hg > toybox
comparison main.c @ 402:2551e517b800
Expand comments.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 14 Jan 2012 23:28:15 -0600 |
parents | 5e68c7cab1a4 |
children | fdb05e02b485 |
comparison
equal
deleted
inserted
replaced
401:bc435ea49b2c | 402:2551e517b800 |
---|---|
27 | 27 |
28 struct toy_list *toy_find(char *name) | 28 struct toy_list *toy_find(char *name) |
29 { | 29 { |
30 int top, bottom, middle; | 30 int top, bottom, middle; |
31 | 31 |
32 // If the name starts with "toybox", accept that as a match. Otherwise | 32 // If the name starts with "toybox" accept that as a match. Otherwise |
33 // skip the first entry, which is out of order. | 33 // skip the first entry, which is out of order. |
34 | 34 |
35 if (!strncmp(name,"toybox",6)) return toy_list; | 35 if (!strncmp(name,"toybox",6)) return toy_list; |
36 bottom = 1; | 36 bottom = 1; |
37 | 37 |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
53 // Figure out whether or not anything is using the option parsing logic, | 53 // Figure out whether or not anything is using the option parsing logic, |
54 // because the compiler can't figure out whether or not to optimize it away | 54 // because the compiler can't figure out whether or not to optimize it away |
55 // on its' own. | 55 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize |
56 // stuff out via dead code elimination. | |
56 | 57 |
57 #undef NEWTOY | 58 #undef NEWTOY |
58 #undef OLDTOY | 59 #undef OLDTOY |
59 #define NEWTOY(name, opts, flags) opts || | 60 #define NEWTOY(name, opts, flags) opts || |
60 #define OLDTOY(name, oldname, opts, flags) opts || | 61 #define OLDTOY(name, oldname, opts, flags) opts || |
61 static const int NEED_OPTIONS = | 62 static const int NEED_OPTIONS = |
62 #include "generated/newtoys.h" | 63 #include "generated/newtoys.h" |
63 0; // Ends the opts || opts || opts... | 64 0; // Ends the opts || opts || opts... |
65 | |
66 // Setup toybox global state for this command. | |
64 | 67 |
65 void toy_init(struct toy_list *which, char *argv[]) | 68 void toy_init(struct toy_list *which, char *argv[]) |
66 { | 69 { |
67 // Drop permissions for non-suid commands. | 70 // Drop permissions for non-suid commands. |
68 | 71 |
88 if (NEED_OPTIONS && which->options) get_optflags(); | 91 if (NEED_OPTIONS && which->options) get_optflags(); |
89 else toys.optargs = argv+1; | 92 else toys.optargs = argv+1; |
90 if (which->flags & TOYFLAG_UMASK) toys.old_umask = umask(0); | 93 if (which->flags & TOYFLAG_UMASK) toys.old_umask = umask(0); |
91 } | 94 } |
92 | 95 |
93 // Run a toy. | 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. | |
94 void toy_exec(char *argv[]) | 98 void toy_exec(char *argv[]) |
95 { | 99 { |
96 struct toy_list *which; | 100 struct toy_list *which; |
97 | 101 |
98 which = toy_find(argv[0]); | 102 which = toy_find(argv[0]); |
99 if (!which) return; | 103 if (!which) return; |
100 toy_init(which, argv); | 104 toy_init(which, argv); |
101 toys.which->toy_main(); | 105 toys.which->toy_main(); |
102 exit(toys.exitval); | 106 exit(toys.exitval); |
103 } | 107 } |
108 | |
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. | |
104 | 111 |
105 void toybox_main(void) | 112 void toybox_main(void) |
106 { | 113 { |
107 static char *toy_paths[]={"usr/","bin/","sbin/",0}; | 114 static char *toy_paths[]={"usr/","bin/","sbin/",0}; |
108 int i, len = 0; | 115 int i, len = 0; |
138 { | 145 { |
139 // Artificial scope to eat less stack for things we call | 146 // Artificial scope to eat less stack for things we call |
140 { | 147 { |
141 char *name; | 148 char *name; |
142 | 149 |
143 // Figure out which applet to call. | 150 // Trim path off of command name |
144 name = rindex(argv[0], '/'); | 151 name = rindex(argv[0], '/'); |
145 if (!name) name=argv[0]; | 152 if (!name) name=argv[0]; |
146 else name++; | 153 else name++; |
147 argv[0] = name; | 154 argv[0] = name; |
148 } | 155 } |
149 | 156 |
157 // Call the multiplexer, adjusting this argv[] to be its' argv[1]. | |
158 // (It will adjust it back before calling toy_exec().) | |
150 toys.argv = argv-1; | 159 toys.argv = argv-1; |
151 toybox_main(); | 160 toybox_main(); |
152 return 0; | 161 return 0; |
153 } | 162 } |