4b55b93f4c2a7c9af12de9462d7428fd26d6f018
[oweals/busybox.git] / networking / wget.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * wget - retrieve a file using HTTP or FTP
4  *
5  * Chip Rosenthal Covad Communications <chip@laserlink.net>
6  *
7  */
8
9 #include "busybox.h"
10 #include <getopt.h>
11
12 struct host_info {
13         char *host;
14         int port;
15         char *path;
16         int is_ftp;
17         char *user;
18 };
19
20 static void parse_url(char *url, struct host_info *h);
21 static FILE *open_socket(struct sockaddr_in *s_in);
22 static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
23 static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
24
25 /* Globals (can be accessed from signal handlers */
26 static off_t filesize;          /* content-length of the file */
27 static int chunked;             /* chunked transfer encoding */
28 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
29 static void progressmeter(int flag);
30 static char *curfile;           /* Name of current file being transferred. */
31 static struct timeval start;    /* Time a transfer started. */
32 static off_t transferred;       /* Number of bytes transferred so far. */
33 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
34 enum {
35         STALLTIME = 5
36 };
37 #else
38 static void progressmeter(int flag) {}
39 #endif
40
41 /* Read NMEMB elements of SIZE bytes into PTR from STREAM.  Returns the
42  * number of elements read, and a short count if an eof or non-interrupt
43  * error is encountered.  */
44 static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
45 {
46         size_t ret = 0;
47
48         do {
49                 clearerr(stream);
50                 ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
51         } while (ret < nmemb && ferror(stream) && errno == EINTR);
52
53         return ret;
54 }
55
56 /* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
57  * Returns S, or NULL if an eof or non-interrupt error is encountered.  */
58 static char *safe_fgets(char *s, int size, FILE *stream)
59 {
60         char *ret;
61
62         do {
63                 clearerr(stream);
64                 ret = fgets(s, size, stream);
65         } while (ret == NULL && ferror(stream) && errno == EINTR);
66
67         return ret;
68 }
69
70 #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
71 /*
72  *  Base64-encode character string and return the string.
73  */
74 static char *base64enc(unsigned char *p, char *buf, int len)
75 {
76         bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
77         return buf;
78 }
79 #endif
80
81 #define WGET_OPT_CONTINUE       1
82 #define WGET_OPT_QUIET  2
83 #define WGET_OPT_PASSIVE        4
84 #define WGET_OPT_OUTNAME        8
85 #define WGET_OPT_HEADER 16
86 #define WGET_OPT_PREFIX 32
87 #define WGET_OPT_PROXY  64
88 #define WGET_OPT_USER_AGENT     128
89
90 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
91 static const struct option wget_long_options[] = {
92         { "continue",        0, NULL, 'c' },
93         { "quiet",           0, NULL, 'q' },
94         { "passive-ftp",     0, NULL, 139 },
95         { "output-document", 1, NULL, 'O' },
96         { "header",          1, NULL, 131 },
97         { "directory-prefix",1, NULL, 'P' },
98         { "proxy",           1, NULL, 'Y' },
99         { "user-agent",      1, NULL, 'U' },
100         { 0,                 0, 0, 0 }
101 };
102 #endif
103
104 int wget_main(int argc, char **argv)
105 {
106         int n, try=5, status;
107         unsigned long opt;
108         int port;
109         char *proxy = 0;
110         char *dir_prefix=NULL;
111         char *s, buf[512];
112         char extra_headers[1024];
113         char *extra_headers_ptr = extra_headers;
114         int extra_headers_left = sizeof(extra_headers);
115         struct host_info server, target;
116         struct sockaddr_in s_in;
117         llist_t *headers_llist = NULL;
118
119         FILE *sfp = NULL;               /* socket to web/ftp server         */
120         FILE *dfp = NULL;               /* socket to ftp server (data)      */
121         char *fname_out = NULL;         /* where to direct output (-O)      */
122         int do_continue = 0;            /* continue a prev transfer (-c)    */
123         off64_t beg_range = 0;          /*   range at which continue begins */
124         int got_clen = 0;               /* got content-length: from server  */
125         int output_fd = -1;
126         int quiet_flag = FALSE;         /* Be verry, verry quiet...         */
127         int use_proxy = 1;              /* Use proxies if env vars are set  */
128         char *proxy_flag = "on";        /* Use proxies if env vars are set  */
129         char *user_agent = "Wget"; /* Content of the "User-Agent" header field */
130
131         /*
132          * Crack command line.
133          */
134         bb_opt_complementally = "-1:\203::";
135 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
136         bb_applet_long_options = wget_long_options;
137 #endif
138         opt = bb_getopt_ulflags(argc, argv, "cq\213O:\203:P:Y:U:",
139                                         &fname_out, &headers_llist,
140                                         &dir_prefix, &proxy_flag, &user_agent);
141         if (opt & WGET_OPT_CONTINUE) {
142                 ++do_continue;
143         }
144         if (opt & WGET_OPT_QUIET) {
145                 quiet_flag = TRUE;
146         }
147         if (strcmp(proxy_flag, "off") == 0) {
148                 /* Use the proxy if necessary. */
149                 use_proxy = 0;
150         }
151         if (opt & WGET_OPT_HEADER) {
152                 while (headers_llist) {
153                         int arglen = strlen(headers_llist->data);
154                         if (extra_headers_left - arglen - 2 <= 0)
155                                 bb_error_msg_and_die("extra_headers buffer too small "
156                                         "(need %i)", extra_headers_left - arglen);
157                         strcpy(extra_headers_ptr, headers_llist->data);
158                         extra_headers_ptr += arglen;
159                         extra_headers_left -= ( arglen + 2 );
160                         *extra_headers_ptr++ = '\r';
161                         *extra_headers_ptr++ = '\n';
162                         *(extra_headers_ptr + 1) = 0;
163                         headers_llist = headers_llist->link;
164                 }
165         }
166
167         parse_url(argv[optind], &target);
168         server.host = target.host;
169         server.port = target.port;
170
171         /*
172          * Use the proxy if necessary.
173          */
174         if (use_proxy) {
175                 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
176                 if (proxy && *proxy) {
177                         parse_url(xstrdup(proxy), &server);
178                 } else {
179                         use_proxy = 0;
180                 }
181         }
182
183         /* Guess an output filename */
184         if (!fname_out) {
185                 // Dirty hack. Needed because bb_get_last_path_component
186                 // will destroy trailing / by storing '\0' in last byte!
187                 if (*target.path && target.path[strlen(target.path)-1] != '/') {
188                         fname_out =
189 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
190                                 curfile =
191 #endif
192                                 bb_get_last_path_component(target.path);
193                 }
194                 if (!fname_out || !fname_out[0]) {
195                         fname_out =
196 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
197                                 curfile =
198 #endif
199                                 "index.html";
200                 }
201                 if (dir_prefix != NULL)
202                         fname_out = concat_path_file(dir_prefix, fname_out);
203 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
204         } else {
205                 curfile = bb_get_last_path_component(fname_out);
206 #endif
207         }
208         if (do_continue && !fname_out)
209                 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
210
211         /*
212          * Determine where to start transfer.
213          */
214         if (!strcmp(fname_out, "-")) {
215                 output_fd = 1;
216                 quiet_flag = TRUE;
217                 do_continue = 0;
218         } else if (do_continue) {
219                 output_fd = open(fname_out, O_WRONLY|O_LARGEFILE);
220                 if (output_fd >= 0) {
221                         beg_range = lseek64(output_fd, 0, SEEK_END);
222                         if (beg_range == (off64_t)-1)
223                                 bb_perror_msg_and_die("lseek64");
224                 }
225                 /* File doesn't exist. We do not create file here yet.
226                    We are not sure it exists on remove side */
227         }
228
229         /* We want to do exactly _one_ DNS lookup, since some
230          * sites (i.e. ftp.us.debian.org) use round-robin DNS
231          * and we want to connect to only one IP... */
232         bb_lookup_host(&s_in, server.host);
233         s_in.sin_port = server.port;
234         if (quiet_flag == FALSE) {
235                 printf("Connecting to %s[%s]:%d\n",
236                                 server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
237         }
238
239         if (use_proxy || !target.is_ftp) {
240                 /*
241                  *  HTTP session
242                  */
243                 do {
244                         got_clen = chunked = 0;
245
246                         if (!--try)
247                                 bb_error_msg_and_die("too many redirections");
248
249                         /*
250                          * Open socket to http server
251                          */
252                         if (sfp) fclose(sfp);
253                         sfp = open_socket(&s_in);
254
255                         /*
256                          * Send HTTP request.
257                          */
258                         if (use_proxy) {
259                                 const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
260 #ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
261                                 if (strchr(target.host, ':'))
262                                         format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
263 #endif
264                                 fprintf(sfp, format,
265                                         target.is_ftp ? "f" : "ht", target.host,
266                                         ntohs(target.port), target.path);
267                         } else {
268                                 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
269                         }
270
271                         fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", target.host,
272                                 user_agent);
273
274 #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
275                         if (target.user) {
276                                 fprintf(sfp, "Authorization: Basic %s\r\n",
277                                         base64enc((unsigned char*)target.user, buf, sizeof(buf)));
278                         }
279                         if (use_proxy && server.user) {
280                                 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
281                                         base64enc((unsigned char*)server.user, buf, sizeof(buf)));
282                         }
283 #endif
284
285                         if (beg_range)
286                                 fprintf(sfp, "Range: bytes=%lld-\r\n", (long long)beg_range);
287                         if(extra_headers_left < sizeof(extra_headers))
288                                 fputs(extra_headers,sfp);
289                         fprintf(sfp,"Connection: close\r\n\r\n");
290
291                         /*
292                         * Retrieve HTTP response line and check for "200" status code.
293                         */
294 read_response:
295                         if (fgets(buf, sizeof(buf), sfp) == NULL)
296                                 bb_error_msg_and_die("no response from server");
297
298                         for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
299                                 ;
300                         for ( ; isspace(*s) ; ++s)
301                                 ;
302                         switch (status = atoi(s)) {
303                                 case 0:
304                                 case 100:
305                                         while (gethdr(buf, sizeof(buf), sfp, &n) != NULL);
306                                         goto read_response;
307                                 case 200:
308                                         break;
309                                 case 300:       /* redirection */
310                                 case 301:
311                                 case 302:
312                                 case 303:
313                                         break;
314                                 case 206:
315                                         if (beg_range)
316                                                 break;
317                                         /*FALLTHRU*/
318                                 default:
319                                         chomp(buf);
320                                         bb_error_msg_and_die("server returned error %d: %s", atoi(s), buf);
321                         }
322
323                         /*
324                          * Retrieve HTTP headers.
325                          */
326                         while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
327                                 if (strcasecmp(buf, "content-length") == 0) {
328                                         unsigned long value;
329                                         if (safe_strtoul(s, &value)) {
330                                                 bb_error_msg_and_die("content-length %s is garbage", s);
331                                         }
332                                         filesize = value;
333                                         got_clen = 1;
334                                         continue;
335                                 }
336                                 if (strcasecmp(buf, "transfer-encoding") == 0) {
337                                         if (strcasecmp(s, "chunked") == 0) {
338                                                 chunked = got_clen = 1;
339                                         } else {
340                                                 bb_error_msg_and_die("server wants to do %s transfer encoding", s);
341                                         }
342                                 }
343                                 if (strcasecmp(buf, "location") == 0) {
344                                         if (s[0] == '/')
345                                                 target.path = xstrdup(s+1);
346                                         else {
347                                                 parse_url(xstrdup(s), &target);
348                                                 if (use_proxy == 0) {
349                                                         server.host = target.host;
350                                                         server.port = target.port;
351                                                 }
352                                                 bb_lookup_host(&s_in, server.host);
353                                                 s_in.sin_port = server.port;
354                                                 break;
355                                         }
356                                 }
357                         }
358                 } while(status >= 300);
359
360                 dfp = sfp;
361         }
362         else
363         {
364                 /*
365                  *  FTP session
366                  */
367                 if (!target.user)
368                         target.user = xstrdup("anonymous:busybox@");
369
370                 sfp = open_socket(&s_in);
371                 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
372                         bb_error_msg_and_die("%s", buf+4);
373
374                 /*
375                  * Splitting username:password pair,
376                  * trying to log in
377                  */
378                 s = strchr(target.user, ':');
379                 if (s)
380                         *(s++) = '\0';
381                 switch(ftpcmd("USER ", target.user, sfp, buf)) {
382                         case 230:
383                                 break;
384                         case 331:
385                                 if (ftpcmd("PASS ", s, sfp, buf) == 230)
386                                         break;
387                                 /* FALLTHRU (failed login) */
388                         default:
389                                 bb_error_msg_and_die("ftp login: %s", buf+4);
390                 }
391
392                 ftpcmd("TYPE I", NULL, sfp, buf);
393
394                 /*
395                  * Querying file size
396                  */
397                 if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
398                         unsigned long value;
399                         if (safe_strtoul(buf+4, &value)) {
400                                 bb_error_msg_and_die("SIZE value is garbage");
401                         }
402                         filesize = value;
403                         got_clen = 1;
404                 }
405
406                 /*
407                  * Entering passive mode
408                  */
409                 if (ftpcmd("PASV", NULL, sfp, buf) !=  227)
410                         bb_error_msg_and_die("PASV: %s", buf+4);
411                 s = strrchr(buf, ',');
412                 *s = 0;
413                 port = atoi(s+1);
414                 s = strrchr(buf, ',');
415                 port += atoi(s+1) * 256;
416                 s_in.sin_port = htons(port);
417                 dfp = open_socket(&s_in);
418
419                 if (beg_range) {
420                         sprintf(buf, "REST %lld", (long long)beg_range);
421                         if (ftpcmd(buf, NULL, sfp, buf) == 350)
422                                 filesize -= beg_range;
423                 }
424
425                 if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
426                         bb_error_msg_and_die("RETR: %s", buf+4);
427         }
428
429
430         /*
431          * Retrieve file
432          */
433         if (chunked) {
434                 fgets(buf, sizeof(buf), dfp);
435                 filesize = strtol(buf, (char **) NULL, 16);
436         }
437
438         if (quiet_flag == FALSE)
439                 progressmeter(-1);
440
441         do {
442                 while (filesize > 0 || !got_clen) {
443                         unsigned rdsz = sizeof(buf);
444                         if (filesize < sizeof(buf) && (chunked || got_clen))
445                                 rdsz = filesize;
446                         n = safe_fread(buf, 1, rdsz, dfp);
447                         if (n <= 0)
448                                 break;
449                         if (output_fd < 0)
450                                 output_fd = xopen3(fname_out, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE, 0666);
451                         if (full_write(output_fd, buf, n) != n) {
452                                 bb_perror_msg_and_die(bb_msg_write_error);
453                         }
454 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
455                         transferred += n;
456 #endif
457                         if (got_clen) {
458                                 filesize -= n;
459                         }
460                 }
461
462                 if (chunked) {
463                         safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
464                         safe_fgets(buf, sizeof(buf), dfp);
465                         filesize = strtol(buf, (char **) NULL, 16);
466                         if (filesize == 0) {
467                                 chunked = 0; /* all done! */
468                         }
469                 }
470
471                 if (n == 0 && ferror(dfp)) {
472                         bb_perror_msg_and_die(bb_msg_read_error);
473                 }
474         } while (chunked);
475
476         if (quiet_flag == FALSE)
477                 progressmeter(1);
478
479         if ((use_proxy == 0) && target.is_ftp) {
480                 fclose(dfp);
481                 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
482                         bb_error_msg_and_die("ftp error: %s", buf+4);
483                 ftpcmd("QUIT", NULL, sfp, buf);
484         }
485         exit(EXIT_SUCCESS);
486 }
487
488
489 static void parse_url(char *url, struct host_info *h)
490 {
491         char *cp, *sp, *up, *pp;
492
493         if (strncmp(url, "http://", 7) == 0) {
494                 h->port = bb_lookup_port("http", "tcp", 80);
495                 h->host = url + 7;
496                 h->is_ftp = 0;
497         } else if (strncmp(url, "ftp://", 6) == 0) {
498                 h->port = bb_lookup_port("ftp", "tfp", 21);
499                 h->host = url + 6;
500                 h->is_ftp = 1;
501         } else
502                 bb_error_msg_and_die("not an http or ftp url: %s", url);
503
504         sp = strchr(h->host, '/');
505         if (sp) {
506                 *sp++ = '\0';
507                 h->path = sp;
508         } else
509                 h->path = xstrdup("");
510
511         up = strrchr(h->host, '@');
512         if (up != NULL) {
513                 h->user = h->host;
514                 *up++ = '\0';
515                 h->host = up;
516         } else
517                 h->user = NULL;
518
519         pp = h->host;
520
521 #ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
522         if (h->host[0] == '[') {
523                 char *ep;
524
525                 ep = h->host + 1;
526                 while (*ep == ':' || isxdigit (*ep))
527                         ep++;
528                 if (*ep == ']') {
529                         h->host++;
530                         *ep = '\0';
531                         pp = ep + 1;
532                 }
533         }
534 #endif
535
536         cp = strchr(pp, ':');
537         if (cp != NULL) {
538                 *cp++ = '\0';
539                 h->port = htons(atoi(cp));
540         }
541 }
542
543
544 static FILE *open_socket(struct sockaddr_in *s_in)
545 {
546         FILE *fp;
547
548         fp = fdopen(xconnect(s_in), "r+");
549         if (fp == NULL)
550                 bb_perror_msg_and_die("fdopen");
551
552         return fp;
553 }
554
555
556 static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
557 {
558         char *s, *hdrval;
559         int c;
560
561         *istrunc = 0;
562
563         /* retrieve header line */
564         if (fgets(buf, bufsiz, fp) == NULL)
565                 return NULL;
566
567         /* see if we are at the end of the headers */
568         for (s = buf ; *s == '\r' ; ++s)
569                 ;
570         if (s[0] == '\n')
571                 return NULL;
572
573         /* convert the header name to lower case */
574         for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
575                 *s = tolower(*s);
576
577         /* verify we are at the end of the header name */
578         if (*s != ':')
579                 bb_error_msg_and_die("bad header line: %s", buf);
580
581         /* locate the start of the header value */
582         for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
583                 ;
584         hdrval = s;
585
586         /* locate the end of header */
587         while (*s != '\0' && *s != '\r' && *s != '\n')
588                 ++s;
589
590         /* end of header found */
591         if (*s != '\0') {
592                 *s = '\0';
593                 return hdrval;
594         }
595
596         /* Rats!  The buffer isn't big enough to hold the entire header value. */
597         while (c = getc(fp), c != EOF && c != '\n')
598                 ;
599         *istrunc = 1;
600         return hdrval;
601 }
602
603 static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
604 {
605         if (s1) {
606                 if (!s2) s2="";
607                 fprintf(fp, "%s%s\r\n", s1, s2);
608                 fflush(fp);
609         }
610
611         do {
612                 char *buf_ptr;
613
614                 if (fgets(buf, 510, fp) == NULL) {
615                         bb_perror_msg_and_die("fgets");
616                 }
617                 buf_ptr = strstr(buf, "\r\n");
618                 if (buf_ptr) {
619                         *buf_ptr = '\0';
620                 }
621         } while (!isdigit(buf[0]) || buf[3] != ' ');
622
623         return atoi(buf);
624 }
625
626 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
627 /* Stuff below is from BSD rcp util.c, as added to openshh.
628  * Original copyright notice is retained at the end of this file.
629  *
630  */
631
632
633 static int
634 getttywidth(void)
635 {
636         int width=0;
637         get_terminal_width_height(0, &width, NULL);
638         return (width);
639 }
640
641 static void
642 updateprogressmeter(int ignore)
643 {
644         int save_errno = errno;
645
646         progressmeter(0);
647         errno = save_errno;
648 }
649
650 static void alarmtimer(int iwait)
651 {
652         struct itimerval itv;
653
654         itv.it_value.tv_sec = iwait;
655         itv.it_value.tv_usec = 0;
656         itv.it_interval = itv.it_value;
657         setitimer(ITIMER_REAL, &itv, NULL);
658 }
659
660
661 static void
662 progressmeter(int flag)
663 {
664         static struct timeval lastupdate;
665         static off_t lastsize, totalsize;
666
667         struct timeval now, td, tvwait;
668         off_t abbrevsize;
669         int elapsed, ratio, barlength, i;
670         char buf[256];
671
672         if (flag == -1) {
673                 (void) gettimeofday(&start, (struct timezone *) 0);
674                 lastupdate = start;
675                 lastsize = 0;
676                 totalsize = filesize; /* as filesize changes.. */
677         }
678
679         (void) gettimeofday(&now, (struct timezone *) 0);
680         ratio = 100;
681         if (totalsize != 0 && !chunked) {
682                 ratio = (int) (100 * transferred / totalsize);
683                 ratio = MIN(ratio, 100);
684         }
685
686         fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
687
688         barlength = getttywidth() - 51;
689         if (barlength > 0 && barlength < sizeof(buf)) {
690                 i = barlength * ratio / 100;
691                 memset(buf, '*', i);
692                 memset(buf + i, ' ', barlength - i);
693                 buf[barlength] = '\0';
694                 fprintf(stderr, "|%s|", buf);
695         }
696         i = 0;
697         abbrevsize = transferred;
698         while (abbrevsize >= 100000) {
699                 i++;
700                 abbrevsize >>= 10;
701         }
702         /* See http://en.wikipedia.org/wiki/Tera */
703         fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
704
705         timersub(&now, &lastupdate, &tvwait);
706         if (transferred > lastsize) {
707                 lastupdate = now;
708                 lastsize = transferred;
709                 if (tvwait.tv_sec >= STALLTIME)
710                         timeradd(&start, &tvwait, &start);
711                 tvwait.tv_sec = 0;
712         }
713         timersub(&now, &start, &td);
714         elapsed = td.tv_sec;
715
716         if (tvwait.tv_sec >= STALLTIME) {
717                 fprintf(stderr, " - stalled -");
718         } else if (transferred <= 0 || elapsed <= 0 || transferred > totalsize || chunked) {
719                 fprintf(stderr, "--:--:-- ETA");
720         } else {
721                 /* totalsize / (transferred/elapsed) - elapsed: */
722                 int eta = (int) (totalsize*elapsed/transferred - elapsed);
723                 i = eta % 3600;
724                 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
725         }
726
727         if (flag == -1) {
728                 struct sigaction sa;
729                 sa.sa_handler = updateprogressmeter;
730                 sigemptyset(&sa.sa_mask);
731                 sa.sa_flags = SA_RESTART;
732                 sigaction(SIGALRM, &sa, NULL);
733                 alarmtimer(1);
734         } else if (flag == 1) {
735                 alarmtimer(0);
736                 transferred = 0;
737                 putc('\n', stderr);
738         }
739 }
740 #endif
741
742 /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
743  * much of which was blatantly stolen from openssh.  */
744
745 /*-
746  * Copyright (c) 1992, 1993
747  *      The Regents of the University of California.  All rights reserved.
748  *
749  * Redistribution and use in source and binary forms, with or without
750  * modification, are permitted provided that the following conditions
751  * are met:
752  * 1. Redistributions of source code must retain the above copyright
753  *    notice, this list of conditions and the following disclaimer.
754  * 2. Redistributions in binary form must reproduce the above copyright
755  *    notice, this list of conditions and the following disclaimer in the
756  *    documentation and/or other materials provided with the distribution.
757  *
758  * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
759  *              ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
760  *
761  * 4. Neither the name of the University nor the names of its contributors
762  *    may be used to endorse or promote products derived from this software
763  *    without specific prior written permission.
764  *
765  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
766  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
767  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
768  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
769  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
770  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
771  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
772  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
773  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
774  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
775  * SUCH DAMAGE.
776  *
777  *      $Id: wget.c,v 1.75 2004/10/08 08:27:40 andersen Exp $
778  */