blob: db222156b29e7f99e0374e7a0cae350beff2c51c [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
Eric Andersen79757c92001-04-05 21:45:54 +000016struct host_info {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +000017 // May be used if we ever will want to free() all xstrdup()s...
18 /* char *allocated; */
Eric Andersen79757c92001-04-05 21:45:54 +000019 char *host;
20 int port;
21 char *path;
22 int is_ftp;
23 char *user;
24};
25
26static void parse_url(char *url, struct host_info *h);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000027static FILE *open_socket(len_and_sockaddr *lsa);
Eric Andersen3e6ff902001-03-09 21:24:12 +000028static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000029static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf);
Eric Andersen96700832000-09-04 15:15:55 +000030
Eric Andersenb520e082000-10-03 00:21:45 +000031/* Globals (can be accessed from signal handlers */
Denis Vlasenko7039a662006-10-08 17:54:47 +000032static off_t content_len; /* Content-length of the file */
33static off_t beg_range; /* Range at which continue begins */
Denis Vlasenko9cade082006-11-21 10:43:02 +000034#if ENABLE_FEATURE_WGET_STATUSBAR
Denis Vlasenko7039a662006-10-08 17:54:47 +000035static off_t transferred; /* Number of bytes transferred so far */
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000036#endif
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +000037static int chunked; /* chunked transfer encoding */
Denis Vlasenko9cade082006-11-21 10:43:02 +000038#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersen20aab262001-07-19 22:28:02 +000039static void progressmeter(int flag);
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000040static const char *curfile; /* Name of current file being transferred */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000041static struct timeval start; /* Time a transfer started */
Rob Landley19a39402006-06-13 17:10:26 +000042enum {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +000043 STALLTIME = 5 /* Seconds when xfer considered "stalled" */
Rob Landley19a39402006-06-13 17:10:26 +000044};
45#else
Rob Landleyf5fc1382006-09-15 04:08:25 +000046static void progressmeter(int flag) {}
Eric Andersenb520e082000-10-03 00:21:45 +000047#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048
Matt Kraai854125f2001-05-09 19:15:46 +000049/* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the
50 * number of elements read, and a short count if an eof or non-interrupt
51 * error is encountered. */
52static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
53{
54 size_t ret = 0;
55
56 do {
57 clearerr(stream);
58 ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
59 } while (ret < nmemb && ferror(stream) && errno == EINTR);
60
61 return ret;
62}
63
Matt Kraai854125f2001-05-09 19:15:46 +000064/* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
65 * Returns S, or NULL if an eof or non-interrupt error is encountered. */
66static char *safe_fgets(char *s, int size, FILE *stream)
67{
68 char *ret;
69
70 do {
71 clearerr(stream);
72 ret = fgets(s, size, stream);
73 } while (ret == NULL && ferror(stream) && errno == EINTR);
74
75 return ret;
76}
77
Denis Vlasenko9cade082006-11-21 10:43:02 +000078#if ENABLE_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +000079/*
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000080 * Base64-encode character string and return the string.
Eric Andersen79757c92001-04-05 21:45:54 +000081 */
Denis Vlasenko3526a132006-09-09 12:20:57 +000082static char *base64enc(unsigned char *p, char *buf, int len)
83{
Denis Vlasenko21afc7d2006-09-03 15:49:40 +000084 bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
Rob Landley76ef08c2006-06-13 16:44:26 +000085 return buf;
Eric Andersen79757c92001-04-05 21:45:54 +000086}
87#endif
88
Denis Vlasenko06af2162007-02-03 17:28:39 +000089int wget_main(int argc, char **argv);
Eric Andersen96700832000-09-04 15:15:55 +000090int wget_main(int argc, char **argv)
91{
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000092 char buf[512];
Eric Andersen79757c92001-04-05 21:45:54 +000093 struct host_info server, target;
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000094 len_and_sockaddr *lsa;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000095 int n, status;
96 int port;
97 int try = 5;
98 unsigned opt;
99 char *s;
100 char *proxy = 0;
101 char *dir_prefix = NULL;
102#if ENABLE_FEATURE_WGET_LONG_OPTIONS
103 char *extra_headers = NULL;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000104 llist_t *headers_llist = NULL;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000105#endif
Eric Andersen79757c92001-04-05 21:45:54 +0000106
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000107 /* server.allocated = target.allocated = NULL; */
108
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000109 FILE *sfp = NULL; /* socket to web/ftp server */
110 FILE *dfp = NULL; /* socket to ftp server (data) */
111 char *fname_out = NULL; /* where to direct output (-O) */
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000112 int got_clen = 0; /* got content-length: from server */
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000113 int output_fd = -1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000114 int use_proxy = 1; /* Use proxies if env vars are set */
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000115 const char *proxy_flag = "on"; /* Use proxies if env vars are set */
116 const char *user_agent = "Wget";/* Content of the "User-Agent" header field */
Eric Andersen29edd002000-12-09 16:55:35 +0000117
Eric Andersen96700832000-09-04 15:15:55 +0000118 /*
119 * Crack command line.
120 */
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000121 enum {
122 WGET_OPT_CONTINUE = 0x1,
123 WGET_OPT_QUIET = 0x2,
124 WGET_OPT_OUTNAME = 0x4,
125 WGET_OPT_PREFIX = 0x8,
126 WGET_OPT_PROXY = 0x10,
127 WGET_OPT_USER_AGENT = 0x20,
128 WGET_OPT_PASSIVE = 0x40,
129 WGET_OPT_HEADER = 0x80,
130 };
Bernhard Reutner-Fischer289e86a2006-08-20 20:01:24 +0000131#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000132 static const struct option wget_long_options[] = {
133 // name, has_arg, flag, val
134 { "continue", no_argument, NULL, 'c' },
135 { "quiet", no_argument, NULL, 'q' },
136 { "output-document", required_argument, NULL, 'O' },
137 { "directory-prefix", required_argument, NULL, 'P' },
138 { "proxy", required_argument, NULL, 'Y' },
139 { "user-agent", required_argument, NULL, 'U' },
140 { "passive-ftp", no_argument, NULL, 0xff },
141 { "header", required_argument, NULL, 0xfe },
142 { 0, 0, 0, 0 }
Denis Vlasenko601ae132006-11-28 23:37:46 +0000143 };
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000144 applet_long_options = wget_long_options;
Bernhard Reutner-Fischer8d3a6f72006-05-31 14:11:38 +0000145#endif
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000146 opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
147 opt = getopt32(argc, argv, "cqO:P:Y:U:",
148 &fname_out, &dir_prefix,
149 &proxy_flag, &user_agent
150 USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
151 );
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000152 if (strcmp(proxy_flag, "off") == 0) {
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000153 /* Use the proxy if necessary. */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000154 use_proxy = 0;
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000155 }
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000156#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko7534e082006-10-23 23:21:58 +0000157 if (headers_llist) {
158 int size = 1;
159 char *cp;
Denis Vlasenko54cf5112007-02-17 18:11:45 +0000160 llist_t *ll = headers_llist = llist_rev(headers_llist);
Denis Vlasenko7534e082006-10-23 23:21:58 +0000161 while (ll) {
162 size += strlen(ll->data) + 2;
163 ll = ll->link;
164 }
165 extra_headers = cp = xmalloc(size);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000166 while (headers_llist) {
Denis Vlasenko7534e082006-10-23 23:21:58 +0000167 cp += sprintf(cp, "%s\r\n", headers_llist->data);
Glenn L McGrath514aeab2003-12-19 12:08:56 +0000168 headers_llist = headers_llist->link;
Eric Andersen96700832000-09-04 15:15:55 +0000169 }
170 }
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000171#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000172
Eric Andersen79757c92001-04-05 21:45:54 +0000173 parse_url(argv[optind], &target);
174 server.host = target.host;
175 server.port = target.port;
176
Eric Andersen96700832000-09-04 15:15:55 +0000177 /*
Eric Andersenf3b2b522000-12-07 22:42:11 +0000178 * Use the proxy if necessary.
Eric Andersen96700832000-09-04 15:15:55 +0000179 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000180 if (use_proxy) {
Robert Griebld7760112002-05-14 23:36:45 +0000181 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000182 if (proxy && *proxy) {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000183 parse_url(proxy, &server);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000184 } else {
185 use_proxy = 0;
186 }
Robert Griebld7760112002-05-14 23:36:45 +0000187 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000188
Eric Andersen29edd002000-12-09 16:55:35 +0000189 /* Guess an output filename */
190 if (!fname_out) {
Eric Andersen751750e2004-10-08 08:27:40 +0000191 // Dirty hack. Needed because bb_get_last_path_component
192 // will destroy trailing / by storing '\0' in last byte!
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000193 if (!last_char_is(target.path, '/')) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000194 fname_out = bb_get_last_path_component(target.path);
Denis Vlasenko9cade082006-11-21 10:43:02 +0000195#if ENABLE_FEATURE_WGET_STATUSBAR
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000196 curfile = fname_out;
Eric Andersen29edd002000-12-09 16:55:35 +0000197#endif
Eric Andersen751750e2004-10-08 08:27:40 +0000198 }
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000199 if (!fname_out || !fname_out[0]) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000200 /* bb_get_last_path_component writes
201 * to last '/' only. We don't have one here... */
202 fname_out = (char*)"index.html";
Denis Vlasenko9cade082006-11-21 10:43:02 +0000203#if ENABLE_FEATURE_WGET_STATUSBAR
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000204 curfile = fname_out;
Eric Andersen29edd002000-12-09 16:55:35 +0000205#endif
Eric Andersen29edd002000-12-09 16:55:35 +0000206 }
Matt Kraai0382eb82001-07-19 19:13:55 +0000207 if (dir_prefix != NULL)
208 fname_out = concat_path_file(dir_prefix, fname_out);
Denis Vlasenko9cade082006-11-21 10:43:02 +0000209#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersen29edd002000-12-09 16:55:35 +0000210 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 curfile = bb_get_last_path_component(fname_out);
Eric Andersen29edd002000-12-09 16:55:35 +0000212#endif
213 }
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000214 /* Impossible?
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000215 if ((opt & WGET_OPT_CONTINUE) && !fname_out)
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000216 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
Eric Andersen29edd002000-12-09 16:55:35 +0000217
Eric Andersen96700832000-09-04 15:15:55 +0000218 /*
219 * Determine where to start transfer.
220 */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000221 if (LONE_DASH(fname_out)) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000222 output_fd = 1;
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000223 opt &= ~WGET_OPT_CONTINUE;
Denis Vlasenko4e4662c2006-11-23 13:10:23 +0000224 }
225 if (opt & WGET_OPT_CONTINUE) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000226 output_fd = open(fname_out, O_WRONLY);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000227 if (output_fd >= 0) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000228 beg_range = xlseek(output_fd, 0, SEEK_END);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000229 }
230 /* File doesn't exist. We do not create file here yet.
231 We are not sure it exists on remove side */
Eric Andersen96700832000-09-04 15:15:55 +0000232 }
233
Eric Andersene6dc4392003-10-31 09:31:46 +0000234 /* We want to do exactly _one_ DNS lookup, since some
235 * sites (i.e. ftp.us.debian.org) use round-robin DNS
236 * and we want to connect to only one IP... */
Denis Vlasenko42823d52007-02-04 02:39:08 +0000237 lsa = xhost2sockaddr(server.host, server.port);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000238 if (!(opt & WGET_OPT_QUIET)) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000239 fprintf(stderr, "Connecting to %s (%s)\n", server.host,
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000240 xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
Denis Vlasenko85629f02007-01-22 09:36:41 +0000241 /* We leak result of xmalloc_sockaddr2dotted */
Eric Andersene6dc4392003-10-31 09:31:46 +0000242 }
243
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000244 if (use_proxy || !target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000245 /*
246 * HTTP session
247 */
248 do {
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000249 got_clen = chunked = 0;
250
Denis Vlasenko3526a132006-09-09 12:20:57 +0000251 if (!--try)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000252 bb_error_msg_and_die("too many redirections");
Eric Andersen7d697012001-01-24 20:28:35 +0000253
Eric Andersen79757c92001-04-05 21:45:54 +0000254 /*
255 * Open socket to http server
256 */
257 if (sfp) fclose(sfp);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000258 sfp = open_socket(lsa);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000259
Eric Andersen79757c92001-04-05 21:45:54 +0000260 /*
261 * Send HTTP request.
262 */
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000263 if (use_proxy) {
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000264 fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
Eric Andersen79757c92001-04-05 21:45:54 +0000265 target.is_ftp ? "f" : "ht", target.host,
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000266 target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000267 } else {
Eric Andersen6d7fa432001-04-10 18:17:05 +0000268 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
Eric Andersen79757c92001-04-05 21:45:54 +0000269 }
Eric Andersen96700832000-09-04 15:15:55 +0000270
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000271 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
272 target.host, user_agent);
Eric Andersen79757c92001-04-05 21:45:54 +0000273
Denis Vlasenko9cade082006-11-21 10:43:02 +0000274#if ENABLE_FEATURE_WGET_AUTHENTICATION
Eric Andersen79757c92001-04-05 21:45:54 +0000275 if (target.user) {
276 fprintf(sfp, "Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000277 base64enc((unsigned char*)target.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000278 }
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000279 if (use_proxy && server.user) {
Eric Andersen79757c92001-04-05 21:45:54 +0000280 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000281 base64enc((unsigned char*)server.user, buf, sizeof(buf)));
Eric Andersen79757c92001-04-05 21:45:54 +0000282 }
283#endif
284
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000285 if (beg_range)
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000286 fprintf(sfp, "Range: bytes=%"OFF_FMT"d-\r\n", beg_range);
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000287#if ENABLE_FEATURE_WGET_LONG_OPTIONS
Denis Vlasenko7534e082006-10-23 23:21:58 +0000288 if (extra_headers)
289 fputs(extra_headers, sfp);
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000290#endif
Denis Vlasenko7534e082006-10-23 23:21:58 +0000291 fprintf(sfp, "Connection: close\r\n\r\n");
Eric Andersen79757c92001-04-05 21:45:54 +0000292
293 /*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000294 * Retrieve HTTP response line and check for "200" status code.
295 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000296 read_response:
Glenn L McGrathe7bdfcc2003-08-28 22:03:19 +0000297 if (fgets(buf, sizeof(buf), sfp) == NULL)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000298 bb_error_msg_and_die("no response from server");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000299
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000300 s = buf;
301 while (*s != '\0' && !isspace(*s)) ++s;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000302 s = skip_whitespace(s);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000303 // FIXME: no error check
304 // xatou wouldn't work: "200 OK"
305 status = atoi(s);
306 switch (status) {
307 case 0:
308 case 100:
309 while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
310 /* eat all remaining headers */;
311 goto read_response;
312 case 200:
313 break;
314 case 300: /* redirection */
315 case 301:
316 case 302:
317 case 303:
318 break;
319 case 206:
320 if (beg_range)
Eric Andersen79757c92001-04-05 21:45:54 +0000321 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000322 /*FALLTHRU*/
323 default:
Denis Vlasenko067e3f02006-11-10 23:25:53 +0000324 /* Show first line only and kill any ESC tricks */
325 buf[strcspn(buf, "\n\r\x1b")] = '\0';
326 bb_error_msg_and_die("server returned error: %s", buf);
Eric Andersen79757c92001-04-05 21:45:54 +0000327 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000328
Eric Andersen79757c92001-04-05 21:45:54 +0000329 /*
330 * Retrieve HTTP headers.
331 */
332 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
333 if (strcasecmp(buf, "content-length") == 0) {
Denis Vlasenkod686a042006-11-27 14:43:21 +0000334 content_len = BB_STRTOOFF(s, NULL, 10);
335 if (errno || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000336 bb_error_msg_and_die("content-length %s is garbage", s);
Eric Andersen24794452004-03-06 22:11:45 +0000337 }
Eric Andersen79757c92001-04-05 21:45:54 +0000338 got_clen = 1;
339 continue;
340 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000341 if (strcasecmp(buf, "transfer-encoding") == 0) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000342 if (strcasecmp(s, "chunked") != 0)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000343 bb_error_msg_and_die("server wants to do %s transfer encoding", s);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000344 chunked = got_clen = 1;
Eric Andersen6d7fa432001-04-10 18:17:05 +0000345 }
Eric Andersen79757c92001-04-05 21:45:54 +0000346 if (strcasecmp(buf, "location") == 0) {
347 if (s[0] == '/')
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000348 /* free(target.allocated); */
349 target.path = /* target.allocated = */ xstrdup(s+1);
Eric Andersen79757c92001-04-05 21:45:54 +0000350 else {
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000351 parse_url(s, &target);
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000352 if (use_proxy == 0) {
Eric Andersen79757c92001-04-05 21:45:54 +0000353 server.host = target.host;
354 server.port = target.port;
355 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000356 free(lsa);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000357 lsa = xhost2sockaddr(server.host, server.port);
Glenn L McGrath58a2e0e2004-01-17 23:07:14 +0000358 break;
Eric Andersen79757c92001-04-05 21:45:54 +0000359 }
360 }
361 }
Denis Vlasenko3469c182006-12-16 22:19:47 +0000362 } while (status >= 300);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000363
Eric Andersen79757c92001-04-05 21:45:54 +0000364 dfp = sfp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000365
366 } else {
367
Eric Andersen79757c92001-04-05 21:45:54 +0000368 /*
369 * FTP session
370 */
Denis Vlasenko3526a132006-09-09 12:20:57 +0000371 if (!target.user)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000372 target.user = xstrdup("anonymous:busybox@");
Eric Andersen79757c92001-04-05 21:45:54 +0000373
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000374 sfp = open_socket(lsa);
Eric Andersen79757c92001-04-05 21:45:54 +0000375 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000376 bb_error_msg_and_die("%s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000377
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000378 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000379 * Splitting username:password pair,
380 * trying to log in
381 */
382 s = strchr(target.user, ':');
383 if (s)
384 *(s++) = '\0';
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000385 switch (ftpcmd("USER ", target.user, sfp, buf)) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000386 case 230:
387 break;
388 case 331:
389 if (ftpcmd("PASS ", s, sfp, buf) == 230)
Eric Andersenb520e082000-10-03 00:21:45 +0000390 break;
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000391 /* FALLTHRU (failed login) */
392 default:
393 bb_error_msg_and_die("ftp login: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000394 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000395
Eric Andersen79757c92001-04-05 21:45:54 +0000396 ftpcmd("TYPE I", NULL, sfp, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000397
Eric Andersen79757c92001-04-05 21:45:54 +0000398 /*
399 * Querying file size
400 */
Rob Landleyaf12cb32006-06-27 18:41:03 +0000401 if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
Denis Vlasenkod686a042006-11-27 14:43:21 +0000402 content_len = BB_STRTOOFF(buf+4, NULL, 10);
403 if (errno || content_len < 0) {
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000404 bb_error_msg_and_die("SIZE value is garbage");
Eric Andersen24794452004-03-06 22:11:45 +0000405 }
Eric Andersen96700832000-09-04 15:15:55 +0000406 got_clen = 1;
Eric Andersen96700832000-09-04 15:15:55 +0000407 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000408
Eric Andersen79757c92001-04-05 21:45:54 +0000409 /*
410 * Entering passive mode
411 */
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000412 if (ftpcmd("PASV", NULL, sfp, buf) != 227) {
413 pasv_error:
414 bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
415 }
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000416 // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000417 // Server's IP is N1.N2.N3.N4 (we ignore it)
418 // Server's port for data connection is P1*256+P2
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000419 s = strrchr(buf, ')');
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000420 if (s) s[0] = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000421 s = strrchr(buf, ',');
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000422 if (!s) goto pasv_error;
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000423 port = xatou_range(s+1, 0, 255);
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000424 *s = '\0';
Eric Andersen79757c92001-04-05 21:45:54 +0000425 s = strrchr(buf, ',');
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000426 if (!s) goto pasv_error;
Denis Vlasenkof8c8bb12006-11-21 19:10:26 +0000427 port += xatou_range(s+1, 0, 255) * 256;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000428 set_nport(lsa, htons(port));
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000429 dfp = open_socket(lsa);
Eric Andersen79757c92001-04-05 21:45:54 +0000430
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000431 if (beg_range) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000432 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000433 if (ftpcmd(buf, NULL, sfp, buf) == 350)
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000434 content_len -= beg_range;
Eric Andersen96700832000-09-04 15:15:55 +0000435 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000436
Rob Landleyaf12cb32006-06-27 18:41:03 +0000437 if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000438 bb_error_msg_and_die("bad response to RETR: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000439 }
440
Eric Andersen79757c92001-04-05 21:45:54 +0000441
Eric Andersen96700832000-09-04 15:15:55 +0000442 /*
Eric Andersen79757c92001-04-05 21:45:54 +0000443 * Retrieve file
Eric Andersen96700832000-09-04 15:15:55 +0000444 */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000445 if (chunked) {
446 fgets(buf, sizeof(buf), dfp);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000447 content_len = STRTOOFF(buf, NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000448 /* FIXME: error check?? */
Eric Andersen6d7fa432001-04-10 18:17:05 +0000449 }
Rob Landley19a39402006-06-13 17:10:26 +0000450
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000451 /* Do it before progressmeter (want to have nice error message) */
452 if (output_fd < 0)
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000453 output_fd = xopen(fname_out,
454 O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000455
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000456 if (!(opt & WGET_OPT_QUIET))
Glenn L McGrath1bca5ed2000-12-09 08:12:06 +0000457 progressmeter(-1);
Rob Landley19a39402006-06-13 17:10:26 +0000458
Mark Whitley30ac01c2001-04-17 18:13:16 +0000459 do {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000460 while (content_len > 0 || !got_clen) {
Denis Vlasenko3526a132006-09-09 12:20:57 +0000461 unsigned rdsz = sizeof(buf);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000462 if (content_len < sizeof(buf) && (chunked || got_clen))
463 rdsz = (unsigned)content_len;
Denis Vlasenko3526a132006-09-09 12:20:57 +0000464 n = safe_fread(buf, 1, rdsz, dfp);
465 if (n <= 0)
466 break;
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000467 if (full_write(output_fd, buf, n) != n) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000468 bb_perror_msg_and_die(bb_msg_write_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000469 }
Denis Vlasenko9cade082006-11-21 10:43:02 +0000470#if ENABLE_FEATURE_WGET_STATUSBAR
Rob Landley19a39402006-06-13 17:10:26 +0000471 transferred += n;
Eric Andersenb520e082000-10-03 00:21:45 +0000472#endif
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000473 if (got_clen) {
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000474 content_len -= n;
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000475 }
476 }
Eric Andersen79757c92001-04-05 21:45:54 +0000477
Eric Andersen6d7fa432001-04-10 18:17:05 +0000478 if (chunked) {
Matt Kraai854125f2001-05-09 19:15:46 +0000479 safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
480 safe_fgets(buf, sizeof(buf), dfp);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000481 content_len = STRTOOFF(buf, NULL, 16);
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000482 /* FIXME: error check? */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000483 if (content_len == 0) {
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000484 chunked = 0; /* all done! */
485 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000486 }
487
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000488 if (n == 0 && ferror(dfp)) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000489 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath83e4a5b2003-08-28 21:55:22 +0000490 }
Eric Andersen6d7fa432001-04-10 18:17:05 +0000491 } while (chunked);
Rob Landley19a39402006-06-13 17:10:26 +0000492
Denis Vlasenkoa552eeb2006-09-26 09:22:12 +0000493 if (!(opt & WGET_OPT_QUIET))
Mark Whitley30ac01c2001-04-17 18:13:16 +0000494 progressmeter(1);
Rob Landley19a39402006-06-13 17:10:26 +0000495
Glenn L McGrathf1c4b112004-02-22 00:27:34 +0000496 if ((use_proxy == 0) && target.is_ftp) {
Eric Andersen79757c92001-04-05 21:45:54 +0000497 fclose(dfp);
498 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000499 bb_error_msg_and_die("ftp error: %s", buf+4);
Eric Andersen79757c92001-04-05 21:45:54 +0000500 ftpcmd("QUIT", NULL, sfp, buf);
501 }
Eric Andersen79757c92001-04-05 21:45:54 +0000502 exit(EXIT_SUCCESS);
Eric Andersen96700832000-09-04 15:15:55 +0000503}
504
505
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000506static void parse_url(char *src_url, struct host_info *h)
Eric Andersen96700832000-09-04 15:15:55 +0000507{
Denis Vlasenko3469c182006-12-16 22:19:47 +0000508 char *url, *p, *sp;
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000509
510 /* h->allocated = */ url = xstrdup(src_url);
Eric Andersen96700832000-09-04 15:15:55 +0000511
Eric Andersen79757c92001-04-05 21:45:54 +0000512 if (strncmp(url, "http://", 7) == 0) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000513 h->port = bb_lookup_port("http", "tcp", 80);
Eric Andersen79757c92001-04-05 21:45:54 +0000514 h->host = url + 7;
515 h->is_ftp = 0;
516 } else if (strncmp(url, "ftp://", 6) == 0) {
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000517 h->port = bb_lookup_port("ftp", "tcp", 21);
Eric Andersen79757c92001-04-05 21:45:54 +0000518 h->host = url + 6;
519 h->is_ftp = 1;
520 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000521 bb_error_msg_and_die("not an http or ftp url: %s", url);
Eric Andersen96700832000-09-04 15:15:55 +0000522
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000523 // FYI:
524 // "Real" wget 'http://busybox.net?var=a/b' sends this request:
525 // 'GET /?var=a/b HTTP 1.0'
Denis Vlasenko96e9d3c2006-10-07 14:28:55 +0000526 // and saves 'index.html?var=a%2Fb' (we save 'b')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000527 // wget 'http://busybox.net?login=john@doe':
528 // request: 'GET /?login=john@doe HTTP/1.0'
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000529 // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000530 // wget 'http://busybox.net#test/test':
531 // request: 'GET / HTTP/1.0'
532 // saves: 'index.html' (we save 'test')
533 //
534 // We also don't add unique .N suffix if file exists...
Eric Andersen79757c92001-04-05 21:45:54 +0000535 sp = strchr(h->host, '/');
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000536 p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
537 p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
538 if (!sp) {
Denis Vlasenkode55b5d2007-02-01 01:53:25 +0000539 /* must be writable because of bb_get_last_path_component() */
Denis Vlasenkoafe488d2007-01-28 16:07:45 +0000540 static char nullstr[] = "";
541 h->path = nullstr;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000542 } else if (*sp == '/') {
Denis Vlasenko3469c182006-12-16 22:19:47 +0000543 *sp = '\0';
544 h->path = sp + 1;
Denis Vlasenkoa6551522006-10-07 14:28:28 +0000545 } else { // '#' or '?'
546 // http://busybox.net?login=john@doe is a valid URL
547 // memmove converts to:
548 // http:/busybox.nett?login=john@doe...
549 memmove(h->host-1, h->host, sp - h->host);
550 h->host--;
551 sp[-1] = '\0';
552 h->path = sp;
553 }
Eric Andersen79757c92001-04-05 21:45:54 +0000554
Denis Vlasenko3469c182006-12-16 22:19:47 +0000555 sp = strrchr(h->host, '@');
556 h->user = NULL;
557 if (sp != NULL) {
Eric Andersen79757c92001-04-05 21:45:54 +0000558 h->user = h->host;
Denis Vlasenko3469c182006-12-16 22:19:47 +0000559 *sp = '\0';
560 h->host = sp + 1;
561 }
Eric Andersen79757c92001-04-05 21:45:54 +0000562
Denis Vlasenko3469c182006-12-16 22:19:47 +0000563 sp = h->host;
Eric Andersen96700832000-09-04 15:15:55 +0000564}
565
566
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000567static FILE *open_socket(len_and_sockaddr *lsa)
Eric Andersen96700832000-09-04 15:15:55 +0000568{
Eric Andersen96700832000-09-04 15:15:55 +0000569 FILE *fp;
570
Denis Vlasenko067e3f02006-11-10 23:25:53 +0000571 /* glibc 2.4 seems to try seeking on it - ??! */
572 /* hopefully it understands what ESPIPE means... */
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000573 fp = fdopen(xconnect_stream(lsa), "r+");
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000574 if (fp == NULL)
Denis Vlasenko3526a132006-09-09 12:20:57 +0000575 bb_perror_msg_and_die("fdopen");
Eric Andersen96700832000-09-04 15:15:55 +0000576
577 return fp;
578}
579
580
Denis Vlasenkoa94554d2006-09-23 17:49:09 +0000581static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
Eric Andersen96700832000-09-04 15:15:55 +0000582{
583 char *s, *hdrval;
584 int c;
585
586 *istrunc = 0;
587
588 /* retrieve header line */
589 if (fgets(buf, bufsiz, fp) == NULL)
590 return NULL;
591
592 /* see if we are at the end of the headers */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000593 for (s = buf; *s == '\r'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000594 ;
595 if (s[0] == '\n')
596 return NULL;
597
598 /* convert the header name to lower case */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000599 for (s = buf; isalnum(*s) || *s == '-'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000600 *s = tolower(*s);
601
602 /* verify we are at the end of the header name */
603 if (*s != ':')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000604 bb_error_msg_and_die("bad header line: %s", buf);
Eric Andersen96700832000-09-04 15:15:55 +0000605
606 /* locate the start of the header value */
Denis Vlasenko40f62a82006-11-21 11:04:31 +0000607 for (*s++ = '\0'; *s == ' ' || *s == '\t'; ++s)
Eric Andersen96700832000-09-04 15:15:55 +0000608 ;
609 hdrval = s;
610
611 /* locate the end of header */
612 while (*s != '\0' && *s != '\r' && *s != '\n')
613 ++s;
614
615 /* end of header found */
616 if (*s != '\0') {
617 *s = '\0';
618 return hdrval;
619 }
620
Eric Andersen5d638842000-09-14 21:46:30 +0000621 /* Rats! The buffer isn't big enough to hold the entire header value. */
Eric Andersen96700832000-09-04 15:15:55 +0000622 while (c = getc(fp), c != EOF && c != '\n')
623 ;
624 *istrunc = 1;
625 return hdrval;
626}
627
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000628static int ftpcmd(const char *s1, const char *s2, FILE *fp, char *buf)
Eric Andersen79757c92001-04-05 21:45:54 +0000629{
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000630 int result;
Eric Andersen79757c92001-04-05 21:45:54 +0000631 if (s1) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000632 if (!s2) s2 = "";
Eric Andersen3f1cf452003-03-11 18:03:39 +0000633 fprintf(fp, "%s%s\r\n", s1, s2);
Eric Andersen79757c92001-04-05 21:45:54 +0000634 fflush(fp);
635 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000636
Eric Andersen79757c92001-04-05 21:45:54 +0000637 do {
Glenn L McGrath32da8852004-04-08 10:27:11 +0000638 char *buf_ptr;
639
640 if (fgets(buf, 510, fp) == NULL) {
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000641 bb_perror_msg_and_die("error getting response");
Glenn L McGrath32da8852004-04-08 10:27:11 +0000642 }
643 buf_ptr = strstr(buf, "\r\n");
644 if (buf_ptr) {
645 *buf_ptr = '\0';
646 }
Denis Vlasenko3526a132006-09-09 12:20:57 +0000647 } while (!isdigit(buf[0]) || buf[3] != ' ');
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000648
Denis Vlasenko023b57d2006-10-15 17:05:55 +0000649 buf[3] = '\0';
650 result = xatoi_u(buf);
651 buf[3] = ' ';
652 return result;
Eric Andersen79757c92001-04-05 21:45:54 +0000653}
654
Denis Vlasenko9cade082006-11-21 10:43:02 +0000655#if ENABLE_FEATURE_WGET_STATUSBAR
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000656/* Stuff below is from BSD rcp util.c, as added to openshh.
Eric Andersen4e573f42000-11-14 23:29:24 +0000657 * Original copyright notice is retained at the end of this file.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000658 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000659static int
Eric Andersenb520e082000-10-03 00:21:45 +0000660getttywidth(void)
661{
Denis Vlasenko9cade082006-11-21 10:43:02 +0000662 int width;
Eric Andersen8efe9672003-09-15 08:33:45 +0000663 get_terminal_width_height(0, &width, NULL);
Denis Vlasenko621204b2006-10-27 09:03:24 +0000664 return width;
Eric Andersenb520e082000-10-03 00:21:45 +0000665}
666
Eric Andersen3e6ff902001-03-09 21:24:12 +0000667static void
Eric Andersenb520e082000-10-03 00:21:45 +0000668updateprogressmeter(int ignore)
669{
670 int save_errno = errno;
671
672 progressmeter(0);
673 errno = save_errno;
674}
675
Rob Landleyc9c1a412006-07-12 19:17:55 +0000676static void alarmtimer(int iwait)
Eric Andersenb520e082000-10-03 00:21:45 +0000677{
678 struct itimerval itv;
679
Rob Landleyc9c1a412006-07-12 19:17:55 +0000680 itv.it_value.tv_sec = iwait;
Eric Andersenb520e082000-10-03 00:21:45 +0000681 itv.it_value.tv_usec = 0;
682 itv.it_interval = itv.it_value;
683 setitimer(ITIMER_REAL, &itv, NULL);
684}
685
686
Eric Andersen3e6ff902001-03-09 21:24:12 +0000687static void
Eric Andersenb520e082000-10-03 00:21:45 +0000688progressmeter(int flag)
689{
Eric Andersenb520e082000-10-03 00:21:45 +0000690 static struct timeval lastupdate;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000691 static off_t lastsize, totalsize;
Rob Landley19a39402006-06-13 17:10:26 +0000692
Rob Landleyc9c1a412006-07-12 19:17:55 +0000693 struct timeval now, td, tvwait;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000694 off_t abbrevsize;
Rob Landley19a39402006-06-13 17:10:26 +0000695 int elapsed, ratio, barlength, i;
Eric Andersenb520e082000-10-03 00:21:45 +0000696 char buf[256];
697
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000698 if (flag == -1) { /* first call to progressmeter */
Denis Vlasenko9cade082006-11-21 10:43:02 +0000699 gettimeofday(&start, (struct timezone *) 0);
Eric Andersenb520e082000-10-03 00:21:45 +0000700 lastupdate = start;
701 lastsize = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000702 totalsize = content_len + beg_range; /* as content_len changes.. */
Eric Andersenb520e082000-10-03 00:21:45 +0000703 }
704
Denis Vlasenko9cade082006-11-21 10:43:02 +0000705 gettimeofday(&now, (struct timezone *) 0);
Rob Landley19a39402006-06-13 17:10:26 +0000706 ratio = 100;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000707 if (totalsize != 0 && !chunked) {
Denis Vlasenko9cade082006-11-21 10:43:02 +0000708 /* long long helps to have working ETA even if !LFS */
709 ratio = (int) (100 * (unsigned long long)(transferred+beg_range) / totalsize);
Eric Andersenb520e082000-10-03 00:21:45 +0000710 ratio = MIN(ratio, 100);
Rob Landley19a39402006-06-13 17:10:26 +0000711 }
Eric Andersenb520e082000-10-03 00:21:45 +0000712
Rob Landley19a39402006-06-13 17:10:26 +0000713 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
Eric Andersenb520e082000-10-03 00:21:45 +0000714
715 barlength = getttywidth() - 51;
Rob Landley19a39402006-06-13 17:10:26 +0000716 if (barlength > 0 && barlength < sizeof(buf)) {
Eric Andersenb520e082000-10-03 00:21:45 +0000717 i = barlength * ratio / 100;
Rob Landley19a39402006-06-13 17:10:26 +0000718 memset(buf, '*', i);
719 memset(buf + i, ' ', barlength - i);
720 buf[barlength] = '\0';
721 fprintf(stderr, "|%s|", buf);
Eric Andersenb520e082000-10-03 00:21:45 +0000722 }
723 i = 0;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000724 abbrevsize = transferred + beg_range;
Rob Landley19a39402006-06-13 17:10:26 +0000725 while (abbrevsize >= 100000) {
Eric Andersenb520e082000-10-03 00:21:45 +0000726 i++;
727 abbrevsize >>= 10;
728 }
Denis Vlasenko9cade082006-11-21 10:43:02 +0000729 /* see http://en.wikipedia.org/wiki/Tera */
Rob Landley19a39402006-06-13 17:10:26 +0000730 fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
Eric Andersenb520e082000-10-03 00:21:45 +0000731
Rob Landleyc9c1a412006-07-12 19:17:55 +0000732 timersub(&now, &lastupdate, &tvwait);
Rob Landley19a39402006-06-13 17:10:26 +0000733 if (transferred > lastsize) {
Eric Andersenb520e082000-10-03 00:21:45 +0000734 lastupdate = now;
Rob Landley19a39402006-06-13 17:10:26 +0000735 lastsize = transferred;
Rob Landleyc9c1a412006-07-12 19:17:55 +0000736 if (tvwait.tv_sec >= STALLTIME)
737 timeradd(&start, &tvwait, &start);
738 tvwait.tv_sec = 0;
Eric Andersenb520e082000-10-03 00:21:45 +0000739 }
740 timersub(&now, &start, &td);
Rob Landley19a39402006-06-13 17:10:26 +0000741 elapsed = td.tv_sec;
Eric Andersenb520e082000-10-03 00:21:45 +0000742
Rob Landleyc9c1a412006-07-12 19:17:55 +0000743 if (tvwait.tv_sec >= STALLTIME) {
Rob Landley19a39402006-06-13 17:10:26 +0000744 fprintf(stderr, " - stalled -");
Eric Andersenb520e082000-10-03 00:21:45 +0000745 } else {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000746 off_t to_download = totalsize - beg_range;
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000747 if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
748 fprintf(stderr, "--:--:-- ETA");
749 } else {
750 /* to_download / (transferred/elapsed) - elapsed: */
Denis Vlasenko9cade082006-11-21 10:43:02 +0000751 int eta = (int) ((unsigned long long)to_download*elapsed/transferred - elapsed);
752 /* (long long helps to have working ETA even if !LFS) */
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000753 i = eta % 3600;
754 fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
755 }
Eric Andersenb520e082000-10-03 00:21:45 +0000756 }
Eric Andersenb520e082000-10-03 00:21:45 +0000757
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000758 if (flag == -1) { /* first call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000759 struct sigaction sa;
760 sa.sa_handler = updateprogressmeter;
761 sigemptyset(&sa.sa_mask);
762 sa.sa_flags = SA_RESTART;
763 sigaction(SIGALRM, &sa, NULL);
764 alarmtimer(1);
Denis Vlasenkof8aa1092006-10-01 10:58:54 +0000765 } else if (flag == 1) { /* last call to progressmeter */
Eric Andersenb520e082000-10-03 00:21:45 +0000766 alarmtimer(0);
Rob Landley19a39402006-06-13 17:10:26 +0000767 transferred = 0;
Mark Whitley30ac01c2001-04-17 18:13:16 +0000768 putc('\n', stderr);
Eric Andersenb520e082000-10-03 00:21:45 +0000769 }
770}
771#endif
Eric Andersen4e573f42000-11-14 23:29:24 +0000772
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000773/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
Eric Andersenaff114c2004-04-14 17:51:38 +0000774 * much of which was blatantly stolen from openssh. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000775
Eric Andersen4e573f42000-11-14 23:29:24 +0000776/*-
777 * Copyright (c) 1992, 1993
778 * The Regents of the University of California. All rights reserved.
779 *
780 * Redistribution and use in source and binary forms, with or without
781 * modification, are permitted provided that the following conditions
782 * are met:
783 * 1. Redistributions of source code must retain the above copyright
784 * notice, this list of conditions and the following disclaimer.
785 * 2. Redistributions in binary form must reproduce the above copyright
786 * notice, this list of conditions and the following disclaimer in the
787 * documentation and/or other materials provided with the distribution.
788 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000789 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
790 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen4e573f42000-11-14 23:29:24 +0000791 *
792 * 4. Neither the name of the University nor the names of its contributors
793 * may be used to endorse or promote products derived from this software
794 * without specific prior written permission.
795 *
796 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
797 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
798 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
799 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
800 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
801 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
802 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
803 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
804 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
805 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
806 * SUCH DAMAGE.
807 *
Eric Andersen4e573f42000-11-14 23:29:24 +0000808 */