start_stop_daemon: NOMMU fixes, round 2 by Alex Landau <landau_alex@yahoo.com>
[oweals/busybox.git] / networking / wget.c
index ef27ab058b11f51d0fb0924b7d51af0918194600..d944f0173abe7528863b04fcece84fb686c8e736 100644 (file)
@@ -42,25 +42,27 @@ enum {
        STALLTIME = 5                   /* Seconds when xfer considered "stalled" */
 };
 #else
-static void progressmeter(int flag) {}
+static ALWAYS_INLINE void progressmeter(int flag) {}
 #endif
 
-/* Read NMEMB elements of SIZE bytes into PTR from STREAM.  Returns the
- * number of elements read, and a short count if an eof or non-interrupt
- * error is encountered.  */
-static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
+/* Read NMEMB bytes into PTR from STREAM.  Returns the number of bytes read,
+ * and a short count if an eof or non-interrupt error is encountered.  */
+static size_t safe_fread(void *ptr, size_t nmemb, FILE *stream)
 {
-       size_t ret = 0;
+       size_t ret;
+       char *p = (char*)ptr;
 
        do {
                clearerr(stream);
-               ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
-       } while (ret < nmemb && ferror(stream) && errno == EINTR);
+               ret = fread(p, 1, nmemb, stream);
+               p += ret;
+               nmemb -= ret;
+       } while (nmemb && ferror(stream) && errno == EINTR);
 
-       return ret;
+       return p - (char*)ptr;
 }
 
-/* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
+/* Read a line or SIZE-1 bytes into S, whichever is less, from STREAM.
  * Returns S, or NULL if an eof or non-interrupt error is encountered.  */
 static char *safe_fgets(char *s, int size, FILE *stream)
 {
@@ -75,10 +77,13 @@ static char *safe_fgets(char *s, int size, FILE *stream)
 }
 
 #if ENABLE_FEATURE_WGET_AUTHENTICATION
-/* Base64-encode character string and return the string.  */
-static char *base64enc(unsigned char *p, char *buf, int len)
+/* Base64-encode character string. buf is assumed to be char buf[512]. */
+static char *base64enc_512(char buf[512], const char *str)
 {
-       bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
+       unsigned len = strlen(str);
+       if (len > 512/4*3 - 10) /* paranoia */
+               len = 512/4*3 - 10;
+       bb_uuencode(buf, str, len, bb_uuenc_tbl_base64);
        return buf;
 }
 #endif
@@ -109,9 +114,8 @@ int wget_main(int argc, char **argv)
        bool use_proxy = 1;              /* Use proxies if env vars are set  */
        const char *proxy_flag = "on";  /* Use proxies if env vars are set  */
        const char *user_agent = "Wget";/* "User-Agent" header field        */
-       static const char * const keywords[] = {
-               "content-length", "transfer-encoding", "chunked", "location", NULL
-       };
+       static const char keywords[] =
+               "content-length\0""transfer-encoding\0""chunked\0""location\0";
        enum {
                KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
        };
@@ -127,20 +131,19 @@ int wget_main(int argc, char **argv)
                WGET_OPT_HEADER     = 0x100,
        };
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
-       static const struct option wget_long_options[] = {
-               /* name, has_arg, flag, val */
-               { "continue",         no_argument, NULL, 'c' },
-               { "spider",           no_argument, NULL, 's' },
-               { "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;
+       static const char wget_longopts[] =
+               /* name, has_arg, val */
+               "continue\0"         No_argument       "c"
+               "spider\0"           No_argument       "s"
+               "quiet\0"            No_argument       "q"
+               "output-document\0"  Required_argument "O"
+               "directory-prefix\0" Required_argument "P"
+               "proxy\0"            Required_argument "Y"
+               "user-agent\0"       Required_argument "U"
+               "passive-ftp\0"      No_argument       "\xff"
+               "header\0"           Required_argument "\xfe"
+               ;
+       applet_long_options = wget_longopts;
 #endif
        /* server.allocated = target.allocated = NULL; */
        opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
@@ -265,12 +268,12 @@ int wget_main(int argc, char **argv)
 
 #if ENABLE_FEATURE_WGET_AUTHENTICATION
                        if (target.user) {
-                               fprintf(sfp, "Authorization: Basic %s\r\n",
-                                       base64enc((unsigned char*)target.user, buf, sizeof(buf)));
+                               fprintf(sfp, "Proxy-Authorization: Basic %s\r\n"+6,
+                                       base64enc_512(buf, target.user));
                        }
                        if (use_proxy && server.user) {
                                fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
-                                       base64enc((unsigned char*)server.user, buf, sizeof(buf)));
+                                       base64enc_512(buf, server.user));
                        }
 #endif
 
@@ -323,7 +326,7 @@ int wget_main(int argc, char **argv)
                         */
                        while ((str = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
                                /* gethdr did already convert the "FOO:" string to lowercase */
-                               smalluint key = index_in_str_array(keywords, *&buf) + 1;
+                               smalluint key = index_in_strings(keywords, *&buf) + 1;
                                if (key == KEY_content_length) {
                                        content_len = BB_STRTOOFF(str, NULL, 10);
                                        if (errno || content_len < 0) {
@@ -333,7 +336,7 @@ int wget_main(int argc, char **argv)
                                        continue;
                                }
                                if (key == KEY_transfer_encoding) {
-                                       if (index_in_str_array(keywords, str_tolower(str)) + 1 != KEY_chunked)
+                                       if (index_in_strings(keywords, str_tolower(str)) + 1 != KEY_chunked)
                                                bb_error_msg_and_die("server wants to do %s transfer encoding", str);
                                        chunked = got_clen = 1;
                                }
@@ -459,7 +462,7 @@ int wget_main(int argc, char **argv)
                        unsigned rdsz = sizeof(buf);
                        if (content_len < sizeof(buf) && (chunked || got_clen))
                                rdsz = (unsigned)content_len;
-                       n = safe_fread(buf, 1, rdsz, dfp);
+                       n = safe_fread(buf, rdsz, dfp);
                        if (n <= 0)
                                break;
                        if (full_write(output_fd, buf, n) != n) {
@@ -775,7 +778,7 @@ progressmeter(int flag)
                putc('\n', stderr);
        }
 }
-#endif
+#endif /* FEATURE_WGET_STATUSBAR */
 
 /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
  * much of which was blatantly stolen from openssh.  */