diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-25 00:33:44 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-25 00:33:44 +0000 |
commit | 7488dd85691d795666d82ff00f803bbdd92e5f5d (patch) | |
tree | 4aa28c4440e6c150a31188f1910b6a945176c27c | |
parent | 638961ee5247785217bb67aa6d5e27886e5dfab0 (diff) | |
download | busybox-w32-7488dd85691d795666d82ff00f803bbdd92e5f5d.tar.gz busybox-w32-7488dd85691d795666d82ff00f803bbdd92e5f5d.tar.bz2 busybox-w32-7488dd85691d795666d82ff00f803bbdd92e5f5d.zip |
wget: wget $'-\207' ... should not be allowed to work. ever. :)
So fix wget & getopt32. Also fix multiple --header options
order: add and use rev_llist.
git-svn-id: svn://busybox.net/trunk/busybox@16433 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/getopt32.c | 40 | ||||
-rw-r--r-- | libbb/llist.c | 14 | ||||
-rw-r--r-- | networking/wget.c | 72 |
4 files changed, 75 insertions, 53 deletions
diff --git a/include/libbb.h b/include/libbb.h index 3fa49728f..7b8327ff5 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -166,7 +166,7 @@ extern void llist_add_to(llist_t **old_head, void *data); | |||
166 | extern void llist_add_to_end(llist_t **list_head, void *data); | 166 | extern void llist_add_to_end(llist_t **list_head, void *data); |
167 | extern void *llist_pop(llist_t **elm); | 167 | extern void *llist_pop(llist_t **elm); |
168 | extern void llist_free(llist_t *elm, void (*freeit)(void *data)); | 168 | extern void llist_free(llist_t *elm, void (*freeit)(void *data)); |
169 | 169 | extern llist_t* rev_llist(llist_t *list); | |
170 | 170 | ||
171 | enum { | 171 | enum { |
172 | LOGMODE_NONE = 0, | 172 | LOGMODE_NONE = 0, |
diff --git a/libbb/getopt32.c b/libbb/getopt32.c index 73e6b8684..967729a1b 100644 --- a/libbb/getopt32.c +++ b/libbb/getopt32.c | |||
@@ -313,7 +313,7 @@ getopt32(int argc, char **argv, const char *applet_opts, ...) | |||
313 | { | 313 | { |
314 | unsigned flags = 0; | 314 | unsigned flags = 0; |
315 | unsigned requires = 0; | 315 | unsigned requires = 0; |
316 | t_complementary complementary[sizeof(flags) * 8 + 1]; | 316 | t_complementary complementary[33]; |
317 | int c; | 317 | int c; |
318 | const unsigned char *s; | 318 | const unsigned char *s; |
319 | t_complementary *on_off; | 319 | t_complementary *on_off; |
@@ -342,16 +342,13 @@ getopt32(int argc, char **argv, const char *applet_opts, ...) | |||
342 | s = (const unsigned char *)applet_opts; | 342 | s = (const unsigned char *)applet_opts; |
343 | if (*s == '+' || *s == '-') | 343 | if (*s == '+' || *s == '-') |
344 | s++; | 344 | s++; |
345 | for (; *s; s++) { | 345 | while (*s) { |
346 | if (c >= (int)(sizeof(flags)*8)) | 346 | if (c >= 32) break; |
347 | break; | ||
348 | on_off->opt = *s; | 347 | on_off->opt = *s; |
349 | on_off->switch_on = (1 << c); | 348 | on_off->switch_on = (1 << c); |
350 | if (s[1] == ':') { | 349 | if (*++s == ':') { |
351 | on_off->optarg = va_arg(p, void **); | 350 | on_off->optarg = va_arg(p, void **); |
352 | do | 351 | while (*++s == ':') /* skip */; |
353 | s++; | ||
354 | while (s[1] == ':'); | ||
355 | } | 352 | } |
356 | on_off++; | 353 | on_off++; |
357 | c++; | 354 | c++; |
@@ -363,16 +360,14 @@ getopt32(int argc, char **argv, const char *applet_opts, ...) | |||
363 | continue; | 360 | continue; |
364 | for (on_off = complementary; on_off->opt != 0; on_off++) | 361 | for (on_off = complementary; on_off->opt != 0; on_off++) |
365 | if (on_off->opt == l_o->val) | 362 | if (on_off->opt == l_o->val) |
366 | break; | 363 | goto next_long; |
367 | if (on_off->opt == 0) { | 364 | if (c >= 32) break; |
368 | if (c >= (int)(sizeof(flags)*8)) | 365 | on_off->opt = l_o->val; |
369 | break; | 366 | on_off->switch_on = (1 << c); |
370 | on_off->opt = l_o->val; | 367 | if (l_o->has_arg != no_argument) |
371 | on_off->switch_on = (1 << c); | 368 | on_off->optarg = va_arg(p, void **); |
372 | if (l_o->has_arg != no_argument) | 369 | c++; |
373 | on_off->optarg = va_arg(p, void **); | 370 | next_long: ; |
374 | c++; | ||
375 | } | ||
376 | } | 371 | } |
377 | #endif /* ENABLE_GETOPT_LONG */ | 372 | #endif /* ENABLE_GETOPT_LONG */ |
378 | for (s = (const unsigned char *)opt_complementary; s && *s; s++) { | 373 | for (s = (const unsigned char *)opt_complementary; s && *s; s++) { |
@@ -461,12 +456,17 @@ getopt32(int argc, char **argv, const char *applet_opts, ...) | |||
461 | } | 456 | } |
462 | } | 457 | } |
463 | #endif | 458 | #endif |
459 | /* Note: just "getopt() <= 0" will not work good for | ||
460 | * "fake" short options, like this one: | ||
461 | * wget $'-\203' "Test: test" http://kernel.org/ | ||
462 | * (supposed to act as --header, but doesn't) */ | ||
464 | #if ENABLE_GETOPT_LONG | 463 | #if ENABLE_GETOPT_LONG |
465 | while ((c = getopt_long(argc, argv, applet_opts, | 464 | while ((c = getopt_long(argc, argv, applet_opts, |
466 | applet_long_options, NULL)) >= 0) { | 465 | applet_long_options, NULL)) != -1) { |
467 | #else | 466 | #else |
468 | while ((c = getopt(argc, argv, applet_opts)) >= 0) { | 467 | while ((c = getopt(argc, argv, applet_opts)) != -1) { |
469 | #endif /* ENABLE_GETOPT_LONG */ | 468 | #endif /* ENABLE_GETOPT_LONG */ |
469 | c &= 0xff; /* fight libc's sign extends */ | ||
470 | loop_arg_is_opt: | 470 | loop_arg_is_opt: |
471 | for (on_off = complementary; on_off->opt != c; on_off++) { | 471 | for (on_off = complementary; on_off->opt != c; on_off++) { |
472 | /* c==0 if long opt have non NULL flag */ | 472 | /* c==0 if long opt have non NULL flag */ |
diff --git a/libbb/llist.c b/libbb/llist.c index 8bf89a595..8a74832ee 100644 --- a/libbb/llist.c +++ b/libbb/llist.c | |||
@@ -62,3 +62,17 @@ void llist_free(llist_t *elm, void (*freeit)(void *data)) | |||
62 | if (freeit) freeit(data); | 62 | if (freeit) freeit(data); |
63 | } | 63 | } |
64 | } | 64 | } |
65 | |||
66 | /* Reverse list order. Useful since getopt32 saves option params | ||
67 | * in reverse order */ | ||
68 | llist_t* rev_llist(llist_t *list) | ||
69 | { | ||
70 | llist_t *new = NULL; | ||
71 | while (list) { | ||
72 | llist_t *next = list->link; | ||
73 | list->link = new; | ||
74 | new = list; | ||
75 | list = next; | ||
76 | } | ||
77 | return new; | ||
78 | } | ||
diff --git a/networking/wget.c b/networking/wget.c index 91e5e655a..a0d3e15e8 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -86,42 +86,22 @@ static char *base64enc(unsigned char *p, char *buf, int len) | |||
86 | } | 86 | } |
87 | #endif | 87 | #endif |
88 | 88 | ||
89 | #define WGET_OPT_CONTINUE 1 | ||
90 | #define WGET_OPT_QUIET 2 | ||
91 | #define WGET_OPT_PASSIVE 4 | ||
92 | #define WGET_OPT_OUTNAME 8 | ||
93 | #define WGET_OPT_HEADER 16 | ||
94 | #define WGET_OPT_PREFIX 32 | ||
95 | #define WGET_OPT_PROXY 64 | ||
96 | #define WGET_OPT_USER_AGENT 128 | ||
97 | |||
98 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS | ||
99 | static const struct option wget_long_options[] = { | ||
100 | { "continue", 0, NULL, 'c' }, | ||
101 | { "quiet", 0, NULL, 'q' }, | ||
102 | { "passive-ftp", 0, NULL, 139 }, /* FIXME: what is this - 139?? */ | ||
103 | { "output-document", 1, NULL, 'O' }, | ||
104 | { "header", 1, NULL, 131 }, | ||
105 | { "directory-prefix",1, NULL, 'P' }, | ||
106 | { "proxy", 1, NULL, 'Y' }, | ||
107 | { "user-agent", 1, NULL, 'U' }, | ||
108 | { 0, 0, 0, 0 } | ||
109 | }; | ||
110 | #endif | ||
111 | |||
112 | int wget_main(int argc, char **argv) | 89 | int wget_main(int argc, char **argv) |
113 | { | 90 | { |
91 | char buf[512]; | ||
92 | struct host_info server, target; | ||
93 | struct sockaddr_in s_in; | ||
114 | int n, status; | 94 | int n, status; |
115 | int try = 5; | ||
116 | int port; | 95 | int port; |
96 | int try = 5; | ||
117 | unsigned opt; | 97 | unsigned opt; |
98 | char *s; | ||
118 | char *proxy = 0; | 99 | char *proxy = 0; |
119 | char *dir_prefix = NULL; | 100 | char *dir_prefix = NULL; |
120 | char *s, buf[512]; | 101 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
121 | char *extra_headers = NULL; | 102 | char *extra_headers = NULL; |
122 | struct host_info server, target; | ||
123 | struct sockaddr_in s_in; | ||
124 | llist_t *headers_llist = NULL; | 103 | llist_t *headers_llist = NULL; |
104 | #endif | ||
125 | 105 | ||
126 | /* server.allocated = target.allocated = NULL; */ | 106 | /* server.allocated = target.allocated = NULL; */ |
127 | 107 | ||
@@ -137,21 +117,46 @@ int wget_main(int argc, char **argv) | |||
137 | /* | 117 | /* |
138 | * Crack command line. | 118 | * Crack command line. |
139 | */ | 119 | */ |
140 | opt_complementary = "-1:\203::"; | 120 | enum { |
121 | WGET_OPT_CONTINUE = 0x1, | ||
122 | WGET_OPT_QUIET = 0x2, | ||
123 | WGET_OPT_OUTNAME = 0x4, | ||
124 | WGET_OPT_PREFIX = 0x8, | ||
125 | WGET_OPT_PROXY = 0x10, | ||
126 | WGET_OPT_USER_AGENT = 0x20, | ||
127 | WGET_OPT_PASSIVE = 0x40, | ||
128 | WGET_OPT_HEADER = 0x80, | ||
129 | }; | ||
141 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS | 130 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
131 | static const struct option wget_long_options[] = { | ||
132 | // name, has_arg, flag, val | ||
133 | { "continue", no_argument, NULL, 'c' }, | ||
134 | { "quiet", no_argument, NULL, 'q' }, | ||
135 | { "output-document", required_argument, NULL, 'O' }, | ||
136 | { "directory-prefix", required_argument, NULL, 'P' }, | ||
137 | { "proxy", required_argument, NULL, 'Y' }, | ||
138 | { "user-agent", required_argument, NULL, 'U' }, | ||
139 | { "passive-ftp", no_argument, NULL, 0xff }, | ||
140 | { "header", required_argument, NULL, 0xfe }, | ||
141 | { 0, 0, 0, 0 } | ||
142 | }; | ||
142 | applet_long_options = wget_long_options; | 143 | applet_long_options = wget_long_options; |
143 | #endif | 144 | #endif |
144 | opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:", | 145 | opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::"); |
145 | &fname_out, &headers_llist, | 146 | opt = getopt32(argc, argv, "cqO:P:Y:U:", |
146 | &dir_prefix, &proxy_flag, &user_agent); | 147 | &fname_out, &dir_prefix, |
148 | &proxy_flag, &user_agent | ||
149 | USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist) | ||
150 | ); | ||
147 | if (strcmp(proxy_flag, "off") == 0) { | 151 | if (strcmp(proxy_flag, "off") == 0) { |
148 | /* Use the proxy if necessary. */ | 152 | /* Use the proxy if necessary. */ |
149 | use_proxy = 0; | 153 | use_proxy = 0; |
150 | } | 154 | } |
155 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS | ||
151 | if (headers_llist) { | 156 | if (headers_llist) { |
152 | int size = 1; | 157 | int size = 1; |
153 | char *cp; | 158 | char *cp; |
154 | llist_t *ll = headers_llist; | 159 | llist_t *ll = headers_llist = rev_llist(headers_llist); |
155 | while (ll) { | 160 | while (ll) { |
156 | size += strlen(ll->data) + 2; | 161 | size += strlen(ll->data) + 2; |
157 | ll = ll->link; | 162 | ll = ll->link; |
@@ -162,6 +167,7 @@ int wget_main(int argc, char **argv) | |||
162 | headers_llist = headers_llist->link; | 167 | headers_llist = headers_llist->link; |
163 | } | 168 | } |
164 | } | 169 | } |
170 | #endif | ||
165 | 171 | ||
166 | parse_url(argv[optind], &target); | 172 | parse_url(argv[optind], &target); |
167 | server.host = target.host; | 173 | server.host = target.host; |
@@ -281,8 +287,10 @@ int wget_main(int argc, char **argv) | |||
281 | 287 | ||
282 | if (beg_range) | 288 | if (beg_range) |
283 | fprintf(sfp, "Range: bytes="OFF_FMT"-\r\n", beg_range); | 289 | fprintf(sfp, "Range: bytes="OFF_FMT"-\r\n", beg_range); |
290 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS | ||
284 | if (extra_headers) | 291 | if (extra_headers) |
285 | fputs(extra_headers, sfp); | 292 | fputs(extra_headers, sfp); |
293 | #endif | ||
286 | fprintf(sfp, "Connection: close\r\n\r\n"); | 294 | fprintf(sfp, "Connection: close\r\n\r\n"); |
287 | 295 | ||
288 | /* | 296 | /* |