small ipv6 doc changes; nslookup a tiny bit smaller
[oweals/busybox.git] / networking / wget.c
index 91e5e655a8e285924886b0861e294a83d0b5478c..8850fdfb7168f5bc2f8ab28a2cfc85d0467f4cde 100644 (file)
@@ -86,42 +86,22 @@ 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
-
-#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 }, /* FIXME: what is this - 139?? */
-       { "output-document", 1, NULL, 'O' },
-       { "header",          1, NULL, 131 },
-       { "directory-prefix",1, NULL, 'P' },
-       { "proxy",           1, NULL, 'Y' },
-       { "user-agent",      1, NULL, 'U' },
-       { 0,                 0, 0, 0 }
-};
-#endif
-
 int wget_main(int argc, char **argv)
 {
+       char buf[512];
+       struct host_info server, target;
+       struct sockaddr_in s_in;
        int n, status;
-       int try = 5;
        int port;
+       int try = 5;
        unsigned opt;
+       char *s;
        char *proxy = 0;
        char *dir_prefix = NULL;
-       char *s, buf[512];
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
        char *extra_headers = NULL;
-       struct host_info server, target;
-       struct sockaddr_in s_in;
        llist_t *headers_llist = NULL;
+#endif
 
        /* server.allocated = target.allocated = NULL; */
 
@@ -137,21 +117,46 @@ int wget_main(int argc, char **argv)
        /*
         * Crack command line.
         */
-       opt_complementary = "-1:\203::";
+       enum {
+               WGET_OPT_CONTINUE   = 0x1,
+               WGET_OPT_QUIET      = 0x2,
+               WGET_OPT_OUTNAME    = 0x4,
+               WGET_OPT_PREFIX     = 0x8,
+               WGET_OPT_PROXY      = 0x10,
+               WGET_OPT_USER_AGENT = 0x20,
+               WGET_OPT_PASSIVE    = 0x40,
+               WGET_OPT_HEADER     = 0x80,
+       };
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
+       static const struct option wget_long_options[] = {
+               // name, has_arg, flag, val
+               { "continue",         no_argument, NULL, 'c' },
+               { "quiet",            no_argument, NULL, 'q' },
+               { "output-document",  required_argument, NULL, 'O' },
+               { "directory-prefix", required_argument, NULL, 'P' },
+               { "proxy",            required_argument, NULL, 'Y' },
+               { "user-agent",       required_argument, NULL, 'U' },
+               { "passive-ftp",      no_argument, NULL, 0xff },
+               { "header",           required_argument, NULL, 0xfe },
+               { 0, 0, 0, 0 }
+};
        applet_long_options = wget_long_options;
 #endif
-       opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:",
-                                       &fname_out, &headers_llist,
-                                       &dir_prefix, &proxy_flag, &user_agent);
+       opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
+       opt = getopt32(argc, argv, "cqO:P:Y:U:",
+                               &fname_out, &dir_prefix,
+                               &proxy_flag, &user_agent
+                               USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
+                               );
        if (strcmp(proxy_flag, "off") == 0) {
                /* Use the proxy if necessary. */
                use_proxy = 0;
        }
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
        if (headers_llist) {
                int size = 1;
                char *cp;
-               llist_t *ll = headers_llist;
+               llist_t *ll = headers_llist = rev_llist(headers_llist);
                while (ll) {
                        size += strlen(ll->data) + 2;
                        ll = ll->link;
@@ -162,6 +167,7 @@ int wget_main(int argc, char **argv)
                        headers_llist = headers_llist->link;
                }
        }
+#endif
 
        parse_url(argv[optind], &target);
        server.host = target.host;
@@ -280,9 +286,11 @@ int wget_main(int argc, char **argv)
 #endif
 
                        if (beg_range)
-                               fprintf(sfp, "Range: bytes="OFF_FMT"-\r\n", beg_range);
+                               fprintf(sfp, "Range: bytes=%"OFF_FMT"-\r\n", beg_range);
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS
                        if (extra_headers)
                                fputs(extra_headers, sfp);
+#endif
                        fprintf(sfp, "Connection: close\r\n\r\n");
 
                        /*
@@ -294,7 +302,7 @@ int wget_main(int argc, char **argv)
 
                        s = buf;
                        while (*s != '\0' && !isspace(*s)) ++s;
-                       while (isspace(*s)) ++s;
+                       s = skip_whitespace(s);
                        // FIXME: no error check
                        // xatou wouldn't work: "200 OK"
                        status = atoi(s);
@@ -419,7 +427,7 @@ int wget_main(int argc, char **argv)
                dfp = open_socket(&s_in);
 
                if (beg_range) {
-                       sprintf(buf, "REST "OFF_FMT, beg_range);
+                       sprintf(buf, "REST %"OFF_FMT, beg_range);
                        if (ftpcmd(buf, NULL, sfp, buf) == 350)
                                content_len -= beg_range;
                }
@@ -577,7 +585,7 @@ static FILE *open_socket(struct sockaddr_in *s_in)
 {
        FILE *fp;
 
-       fp = fdopen(xconnect(s_in), "r+");
+       fp = fdopen(xconnect_tcp_v4(s_in), "r+");
        if (fp == NULL)
                bb_perror_msg_and_die("fdopen");
 
@@ -668,7 +676,7 @@ getttywidth(void)
 {
        int width=0;
        get_terminal_width_height(0, &width, NULL);
-       return (width);
+       return width;
 }
 
 static void