diff options
author | Rob Landley <rob@landley.net> | 2006-06-13 17:10:26 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-06-13 17:10:26 +0000 |
commit | 19a3940f15d60af7781fc294eae123c64e88ae71 (patch) | |
tree | d0503dea659823700cd6199e95f30a5305c29cdb | |
parent | 76ef08c5e3349accfe6f10a97def7c7f7e85c0ad (diff) | |
download | busybox-w32-19a3940f15d60af7781fc294eae123c64e88ae71.tar.gz busybox-w32-19a3940f15d60af7781fc294eae123c64e88ae71.tar.bz2 busybox-w32-19a3940f15d60af7781fc294eae123c64e88ae71.zip |
Patch from Denis Vlasenko:
* Rename a var: statbytes -> transferred
* cursize == transferred, always. Nuke cursize.
* Make progressmeter() a nop if !CONFIG_FEATURE_WGET_STATUSBAR
(reduces #ifdef forest)
* double elapsed -> int elapsed
* Do not sprintf to buf first and then write(STDERR) it,
just fprintf directly to stderr
* Progress bar printing code made smaller
* Style fixes
-rw-r--r-- | networking/wget.c | 101 |
1 files changed, 44 insertions, 57 deletions
diff --git a/networking/wget.c b/networking/wget.c index 506d329ca..6c1aa74b7 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -42,15 +42,19 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); | |||
42 | static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf); | 42 | static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf); |
43 | 43 | ||
44 | /* Globals (can be accessed from signal handlers */ | 44 | /* Globals (can be accessed from signal handlers */ |
45 | static off_t filesize = 0; /* content-length of the file */ | 45 | static off_t filesize; /* content-length of the file */ |
46 | static int chunked = 0; /* chunked transfer encoding */ | 46 | static int chunked; /* chunked transfer encoding */ |
47 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR | 47 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR |
48 | static void progressmeter(int flag); | 48 | static void progressmeter(int flag); |
49 | static char *curfile; /* Name of current file being transferred. */ | 49 | static char *curfile; /* Name of current file being transferred. */ |
50 | static struct timeval start; /* Time a transfer started. */ | 50 | static struct timeval start; /* Time a transfer started. */ |
51 | static volatile unsigned long statbytes = 0; /* Number of bytes transferred so far. */ | 51 | static off_t transferred; /* Number of bytes transferred so far. */ |
52 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ | 52 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ |
53 | static const int STALLTIME = 5; | 53 | enum { |
54 | STALLTIME = 5 | ||
55 | }; | ||
56 | #else | ||
57 | static inline void progressmeter(int flag) {} | ||
54 | #endif | 58 | #endif |
55 | 59 | ||
56 | static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue) | 60 | static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue) |
@@ -321,7 +325,7 @@ int wget_main(int argc, char **argv) | |||
321 | if (use_proxy) { | 325 | if (use_proxy) { |
322 | const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n"; | 326 | const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n"; |
323 | #ifdef CONFIG_FEATURE_WGET_IP6_LITERAL | 327 | #ifdef CONFIG_FEATURE_WGET_IP6_LITERAL |
324 | if (strchr (target.host, ':')) | 328 | if (strchr(target.host, ':')) |
325 | format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n"; | 329 | format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n"; |
326 | #endif | 330 | #endif |
327 | fprintf(sfp, format, | 331 | fprintf(sfp, format, |
@@ -504,17 +508,17 @@ read_response: | |||
504 | fgets(buf, sizeof(buf), dfp); | 508 | fgets(buf, sizeof(buf), dfp); |
505 | filesize = strtol(buf, (char **) NULL, 16); | 509 | filesize = strtol(buf, (char **) NULL, 16); |
506 | } | 510 | } |
507 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR | 511 | |
508 | if (quiet_flag==FALSE) | 512 | if (quiet_flag==FALSE) |
509 | progressmeter(-1); | 513 | progressmeter(-1); |
510 | #endif | 514 | |
511 | do { | 515 | do { |
512 | while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, ((chunked || got_clen) && (filesize < sizeof(buf)) ? filesize : sizeof(buf)), dfp)) > 0) { | 516 | while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, ((chunked || got_clen) && (filesize < sizeof(buf)) ? filesize : sizeof(buf)), dfp)) > 0) { |
513 | if (safe_fwrite(buf, 1, n, output) != n) { | 517 | if (safe_fwrite(buf, 1, n, output) != n) { |
514 | bb_perror_msg_and_die(bb_msg_write_error); | 518 | bb_perror_msg_and_die(bb_msg_write_error); |
515 | } | 519 | } |
516 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR | 520 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR |
517 | statbytes+=n; | 521 | transferred += n; |
518 | #endif | 522 | #endif |
519 | if (got_clen) { | 523 | if (got_clen) { |
520 | filesize -= n; | 524 | filesize -= n; |
@@ -534,10 +538,10 @@ read_response: | |||
534 | bb_perror_msg_and_die(bb_msg_read_error); | 538 | bb_perror_msg_and_die(bb_msg_read_error); |
535 | } | 539 | } |
536 | } while (chunked); | 540 | } while (chunked); |
537 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR | 541 | |
538 | if (quiet_flag==FALSE) | 542 | if (quiet_flag==FALSE) |
539 | progressmeter(1); | 543 | progressmeter(1); |
540 | #endif | 544 | |
541 | if ((use_proxy == 0) && target.is_ftp) { | 545 | if ((use_proxy == 0) && target.is_ftp) { |
542 | fclose(dfp); | 546 | fclose(dfp); |
543 | if (ftpcmd(NULL, NULL, sfp, buf) != 226) | 547 | if (ftpcmd(NULL, NULL, sfp, buf) != 226) |
@@ -724,13 +728,12 @@ alarmtimer(int wait) | |||
724 | static void | 728 | static void |
725 | progressmeter(int flag) | 729 | progressmeter(int flag) |
726 | { | 730 | { |
727 | static const char prefixes[] = " KMGTP"; | ||
728 | static struct timeval lastupdate; | 731 | static struct timeval lastupdate; |
729 | static off_t lastsize, totalsize; | 732 | static off_t lastsize, totalsize; |
733 | |||
730 | struct timeval now, td, wait; | 734 | struct timeval now, td, wait; |
731 | off_t cursize, abbrevsize; | 735 | off_t abbrevsize; |
732 | double elapsed; | 736 | int elapsed, ratio, barlength, i; |
733 | int ratio, barlength, i, remaining; | ||
734 | char buf[256]; | 737 | char buf[256]; |
735 | 738 | ||
736 | if (flag == -1) { | 739 | if (flag == -1) { |
@@ -741,68 +744,52 @@ progressmeter(int flag) | |||
741 | } | 744 | } |
742 | 745 | ||
743 | (void) gettimeofday(&now, (struct timezone *) 0); | 746 | (void) gettimeofday(&now, (struct timezone *) 0); |
744 | cursize = statbytes; | 747 | ratio = 100; |
745 | if (totalsize != 0 && !chunked) { | 748 | if (totalsize != 0 && !chunked) { |
746 | ratio = 100.0 * cursize / totalsize; | 749 | ratio = (int) (100 * transferred / totalsize); |
747 | ratio = MAX(ratio, 0); | ||
748 | ratio = MIN(ratio, 100); | 750 | ratio = MIN(ratio, 100); |
749 | } else | 751 | } |
750 | ratio = 100; | ||
751 | 752 | ||
752 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); | 753 | fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio); |
753 | 754 | ||
754 | barlength = getttywidth() - 51; | 755 | barlength = getttywidth() - 51; |
755 | if (barlength > 0) { | 756 | if (barlength > 0 && barlength < sizeof(buf)) { |
756 | i = barlength * ratio / 100; | 757 | i = barlength * ratio / 100; |
757 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 758 | memset(buf, '*', i); |
758 | "|%.*s%*s|", i, | 759 | memset(buf + i, ' ', barlength - i); |
759 | "*****************************************************************************" | 760 | buf[barlength] = '\0'; |
760 | "*****************************************************************************", | 761 | fprintf(stderr, "|%s|", buf); |
761 | barlength - i, ""); | ||
762 | } | 762 | } |
763 | i = 0; | 763 | i = 0; |
764 | abbrevsize = cursize; | 764 | abbrevsize = transferred; |
765 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { | 765 | while (abbrevsize >= 100000) { |
766 | i++; | 766 | i++; |
767 | abbrevsize >>= 10; | 767 | abbrevsize >>= 10; |
768 | } | 768 | } |
769 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ", | 769 | /* See http://en.wikipedia.org/wiki/Tera */ |
770 | (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : | 770 | fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' '); |
771 | 'B'); | ||
772 | 771 | ||
773 | timersub(&now, &lastupdate, &wait); | 772 | timersub(&now, &lastupdate, &wait); |
774 | if (cursize > lastsize) { | 773 | if (transferred > lastsize) { |
775 | lastupdate = now; | 774 | lastupdate = now; |
776 | lastsize = cursize; | 775 | lastsize = transferred; |
777 | if (wait.tv_sec >= STALLTIME) { | 776 | if (wait.tv_sec >= STALLTIME) |
778 | start.tv_sec += wait.tv_sec; | 777 | timeradd(&start, &wait, &start); |
779 | start.tv_usec += wait.tv_usec; | ||
780 | } | ||
781 | wait.tv_sec = 0; | 778 | wait.tv_sec = 0; |
782 | } | 779 | } |
783 | timersub(&now, &start, &td); | 780 | timersub(&now, &start, &td); |
784 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); | 781 | elapsed = td.tv_sec; |
785 | 782 | ||
786 | if (wait.tv_sec >= STALLTIME) { | 783 | if (wait.tv_sec >= STALLTIME) { |
787 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 784 | fprintf(stderr, " - stalled -"); |
788 | " - stalled -"); | 785 | } else if (transferred <= 0 || elapsed <= 0 || transferred > totalsize || chunked) { |
789 | } else if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalsize || chunked) { | 786 | fprintf(stderr, "--:--:-- ETA"); |
790 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | ||
791 | " --:-- ETA"); | ||
792 | } else { | 787 | } else { |
793 | remaining = (int) (totalsize / (statbytes / elapsed) - elapsed); | 788 | /* totalsize / (transferred/elapsed) - elapsed: */ |
794 | i = remaining / 3600; | 789 | int eta = (int) (totalsize*elapsed/transferred - elapsed); |
795 | if (i) | 790 | i = eta % 3600; |
796 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 791 | fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60); |
797 | "%2d:", i); | ||
798 | else | ||
799 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | ||
800 | " "); | ||
801 | i = remaining % 3600; | ||
802 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | ||
803 | "%02d:%02d ETA", i / 60, i % 60); | ||
804 | } | 792 | } |
805 | write(STDERR_FILENO, buf, strlen(buf)); | ||
806 | 793 | ||
807 | if (flag == -1) { | 794 | if (flag == -1) { |
808 | struct sigaction sa; | 795 | struct sigaction sa; |
@@ -813,7 +800,7 @@ progressmeter(int flag) | |||
813 | alarmtimer(1); | 800 | alarmtimer(1); |
814 | } else if (flag == 1) { | 801 | } else if (flag == 1) { |
815 | alarmtimer(0); | 802 | alarmtimer(0); |
816 | statbytes = 0; | 803 | transferred = 0; |
817 | putc('\n', stderr); | 804 | putc('\n', stderr); |
818 | } | 805 | } |
819 | } | 806 | } |