blob: f62d835e509d21c4550b491815f7bc89b024ef5b [file] [log] [blame]
Eric Andersen96700832000-09-04 15:15:55 +00001/* vi: set sw=4 ts=4: */
2/*
3 * wget - retrieve a file using HTTP
4 *
Eric Andersen4e573f42000-11-14 23:29:24 +00005 * Chip Rosenthal Covad Communications <chip@laserlink.net>
Eric Andersenb520e082000-10-03 00:21:45 +00006 *
Eric Andersen4e573f42000-11-14 23:29:24 +00007 * Note: According to RFC2616 section 3.6.1, "All HTTP/1.1 applications MUST be
8 * able to receive and decode the "chunked" transfer-coding, and MUST ignore
9 * chunk-extension extensions they do not understand."
Eric Andersenb520e082000-10-03 00:21:45 +000010 *
Eric Andersen4e573f42000-11-14 23:29:24 +000011 * This prevents this particular wget app from completely RFC compliant, and as
12 * such, prevents it from being used as a general purpose web browser... This
13 * is a design decision, since it makes the code smaller.
Eric Andersenb520e082000-10-03 00:21:45 +000014 *
Eric Andersen96700832000-09-04 15:15:55 +000015 */
16
Eric Andersen96700832000-09-04 15:15:55 +000017#include <stdio.h>
Eric Andersendff9d542001-01-26 02:04:49 +000018#include <errno.h>
Eric Andersen96700832000-09-04 15:15:55 +000019#include <stdlib.h>
20#include <unistd.h>
21#include <ctype.h>
22#include <string.h>
Eric Andersenb520e082000-10-03 00:21:45 +000023#include <unistd.h>
24#include <signal.h>
25#include <sys/ioctl.h>
Eric Andersen96700832000-09-04 15:15:55 +000026
Eric Andersenb520e082000-10-03 00:21:45 +000027#include <sys/time.h>
Eric Andersen96700832000-09-04 15:15:55 +000028#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <netdb.h>
34
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
36
Eric Andersened3ef502001-01-27 08:24:39 +000037/* Stupid libc5 doesn't define this... */
38#ifndef timersub
39#define timersub(a, b, result) \
40 do { \
41 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
42 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
43 if ((result)->tv_usec < 0) { \
44 --(result)->tv_sec; \
45 (result)->tv_usec += 1000000; \
46 } \
47 } while (0)
48#endif
Eric Andersen96700832000-09-04 15:15:55 +000049
Eric Andersen3e6ff902001-03-09 21:24:12 +000050static void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path);
51static FILE *open_socket(char *host, int port);
52static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
53static void progressmeter(int flag);
Eric Andersen96700832000-09-04 15:15:55 +000054
Eric Andersenb520e082000-10-03 00:21:45 +000055/* Globals (can be accessed from signal handlers */
56static off_t filesize = 0; /* content-length of the file */
Eric Andersen370fb082001-01-20 20:07:00 +000057#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersenb520e082000-10-03 00:21:45 +000058static char *curfile; /* Name of current file being transferred. */
59static struct timeval start; /* Time a transfer started. */
Eric Andersen3e6ff902001-03-09 21:24:12 +000060static volatile unsigned long statbytes; /* Number of bytes transferred so far. */
Eric Andersenb520e082000-10-03 00:21:45 +000061/* For progressmeter() -- number of seconds before xfer considered "stalled" */
Mark Whitley59ab0252001-01-23 22:30:04 +000062static const int STALLTIME = 5;
Eric Andersenb520e082000-10-03 00:21:45 +000063#endif
Eric Andersen7d697012001-01-24 20:28:35 +000064
Eric Andersen3e6ff902001-03-09 21:24:12 +000065static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue)
Eric Andersen7d697012001-01-24 20:28:35 +000066{
67 if (output != stdout && do_continue==0) {
68 fclose(output);
69 unlink(fname_out);
70 }
71}
Eric Andersen96700832000-09-04 15:15:55 +000072
73int wget_main(int argc, char **argv)
74{
Eric Andersen29edd002000-12-09 16:55:35 +000075 int n;
76 char *proxy, *proxy_host;
77 int uri_port, proxy_port;
78 char *s, buf[512];
79 struct stat sbuf;
80
Eric Andersen96700832000-09-04 15:15:55 +000081 FILE *sfp; /* socket to web server */
82 char *uri_host, *uri_path; /* parsed from command line url */
Eric Andersen96700832000-09-04 15:15:55 +000083 char *fname_out = NULL; /* where to direct output (-O) */
84 int do_continue = 0; /* continue a prev transfer (-c) */
85 long beg_range = 0L; /* range at which continue begins */
86 int got_clen = 0; /* got content-length: from server */
Eric Andersen29edd002000-12-09 16:55:35 +000087 FILE *output; /* socket to web server */
88 int quiet_flag = FALSE; /* Be verry, verry quiet... */
89
Eric Andersen96700832000-09-04 15:15:55 +000090 /*
91 * Crack command line.
92 */
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +000093 while ((n = getopt(argc, argv, "cqO:")) != EOF) {
Eric Andersen96700832000-09-04 15:15:55 +000094 switch (n) {
95 case 'c':
96 ++do_continue;
97 break;
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +000098 case 'q':
99 quiet_flag = TRUE;
100 break;
Eric Andersen96700832000-09-04 15:15:55 +0000101 case 'O':
Randolph Chung02553a22000-12-07 03:53:47 +0000102 /* can't set fname_out to NULL if outputting to stdout, because
103 * this gets interpreted as the auto-gen output filename
104 * case below - tausq@debian.org
105 */
106 fname_out = (strcmp(optarg, "-") == 0 ? (char *)1 : optarg);
Eric Andersen96700832000-09-04 15:15:55 +0000107 break;
108 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000109 show_usage();
Eric Andersen96700832000-09-04 15:15:55 +0000110 }
111 }
Eric Andersen25b669c2000-10-02 23:19:38 +0000112
Eric Andersen96700832000-09-04 15:15:55 +0000113 if (argc - optind != 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000114 show_usage();
Eric Andersen25b669c2000-10-02 23:19:38 +0000115
Eric Andersen96700832000-09-04 15:15:55 +0000116 /*
Eric Andersenf3b2b522000-12-07 22:42:11 +0000117 * Use the proxy if necessary.
Eric Andersen96700832000-09-04 15:15:55 +0000118 */
Eric Andersenf3b2b522000-12-07 22:42:11 +0000119 if ((proxy = getenv("http_proxy")) != NULL) {
120 proxy = xstrdup(proxy);
Eric Andersen29edd002000-12-09 16:55:35 +0000121 parse_url(proxy, &proxy_host, &proxy_port, &uri_path);
122 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
Eric Andersenf3b2b522000-12-07 22:42:11 +0000123 } else {
124 /*
125 * Parse url into components.
126 */
127 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
Eric Andersen29edd002000-12-09 16:55:35 +0000128 proxy_host=uri_host;
129 proxy_port=uri_port;
Eric Andersenf3b2b522000-12-07 22:42:11 +0000130 }
Eric Andersen29edd002000-12-09 16:55:35 +0000131
132 /* Guess an output filename */
133 if (!fname_out) {
134 fname_out =
Eric Andersen370fb082001-01-20 20:07:00 +0000135#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000136 curfile =
137#endif
138 get_last_path_component(uri_path);
139 if (fname_out==NULL || strlen(fname_out)<1) {
140 fname_out =
Eric Andersen370fb082001-01-20 20:07:00 +0000141#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000142 curfile =
143#endif
144 "index.html";
145 }
Eric Andersen370fb082001-01-20 20:07:00 +0000146#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000147 } else {
148 curfile=argv[optind];
149#endif
150 }
Eric Andersen7d697012001-01-24 20:28:35 +0000151 if (do_continue && !fname_out)
Matt Kraaidd19c692001-01-31 19:00:21 +0000152 error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
Eric Andersen29edd002000-12-09 16:55:35 +0000153
Eric Andersen96700832000-09-04 15:15:55 +0000154
155 /*
156 * Open socket to server.
157 */
Eric Andersen29edd002000-12-09 16:55:35 +0000158 sfp = open_socket(proxy_host, proxy_port);
159
160 /* Make the assumption that if the file already exists
161 * on disk that the intention is to continue downloading
162 * a previously aborted download -Erik */
163 if (stat(fname_out, &sbuf) == 0) {
164 ++do_continue;
165 }
Eric Andersen96700832000-09-04 15:15:55 +0000166
167 /*
Eric Andersen29edd002000-12-09 16:55:35 +0000168 * Open the output file stream.
Eric Andersen96700832000-09-04 15:15:55 +0000169 */
Randolph Chung02553a22000-12-07 03:53:47 +0000170 if (fname_out != (char *)1) {
Eric Andersen79e898a2001-01-31 17:49:47 +0000171 output = xfopen( fname_out, (do_continue ? "a" : "w") );
Randolph Chung02553a22000-12-07 03:53:47 +0000172 } else {
173 output = stdout;
Eric Andersen96700832000-09-04 15:15:55 +0000174 }
175
176 /*
177 * Determine where to start transfer.
178 */
179 if (do_continue) {
Eric Andersenb520e082000-10-03 00:21:45 +0000180 if (fstat(fileno(output), &sbuf) < 0)
Matt Kraai0dab8292000-12-18 03:08:29 +0000181 perror_msg_and_die("fstat()");
Eric Andersen96700832000-09-04 15:15:55 +0000182 if (sbuf.st_size > 0)
183 beg_range = sbuf.st_size;
184 else
185 do_continue = 0;
186 }
187
188 /*
189 * Send HTTP request.
190 */
Eric Andersen29edd002000-12-09 16:55:35 +0000191 fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n",
192 uri_host, uri_port, uri_path);
193 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host);
Eric Andersen7d697012001-01-24 20:28:35 +0000194
Eric Andersen96700832000-09-04 15:15:55 +0000195 if (do_continue)
196 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
Eric Andersen29edd002000-12-09 16:55:35 +0000197 fprintf(sfp,"Connection: close\r\n\r\n");
Eric Andersen96700832000-09-04 15:15:55 +0000198
199 /*
200 * Retrieve HTTP response line and check for "200" status code.
201 */
Eric Andersen29edd002000-12-09 16:55:35 +0000202 if (fgets(buf, sizeof(buf), sfp) == NULL) {
Eric Andersen7d697012001-01-24 20:28:35 +0000203 close_and_delete_outfile(output, fname_out, do_continue);
Matt Kraaidd19c692001-01-31 19:00:21 +0000204 error_msg_and_die("no response from server");
Eric Andersen29edd002000-12-09 16:55:35 +0000205 }
Eric Andersen96700832000-09-04 15:15:55 +0000206 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
207 ;
208 for ( ; isspace(*s) ; ++s)
209 ;
210 switch (atoi(s)) {
Eric Andersen7d697012001-01-24 20:28:35 +0000211 case 0:
Eric Andersenb520e082000-10-03 00:21:45 +0000212 case 200:
Eric Andersen7d697012001-01-24 20:28:35 +0000213 break;
Eric Andersenb520e082000-10-03 00:21:45 +0000214 case 206:
215 if (do_continue)
216 break;
217 /*FALLTHRU*/
218 default:
Eric Andersen7d697012001-01-24 20:28:35 +0000219 close_and_delete_outfile(output, fname_out, do_continue);
Matt Kraai05e782d2001-02-01 16:49:30 +0000220 chomp(buf);
Eric Andersen7d697012001-01-24 20:28:35 +0000221 error_msg_and_die("server returned error %d: %s", atoi(s), buf);
Eric Andersen96700832000-09-04 15:15:55 +0000222 }
223
224 /*
225 * Retrieve HTTP headers.
226 */
227 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
Eric Andersen7d697012001-01-24 20:28:35 +0000228 if (strcasecmp(buf, "content-length") == 0) {
Eric Andersenb520e082000-10-03 00:21:45 +0000229 filesize = atol(s);
Eric Andersen96700832000-09-04 15:15:55 +0000230 got_clen = 1;
231 continue;
232 }
Eric Andersen7d697012001-01-24 20:28:35 +0000233 if (strcasecmp(buf, "transfer-encoding") == 0) {
234 close_and_delete_outfile(output, fname_out, do_continue);
Matt Kraaidd19c692001-01-31 19:00:21 +0000235 error_msg_and_die("server wants to do %s transfer encoding", s);
Eric Andersen96700832000-09-04 15:15:55 +0000236 continue;
237 }
238 }
239
240 /*
241 * Retrieve HTTP body.
242 */
Eric Andersen370fb082001-01-20 20:07:00 +0000243#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersenb520e082000-10-03 00:21:45 +0000244 statbytes=0;
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000245 if (quiet_flag==FALSE)
246 progressmeter(-1);
Eric Andersenb520e082000-10-03 00:21:45 +0000247#endif
248 while (filesize > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) {
249 fwrite(buf, 1, n, output);
Eric Andersen370fb082001-01-20 20:07:00 +0000250#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersenb520e082000-10-03 00:21:45 +0000251 statbytes+=n;
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000252 if (quiet_flag==FALSE)
253 progressmeter(1);
Eric Andersenb520e082000-10-03 00:21:45 +0000254#endif
Eric Andersen96700832000-09-04 15:15:55 +0000255 if (got_clen)
Eric Andersenb520e082000-10-03 00:21:45 +0000256 filesize -= n;
Eric Andersen96700832000-09-04 15:15:55 +0000257 }
258 if (n == 0 && ferror(sfp))
Mark Whitleyf57c9442000-12-07 19:56:48 +0000259 perror_msg_and_die("network read error");
Eric Andersen96700832000-09-04 15:15:55 +0000260
261 exit(0);
262}
263
264
265void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
266{
Matt Kraaia9711a52001-01-03 16:15:15 +0000267 char *cp, *sp;
Eric Andersen96700832000-09-04 15:15:55 +0000268
269 *uri_port = 80;
270
271 if (strncmp(url, "http://", 7) != 0)
Matt Kraaidd19c692001-01-31 19:00:21 +0000272 error_msg_and_die("not an http url: %s", url);
Eric Andersen96700832000-09-04 15:15:55 +0000273
Matt Kraaia9711a52001-01-03 16:15:15 +0000274 *uri_host = url + 7;
275
276 cp = strchr(*uri_host, ':');
277 sp = strchr(*uri_host, '/');
278
279 if (cp != NULL && (sp == NULL || cp < sp)) {
280 *cp++ = '\0';
281 *uri_port = atoi(cp);
Eric Andersen96700832000-09-04 15:15:55 +0000282 }
Randolph Chung02553a22000-12-07 03:53:47 +0000283
Matt Kraaia9711a52001-01-03 16:15:15 +0000284 if (sp != NULL) {
285 *sp++ = '\0';
286 *uri_path = sp;
287 } else
288 *uri_path = "";
Eric Andersen96700832000-09-04 15:15:55 +0000289}
290
291
292FILE *open_socket(char *host, int port)
293{
Eric Andersen1ca20a72001-03-21 07:34:27 +0000294 struct sockaddr_in s_in;
Eric Andersen96700832000-09-04 15:15:55 +0000295 struct hostent *hp;
296 int fd;
297 FILE *fp;
298
Eric Andersen1ca20a72001-03-21 07:34:27 +0000299 memset(&s_in, 0, sizeof(s_in));
300 s_in.sin_family = AF_INET;
Eric Andersen96700832000-09-04 15:15:55 +0000301 if ((hp = (struct hostent *) gethostbyname(host)) == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000302 error_msg_and_die("cannot resolve %s", host);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000303 memcpy(&s_in.sin_addr, hp->h_addr_list[0], hp->h_length);
304 s_in.sin_port = htons(port);
Eric Andersen96700832000-09-04 15:15:55 +0000305
306 /*
307 * Get the server onto a stdio stream.
308 */
309 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000310 perror_msg_and_die("socket()");
Eric Andersen1ca20a72001-03-21 07:34:27 +0000311 if (connect(fd, (struct sockaddr *) &s_in, sizeof(s_in)) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000312 perror_msg_and_die("connect(%s)", host);
Eric Andersen96700832000-09-04 15:15:55 +0000313 if ((fp = fdopen(fd, "r+")) == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000314 perror_msg_and_die("fdopen()");
Eric Andersen96700832000-09-04 15:15:55 +0000315
316 return fp;
317}
318
319
320char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
321{
322 char *s, *hdrval;
323 int c;
324
325 *istrunc = 0;
326
327 /* retrieve header line */
328 if (fgets(buf, bufsiz, fp) == NULL)
329 return NULL;
330
331 /* see if we are at the end of the headers */
332 for (s = buf ; *s == '\r' ; ++s)
333 ;
334 if (s[0] == '\n')
335 return NULL;
336
337 /* convert the header name to lower case */
338 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
339 *s = tolower(*s);
340
341 /* verify we are at the end of the header name */
342 if (*s != ':')
Matt Kraaidd19c692001-01-31 19:00:21 +0000343 error_msg_and_die("bad header line: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000344
345 /* locate the start of the header value */
346 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
347 ;
348 hdrval = s;
349
350 /* locate the end of header */
351 while (*s != '\0' && *s != '\r' && *s != '\n')
352 ++s;
353
354 /* end of header found */
355 if (*s != '\0') {
356 *s = '\0';
357 return hdrval;
358 }
359
Eric Andersen5d638842000-09-14 21:46:30 +0000360 /* Rats! The buffer isn't big enough to hold the entire header value. */
Eric Andersen96700832000-09-04 15:15:55 +0000361 while (c = getc(fp), c != EOF && c != '\n')
362 ;
363 *istrunc = 1;
364 return hdrval;
365}
366
Eric Andersen370fb082001-01-20 20:07:00 +0000367#ifdef BB_FEATURE_WGET_STATUSBAR
Eric Andersen4e573f42000-11-14 23:29:24 +0000368/* Stuff below is from BSD rcp util.c, as added to openshh.
369 * Original copyright notice is retained at the end of this file.
370 *
371 */
Eric Andersenb520e082000-10-03 00:21:45 +0000372
373
Eric Andersen3e6ff902001-03-09 21:24:12 +0000374static int
Eric Andersenb520e082000-10-03 00:21:45 +0000375getttywidth(void)
376{
377 struct winsize winsize;
378
379 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
380 return (winsize.ws_col ? winsize.ws_col : 80);
381 else
382 return (80);
383}
384
Eric Andersen3e6ff902001-03-09 21:24:12 +0000385static void
Eric Andersenb520e082000-10-03 00:21:45 +0000386updateprogressmeter(int ignore)
387{
388 int save_errno = errno;
389
390 progressmeter(0);
391 errno = save_errno;
392}
393
Eric Andersen3e6ff902001-03-09 21:24:12 +0000394static void
Eric Andersenb520e082000-10-03 00:21:45 +0000395alarmtimer(int wait)
396{
397 struct itimerval itv;
398
399 itv.it_value.tv_sec = wait;
400 itv.it_value.tv_usec = 0;
401 itv.it_interval = itv.it_value;
402 setitimer(ITIMER_REAL, &itv, NULL);
403}
404
405
Eric Andersen3e6ff902001-03-09 21:24:12 +0000406static void
Eric Andersenb520e082000-10-03 00:21:45 +0000407progressmeter(int flag)
408{
409 static const char prefixes[] = " KMGTP";
410 static struct timeval lastupdate;
411 static off_t lastsize;
412 struct timeval now, td, wait;
413 off_t cursize, abbrevsize;
414 double elapsed;
415 int ratio, barlength, i, remaining;
416 char buf[256];
417
418 if (flag == -1) {
419 (void) gettimeofday(&start, (struct timezone *) 0);
420 lastupdate = start;
421 lastsize = 0;
422 }
423
424 (void) gettimeofday(&now, (struct timezone *) 0);
425 cursize = statbytes;
426 if (filesize != 0) {
427 ratio = 100.0 * cursize / filesize;
428 ratio = MAX(ratio, 0);
429 ratio = MIN(ratio, 100);
430 } else
431 ratio = 100;
432
433 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
434
435 barlength = getttywidth() - 51;
436 if (barlength > 0) {
437 i = barlength * ratio / 100;
438 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
439 "|%.*s%*s|", i,
440 "*****************************************************************************"
441 "*****************************************************************************",
442 barlength - i, "");
443 }
444 i = 0;
445 abbrevsize = cursize;
446 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
447 i++;
448 abbrevsize >>= 10;
449 }
450 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
451 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
452 'B');
453
454 timersub(&now, &lastupdate, &wait);
455 if (cursize > lastsize) {
456 lastupdate = now;
457 lastsize = cursize;
458 if (wait.tv_sec >= STALLTIME) {
459 start.tv_sec += wait.tv_sec;
460 start.tv_usec += wait.tv_usec;
461 }
462 wait.tv_sec = 0;
463 }
464 timersub(&now, &start, &td);
465 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
466
467 if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
468 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
469 " --:-- ETA");
470 } else if (wait.tv_sec >= STALLTIME) {
471 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
472 " - stalled -");
473 } else {
474 remaining = (int) (filesize / (statbytes / elapsed) - elapsed);
475 i = remaining / 3600;
476 if (i)
477 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
478 "%2d:", i);
479 else
480 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
481 " ");
482 i = remaining % 3600;
483 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
484 "%02d:%02d ETA", i / 60, i % 60);
485 }
Randolph Chungda7b8292000-12-07 03:55:35 +0000486 write(fileno(stderr), buf, strlen(buf));
Eric Andersenb520e082000-10-03 00:21:45 +0000487
488 if (flag == -1) {
489 struct sigaction sa;
490 sa.sa_handler = updateprogressmeter;
491 sigemptyset(&sa.sa_mask);
492 sa.sa_flags = SA_RESTART;
493 sigaction(SIGALRM, &sa, NULL);
494 alarmtimer(1);
495 } else if (flag == 1) {
496 alarmtimer(0);
497 statbytes = 0;
498 }
499}
500#endif
Eric Andersen4e573f42000-11-14 23:29:24 +0000501
Eric Andersen370fb082001-01-20 20:07:00 +0000502/* Original copyright notice which applies to the BB_FEATURE_WGET_STATUSBAR stuff,
Eric Andersen4e573f42000-11-14 23:29:24 +0000503 * much of which was blatently stolen from openssh. */
504
505/*-
506 * Copyright (c) 1992, 1993
507 * The Regents of the University of California. All rights reserved.
508 *
509 * Redistribution and use in source and binary forms, with or without
510 * modification, are permitted provided that the following conditions
511 * are met:
512 * 1. Redistributions of source code must retain the above copyright
513 * notice, this list of conditions and the following disclaimer.
514 * 2. Redistributions in binary form must reproduce the above copyright
515 * notice, this list of conditions and the following disclaimer in the
516 * documentation and/or other materials provided with the distribution.
517 *
518 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
519 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
520 *
521 * 4. Neither the name of the University nor the names of its contributors
522 * may be used to endorse or promote products derived from this software
523 * without specific prior written permission.
524 *
525 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
526 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
527 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
528 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
529 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
530 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
531 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
532 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
533 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
534 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
535 * SUCH DAMAGE.
536 *
Eric Andersen1ca20a72001-03-21 07:34:27 +0000537 * $Id: wget.c,v 1.30 2001/03/21 07:34:26 andersen Exp $
Eric Andersen4e573f42000-11-14 23:29:24 +0000538 */
539
540
541
Eric Andersen96700832000-09-04 15:15:55 +0000542/*
543Local Variables:
544c-file-style: "linux"
545c-basic-offset: 4
546tab-width: 4
547End:
548*/
Eric Andersenb520e082000-10-03 00:21:45 +0000549
Eric Andersen4e573f42000-11-14 23:29:24 +0000550
551