diff options
Diffstat (limited to 'applets')
-rw-r--r-- | applets/applets.c | 52 | ||||
-rw-r--r-- | applets/busybox.c | 129 |
2 files changed, 58 insertions, 123 deletions
diff --git a/applets/applets.c b/applets/applets.c index ce9ecbbc5..21b0c3dbc 100644 --- a/applets/applets.c +++ b/applets/applets.c | |||
@@ -90,8 +90,7 @@ static int suid_cfg_readable; | |||
90 | 90 | ||
91 | 91 | ||
92 | 92 | ||
93 | extern void | 93 | extern void bb_show_usage (void) |
94 | bb_show_usage (void) | ||
95 | { | 94 | { |
96 | const char *format_string; | 95 | const char *format_string; |
97 | const char *usage_string = usage_messages; | 96 | const char *usage_string = usage_messages; |
@@ -112,8 +111,7 @@ bb_show_usage (void) | |||
112 | exit (EXIT_FAILURE); | 111 | exit (EXIT_FAILURE); |
113 | } | 112 | } |
114 | 113 | ||
115 | static int | 114 | static int applet_name_compare (const void *x, const void *y) |
116 | applet_name_compare (const void *x, const void *y) | ||
117 | { | 115 | { |
118 | const char *name = x; | 116 | const char *name = x; |
119 | const struct BB_applet *applet = y; | 117 | const struct BB_applet *applet = y; |
@@ -123,47 +121,25 @@ applet_name_compare (const void *x, const void *y) | |||
123 | 121 | ||
124 | extern const size_t NUM_APPLETS; | 122 | extern const size_t NUM_APPLETS; |
125 | 123 | ||
126 | struct BB_applet * | 124 | struct BB_applet *find_applet_by_name (const char *name) |
127 | find_applet_by_name (const char *name) | ||
128 | { | 125 | { |
129 | return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet), | 126 | return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet), |
130 | applet_name_compare); | 127 | applet_name_compare); |
131 | } | 128 | } |
132 | 129 | ||
133 | void | 130 | void run_applet_by_name (const char *name, int argc, char **argv) |
134 | run_applet_by_name (const char *name, int argc, char **argv) | ||
135 | { | 131 | { |
136 | static int recurse_level = 0; | 132 | if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file (); |
137 | extern int been_there_done_that; /* From busybox.c */ | 133 | |
138 | 134 | if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv); | |
139 | #ifdef CONFIG_FEATURE_SUID_CONFIG | 135 | /* Do a binary search to find the applet entry given the name. */ |
140 | if (recurse_level == 0) | 136 | applet_using = find_applet_by_name(name); |
141 | parse_config_file (); | 137 | if(applet_using) { |
142 | #endif | 138 | bb_applet_name = applet_using->name; |
143 | 139 | if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage (); | |
144 | recurse_level++; | 140 | if(ENABLE_FEATURE_SUID) check_suid (applet_using); |
145 | /* Do a binary search to find the applet entry given the name. */ | 141 | exit ((*(applet_using->main)) (argc, argv)); |
146 | if ((applet_using = find_applet_by_name (name)) != NULL) { | ||
147 | bb_applet_name = applet_using->name; | ||
148 | if (argv[1] && strcmp (argv[1], "--help") == 0) { | ||
149 | if (strcmp (applet_using->name, "busybox") == 0) { | ||
150 | if (argv[2]) | ||
151 | applet_using = find_applet_by_name (argv[2]); | ||
152 | else | ||
153 | applet_using = NULL; | ||
154 | } | ||
155 | if (applet_using) | ||
156 | bb_show_usage (); | ||
157 | been_there_done_that = 1; | ||
158 | busybox_main (0, NULL); | ||
159 | } | 142 | } |
160 | #ifdef CONFIG_FEATURE_SUID | ||
161 | check_suid (applet_using); | ||
162 | #endif | ||
163 | |||
164 | exit ((*(applet_using->main)) (argc, argv)); | ||
165 | } | ||
166 | recurse_level--; | ||
167 | } | 143 | } |
168 | 144 | ||
169 | 145 | ||
diff --git a/applets/busybox.c b/applets/busybox.c index ee74b4c18..420c9c0e5 100644 --- a/applets/busybox.c +++ b/applets/busybox.c | |||
@@ -9,7 +9,6 @@ | |||
9 | #include <locale.h> | 9 | #include <locale.h> |
10 | #endif | 10 | #endif |
11 | 11 | ||
12 | int been_there_done_that = 0; /* Also used in applets.c */ | ||
13 | const char *bb_applet_name; | 12 | const char *bb_applet_name; |
14 | 13 | ||
15 | #ifdef CONFIG_FEATURE_INSTALLER | 14 | #ifdef CONFIG_FEATURE_INSTALLER |
@@ -32,17 +31,6 @@ static const char* const install_dir[] = { | |||
32 | /* abstract link() */ | 31 | /* abstract link() */ |
33 | typedef int (*__link_f)(const char *, const char *); | 32 | typedef int (*__link_f)(const char *, const char *); |
34 | 33 | ||
35 | /* | ||
36 | * Where in the filesystem is this busybox? | ||
37 | * [return] | ||
38 | * malloc'd string w/ full pathname of busybox's location | ||
39 | * NULL on failure | ||
40 | */ | ||
41 | static inline char *busybox_fullpath(void) | ||
42 | { | ||
43 | return xreadlink("/proc/self/exe"); | ||
44 | } | ||
45 | |||
46 | /* create (sym)links for each applet */ | 34 | /* create (sym)links for each applet */ |
47 | static void install_links(const char *busybox, int use_symbolic_links) | 35 | static void install_links(const char *busybox, int use_symbolic_links) |
48 | { | 36 | { |
@@ -72,41 +60,27 @@ int main(int argc, char **argv) | |||
72 | { | 60 | { |
73 | const char *s; | 61 | const char *s; |
74 | 62 | ||
75 | bb_applet_name = argv[0]; | 63 | bb_applet_name=argv[0]; |
76 | 64 | if (*bb_applet_name == '-') bb_applet_name++; | |
77 | if (bb_applet_name[0] == '-') | 65 | for (s = bb_applet_name; *s ;) |
78 | bb_applet_name++; | 66 | if (*(s++) == '/') bb_applet_name = s; |
79 | 67 | ||
80 | for (s = bb_applet_name; *s != '\0';) { | 68 | /* Set locale for everybody except `init' */ |
81 | if (*s++ == '/') | 69 | if(ENABLE_LOCALE_SUPPORT && (!ENABLE_INIT || getpid()==1)) |
82 | bb_applet_name = s; | ||
83 | } | ||
84 | |||
85 | #ifdef CONFIG_LOCALE_SUPPORT | ||
86 | #ifdef CONFIG_INIT | ||
87 | if(getpid()!=1) /* Do not set locale for `init' */ | ||
88 | #endif | ||
89 | { | ||
90 | setlocale(LC_ALL, ""); | 70 | setlocale(LC_ALL, ""); |
91 | } | ||
92 | #endif | ||
93 | 71 | ||
94 | run_applet_by_name(bb_applet_name, argc, argv); | 72 | run_applet_by_name(bb_applet_name, argc, argv); |
95 | bb_error_msg_and_die("applet not found"); | 73 | bb_error_msg_and_die("applet not found"); |
96 | } | 74 | } |
97 | 75 | ||
98 | |||
99 | int busybox_main(int argc, char **argv) | 76 | int busybox_main(int argc, char **argv) |
100 | { | 77 | { |
101 | int col = 0, len, i; | ||
102 | |||
103 | #ifdef CONFIG_FEATURE_INSTALLER | ||
104 | /* | 78 | /* |
105 | * This style of argument parsing doesn't scale well | 79 | * This style of argument parsing doesn't scale well |
106 | * in the event that busybox starts wanting more --options. | 80 | * in the event that busybox starts wanting more --options. |
107 | * If someone has a cleaner approach, by all means implement it. | 81 | * If someone has a cleaner approach, by all means implement it. |
108 | */ | 82 | */ |
109 | if (argc > 1 && (strcmp(argv[1], "--install") == 0)) { | 83 | if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) { |
110 | int use_symbolic_links = 0; | 84 | int use_symbolic_links = 0; |
111 | int rc = 0; | 85 | int rc = 0; |
112 | char *busybox; | 86 | char *busybox; |
@@ -119,7 +93,7 @@ int busybox_main(int argc, char **argv) | |||
119 | } | 93 | } |
120 | 94 | ||
121 | /* link */ | 95 | /* link */ |
122 | busybox = busybox_fullpath(); | 96 | busybox = xreadlink("/proc/self/exe"); |
123 | if (busybox) { | 97 | if (busybox) { |
124 | install_links(busybox, use_symbolic_links); | 98 | install_links(busybox, use_symbolic_links); |
125 | free(busybox); | 99 | free(busybox); |
@@ -128,60 +102,45 @@ int busybox_main(int argc, char **argv) | |||
128 | } | 102 | } |
129 | return rc; | 103 | return rc; |
130 | } | 104 | } |
131 | #endif /* CONFIG_FEATURE_INSTALLER */ | ||
132 | |||
133 | argc--; | ||
134 | |||
135 | /* If we've already been here once, exit now */ | ||
136 | if (been_there_done_that == 1 || argc < 1) { | ||
137 | const struct BB_applet *a = applets; | ||
138 | int output_width = 60; | ||
139 | |||
140 | #ifdef CONFIG_FEATURE_AUTOWIDTH | ||
141 | /* Obtain the terminal width. */ | ||
142 | get_terminal_width_height(0, &output_width, NULL); | ||
143 | /* leading tab and room to wrap */ | ||
144 | output_width -= 20; | ||
145 | #endif | ||
146 | 105 | ||
147 | printf("%s\n\n" | 106 | /* Deal with --help. (Also print help when called with no arguments) */ |
148 | "Usage: busybox [function] [arguments]...\n" | 107 | |
149 | " or: [function] [arguments]...\n\n" | 108 | if (argc==1 || !strcmp(argv[1],"--help") ) { |
150 | "\tBusyBox is a multi-call binary that combines many common Unix\n" | 109 | if (argc>2) run_applet_by_name(bb_applet_name=argv[2], argc, argv); |
151 | "\tutilities into a single executable. Most people will create a\n" | 110 | else { |
152 | "\tlink to busybox for each function they wish to use and BusyBox\n" | 111 | const struct BB_applet *a; |
153 | "\twill act like whatever it was invoked as!\n" | 112 | int col, output_width; |
154 | "\nCurrently defined functions:\n", bb_msg_full_version); | 113 | |
155 | 114 | if (ENABLE_FEATURE_AUTOWIDTH) { | |
156 | while (a->name != 0) { | 115 | /* Obtain the terminal width. */ |
157 | col += | 116 | get_terminal_width_height(0, &output_width, NULL); |
158 | printf("%s%s", ((col == 0) ? "\t" : ", "), | 117 | /* leading tab and room to wrap */ |
159 | (a++)->name); | 118 | output_width -= 20; |
160 | if (col > output_width && a->name != 0) { | 119 | } else output_width = 60; |
161 | printf(",\n"); | 120 | |
162 | col = 0; | 121 | printf("%s\n\n" |
122 | "Usage: busybox [function] [arguments]...\n" | ||
123 | " or: [function] [arguments]...\n\n" | ||
124 | "\tBusyBox is a multi-call binary that combines many common Unix\n" | ||
125 | "\tutilities into a single executable. Most people will create a\n" | ||
126 | "\tlink to busybox for each function they wish to use and BusyBox\n" | ||
127 | "\twill act like whatever it was invoked as!\n" | ||
128 | "\nCurrently defined functions:\n", bb_msg_full_version); | ||
129 | |||
130 | col=0; | ||
131 | for(a = applets; a->name;) { | ||
132 | col += printf("%s%s", (col ? ", " : "\t"), (a++)->name); | ||
133 | if (col > output_width && a->name) { | ||
134 | printf(",\n"); | ||
135 | col = 0; | ||
136 | } | ||
163 | } | 137 | } |
138 | printf("\n\n"); | ||
139 | exit(0); | ||
164 | } | 140 | } |
165 | printf("\n\n"); | 141 | } else run_applet_by_name(bb_applet_name=argv[1], argc-1, argv+1); |
166 | exit(0); | 142 | |
167 | } | 143 | bb_error_msg_and_die("applet not found"); |
168 | |||
169 | /* Flag that we've been here already */ | ||
170 | been_there_done_that = 1; | ||
171 | |||
172 | /* Move the command line down a notch */ | ||
173 | /* Preserve pointers so setproctitle() works consistently */ | ||
174 | len = argv[argc] + strlen(argv[argc]) - argv[1]; | ||
175 | memmove(argv[0], argv[1], len); | ||
176 | memset(argv[0] + len, 0, argv[1] - argv[0]); | ||
177 | |||
178 | /* Fix up the argv pointers */ | ||
179 | len = argv[1] - argv[0]; | ||
180 | memmove(argv, argv + 1, sizeof(char *) * (argc + 1)); | ||
181 | for (i = 0; i < argc; i++) | ||
182 | argv[i] -= len; | ||
183 | |||
184 | return (main(argc, argv)); | ||
185 | } | 144 | } |
186 | 145 | ||
187 | /* | 146 | /* |