bb_applet_name -> applet_name
[oweals/busybox.git] / networking / wget.c
index 4b55b93f4c2a7c9af12de9462d7428fd26d6f018..0054a9876094b3e738a3bc7fec8fd663e1bd529c 100644 (file)
@@ -6,8 +6,28 @@
  *
  */
 
+/* We want libc to give us xxx64 functions also */
+/* http://www.unix.org/version2/whatsnew/lfs20mar.html */
+#define _LARGEFILE64_SOURCE 1
+
 #include "busybox.h"
-#include <getopt.h>
+#include <getopt.h>    /* for struct option */
+
+#ifdef CONFIG_LFS
+# define FILEOFF_TYPE off64_t
+# define FILEOFF_FMT "%lld"
+# define LSEEK lseek64
+# define STRTOOFF strtoll
+# define SAFE_STRTOOFF safe_strtoll
+/* stat64 etc as needed...  */
+#else
+# define FILEOFF_TYPE off_t
+# define FILEOFF_FMT "%ld"
+# define LSEEK lseek
+# define STRTOOFF strtol
+# define SAFE_STRTOOFF safe_strtol
+/* Do we need to undefine O_LARGEFILE? */
+#endif
 
 struct host_info {
        char *host;
@@ -23,16 +43,16 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
 static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
 
 /* Globals (can be accessed from signal handlers */
-static off_t filesize;         /* content-length of the file */
-static int chunked;            /* chunked transfer encoding */
+static FILEOFF_TYPE content_len;        /* Content-length of the file */
+static FILEOFF_TYPE beg_range;          /* Range at which continue begins */
+static FILEOFF_TYPE transferred;        /* Number of bytes transferred so far */
+static int chunked;                     /* chunked transfer encoding */
 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
 static void progressmeter(int flag);
-static char *curfile;          /* Name of current file being transferred. */
-static struct timeval start;   /* Time a transfer started. */
-static off_t transferred;      /* Number of bytes transferred so far. */
-/* For progressmeter() -- number of seconds before xfer considered "stalled" */
+static char *curfile;                   /* Name of current file being transferred */
+static struct timeval start;            /* Time a transfer started */
 enum {
-       STALLTIME = 5
+       STALLTIME = 5                   /* Seconds when xfer considered "stalled" */
 };
 #else
 static void progressmeter(int flag) {}
@@ -78,20 +98,20 @@ static char *base64enc(unsigned char *p, char *buf, int len)
 }
 #endif
 
-#define WGET_OPT_CONTINUE      1
-#define WGET_OPT_QUIET 2
-#define WGET_OPT_PASSIVE       4
-#define WGET_OPT_OUTNAME       8
-#define WGET_OPT_HEADER        16
-#define WGET_OPT_PREFIX        32
-#define WGET_OPT_PROXY 64
-#define WGET_OPT_USER_AGENT    128
+#define WGET_OPT_CONTINUE     1
+#define WGET_OPT_QUIET        2
+#define WGET_OPT_PASSIVE      4
+#define WGET_OPT_OUTNAME      8
+#define WGET_OPT_HEADER      16
+#define WGET_OPT_PREFIX      32
+#define WGET_OPT_PROXY       64
+#define WGET_OPT_USER_AGENT 128
 
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
 static const struct option wget_long_options[] = {
        { "continue",        0, NULL, 'c' },
        { "quiet",           0, NULL, 'q' },
-       { "passive-ftp",     0, NULL, 139 },
+       { "passive-ftp",     0, NULL, 139 }, /* FIXME: what is this - 139?? */
        { "output-document", 1, NULL, 'O' },
        { "header",          1, NULL, 131 },
        { "directory-prefix",1, NULL, 'P' },
@@ -104,7 +124,7 @@ static const struct option wget_long_options[] = {
 int wget_main(int argc, char **argv)
 {
        int n, try=5, status;
-       unsigned long opt;
+       unsigned opt;
        int port;
        char *proxy = 0;
        char *dir_prefix=NULL;
@@ -116,34 +136,25 @@ int wget_main(int argc, char **argv)
        struct sockaddr_in s_in;
        llist_t *headers_llist = NULL;
 
-       FILE *sfp = NULL;               /* socket to web/ftp server         */
-       FILE *dfp = NULL;               /* socket to ftp server (data)      */
-       char *fname_out = NULL;         /* where to direct output (-O)      */
-       int do_continue = 0;            /* continue a prev transfer (-c)    */
-       off64_t beg_range = 0;          /*   range at which continue begins */
-       int got_clen = 0;               /* got content-length: from server  */
+       FILE *sfp = NULL;               /* socket to web/ftp server         */
+       FILE *dfp = NULL;               /* socket to ftp server (data)      */
+       char *fname_out = NULL;         /* where to direct output (-O)      */
+       int got_clen = 0;               /* got content-length: from server  */
        int output_fd = -1;
-       int quiet_flag = FALSE;         /* Be verry, verry quiet...         */
-       int use_proxy = 1;              /* Use proxies if env vars are set  */
-       char *proxy_flag = "on";        /* Use proxies if env vars are set  */
-       char *user_agent = "Wget"; /* Content of the "User-Agent" header field */
+       int use_proxy = 1;              /* Use proxies if env vars are set  */
+       char *proxy_flag = "on";        /* Use proxies if env vars are set  */
+       char *user_agent = "Wget";      /* Content of the "User-Agent" header field */
 
        /*
         * Crack command line.
         */
-       bb_opt_complementally = "-1:\203::";
+       opt_complementary = "-1:\203::";
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
-       bb_applet_long_options = wget_long_options;
+       applet_long_options = wget_long_options;
 #endif
-       opt = bb_getopt_ulflags(argc, argv, "cq\213O:\203:P:Y:U:",
+       opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:",
                                        &fname_out, &headers_llist,
                                        &dir_prefix, &proxy_flag, &user_agent);
-       if (opt & WGET_OPT_CONTINUE) {
-               ++do_continue;
-       }
-       if (opt & WGET_OPT_QUIET) {
-               quiet_flag = TRUE;
-       }
        if (strcmp(proxy_flag, "off") == 0) {
                /* Use the proxy if necessary. */
                use_proxy = 0;
@@ -205,7 +216,7 @@ int wget_main(int argc, char **argv)
                curfile = bb_get_last_path_component(fname_out);
 #endif
        }
-       if (do_continue && !fname_out)
+       if ((opt & WGET_OPT_CONTINUE) && !fname_out)
                bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
 
        /*
@@ -213,14 +224,14 @@ int wget_main(int argc, char **argv)
         */
        if (!strcmp(fname_out, "-")) {
                output_fd = 1;
-               quiet_flag = TRUE;
-               do_continue = 0;
-       } else if (do_continue) {
+               opt |= WGET_OPT_QUIET;
+               opt &= ~WGET_OPT_CONTINUE;
+       } else if (opt & WGET_OPT_CONTINUE) {
                output_fd = open(fname_out, O_WRONLY|O_LARGEFILE);
                if (output_fd >= 0) {
-                       beg_range = lseek64(output_fd, 0, SEEK_END);
-                       if (beg_range == (off64_t)-1)
-                               bb_perror_msg_and_die("lseek64");
+                       beg_range = LSEEK(output_fd, 0, SEEK_END);
+                       if (beg_range == (FILEOFF_TYPE)-1)
+                               bb_perror_msg_and_die("lseek");
                }
                /* File doesn't exist. We do not create file here yet.
                   We are not sure it exists on remove side */
@@ -231,7 +242,7 @@ int wget_main(int argc, char **argv)
         * and we want to connect to only one IP... */
        bb_lookup_host(&s_in, server.host);
        s_in.sin_port = server.port;
-       if (quiet_flag == FALSE) {
+       if (!(opt & WGET_OPT_QUIET)) {
                printf("Connecting to %s[%s]:%d\n",
                                server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
        }
@@ -283,7 +294,7 @@ int wget_main(int argc, char **argv)
 #endif
 
                        if (beg_range)
-                               fprintf(sfp, "Range: bytes=%lld-\r\n", (long long)beg_range);
+                               fprintf(sfp, "Range: bytes="FILEOFF_FMT"-\r\n", beg_range);
                        if(extra_headers_left < sizeof(extra_headers))
                                fputs(extra_headers,sfp);
                        fprintf(sfp,"Connection: close\r\n\r\n");
@@ -325,11 +336,9 @@ read_response:
                         */
                        while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
                                if (strcasecmp(buf, "content-length") == 0) {
-                                       unsigned long value;
-                                       if (safe_strtoul(s, &value)) {
+                                       if (SAFE_STRTOOFF(s, &content_len) || content_len < 0) {
                                                bb_error_msg_and_die("content-length %s is garbage", s);
                                        }
-                                       filesize = value;
                                        got_clen = 1;
                                        continue;
                                }
@@ -378,7 +387,7 @@ read_response:
                s = strchr(target.user, ':');
                if (s)
                        *(s++) = '\0';
-               switch(ftpcmd("USER ", target.user, sfp, buf)) {
+               switch (ftpcmd("USER ", target.user, sfp, buf)) {
                        case 230:
                                break;
                        case 331:
@@ -395,11 +404,9 @@ read_response:
                 * Querying file size
                 */
                if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
-                       unsigned long value;
-                       if (safe_strtoul(buf+4, &value)) {
+                       if (SAFE_STRTOOFF(buf+4, &content_len) || content_len < 0) {
                                bb_error_msg_and_die("SIZE value is garbage");
                        }
-                       filesize = value;
                        got_clen = 1;
                }
 
@@ -417,9 +424,9 @@ read_response:
                dfp = open_socket(&s_in);
 
                if (beg_range) {
-                       sprintf(buf, "REST %lld", (long long)beg_range);
+                       sprintf(buf, "REST "FILEOFF_FMT, beg_range);
                        if (ftpcmd(buf, NULL, sfp, buf) == 350)
-                               filesize -= beg_range;
+                               content_len -= beg_range;
                }
 
                if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
@@ -432,22 +439,26 @@ read_response:
         */
        if (chunked) {
                fgets(buf, sizeof(buf), dfp);
-               filesize = strtol(buf, (char **) NULL, 16);
+               content_len = STRTOOFF(buf, (char **) NULL, 16);
+               /* FIXME: error check?? */
        }
 
-       if (quiet_flag == FALSE)
+       /* Do it before progressmeter (want to have nice error message) */
+       if (output_fd < 0)
+               output_fd = xopen3(fname_out,
+                       O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE, 0666);
+
+       if (!(opt & WGET_OPT_QUIET))
                progressmeter(-1);
 
        do {
-               while (filesize > 0 || !got_clen) {
+               while (content_len > 0 || !got_clen) {
                        unsigned rdsz = sizeof(buf);
-                       if (filesize < sizeof(buf) && (chunked || got_clen))
-                               rdsz = filesize;
+                       if (content_len < sizeof(buf) && (chunked || got_clen))
+                               rdsz = (unsigned)content_len;
                        n = safe_fread(buf, 1, rdsz, dfp);
                        if (n <= 0)
                                break;
-                       if (output_fd < 0)
-                               output_fd = xopen3(fname_out, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE, 0666);
                        if (full_write(output_fd, buf, n) != n) {
                                bb_perror_msg_and_die(bb_msg_write_error);
                        }
@@ -455,15 +466,16 @@ read_response:
                        transferred += n;
 #endif
                        if (got_clen) {
-                               filesize -= n;
+                               content_len -= n;
                        }
                }
 
                if (chunked) {
                        safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
                        safe_fgets(buf, sizeof(buf), dfp);
-                       filesize = strtol(buf, (char **) NULL, 16);
-                       if (filesize == 0) {
+                       content_len = STRTOOFF(buf, (char **) NULL, 16);
+                       /* FIXME: error check? */
+                       if (content_len == 0) {
                                chunked = 0; /* all done! */
                        }
                }
@@ -473,7 +485,7 @@ read_response:
                }
        } while (chunked);
 
-       if (quiet_flag == FALSE)
+       if (!(opt & WGET_OPT_QUIET))
                progressmeter(1);
 
        if ((use_proxy == 0) && target.is_ftp) {
@@ -626,10 +638,7 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
 #ifdef CONFIG_FEATURE_WGET_STATUSBAR
 /* Stuff below is from BSD rcp util.c, as added to openshh.
  * Original copyright notice is retained at the end of this file.
- *
  */
-
-
 static int
 getttywidth(void)
 {
@@ -662,24 +671,24 @@ static void
 progressmeter(int flag)
 {
        static struct timeval lastupdate;
-       static off_t lastsize, totalsize;
+       static FILEOFF_TYPE lastsize, totalsize;
 
        struct timeval now, td, tvwait;
-       off_t abbrevsize;
+       FILEOFF_TYPE abbrevsize;
        int elapsed, ratio, barlength, i;
        char buf[256];
 
-       if (flag == -1) {
+       if (flag == -1) { /* first call to progressmeter */
                (void) gettimeofday(&start, (struct timezone *) 0);
                lastupdate = start;
                lastsize = 0;
-               totalsize = filesize; /* as filesize changes.. */
+               totalsize = content_len + beg_range; /* as content_len changes.. */
        }
 
        (void) gettimeofday(&now, (struct timezone *) 0);
        ratio = 100;
        if (totalsize != 0 && !chunked) {
-               ratio = (int) (100 * transferred / totalsize);
+               ratio = (int) (100 * (transferred+beg_range) / totalsize);
                ratio = MIN(ratio, 100);
        }
 
@@ -694,7 +703,7 @@ progressmeter(int flag)
                fprintf(stderr, "|%s|", buf);
        }
        i = 0;
-       abbrevsize = transferred;
+       abbrevsize = transferred + beg_range;
        while (abbrevsize >= 100000) {
                i++;
                abbrevsize >>= 10;
@@ -715,23 +724,26 @@ progressmeter(int flag)
 
        if (tvwait.tv_sec >= STALLTIME) {
                fprintf(stderr, " - stalled -");
-       } else if (transferred <= 0 || elapsed <= 0 || transferred > totalsize || chunked) {
-               fprintf(stderr, "--:--:-- ETA");
        } else {
-               /* totalsize / (transferred/elapsed) - elapsed: */
-               int eta = (int) (totalsize*elapsed/transferred - elapsed);
-               i = eta % 3600;
-               fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
+               FILEOFF_TYPE to_download = totalsize - beg_range;
+               if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
+                       fprintf(stderr, "--:--:-- ETA");
+               } else {
+                       /* to_download / (transferred/elapsed) - elapsed: */
+                       int eta = (int) (to_download*elapsed/transferred - elapsed);
+                       i = eta % 3600;
+                       fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
+               }
        }
 
-       if (flag == -1) {
+       if (flag == -1) { /* first call to progressmeter */
                struct sigaction sa;
                sa.sa_handler = updateprogressmeter;
                sigemptyset(&sa.sa_mask);
                sa.sa_flags = SA_RESTART;
                sigaction(SIGALRM, &sa, NULL);
                alarmtimer(1);
-       } else if (flag == 1) {
+       } else if (flag == 1) { /* last call to progressmeter */
                alarmtimer(0);
                transferred = 0;
                putc('\n', stderr);