wget.c 21 KB

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