diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-24 15:04:00 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-24 15:04:00 +0000 |
commit | 7710563858a1e1d1a0af81d18222eee0259c00f2 (patch) | |
tree | 85fca36c351836e9c92edf4ea1fe028eeccf9733 | |
parent | 06783a51409e4245a54c751e426f1c5eaf117e39 (diff) | |
download | busybox-w32-7710563858a1e1d1a0af81d18222eee0259c00f2.tar.gz busybox-w32-7710563858a1e1d1a0af81d18222eee0259c00f2.tar.bz2 busybox-w32-7710563858a1e1d1a0af81d18222eee0259c00f2.zip |
wget: code shrink, move data out of bss
-rw-r--r-- | networking/wget.c | 76 |
1 files changed, 48 insertions, 28 deletions
diff --git a/networking/wget.c b/networking/wget.c index 39d63f116..2cc426f19 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -6,10 +6,6 @@ | |||
6 | * | 6 | * |
7 | */ | 7 | */ |
8 | 8 | ||
9 | /* We want libc to give us xxx64 functions also */ | ||
10 | /* http://www.unix.org/version2/whatsnew/lfs20mar.html */ | ||
11 | //#define _LARGEFILE64_SOURCE 1 | ||
12 | |||
13 | #include <getopt.h> /* for struct option */ | 9 | #include <getopt.h> /* for struct option */ |
14 | #include "libbb.h" | 10 | #include "libbb.h" |
15 | 11 | ||
@@ -23,21 +19,44 @@ struct host_info { | |||
23 | char *user; | 19 | char *user; |
24 | }; | 20 | }; |
25 | 21 | ||
22 | |||
23 | /* Globals (can be accessed from signal handlers) */ | ||
24 | struct globals { | ||
25 | off_t content_len; /* Content-length of the file */ | ||
26 | off_t beg_range; /* Range at which continue begins */ | ||
27 | #if ENABLE_FEATURE_WGET_STATUSBAR | ||
28 | off_t lastsize; | ||
29 | off_t totalsize; | ||
30 | off_t transferred; /* Number of bytes transferred so far */ | ||
31 | const char *curfile; /* Name of current file being transferred */ | ||
32 | unsigned lastupdate_sec; | ||
33 | unsigned start_sec; | ||
34 | #endif | ||
35 | bool chunked; /* chunked transfer encoding */ | ||
36 | }; | ||
37 | #define G (*(struct globals*)&bb_common_bufsiz1) | ||
38 | struct BUG_G_too_big { | ||
39 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; | ||
40 | }; | ||
41 | #define content_len (G.content_len ) | ||
42 | #define beg_range (G.beg_range ) | ||
43 | #define lastsize (G.lastsize ) | ||
44 | #define totalsize (G.totalsize ) | ||
45 | #define transferred (G.transferred ) | ||
46 | #define curfile (G.curfile ) | ||
47 | #define lastupdate_sec (G.lastupdate_sec ) | ||
48 | #define start_sec (G.start_sec ) | ||
49 | #define chunked (G.chunked ) | ||
50 | #define INIT_G() do { } while (0) | ||
51 | |||
52 | |||
26 | static void parse_url(char *url, struct host_info *h); | 53 | static void parse_url(char *url, struct host_info *h); |
27 | static FILE *open_socket(len_and_sockaddr *lsa); | 54 | static FILE *open_socket(len_and_sockaddr *lsa); |
28 | static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*,int *istrunc*/ ); | 55 | static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*,int *istrunc*/ ); |
29 | static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf); | 56 | static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf); |
30 | 57 | ||
31 | /* Globals (can be accessed from signal handlers */ | ||
32 | static off_t content_len; /* Content-length of the file */ | ||
33 | static off_t beg_range; /* Range at which continue begins */ | ||
34 | #if ENABLE_FEATURE_WGET_STATUSBAR | ||
35 | static off_t transferred; /* Number of bytes transferred so far */ | ||
36 | #endif | ||
37 | static bool chunked; /* chunked transfer encoding */ | ||
38 | #if ENABLE_FEATURE_WGET_STATUSBAR | 58 | #if ENABLE_FEATURE_WGET_STATUSBAR |
39 | static void progressmeter(int flag); | 59 | static void progressmeter(int flag); |
40 | static const char *curfile; /* Name of current file being transferred */ | ||
41 | enum { | 60 | enum { |
42 | STALLTIME = 5 /* Seconds when xfer considered "stalled" */ | 61 | STALLTIME = 5 /* Seconds when xfer considered "stalled" */ |
43 | }; | 62 | }; |
@@ -105,7 +124,6 @@ int wget_main(int argc, char **argv) | |||
105 | char *extra_headers = NULL; | 124 | char *extra_headers = NULL; |
106 | llist_t *headers_llist = NULL; | 125 | llist_t *headers_llist = NULL; |
107 | #endif | 126 | #endif |
108 | |||
109 | FILE *sfp = NULL; /* socket to web/ftp server */ | 127 | FILE *sfp = NULL; /* socket to web/ftp server */ |
110 | FILE *dfp = NULL; /* socket to ftp server (data) */ | 128 | FILE *dfp = NULL; /* socket to ftp server (data) */ |
111 | char *fname_out = NULL; /* where to direct output (-O) */ | 129 | char *fname_out = NULL; /* where to direct output (-O) */ |
@@ -114,6 +132,7 @@ int wget_main(int argc, char **argv) | |||
114 | bool use_proxy = 1; /* Use proxies if env vars are set */ | 132 | bool use_proxy = 1; /* Use proxies if env vars are set */ |
115 | const char *proxy_flag = "on"; /* Use proxies if env vars are set */ | 133 | const char *proxy_flag = "on"; /* Use proxies if env vars are set */ |
116 | const char *user_agent = "Wget";/* "User-Agent" header field */ | 134 | const char *user_agent = "Wget";/* "User-Agent" header field */ |
135 | |||
117 | static const char keywords[] ALIGN1 = | 136 | static const char keywords[] ALIGN1 = |
118 | "content-length\0""transfer-encoding\0""chunked\0""location\0"; | 137 | "content-length\0""transfer-encoding\0""chunked\0""location\0"; |
119 | enum { | 138 | enum { |
@@ -143,6 +162,11 @@ int wget_main(int argc, char **argv) | |||
143 | "passive-ftp\0" No_argument "\xff" | 162 | "passive-ftp\0" No_argument "\xff" |
144 | "header\0" Required_argument "\xfe" | 163 | "header\0" Required_argument "\xfe" |
145 | ; | 164 | ; |
165 | #endif | ||
166 | |||
167 | INIT_G(); | ||
168 | |||
169 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS | ||
146 | applet_long_options = wget_longopts; | 170 | applet_long_options = wget_longopts; |
147 | #endif | 171 | #endif |
148 | /* server.allocated = target.allocated = NULL; */ | 172 | /* server.allocated = target.allocated = NULL; */ |
@@ -337,7 +361,7 @@ int wget_main(int argc, char **argv) | |||
337 | } | 361 | } |
338 | if (key == KEY_transfer_encoding) { | 362 | if (key == KEY_transfer_encoding) { |
339 | if (index_in_strings(keywords, str_tolower(str)) + 1 != KEY_chunked) | 363 | if (index_in_strings(keywords, str_tolower(str)) + 1 != KEY_chunked) |
340 | bb_error_msg_and_die("server wants to do %s transfer encoding", str); | 364 | bb_error_msg_and_die("transfer encoding '%s' is not supported", str); |
341 | chunked = got_clen = 1; | 365 | chunked = got_clen = 1; |
342 | } | 366 | } |
343 | if (key == KEY_location) { | 367 | if (key == KEY_location) { |
@@ -432,22 +456,18 @@ int wget_main(int argc, char **argv) | |||
432 | } | 456 | } |
433 | 457 | ||
434 | if (ftpcmd("RETR ", target.path, sfp, buf) > 150) | 458 | if (ftpcmd("RETR ", target.path, sfp, buf) > 150) |
435 | bb_error_msg_and_die("bad response to RETR: %s", buf); | 459 | bb_error_msg_and_die("bad response to %s: %s", "RETR", buf); |
436 | } | 460 | } |
461 | |||
437 | if (opt & WGET_OPT_SPIDER) { | 462 | if (opt & WGET_OPT_SPIDER) { |
438 | if (ENABLE_FEATURE_CLEAN_UP) | 463 | if (ENABLE_FEATURE_CLEAN_UP) |
439 | fclose(sfp); | 464 | fclose(sfp); |
440 | goto done; | 465 | return EXIT_SUCCESS; |
441 | } | 466 | } |
442 | 467 | ||
443 | /* | 468 | /* |
444 | * Retrieve file | 469 | * Retrieve file |
445 | */ | 470 | */ |
446 | if (chunked) { | ||
447 | fgets(buf, sizeof(buf), dfp); | ||
448 | content_len = STRTOOFF(buf, NULL, 16); | ||
449 | /* FIXME: error check?? */ | ||
450 | } | ||
451 | 471 | ||
452 | /* Do it before progressmeter (want to have nice error message) */ | 472 | /* Do it before progressmeter (want to have nice error message) */ |
453 | if (output_fd < 0) | 473 | if (output_fd < 0) |
@@ -457,6 +477,9 @@ int wget_main(int argc, char **argv) | |||
457 | if (!(opt & WGET_OPT_QUIET)) | 477 | if (!(opt & WGET_OPT_QUIET)) |
458 | progressmeter(-1); | 478 | progressmeter(-1); |
459 | 479 | ||
480 | if (chunked) | ||
481 | goto get_clen; | ||
482 | |||
460 | /* Loops only if chunked */ | 483 | /* Loops only if chunked */ |
461 | while (1) { | 484 | while (1) { |
462 | while (content_len > 0 || !got_clen) { | 485 | while (content_len > 0 || !got_clen) { |
@@ -485,6 +508,7 @@ int wget_main(int argc, char **argv) | |||
485 | break; | 508 | break; |
486 | 509 | ||
487 | safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */ | 510 | safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */ |
511 | get_clen: | ||
488 | safe_fgets(buf, sizeof(buf), dfp); | 512 | safe_fgets(buf, sizeof(buf), dfp); |
489 | content_len = STRTOOFF(buf, NULL, 16); | 513 | content_len = STRTOOFF(buf, NULL, 16); |
490 | /* FIXME: error check? */ | 514 | /* FIXME: error check? */ |
@@ -501,9 +525,9 @@ int wget_main(int argc, char **argv) | |||
501 | bb_error_msg_and_die("ftp error: %s", buf+4); | 525 | bb_error_msg_and_die("ftp error: %s", buf+4); |
502 | ftpcmd("QUIT", NULL, sfp, buf); | 526 | ftpcmd("QUIT", NULL, sfp, buf); |
503 | } | 527 | } |
504 | done: | 528 | |
505 | exit(EXIT_SUCCESS); | 529 | return EXIT_SUCCESS; |
506 | } | 530 | } /* wget_main() */ |
507 | 531 | ||
508 | 532 | ||
509 | static void parse_url(char *src_url, struct host_info *h) | 533 | static void parse_url(char *src_url, struct host_info *h) |
@@ -688,10 +712,6 @@ static void alarmtimer(int iwait) | |||
688 | static void | 712 | static void |
689 | progressmeter(int flag) | 713 | progressmeter(int flag) |
690 | { | 714 | { |
691 | static unsigned lastupdate_sec; | ||
692 | static unsigned start_sec; | ||
693 | static off_t lastsize, totalsize; | ||
694 | |||
695 | off_t abbrevsize; | 715 | off_t abbrevsize; |
696 | unsigned since_last_update, elapsed; | 716 | unsigned since_last_update, elapsed; |
697 | unsigned ratio; | 717 | unsigned ratio; |