mount: add --rbind option support
[oweals/busybox.git] / networking / wget.c
index 6c9a512117fb44ab2ae334654839ea9c5cd1817a..c22a76b977b1a59f2393bc255cc3536868455ef4 100644 (file)
@@ -8,6 +8,32 @@
  * Copyright (C) 2010 Bradley M. Kuhn <bkuhn@ebb.org>
  * Kuhn's copyrights are licensed GPLv2-or-later.  File as a whole remains GPLv2.
  */
+
+//usage:#define wget_trivial_usage
+//usage:       IF_FEATURE_WGET_LONG_OPTIONS(
+//usage:       "[-c|--continue] [-s|--spider] [-q|--quiet] [-O|--output-document FILE]\n"
+//usage:       "       [--header 'header: value'] [-Y|--proxy on/off] [-P DIR]\n"
+//usage:       "       [--no-check-certificate] [-U|--user-agent AGENT]"
+//usage:                       IF_FEATURE_WGET_TIMEOUT(" [-T SEC]") " URL..."
+//usage:       )
+//usage:       IF_NOT_FEATURE_WGET_LONG_OPTIONS(
+//usage:       "[-csq] [-O FILE] [-Y on/off] [-P DIR] [-U AGENT]"
+//usage:                       IF_FEATURE_WGET_TIMEOUT(" [-T SEC]") " URL..."
+//usage:       )
+//usage:#define wget_full_usage "\n\n"
+//usage:       "Retrieve files via HTTP or FTP\n"
+//usage:     "\nOptions:"
+//usage:     "\n       -s      Spider mode - only check file existence"
+//usage:     "\n       -c      Continue retrieval of aborted transfer"
+//usage:     "\n       -q      Quiet"
+//usage:     "\n       -P DIR  Save to DIR (default .)"
+//usage:       IF_FEATURE_WGET_TIMEOUT(
+//usage:     "\n       -T SEC  Network read timeout is SEC seconds"
+//usage:       )
+//usage:     "\n       -O FILE Save to FILE ('-' for stdout)"
+//usage:     "\n       -U STR  Use STR for User-Agent header"
+//usage:     "\n       -Y      Use proxy ('on' or 'off')"
+
 #include "libbb.h"
 
 //#define log_io(...) bb_error_msg(__VA_ARGS__)
@@ -556,7 +582,7 @@ static void NOINLINE retrieve_file_data(FILE *dfp)
        progress_meter(PROGRESS_END);
 }
 
-static int download_one_url(const char *url)
+static void download_one_url(const char *url)
 {
        bool use_proxy;                 /* Use proxies if env vars are set  */
        int redir_limit;
@@ -604,8 +630,14 @@ static int download_one_url(const char *url)
                if (G.fname_out[0] == '/' || !G.fname_out[0])
                        G.fname_out = (char*)"index.html";
                /* -P DIR is considered only if there was no -O FILE */
-               if (G.dir_prefix)
-                       G.fname_out = fname_out_alloc = concat_path_file(G.dir_prefix, G.fname_out);
+               else {
+                       if (G.dir_prefix)
+                               G.fname_out = fname_out_alloc = concat_path_file(G.dir_prefix, G.fname_out);
+                       else {
+                               /* redirects may free target.path later, need to make a copy */
+                               G.fname_out = fname_out_alloc = xstrdup(G.fname_out);
+                       }
+               }
        }
 #if ENABLE_FEATURE_WGET_STATUSBAR
        G.curfile = bb_get_last_path_component_nostrip(G.fname_out);
@@ -853,8 +885,6 @@ However, in real world it was observed that some web servers
        free(server.allocated);
        free(target.allocated);
        free(fname_out_alloc);
-
-       return EXIT_SUCCESS;
 }
 
 int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -885,7 +915,6 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
                ;
 #endif
 
-       int exitcode;
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
        llist_t *headers_llist = NULL;
 #endif
@@ -937,9 +966,11 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
                G.o_flags = O_WRONLY | O_CREAT | O_TRUNC;
        }
 
-       exitcode = 0;
        while (*argv)
-               exitcode |= download_one_url(*argv++);
+               download_one_url(*argv++);
 
-       return exitcode;
+       if (G.output_fd >= 0)
+               xclose(G.output_fd);
+
+       return EXIT_SUCCESS;
 }