X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fwget.c;h=41b4e30af69952e86681b60d8506e7e30f96b0ec;hb=85dd4b3b272793502b513d0d54b546c741ad1a5d;hp=c416a5862a7fe36257395f796cfcc7b7e5e2d8f4;hpb=65317ea27fd048686d4d75a116db3e869b7c0093;p=oweals%2Fbusybox.git diff --git a/networking/wget.c b/networking/wget.c index c416a5862..41b4e30af 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -24,6 +24,11 @@ #include #include +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include + #include "busybox.h" /* Stupid libc5 doesn't define this... */ @@ -51,15 +56,15 @@ static void parse_url(char *url, struct host_info *h); static FILE *open_socket(char *host, int port); static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf); -static void progressmeter(int flag); /* Globals (can be accessed from signal handlers */ static off_t filesize = 0; /* content-length of the file */ static int chunked = 0; /* chunked transfer encoding */ -#ifdef BB_FEATURE_WGET_STATUSBAR +#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 volatile unsigned long statbytes; /* Number of bytes transferred so far. */ +static volatile unsigned long statbytes = 0; /* Number of bytes transferred so far. */ /* For progressmeter() -- number of seconds before xfer considered "stalled" */ static const int STALLTIME = 5; #endif @@ -72,12 +77,56 @@ static void close_and_delete_outfile(FILE* output, char *fname_out, int do_conti } } +/* 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) +{ + size_t ret = 0; + + do { + clearerr(stream); + ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream); + } while (ret < nmemb && ferror(stream) && errno == EINTR); + + return ret; +} + +/* Write NMEMB elements of SIZE bytes from PTR to STREAM. Returns the + * number of elements written, and a short count if an eof or non-interrupt + * error is encountered. */ +static size_t safe_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret = 0; + + do { + clearerr(stream); + ret += fwrite((char *)ptr + (ret * size), size, nmemb - ret, stream); + } while (ret < nmemb && ferror(stream) && errno == EINTR); + + return ret; +} + +/* 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) +{ + char *ret; + + do { + clearerr(stream); + ret = fgets(s, size, stream); + } while (ret == NULL && ferror(stream) && errno == EINTR); + + return ret; +} + #define close_delete_and_die(s...) { \ close_and_delete_outfile(output, fname_out, do_continue); \ error_msg_and_die(s); } -#ifdef BB_FEATURE_WGET_AUTHENTICATION +#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION /* * Base64-encode character string * oops... isn't something similar in uuencode.c? @@ -111,9 +160,13 @@ int wget_main(int argc, char **argv) int n, try=5, status; int port; char *proxy; + char *dir_prefix=NULL; char *s, buf[512]; struct stat sbuf; - + char extra_headers[1024]; + char *extra_headers_ptr = extra_headers; + int extra_headers_left = sizeof(extra_headers); + int which_long_opt = 0, option_index = -1; struct host_info server, target; FILE *sfp = NULL; /* socket to web/ftp server */ @@ -125,14 +178,25 @@ int wget_main(int argc, char **argv) FILE *output; /* socket to web server */ int quiet_flag = FALSE; /* Be verry, verry quiet... */ +#define LONG_HEADER 1 + struct option long_options[] = { + { "continue", 0, NULL, 'c' }, + { "quiet", 0, NULL, 'q' }, + { "output-document", 1, NULL, 'O' }, + { "header", 1, &which_long_opt, LONG_HEADER }, + { 0, 0, 0, 0 } + }; /* * Crack command line. */ - while ((n = getopt(argc, argv, "cqO:")) != EOF) { + while ((n = getopt_long(argc, argv, "cqO:P:", long_options, &option_index)) != EOF) { switch (n) { case 'c': ++do_continue; break; + case 'P': + dir_prefix = optarg; + break; case 'q': quiet_flag = TRUE; break; @@ -143,6 +207,22 @@ int wget_main(int argc, char **argv) */ fname_out = optarg; break; + case 0: + switch (which_long_opt) { + case LONG_HEADER: { + int arglen = strlen(optarg); + if(extra_headers_left - arglen - 2 <= 0) + error_msg_and_die("extra_headers buffer too small(need %i)", extra_headers_left - arglen); + strcpy(extra_headers_ptr, optarg); + extra_headers_ptr += arglen; + extra_headers_left -= ( arglen + 2 ); + *extra_headers_ptr++ = '\r'; + *extra_headers_ptr++ = '\n'; + *(extra_headers_ptr + 1) = 0; + break; + } + } + break; default: show_usage(); } @@ -165,18 +245,20 @@ int wget_main(int argc, char **argv) /* Guess an output filename */ if (!fname_out) { fname_out = -#ifdef BB_FEATURE_WGET_STATUSBAR +#ifdef CONFIG_FEATURE_WGET_STATUSBAR curfile = #endif get_last_path_component(target.path); if (fname_out==NULL || strlen(fname_out)<1) { fname_out = -#ifdef BB_FEATURE_WGET_STATUSBAR +#ifdef CONFIG_FEATURE_WGET_STATUSBAR curfile = #endif "index.html"; } -#ifdef BB_FEATURE_WGET_STATUSBAR + if (dir_prefix != NULL) + fname_out = concat_path_file(dir_prefix, fname_out); +#ifdef CONFIG_FEATURE_WGET_STATUSBAR } else { curfile = get_last_path_component(fname_out); #endif @@ -190,6 +272,7 @@ int wget_main(int argc, char **argv) */ if (strcmp(fname_out, "-") == 0) { output = stdout; + quiet_flag = TRUE; } else { output = xfopen(fname_out, (do_continue ? "a" : "w")); } @@ -233,7 +316,7 @@ int wget_main(int argc, char **argv) fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", target.host); -#ifdef BB_FEATURE_WGET_AUTHENTICATION +#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION if (target.user) { fprintf(sfp, "Authorization: Basic %s\r\n", base64enc(target.user, buf, sizeof(buf))); @@ -246,6 +329,8 @@ int wget_main(int argc, char **argv) if (do_continue) fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); + if(extra_headers_left < sizeof(extra_headers)) + fputs(extra_headers,sfp); fprintf(sfp,"Connection: close\r\n\r\n"); /* @@ -390,26 +475,23 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL) fgets(buf, sizeof(buf), dfp); filesize = strtol(buf, (char **) NULL, 16); } - do { -#ifdef BB_FEATURE_WGET_STATUSBAR - statbytes=0; +#ifdef CONFIG_FEATURE_WGET_STATUSBAR if (quiet_flag==FALSE) progressmeter(-1); #endif - while ((filesize > 0 || !got_clen) && (n = fread(buf, 1, chunked ? (filesize > sizeof(buf) ? sizeof(buf) : filesize) : sizeof(buf), dfp)) > 0) { - fwrite(buf, 1, n, output); -#ifdef BB_FEATURE_WGET_STATUSBAR + do { + while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, chunked ? (filesize > sizeof(buf) ? sizeof(buf) : filesize) : sizeof(buf), dfp)) > 0) { + safe_fwrite(buf, 1, n, output); +#ifdef CONFIG_FEATURE_WGET_STATUSBAR statbytes+=n; - if (quiet_flag==FALSE) - progressmeter(1); #endif if (got_clen) filesize -= n; } if (chunked) { - fgets(buf, sizeof(buf), dfp); /* This is a newline */ - fgets(buf, sizeof(buf), dfp); + 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) chunked = 0; /* all done! */ } @@ -417,14 +499,16 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL) if (n == 0 && ferror(dfp)) perror_msg_and_die("network read error"); } while (chunked); - +#ifdef CONFIG_FEATURE_WGET_STATUSBAR + if (quiet_flag==FALSE) + progressmeter(1); +#endif if (!proxy && target.is_ftp) { fclose(dfp); if (ftpcmd(NULL, NULL, sfp, buf) != 226) error_msg_and_die("ftp error: %s", buf+4); ftpcmd("QUIT", NULL, sfp, buf); } - printf("\n"); exit(EXIT_SUCCESS); } @@ -477,8 +561,7 @@ FILE *open_socket(char *host, int port) memset(&s_in, 0, sizeof(s_in)); s_in.sin_family = AF_INET; - if ((hp = (struct hostent *) gethostbyname(host)) == NULL) - error_msg_and_die("cannot resolve %s", host); + hp = xgethostbyname(host); memcpy(&s_in.sin_addr, hp->h_addr_list[0], hp->h_length); s_in.sin_port = htons(port); @@ -562,7 +645,7 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf) return atoi(buf); } -#ifdef BB_FEATURE_WGET_STATUSBAR +#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. * @@ -606,7 +689,7 @@ progressmeter(int flag) { static const char prefixes[] = " KMGTP"; static struct timeval lastupdate; - static off_t lastsize; + static off_t lastsize, totalsize; struct timeval now, td, wait; off_t cursize, abbrevsize; double elapsed; @@ -617,12 +700,13 @@ progressmeter(int flag) (void) gettimeofday(&start, (struct timezone *) 0); lastupdate = start; lastsize = 0; + totalsize = filesize; /* as filesize changes.. */ } (void) gettimeofday(&now, (struct timezone *) 0); cursize = statbytes; - if (filesize != 0 && !chunked) { - ratio = 100.0 * cursize / filesize; + if (totalsize != 0 && !chunked) { + ratio = 100.0 * cursize / totalsize; ratio = MAX(ratio, 0); ratio = MIN(ratio, 100); } else @@ -662,14 +746,14 @@ progressmeter(int flag) timersub(&now, &start, &td); elapsed = td.tv_sec + (td.tv_usec / 1000000.0); - if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) { - snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), - " --:-- ETA"); - } else if (wait.tv_sec >= STALLTIME) { + if (wait.tv_sec >= STALLTIME) { snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " - stalled -"); + } else if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalsize || chunked) { + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + " --:-- ETA"); } else { - remaining = (int) (filesize / (statbytes / elapsed) - elapsed); + remaining = (int) (totalsize / (statbytes / elapsed) - elapsed); i = remaining / 3600; if (i) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), @@ -693,11 +777,12 @@ progressmeter(int flag) } else if (flag == 1) { alarmtimer(0); statbytes = 0; + putc('\n', stderr); } } #endif -/* Original copyright notice which applies to the BB_FEATURE_WGET_STATUSBAR stuff, +/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff, * much of which was blatently stolen from openssh. */ /*- @@ -732,7 +817,7 @@ progressmeter(int flag) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: wget.c,v 1.33 2001/04/11 20:03:01 kraai Exp $ + * $Id: wget.c,v 1.46 2001/10/24 04:59:56 andersen Exp $ */