wget.c 21 KB

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