blob: bbe1bba9a82dfefd9ea73e0d1e1275c8f03e1437 [file] [log] [blame]
Eric Andersen96700832000-09-04 15:15:55 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersen79757c92001-04-05 21:45:54 +00003 * wget - retrieve a file using HTTP or FTP
Eric Andersen96700832000-09-04 15:15:55 +00004 *
Eric Andersen4e573f42000-11-14 23:29:24 +00005 * Chip Rosenthal Covad Communications <chip@laserlink.net>
Eric Andersenb520e082000-10-03 00:21:45 +00006 *
Eric Andersen96700832000-09-04 15:15:55 +00007 */
8
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +00009/* We want libc to give us xxx64 functions also */
10/* http://www.unix.org/version2/whatsnew/lfs20mar.html */
11#define _LARGEFILE64_SOURCE 1
12
Rob Landleyaf12cb32006-06-27 18:41:03 +000013#include "busybox.h"
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000014#include <getopt.h> /* for struct option */
15
16#ifdef CONFIG_LFS
17# define FILEOFF_TYPE off64_t
18# define FILEOFF_FMT "%lld"
19# define LSEEK lseek64
20# define STRTOOFF strtoll
21# define SAFE_STRTOOFF safe_strtoll
22/* stat64 etc as needed... */
23#else
24# define FILEOFF_TYPE off_t
25# define FILEOFF_FMT "%ld"
26# define LSEEK lseek
27# define STRTOOFF strtol
28# define SAFE_STRTOOFF safe_strtol
29/* Do we need to undefine O_LARGEFILE? */
30#endif
Eric Andersen50ae3102001-05-15 17:51:37 +000031
Eric Andersen79757c92001-04-05 21:45:54 +000032struct host_info {
33 char *host;
34 int port;
35 char *path;
36 int is_ftp;
37 char *user;
38};
39
40static void parse_url(char *url, struct host_info *h);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000041static FILE *open_socket(struct sockaddr_in *s_in);
Eric Andersen3e6ff902001-03-09 21:24:12 +000042static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
Eric Andersen79757c92001-04-05 21:45:54 +000043static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
Eric Andersen96700832000-09-04 15:15:55 +000044
Eric Andersenb520e082000-10-03 00:21:45 +000045/* Globals (can be accessed from signal handlers */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000046static FILEOFF_TYPE content_len; /* Content-length of the file */
47static FILEOFF_TYPE beg_range; /* Range at which continue begins */
48static FILEOFF_TYPE transferred; /* Number of bytes transferred so far */
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000049static int chunked; /* chunked transfer encoding */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000050#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersen20aab262001-07-19 22:28:02 +000051static void progressmeter(int flag);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000052static char *curfile; /* Name of current file being transferred */
53static struct timeval start; /* Time a transfer started */
Rob Landley19a39402006-06-13 17:10:26 +000054enum {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000055 STALLTIME = 5 /* Seconds when xfer considered "stalled" */
Rob Landley19a39402006-06-13 17:10:26 +000056};
57#else
Rob Landleyf5fc1382006-09-15 04:08:25 +000058static void progressmeter(int flag) {}
Eric Andersenb520e082000-10-03 00:21:45 +000059#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060
Matt Kraai854125f2001-05-09 19:15:46 +000061/* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the
62 * number of elements read, and a short count if an eof or non-interrupt
63 * error is encountered. */
64static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
65{
66 size_t ret = 0;
67
68 do {
69 clearerr(stream);
70 ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
71 } while (ret < nmemb && ferror(stream) && errno == EINTR);
72
73 return ret;
74}
75
Matt Kraai854125f2001-05-09 19:15:46 +000076/* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
77 * Returns S, or NULL if an eof or non-interrupt error is encountered. */
78static char *safe_fgets(char *s, int size, FILE *stream)
79{
80 char *ret;
81
82 do {
83 clearerr(stream);
84 ret = fgets(s, size, stream);
85 } while (ret == NULL && ferror(stream) && errno == EINTR);
86
87 return ret;
88}
89
Eric Andersenbdfd0d72001-10-24 05:00:29 +000090#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +000091/*
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000092 * Base64-encode character string and return the string.
Eric Andersen79757c92001-04-05 21:45:54 +000093 */
Denis Vlasenko3526a132006-09-09 12:20:57 +000094static char *base64enc(unsigned char *p, char *buf, int len)
95{
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000096 bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
Rob Landley76ef08c2006-06-13 16:44:26 +000097 return buf;
Eric Andersen79757c92001-04-05 21:45:54 +000098}
99#endif
100
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000101#define WGET_OPT_CONTINUE 1
102#define WGET_OPT_QUIET 2
103#define WGET_OPT_PASSIVE 4
104#define WGET_OPT_OUTNAME 8
105#define WGET_OPT_HEADER 16
106#define WGET_OPT_PREFIX 32
107#define WGET_OPT_PROXY 64
108#define WGET_OPT_USER_AGENT 128
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000109
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +0000110#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000111static const struct option wget_long_options[] = {
112 { "continue", 0, NULL, 'c' },
113 { "quiet", 0, NULL, 'q' },
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000114 { "passive-ftp", 0, NULL, 139 }, /* FIXME: what is this - 139?? */
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000115 { "output-document", 1, NULL, 'O' },
Rob Landley76ef08c2006-06-13 16:44:26 +0000116 { "header", 1, NULL, 131 },
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000117 { "directory-prefix",1, NULL, 'P' },
118 { "proxy", 1, NULL, 'Y' },
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000119 { "user-agent", 1, NULL, 'U' },
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000120 { 0, 0, 0, 0 }
121};
Bernhard Reutner-Fischer8d3a6f72006-05-31 14:11:38 +0000122#endif
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000123
Eric Andersen96700832000-09-04 15:15:55 +0000124int wget_main(int argc, char **argv)
125{
Eric Andersen79757c92001-04-05 21:45:54 +0000126 int n, try=5, status;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000127 unsigned long opt;
Eric Andersen79757c92001-04-05 21:45:54 +0000128 int port;
Robert Griebld7760112002-05-14 23:36:45 +0000129 char *proxy = 0;
Eric Andersen8071c022001-06-21 19:45:06 +0000130 char *dir_prefix=NULL;
Eric Andersen29edd002000-12-09 16:55:35 +0000131 char *s, buf[512];
Eric Andersen50ae3102001-05-15 17:51:37 +0000132 char extra_headers[1024];
133 char *extra_headers_ptr = extra_headers;
134 int extra_headers_left = sizeof(extra_headers);
Eric Andersen79757c92001-04-05 21:45:54 +0000135 struct host_info server, target;
Eric Andersene6dc4392003-10-31 09:31:46 +0000136 struct sockaddr_in s_in;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000137 llist_t *headers_llist = NULL;
Eric Andersen79757c92001-04-05 21:45:54 +0000138
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000139 FILE *sfp = NULL; /* socket to web/ftp server */
140 FILE *dfp = NULL; /* socket to ftp server (data) */
141 char *fname_out = NULL; /* where to direct output (-O) */
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000142 int got_clen = 0; /* got content-length: from server */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000143 int output_fd = -1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000144 int use_proxy = 1; /* Use proxies if env vars are set */
145 char *proxy_flag = "on"; /* Use proxies if env vars are set */
146 char *user_agent = "Wget"; /* Content of the "User-Agent" header field */
Eric Andersen29edd002000-12-09 16:55:35 +0000147
Eric Andersen96700832000-09-04 15:15:55 +0000148 /*
149 * Crack command line.
150 */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000151 bb_opt_complementally = "-1:\203::";
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +0000152#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000153 bb_applet_long_options = wget_long_options;
Bernhard Reutner-Fischer8d3a6f72006-05-31 14:11:38 +0000154#endif
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000155 opt = bb_getopt_ulflags(argc, argv, "cq\213O:\203:P:Y:U:",
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000156 &fname_out, &headers_llist,
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000157 &dir_prefix, &proxy_flag, &user_agent);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000158 if (strcmp(proxy_flag, "off") == 0) {
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000159 /* Use the proxy if necessary. */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000160 use_proxy = 0;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000161 }
162 if (opt & WGET_OPT_HEADER) {
163 while (headers_llist) {
164 int arglen = strlen(headers_llist->data);
165 if (extra_headers_left - arglen - 2 <= 0)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000166 bb_error_msg_and_die("extra_headers buffer too small "
167 "(need %i)", extra_headers_left - arglen);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000168 strcpy(extra_headers_ptr, headers_llist->data);
169 extra_headers_ptr += arglen;
170 extra_headers_left -= ( arglen + 2 );
171 *extra_headers_ptr++ = '\r';
172 *extra_headers_ptr++ = '\n';
173 *(extra_headers_ptr + 1) = 0;
174 headers_llist = headers_llist->link;
Eric Andersen96700832000-09-04 15:15:55 +0000175 }
176 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000177
Eric Andersen79757c92001-04-05 21:45:54 +0000178 parse_url(argv[optind], &target);
179 server.host = target.host;
180 server.port = target.port;
181
Eric Andersen96700832000-09-04 15:15:55 +0000182 /*
Eric Andersenf3b2b522000-12-07 22:42:11 +0000183 * Use the proxy if necessary.
Eric Andersen96700832000-09-04 15:15:55 +0000184 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000185 if (use_proxy) {
Robert Griebld7760112002-05-14 23:36:45 +0000186 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000187 if (proxy && *proxy) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000188 parse_url(xstrdup(proxy), &server);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000189 } else {
190 use_proxy = 0;
191 }
Robert Griebld7760112002-05-14 23:36:45 +0000192 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000193
Eric Andersen29edd002000-12-09 16:55:35 +0000194 /* Guess an output filename */
195 if (!fname_out) {
Eric Andersen751750e2004-10-08 08:27:40 +0000196 // Dirty hack. Needed because bb_get_last_path_component
197 // will destroy trailing / by storing '\0' in last byte!
Denis Vlasenko3526a132006-09-09 12:20:57 +0000198 if (*target.path && target.path[strlen(target.path)-1] != '/') {
Eric Andersen751750e2004-10-08 08:27:40 +0000199 fname_out =
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000200#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersen751750e2004-10-08 08:27:40 +0000201 curfile =
Eric Andersen29edd002000-12-09 16:55:35 +0000202#endif
Eric Andersen751750e2004-10-08 08:27:40 +0000203 bb_get_last_path_component(target.path);
204 }
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000205 if (!fname_out || !fname_out[0]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000206 fname_out =
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000207#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000208 curfile =
Eric Andersen29edd002000-12-09 16:55:35 +0000209#endif
210 "index.html";
211 }
Matt Kraai0382eb82001-07-19 19:13:55 +0000212 if (dir_prefix != NULL)
213 fname_out = concat_path_file(dir_prefix, fname_out);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000214#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000215 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000216 curfile = bb_get_last_path_component(fname_out);
Eric Andersen29edd002000-12-09 16:55:35 +0000217#endif
218 }
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000219 if ((opt & WGET_OPT_CONTINUE) && !fname_out)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
Eric Andersen29edd002000-12-09 16:55:35 +0000221
Eric Andersen96700832000-09-04 15:15:55 +0000222 /*
223 * Determine where to start transfer.
224 */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000225 if (!strcmp(fname_out, "-")) {
226 output_fd = 1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000227 opt |= WGET_OPT_QUIET;
228 opt &= ~WGET_OPT_CONTINUE;
229 } else if (opt & WGET_OPT_CONTINUE) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000230 output_fd = open(fname_out, O_WRONLY|O_LARGEFILE);
231 if (output_fd >= 0) {
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000232 beg_range = LSEEK(output_fd, 0, SEEK_END);
233 if (beg_range == (FILEOFF_TYPE)-1)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000234 bb_perror_msg_and_die("lseek");
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000235 }
236 /* File doesn't exist. We do not create file here yet.
237 We are not sure it exists on remove side */
Eric Andersen96700832000-09-04 15:15:55 +0000238 }
239
Eric Andersene6dc4392003-10-31 09:31:46 +0000240 /* We want to do exactly _one_ DNS lookup, since some
241 * sites (i.e. ftp.us.debian.org) use round-robin DNS
242 * and we want to connect to only one IP... */
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000243 bb_lookup_host(&s_in, server.host);
244 s_in.sin_port = server.port;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000245 if (!(opt & WGET_OPT_QUIET)) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000246 printf("Connecting to %s[%s]:%d\n",
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000247 server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
Eric Andersene6dc4392003-10-31 09:31:46 +0000248 }
249
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000250 if (use_proxy || !target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000251 /*
252 * HTTP session
253 */
254 do {
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000255 got_clen = chunked = 0;
256
Denis Vlasenko3526a132006-09-09 12:20:57 +0000257 if (!--try)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000258 bb_error_msg_and_die("too many redirections");
Eric Andersen7d697012001-01-24 20:28:35 +0000259
Eric Andersen79757c92001-04-05 21:45:54 +0000260 /*
261 * Open socket to http server
262 */
263 if (sfp) fclose(sfp);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000264 sfp = open_socket(&s_in);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000265
Eric Andersen79757c92001-04-05 21:45:54 +0000266 /*
267 * Send HTTP request.
268 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000269 if (use_proxy) {
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000270 const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
271#ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
Rob Landley19a39402006-06-13 17:10:26 +0000272 if (strchr(target.host, ':'))
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000273 format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
274#endif
275 fprintf(sfp, format,
Eric Andersen79757c92001-04-05 21:45:54 +0000276 target.is_ftp ? "f" : "ht", target.host,
Glenn L McGrath24cb17f2004-01-31 08:08:57 +0000277 ntohs(target.port), target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000278 } else {
Eric Andersen6d7fa432001-04-10 18:17:05 +0000279 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000280 }
Eric Andersen96700832000-09-04 15:15:55 +0000281
Bernhard Reutner-Fischerbfbc4eb2006-09-02 15:30:26 +0000282 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", target.host,
283 user_agent);
Eric Andersen79757c92001-04-05 21:45:54 +0000284
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000285#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +0000286 if (target.user) {
287 fprintf(sfp, "Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000288 base64enc((unsigned char*)target.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000289 }
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000290 if (use_proxy && server.user) {
Eric Andersen79757c92001-04-05 21:45:54 +0000291 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000292 base64enc((unsigned char*)server.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000293 }
294#endif
295
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000296 if (beg_range)
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000297 fprintf(sfp, "Range: bytes="FILEOFF_FMT"-\r\n", beg_range);
Eric Andersen50ae3102001-05-15 17:51:37 +0000298 if(extra_headers_left < sizeof(extra_headers))
Eric Andersen9abfe852001-05-15 20:11:49 +0000299 fputs(extra_headers,sfp);
Eric Andersen79757c92001-04-05 21:45:54 +0000300 fprintf(sfp,"Connection: close\r\n\r\n");
301
302 /*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000303 * Retrieve HTTP response line and check for "200" status code.
304 */
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000305read_response:
306 if (fgets(buf, sizeof(buf), sfp) == NULL)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000307 bb_error_msg_and_die("no response from server");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000308
Eric Andersen79757c92001-04-05 21:45:54 +0000309 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
Rob Landley76ef08c2006-06-13 16:44:26 +0000310 ;
Eric Andersen79757c92001-04-05 21:45:54 +0000311 for ( ; isspace(*s) ; ++s)
Rob Landley76ef08c2006-06-13 16:44:26 +0000312 ;
Eric Andersen79757c92001-04-05 21:45:54 +0000313 switch (status = atoi(s)) {
314 case 0:
Eric Andersen6d7fa432001-04-10 18:17:05 +0000315 case 100:
316 while (gethdr(buf, sizeof(buf), sfp, &n) != NULL);
317 goto read_response;
Eric Andersen79757c92001-04-05 21:45:54 +0000318 case 200:
Eric Andersen79757c92001-04-05 21:45:54 +0000319 break;
320 case 300: /* redirection */
321 case 301:
322 case 302:
323 case 303:
324 break;
325 case 206:
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000326 if (beg_range)
Eric Andersen79757c92001-04-05 21:45:54 +0000327 break;
328 /*FALLTHRU*/
329 default:
330 chomp(buf);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000331 bb_error_msg_and_die("server returned error %d: %s", atoi(s), buf);
Eric Andersen79757c92001-04-05 21:45:54 +0000332 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000333
Eric Andersen79757c92001-04-05 21:45:54 +0000334 /*
335 * Retrieve HTTP headers.
336 */
337 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
338 if (strcasecmp(buf, "content-length") == 0) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000339 if (SAFE_STRTOOFF(s, &content_len) || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000340 bb_error_msg_and_die("content-length %s is garbage", s);
Eric Andersen24794452004-03-06 22:11:45 +0000341 }
Eric Andersen79757c92001-04-05 21:45:54 +0000342 got_clen = 1;
343 continue;
344 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000345 if (strcasecmp(buf, "transfer-encoding") == 0) {
346 if (strcasecmp(s, "chunked") == 0) {
347 chunked = got_clen = 1;
348 } else {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000349 bb_error_msg_and_die("server wants to do %s transfer encoding", s);
Eric Andersen6d7fa432001-04-10 18:17:05 +0000350 }
351 }
Eric Andersen79757c92001-04-05 21:45:54 +0000352 if (strcasecmp(buf, "location") == 0) {
353 if (s[0] == '/')
Rob Landleyd921b2e2006-08-03 15:41:12 +0000354 target.path = xstrdup(s+1);
Eric Andersen79757c92001-04-05 21:45:54 +0000355 else {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000356 parse_url(xstrdup(s), &target);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000357 if (use_proxy == 0) {
Eric Andersen79757c92001-04-05 21:45:54 +0000358 server.host = target.host;
359 server.port = target.port;
360 }
Glenn L McGrath58a2e0e2004-01-17 23:07:14 +0000361 bb_lookup_host(&s_in, server.host);
362 s_in.sin_port = server.port;
363 break;
Eric Andersen79757c92001-04-05 21:45:54 +0000364 }
365 }
366 }
367 } while(status >= 300);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000368
Eric Andersen79757c92001-04-05 21:45:54 +0000369 dfp = sfp;
370 }
371 else
372 {
373 /*
374 * FTP session
375 */
Denis Vlasenko3526a132006-09-09 12:20:57 +0000376 if (!target.user)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000377 target.user = xstrdup("anonymous:busybox@");
Eric Andersen79757c92001-04-05 21:45:54 +0000378
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000379 sfp = open_socket(&s_in);
Eric Andersen79757c92001-04-05 21:45:54 +0000380 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000381 bb_error_msg_and_die("%s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000382
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000383 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000384 * Splitting username:password pair,
385 * trying to log in
386 */
387 s = strchr(target.user, ':');
388 if (s)
389 *(s++) = '\0';
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000390 switch (ftpcmd("USER ", target.user, sfp, buf)) {
Eric Andersen79757c92001-04-05 21:45:54 +0000391 case 230:
Eric Andersenb520e082000-10-03 00:21:45 +0000392 break;
Eric Andersen79757c92001-04-05 21:45:54 +0000393 case 331:
394 if (ftpcmd("PASS ", s, sfp, buf) == 230)
395 break;
396 /* FALLTHRU (failed login) */
397 default:
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000398 bb_error_msg_and_die("ftp login: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000399 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000400
Eric Andersen79757c92001-04-05 21:45:54 +0000401 ftpcmd("TYPE I", NULL, sfp, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000402
Eric Andersen79757c92001-04-05 21:45:54 +0000403 /*
404 * Querying file size
405 */
Rob Landleyaf12cb32006-06-27 18:41:03 +0000406 if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000407 if (SAFE_STRTOOFF(buf+4, &content_len) || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000408 bb_error_msg_and_die("SIZE value is garbage");
Eric Andersen24794452004-03-06 22:11:45 +0000409 }
Eric Andersen96700832000-09-04 15:15:55 +0000410 got_clen = 1;
Eric Andersen96700832000-09-04 15:15:55 +0000411 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000412
Eric Andersen79757c92001-04-05 21:45:54 +0000413 /*
414 * Entering passive mode
415 */
416 if (ftpcmd("PASV", NULL, sfp, buf) != 227)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000417 bb_error_msg_and_die("PASV: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000418 s = strrchr(buf, ',');
419 *s = 0;
420 port = atoi(s+1);
421 s = strrchr(buf, ',');
422 port += atoi(s+1) * 256;
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000423 s_in.sin_port = htons(port);
424 dfp = open_socket(&s_in);
Eric Andersen79757c92001-04-05 21:45:54 +0000425
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000426 if (beg_range) {
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000427 sprintf(buf, "REST "FILEOFF_FMT, beg_range);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000428 if (ftpcmd(buf, NULL, sfp, buf) == 350)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000429 content_len -= beg_range;
Eric Andersen96700832000-09-04 15:15:55 +0000430 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000431
Rob Landleyaf12cb32006-06-27 18:41:03 +0000432 if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000433 bb_error_msg_and_die("RETR: %s", buf+4);
Eric Andersen96700832000-09-04 15:15:55 +0000434 }
435
Eric Andersen79757c92001-04-05 21:45:54 +0000436
Eric Andersen96700832000-09-04 15:15:55 +0000437 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000438 * Retrieve file
Eric Andersen96700832000-09-04 15:15:55 +0000439 */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000440 if (chunked) {
441 fgets(buf, sizeof(buf), dfp);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000442 content_len = STRTOOFF(buf, (char **) NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000443 /* FIXME: error check?? */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000444 }
Rob Landley19a39402006-06-13 17:10:26 +0000445
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000446 /* Do it before progressmeter (want to have nice error message) */
447 if (output_fd < 0)
448 output_fd = xopen3(fname_out,
449 O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE, 0666);
450
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000451 if (!(opt & WGET_OPT_QUIET))
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000452 progressmeter(-1);
Rob Landley19a39402006-06-13 17:10:26 +0000453
Mark Whitley30ac01c2001-04-17 18:13:16 +0000454 do {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000455 while (content_len > 0 || !got_clen) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000456 unsigned rdsz = sizeof(buf);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000457 if (content_len < sizeof(buf) && (chunked || got_clen))
458 rdsz = (unsigned)content_len;
Denis Vlasenko3526a132006-09-09 12:20:57 +0000459 n = safe_fread(buf, 1, rdsz, dfp);
460 if (n <= 0)
461 break;
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000462 if (full_write(output_fd, buf, n) != n) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000463 bb_perror_msg_and_die(bb_msg_write_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000464 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000465#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Rob Landley19a39402006-06-13 17:10:26 +0000466 transferred += n;
Eric Andersenb520e082000-10-03 00:21:45 +0000467#endif
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000468 if (got_clen) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000469 content_len -= n;
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000470 }
471 }
Eric Andersen79757c92001-04-05 21:45:54 +0000472
Eric Andersen6d7fa432001-04-10 18:17:05 +0000473 if (chunked) {
Matt Kraai854125f2001-05-09 19:15:46 +0000474 safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
475 safe_fgets(buf, sizeof(buf), dfp);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000476 content_len = STRTOOFF(buf, (char **) NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000477 /* FIXME: error check? */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000478 if (content_len == 0) {
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000479 chunked = 0; /* all done! */
480 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000481 }
482
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000483 if (n == 0 && ferror(dfp)) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000484 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000485 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000486 } while (chunked);
Rob Landley19a39402006-06-13 17:10:26 +0000487
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000488 if (!(opt & WGET_OPT_QUIET))
Mark Whitley30ac01c2001-04-17 18:13:16 +0000489 progressmeter(1);
Rob Landley19a39402006-06-13 17:10:26 +0000490
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000491 if ((use_proxy == 0) && target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000492 fclose(dfp);
493 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000494 bb_error_msg_and_die("ftp error: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000495 ftpcmd("QUIT", NULL, sfp, buf);
496 }
Eric Andersen79757c92001-04-05 21:45:54 +0000497 exit(EXIT_SUCCESS);
Eric Andersen96700832000-09-04 15:15:55 +0000498}
499
500
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000501static void parse_url(char *url, struct host_info *h)
Eric Andersen96700832000-09-04 15:15:55 +0000502{
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000503 char *cp, *sp, *up, *pp;
Eric Andersen96700832000-09-04 15:15:55 +0000504
Eric Andersen79757c92001-04-05 21:45:54 +0000505 if (strncmp(url, "http://", 7) == 0) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000506 h->port = bb_lookup_port("http", "tcp", 80);
Eric Andersen79757c92001-04-05 21:45:54 +0000507 h->host = url + 7;
508 h->is_ftp = 0;
509 } else if (strncmp(url, "ftp://", 6) == 0) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000510 h->port = bb_lookup_port("ftp", "tfp", 21);
Eric Andersen79757c92001-04-05 21:45:54 +0000511 h->host = url + 6;
512 h->is_ftp = 1;
513 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000514 bb_error_msg_and_die("not an http or ftp url: %s", url);
Eric Andersen96700832000-09-04 15:15:55 +0000515
Eric Andersen79757c92001-04-05 21:45:54 +0000516 sp = strchr(h->host, '/');
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000517 if (sp) {
Matt Kraaia9711a52001-01-03 16:15:15 +0000518 *sp++ = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000519 h->path = sp;
Matt Kraaia9711a52001-01-03 16:15:15 +0000520 } else
Rob Landleyd921b2e2006-08-03 15:41:12 +0000521 h->path = xstrdup("");
Eric Andersen79757c92001-04-05 21:45:54 +0000522
523 up = strrchr(h->host, '@');
524 if (up != NULL) {
525 h->user = h->host;
526 *up++ = '\0';
527 h->host = up;
528 } else
529 h->user = NULL;
530
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000531 pp = h->host;
532
533#ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
534 if (h->host[0] == '[') {
535 char *ep;
536
537 ep = h->host + 1;
Eric Andersen6231f092003-09-11 08:25:11 +0000538 while (*ep == ':' || isxdigit (*ep))
Glenn L McGrathcc20ebc2003-09-10 23:52:15 +0000539 ep++;
540 if (*ep == ']') {
541 h->host++;
542 *ep = '\0';
543 pp = ep + 1;
544 }
545 }
546#endif
547
548 cp = strchr(pp, ':');
Eric Andersen79757c92001-04-05 21:45:54 +0000549 if (cp != NULL) {
550 *cp++ = '\0';
Glenn L McGrathf980bd52003-12-27 00:21:47 +0000551 h->port = htons(atoi(cp));
Eric Andersen79757c92001-04-05 21:45:54 +0000552 }
Eric Andersen96700832000-09-04 15:15:55 +0000553}
554
555
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000556static FILE *open_socket(struct sockaddr_in *s_in)
Eric Andersen96700832000-09-04 15:15:55 +0000557{
Eric Andersen96700832000-09-04 15:15:55 +0000558 FILE *fp;
559
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000560 fp = fdopen(xconnect(s_in), "r+");
561 if (fp == NULL)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000562 bb_perror_msg_and_die("fdopen");
Eric Andersen96700832000-09-04 15:15:55 +0000563
564 return fp;
565}
566
567
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000568static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
Eric Andersen96700832000-09-04 15:15:55 +0000569{
570 char *s, *hdrval;
571 int c;
572
573 *istrunc = 0;
574
575 /* retrieve header line */
576 if (fgets(buf, bufsiz, fp) == NULL)
577 return NULL;
578
579 /* see if we are at the end of the headers */
580 for (s = buf ; *s == '\r' ; ++s)
581 ;
582 if (s[0] == '\n')
583 return NULL;
584
585 /* convert the header name to lower case */
586 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
587 *s = tolower(*s);
588
589 /* verify we are at the end of the header name */
590 if (*s != ':')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000591 bb_error_msg_and_die("bad header line: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000592
593 /* locate the start of the header value */
594 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
595 ;
596 hdrval = s;
597
598 /* locate the end of header */
599 while (*s != '\0' && *s != '\r' && *s != '\n')
600 ++s;
601
602 /* end of header found */
603 if (*s != '\0') {
604 *s = '\0';
605 return hdrval;
606 }
607
Eric Andersen5d638842000-09-14 21:46:30 +0000608 /* Rats! The buffer isn't big enough to hold the entire header value. */
Eric Andersen96700832000-09-04 15:15:55 +0000609 while (c = getc(fp), c != EOF && c != '\n')
610 ;
611 *istrunc = 1;
612 return hdrval;
613}
614
Eric Andersen79757c92001-04-05 21:45:54 +0000615static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
616{
Eric Andersen79757c92001-04-05 21:45:54 +0000617 if (s1) {
618 if (!s2) s2="";
Eric Andersen3f1cf452003-03-11 18:03:39 +0000619 fprintf(fp, "%s%s\r\n", s1, s2);
Eric Andersen79757c92001-04-05 21:45:54 +0000620 fflush(fp);
621 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000622
Eric Andersen79757c92001-04-05 21:45:54 +0000623 do {
Glenn L McGrath32da8852004-04-08 10:27:11 +0000624 char *buf_ptr;
625
626 if (fgets(buf, 510, fp) == NULL) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000627 bb_perror_msg_and_die("fgets");
Glenn L McGrath32da8852004-04-08 10:27:11 +0000628 }
629 buf_ptr = strstr(buf, "\r\n");
630 if (buf_ptr) {
631 *buf_ptr = '\0';
632 }
Denis Vlasenko3526a132006-09-09 12:20:57 +0000633 } while (!isdigit(buf[0]) || buf[3] != ' ');
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000634
Eric Andersen79757c92001-04-05 21:45:54 +0000635 return atoi(buf);
636}
637
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000638#ifdef CONFIG_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000639/* Stuff below is from BSD rcp util.c, as added to openshh.
Eric Andersen4e573f42000-11-14 23:29:24 +0000640 * Original copyright notice is retained at the end of this file.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000641 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000642static int
Eric Andersenb520e082000-10-03 00:21:45 +0000643getttywidth(void)
644{
Eric Andersen8efe9672003-09-15 08:33:45 +0000645 int width=0;
646 get_terminal_width_height(0, &width, NULL);
647 return (width);
Eric Andersenb520e082000-10-03 00:21:45 +0000648}
649
Eric Andersen3e6ff902001-03-09 21:24:12 +0000650static void
Eric Andersenb520e082000-10-03 00:21:45 +0000651updateprogressmeter(int ignore)
652{
653 int save_errno = errno;
654
655 progressmeter(0);
656 errno = save_errno;
657}
658
Rob Landleyc9c1a412006-07-12 19:17:55 +0000659static void alarmtimer(int iwait)
Eric Andersenb520e082000-10-03 00:21:45 +0000660{
661 struct itimerval itv;
662
Rob Landleyc9c1a412006-07-12 19:17:55 +0000663 itv.it_value.tv_sec = iwait;
Eric Andersenb520e082000-10-03 00:21:45 +0000664 itv.it_value.tv_usec = 0;
665 itv.it_interval = itv.it_value;
666 setitimer(ITIMER_REAL, &itv, NULL);
667}
668
669
Eric Andersen3e6ff902001-03-09 21:24:12 +0000670static void
Eric Andersenb520e082000-10-03 00:21:45 +0000671progressmeter(int flag)
672{
Eric Andersenb520e082000-10-03 00:21:45 +0000673 static struct timeval lastupdate;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000674 static FILEOFF_TYPE lastsize, totalsize;
Rob Landley19a39402006-06-13 17:10:26 +0000675
Rob Landleyc9c1a412006-07-12 19:17:55 +0000676 struct timeval now, td, tvwait;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000677 FILEOFF_TYPE abbrevsize;
Rob Landley19a39402006-06-13 17:10:26 +0000678 int elapsed, ratio, barlength, i;
Eric Andersenb520e082000-10-03 00:21:45 +0000679 char buf[256];
680
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000681 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000682 (void) gettimeofday(&start, (struct timezone *) 0);
683 lastupdate = start;
684 lastsize = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000685 totalsize = content_len + beg_range; /* as content_len changes.. */
Eric Andersenb520e082000-10-03 00:21:45 +0000686 }
687
688 (void) gettimeofday(&now, (struct timezone *) 0);
Rob Landley19a39402006-06-13 17:10:26 +0000689 ratio = 100;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000690 if (totalsize != 0 && !chunked) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000691 ratio = (int) (100 * (transferred+beg_range) / totalsize);
Eric Andersenb520e082000-10-03 00:21:45 +0000692 ratio = MIN(ratio, 100);
Rob Landley19a39402006-06-13 17:10:26 +0000693 }
Eric Andersenb520e082000-10-03 00:21:45 +0000694
Rob Landley19a39402006-06-13 17:10:26 +0000695 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
Eric Andersenb520e082000-10-03 00:21:45 +0000696
697 barlength = getttywidth() - 51;
Rob Landley19a39402006-06-13 17:10:26 +0000698 if (barlength > 0 && barlength < sizeof(buf)) {
Eric Andersenb520e082000-10-03 00:21:45 +0000699 i = barlength * ratio / 100;
Rob Landley19a39402006-06-13 17:10:26 +0000700 memset(buf, '*', i);
701 memset(buf + i, ' ', barlength - i);
702 buf[barlength] = '\0';
703 fprintf(stderr, "|%s|", buf);
Eric Andersenb520e082000-10-03 00:21:45 +0000704 }
705 i = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000706 abbrevsize = transferred + beg_range;
Rob Landley19a39402006-06-13 17:10:26 +0000707 while (abbrevsize >= 100000) {
Eric Andersenb520e082000-10-03 00:21:45 +0000708 i++;
709 abbrevsize >>= 10;
710 }
Rob Landley19a39402006-06-13 17:10:26 +0000711 /* See http://en.wikipedia.org/wiki/Tera */
712 fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
Eric Andersenb520e082000-10-03 00:21:45 +0000713
Rob Landleyc9c1a412006-07-12 19:17:55 +0000714 timersub(&now, &lastupdate, &tvwait);
Rob Landley19a39402006-06-13 17:10:26 +0000715 if (transferred > lastsize) {
Eric Andersenb520e082000-10-03 00:21:45 +0000716 lastupdate = now;
Rob Landley19a39402006-06-13 17:10:26 +0000717 lastsize = transferred;
Rob Landleyc9c1a412006-07-12 19:17:55 +0000718 if (tvwait.tv_sec >= STALLTIME)
719 timeradd(&start, &tvwait, &start);
720 tvwait.tv_sec = 0;
Eric Andersenb520e082000-10-03 00:21:45 +0000721 }
722 timersub(&now, &start, &td);
Rob Landley19a39402006-06-13 17:10:26 +0000723 elapsed = td.tv_sec;
Eric Andersenb520e082000-10-03 00:21:45 +0000724
Rob Landleyc9c1a412006-07-12 19:17:55 +0000725 if (tvwait.tv_sec >= STALLTIME) {
Rob Landley19a39402006-06-13 17:10:26 +0000726 fprintf(stderr, " - stalled -");
Eric Andersenb520e082000-10-03 00:21:45 +0000727 } else {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000728 FILEOFF_TYPE to_download = totalsize - beg_range;
729 if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
730 fprintf(stderr, "--:--:-- ETA");
731 } else {
732 /* to_download / (transferred/elapsed) - elapsed: */
733 int eta = (int) (to_download*elapsed/transferred - elapsed);
734 i = eta % 3600;
735 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
736 }
Eric Andersenb520e082000-10-03 00:21:45 +0000737 }
Eric Andersenb520e082000-10-03 00:21:45 +0000738
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000739 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000740 struct sigaction sa;
741 sa.sa_handler = updateprogressmeter;
742 sigemptyset(&sa.sa_mask);
743 sa.sa_flags = SA_RESTART;
744 sigaction(SIGALRM, &sa, NULL);
745 alarmtimer(1);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000746 } else if (flag == 1) { /* last call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000747 alarmtimer(0);
Rob Landley19a39402006-06-13 17:10:26 +0000748 transferred = 0;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000749 putc('\n', stderr);
Eric Andersenb520e082000-10-03 00:21:45 +0000750 }
751}
752#endif
Eric Andersen4e573f42000-11-14 23:29:24 +0000753
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000754/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
Eric Andersenaff114c2004-04-14 17:51:38 +0000755 * much of which was blatantly stolen from openssh. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000756
Eric Andersen4e573f42000-11-14 23:29:24 +0000757/*-
758 * Copyright (c) 1992, 1993
759 * The Regents of the University of California. All rights reserved.
760 *
761 * Redistribution and use in source and binary forms, with or without
762 * modification, are permitted provided that the following conditions
763 * are met:
764 * 1. Redistributions of source code must retain the above copyright
765 * notice, this list of conditions and the following disclaimer.
766 * 2. Redistributions in binary form must reproduce the above copyright
767 * notice, this list of conditions and the following disclaimer in the
768 * documentation and/or other materials provided with the distribution.
769 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000770 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
771 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000772 *
773 * 4. Neither the name of the University nor the names of its contributors
774 * may be used to endorse or promote products derived from this software
775 * without specific prior written permission.
776 *
777 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
778 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
779 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
780 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
781 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
782 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
783 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
784 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
785 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
786 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
787 * SUCH DAMAGE.
788 *
Eric Andersen751750e2004-10-08 08:27:40 +0000789 * $Id: wget.c,v 1.75 2004/10/08 08:27:40 andersen Exp $
Eric Andersen4e573f42000-11-14 23:29:24 +0000790 */