Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * wget - retrieve a file using HTTP |
| 4 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 5 | * Chip Rosenthal Covad Communications <chip@laserlink.net> |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 6 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 7 | * Note: According to RFC2616 section 3.6.1, "All HTTP/1.1 applications MUST be |
| 8 | * able to receive and decode the "chunked" transfer-coding, and MUST ignore |
| 9 | * chunk-extension extensions they do not understand." |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 10 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 11 | * This prevents this particular wget app from completely RFC compliant, and as |
| 12 | * such, prevents it from being used as a general purpose web browser... This |
| 13 | * is a design decision, since it makes the code smaller. |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 14 | * |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <ctype.h> |
| 22 | #include <string.h> |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | #include <signal.h> |
| 25 | #include <sys/ioctl.h> |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 26 | |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 27 | #include <sys/time.h> |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <netinet/in.h> |
| 32 | #include <arpa/inet.h> |
| 33 | #include <netdb.h> |
| 34 | |
| 35 | |
| 36 | void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path); |
| 37 | FILE *open_socket(char *host, int port); |
| 38 | char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 39 | void progressmeter(int flag); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 40 | |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 41 | /* Globals (can be accessed from signal handlers */ |
| 42 | static off_t filesize = 0; /* content-length of the file */ |
| 43 | #ifdef BB_FEATURE_STATUSBAR |
| 44 | static char *curfile; /* Name of current file being transferred. */ |
| 45 | static struct timeval start; /* Time a transfer started. */ |
| 46 | volatile unsigned long statbytes; /* Number of bytes transferred so far. */ |
| 47 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ |
| 48 | #define STALLTIME 5 |
| 49 | #endif |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 50 | |
| 51 | int wget_main(int argc, char **argv) |
| 52 | { |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 53 | int n; |
| 54 | char *proxy, *proxy_host; |
| 55 | int uri_port, proxy_port; |
| 56 | char *s, buf[512]; |
| 57 | struct stat sbuf; |
| 58 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 59 | FILE *sfp; /* socket to web server */ |
| 60 | char *uri_host, *uri_path; /* parsed from command line url */ |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 61 | char *fname_out = NULL; /* where to direct output (-O) */ |
| 62 | int do_continue = 0; /* continue a prev transfer (-c) */ |
| 63 | long beg_range = 0L; /* range at which continue begins */ |
| 64 | int got_clen = 0; /* got content-length: from server */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 65 | FILE *output; /* socket to web server */ |
| 66 | int quiet_flag = FALSE; /* Be verry, verry quiet... */ |
| 67 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Crack command line. |
| 70 | */ |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 71 | while ((n = getopt(argc, argv, "cqO:")) != EOF) { |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 72 | switch (n) { |
| 73 | case 'c': |
| 74 | ++do_continue; |
| 75 | break; |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 76 | case 'q': |
| 77 | quiet_flag = TRUE; |
| 78 | break; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 79 | case 'O': |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 80 | /* can't set fname_out to NULL if outputting to stdout, because |
| 81 | * this gets interpreted as the auto-gen output filename |
| 82 | * case below - tausq@debian.org |
| 83 | */ |
| 84 | fname_out = (strcmp(optarg, "-") == 0 ? (char *)1 : optarg); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 85 | break; |
| 86 | default: |
| 87 | usage(wget_usage); |
| 88 | } |
| 89 | } |
Eric Andersen | 25b669c | 2000-10-02 23:19:38 +0000 | [diff] [blame] | 90 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 91 | if (argc - optind != 1) |
| 92 | usage(wget_usage); |
Eric Andersen | 25b669c | 2000-10-02 23:19:38 +0000 | [diff] [blame] | 93 | |
Eric Andersen | 25b669c | 2000-10-02 23:19:38 +0000 | [diff] [blame] | 94 | if (do_continue && !fname_out) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 95 | error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n"); |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 96 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 97 | /* |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 98 | * Use the proxy if necessary. |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 99 | */ |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 100 | if ((proxy = getenv("http_proxy")) != NULL) { |
| 101 | proxy = xstrdup(proxy); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 102 | parse_url(proxy, &proxy_host, &proxy_port, &uri_path); |
| 103 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 104 | } else { |
| 105 | /* |
| 106 | * Parse url into components. |
| 107 | */ |
| 108 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 109 | proxy_host=uri_host; |
| 110 | proxy_port=uri_port; |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 111 | } |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 112 | |
| 113 | /* Guess an output filename */ |
| 114 | if (!fname_out) { |
| 115 | fname_out = |
| 116 | #ifdef BB_FEATURE_STATUSBAR |
| 117 | curfile = |
| 118 | #endif |
| 119 | get_last_path_component(uri_path); |
| 120 | if (fname_out==NULL || strlen(fname_out)<1) { |
| 121 | fname_out = |
| 122 | #ifdef BB_FEATURE_STATUSBAR |
| 123 | curfile = |
| 124 | #endif |
| 125 | "index.html"; |
| 126 | } |
| 127 | #ifdef BB_FEATURE_STATUSBAR |
| 128 | } else { |
| 129 | curfile=argv[optind]; |
| 130 | #endif |
| 131 | } |
| 132 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * Open socket to server. |
| 136 | */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 137 | sfp = open_socket(proxy_host, proxy_port); |
| 138 | |
| 139 | /* Make the assumption that if the file already exists |
| 140 | * on disk that the intention is to continue downloading |
| 141 | * a previously aborted download -Erik */ |
| 142 | if (stat(fname_out, &sbuf) == 0) { |
| 143 | ++do_continue; |
| 144 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 147 | * Open the output file stream. |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 148 | */ |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 149 | if (fname_out != (char *)1) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 150 | if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) |
| 151 | == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 152 | perror_msg_and_die("fopen(%s)", fname_out); |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 153 | } else { |
| 154 | output = stdout; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Determine where to start transfer. |
| 159 | */ |
| 160 | if (do_continue) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 161 | if (fstat(fileno(output), &sbuf) < 0) |
Matt Kraai | 0dab829 | 2000-12-18 03:08:29 +0000 | [diff] [blame] | 162 | perror_msg_and_die("fstat()"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 163 | if (sbuf.st_size > 0) |
| 164 | beg_range = sbuf.st_size; |
| 165 | else |
| 166 | do_continue = 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Send HTTP request. |
| 171 | */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 172 | fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n", |
| 173 | uri_host, uri_port, uri_path); |
| 174 | fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 175 | if (do_continue) |
| 176 | fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 177 | fprintf(sfp,"Connection: close\r\n\r\n"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Retrieve HTTP response line and check for "200" status code. |
| 181 | */ |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 182 | if (fgets(buf, sizeof(buf), sfp) == NULL) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 183 | error_msg_and_die("no response from server\n"); |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 184 | } |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 185 | for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) |
| 186 | ; |
| 187 | for ( ; isspace(*s) ; ++s) |
| 188 | ; |
| 189 | switch (atoi(s)) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 190 | case 200: |
| 191 | if (!do_continue) |
| 192 | break; |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 193 | error_msg_and_die("server does not support ranges\n"); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 194 | case 206: |
| 195 | if (do_continue) |
| 196 | break; |
| 197 | /*FALLTHRU*/ |
| 198 | default: |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 199 | error_msg_and_die("server returned error: %s", buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Retrieve HTTP headers. |
| 204 | */ |
| 205 | while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { |
| 206 | if (strcmp(buf, "content-length") == 0) { |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 207 | filesize = atol(s); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 208 | got_clen = 1; |
| 209 | continue; |
| 210 | } |
| 211 | if (strcmp(buf, "transfer-encoding") == 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 212 | error_msg_and_die("server wants to do %s transfer encoding\n", s); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 213 | continue; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Retrieve HTTP body. |
| 219 | */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 220 | #ifdef BB_FEATURE_STATUSBAR |
| 221 | statbytes=0; |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 222 | if (quiet_flag==FALSE) |
| 223 | progressmeter(-1); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 224 | #endif |
| 225 | while (filesize > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) { |
| 226 | fwrite(buf, 1, n, output); |
| 227 | #ifdef BB_FEATURE_STATUSBAR |
| 228 | statbytes+=n; |
Glenn L McGrath | 1bca5ed | 2000-12-09 08:12:06 +0000 | [diff] [blame] | 229 | if (quiet_flag==FALSE) |
| 230 | progressmeter(1); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 231 | #endif |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 232 | if (got_clen) |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 233 | filesize -= n; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 234 | } |
| 235 | if (n == 0 && ferror(sfp)) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 236 | perror_msg_and_die("network read error"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 237 | |
| 238 | exit(0); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path) |
| 243 | { |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame^] | 244 | char *cp, *sp; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 245 | |
| 246 | *uri_port = 80; |
| 247 | |
| 248 | if (strncmp(url, "http://", 7) != 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 249 | error_msg_and_die("not an http url: %s\n", url); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 250 | |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame^] | 251 | *uri_host = url + 7; |
| 252 | |
| 253 | cp = strchr(*uri_host, ':'); |
| 254 | sp = strchr(*uri_host, '/'); |
| 255 | |
| 256 | if (cp != NULL && (sp == NULL || cp < sp)) { |
| 257 | *cp++ = '\0'; |
| 258 | *uri_port = atoi(cp); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 259 | } |
Randolph Chung | 02553a2 | 2000-12-07 03:53:47 +0000 | [diff] [blame] | 260 | |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame^] | 261 | if (sp != NULL) { |
| 262 | *sp++ = '\0'; |
| 263 | *uri_path = sp; |
| 264 | } else |
| 265 | *uri_path = ""; |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | FILE *open_socket(char *host, int port) |
| 270 | { |
| 271 | struct sockaddr_in sin; |
| 272 | struct hostent *hp; |
| 273 | int fd; |
| 274 | FILE *fp; |
| 275 | |
| 276 | memzero(&sin, sizeof(sin)); |
| 277 | sin.sin_family = AF_INET; |
| 278 | if ((hp = (struct hostent *) gethostbyname(host)) == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 279 | error_msg_and_die("cannot resolve %s\n", host); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 280 | memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length); |
| 281 | sin.sin_port = htons(port); |
| 282 | |
| 283 | /* |
| 284 | * Get the server onto a stdio stream. |
| 285 | */ |
| 286 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 287 | perror_msg_and_die("socket()"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 288 | if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 289 | perror_msg_and_die("connect(%s)", host); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 290 | if ((fp = fdopen(fd, "r+")) == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 291 | perror_msg_and_die("fdopen()"); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 292 | |
| 293 | return fp; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc) |
| 298 | { |
| 299 | char *s, *hdrval; |
| 300 | int c; |
| 301 | |
| 302 | *istrunc = 0; |
| 303 | |
| 304 | /* retrieve header line */ |
| 305 | if (fgets(buf, bufsiz, fp) == NULL) |
| 306 | return NULL; |
| 307 | |
| 308 | /* see if we are at the end of the headers */ |
| 309 | for (s = buf ; *s == '\r' ; ++s) |
| 310 | ; |
| 311 | if (s[0] == '\n') |
| 312 | return NULL; |
| 313 | |
| 314 | /* convert the header name to lower case */ |
| 315 | for (s = buf ; isalnum(*s) || *s == '-' ; ++s) |
| 316 | *s = tolower(*s); |
| 317 | |
| 318 | /* verify we are at the end of the header name */ |
| 319 | if (*s != ':') |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 320 | error_msg_and_die("bad header line: %s\n", buf); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 321 | |
| 322 | /* locate the start of the header value */ |
| 323 | for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s) |
| 324 | ; |
| 325 | hdrval = s; |
| 326 | |
| 327 | /* locate the end of header */ |
| 328 | while (*s != '\0' && *s != '\r' && *s != '\n') |
| 329 | ++s; |
| 330 | |
| 331 | /* end of header found */ |
| 332 | if (*s != '\0') { |
| 333 | *s = '\0'; |
| 334 | return hdrval; |
| 335 | } |
| 336 | |
Eric Andersen | 5d63884 | 2000-09-14 21:46:30 +0000 | [diff] [blame] | 337 | /* Rats! The buffer isn't big enough to hold the entire header value. */ |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 338 | while (c = getc(fp), c != EOF && c != '\n') |
| 339 | ; |
| 340 | *istrunc = 1; |
| 341 | return hdrval; |
| 342 | } |
| 343 | |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 344 | #ifdef BB_FEATURE_STATUSBAR |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 345 | /* Stuff below is from BSD rcp util.c, as added to openshh. |
| 346 | * Original copyright notice is retained at the end of this file. |
| 347 | * |
| 348 | */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 349 | |
| 350 | |
| 351 | int |
| 352 | getttywidth(void) |
| 353 | { |
| 354 | struct winsize winsize; |
| 355 | |
| 356 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) |
| 357 | return (winsize.ws_col ? winsize.ws_col : 80); |
| 358 | else |
| 359 | return (80); |
| 360 | } |
| 361 | |
| 362 | void |
| 363 | updateprogressmeter(int ignore) |
| 364 | { |
| 365 | int save_errno = errno; |
| 366 | |
| 367 | progressmeter(0); |
| 368 | errno = save_errno; |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | alarmtimer(int wait) |
| 373 | { |
| 374 | struct itimerval itv; |
| 375 | |
| 376 | itv.it_value.tv_sec = wait; |
| 377 | itv.it_value.tv_usec = 0; |
| 378 | itv.it_interval = itv.it_value; |
| 379 | setitimer(ITIMER_REAL, &itv, NULL); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | void |
| 384 | progressmeter(int flag) |
| 385 | { |
| 386 | static const char prefixes[] = " KMGTP"; |
| 387 | static struct timeval lastupdate; |
| 388 | static off_t lastsize; |
| 389 | struct timeval now, td, wait; |
| 390 | off_t cursize, abbrevsize; |
| 391 | double elapsed; |
| 392 | int ratio, barlength, i, remaining; |
| 393 | char buf[256]; |
| 394 | |
| 395 | if (flag == -1) { |
| 396 | (void) gettimeofday(&start, (struct timezone *) 0); |
| 397 | lastupdate = start; |
| 398 | lastsize = 0; |
| 399 | } |
| 400 | |
| 401 | (void) gettimeofday(&now, (struct timezone *) 0); |
| 402 | cursize = statbytes; |
| 403 | if (filesize != 0) { |
| 404 | ratio = 100.0 * cursize / filesize; |
| 405 | ratio = MAX(ratio, 0); |
| 406 | ratio = MIN(ratio, 100); |
| 407 | } else |
| 408 | ratio = 100; |
| 409 | |
| 410 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
| 411 | |
| 412 | barlength = getttywidth() - 51; |
| 413 | if (barlength > 0) { |
| 414 | i = barlength * ratio / 100; |
| 415 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 416 | "|%.*s%*s|", i, |
| 417 | "*****************************************************************************" |
| 418 | "*****************************************************************************", |
| 419 | barlength - i, ""); |
| 420 | } |
| 421 | i = 0; |
| 422 | abbrevsize = cursize; |
| 423 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { |
| 424 | i++; |
| 425 | abbrevsize >>= 10; |
| 426 | } |
| 427 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ", |
| 428 | (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : |
| 429 | 'B'); |
| 430 | |
| 431 | timersub(&now, &lastupdate, &wait); |
| 432 | if (cursize > lastsize) { |
| 433 | lastupdate = now; |
| 434 | lastsize = cursize; |
| 435 | if (wait.tv_sec >= STALLTIME) { |
| 436 | start.tv_sec += wait.tv_sec; |
| 437 | start.tv_usec += wait.tv_usec; |
| 438 | } |
| 439 | wait.tv_sec = 0; |
| 440 | } |
| 441 | timersub(&now, &start, &td); |
| 442 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); |
| 443 | |
| 444 | if (statbytes <= 0 || elapsed <= 0.0 || cursize > filesize) { |
| 445 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 446 | " --:-- ETA"); |
| 447 | } else if (wait.tv_sec >= STALLTIME) { |
| 448 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 449 | " - stalled -"); |
| 450 | } else { |
| 451 | remaining = (int) (filesize / (statbytes / elapsed) - elapsed); |
| 452 | i = remaining / 3600; |
| 453 | if (i) |
| 454 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 455 | "%2d:", i); |
| 456 | else |
| 457 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 458 | " "); |
| 459 | i = remaining % 3600; |
| 460 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 461 | "%02d:%02d ETA", i / 60, i % 60); |
| 462 | } |
Randolph Chung | da7b829 | 2000-12-07 03:55:35 +0000 | [diff] [blame] | 463 | write(fileno(stderr), buf, strlen(buf)); |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 464 | |
| 465 | if (flag == -1) { |
| 466 | struct sigaction sa; |
| 467 | sa.sa_handler = updateprogressmeter; |
| 468 | sigemptyset(&sa.sa_mask); |
| 469 | sa.sa_flags = SA_RESTART; |
| 470 | sigaction(SIGALRM, &sa, NULL); |
| 471 | alarmtimer(1); |
| 472 | } else if (flag == 1) { |
| 473 | alarmtimer(0); |
| 474 | statbytes = 0; |
| 475 | } |
| 476 | } |
| 477 | #endif |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 478 | |
| 479 | /* Original copyright notice which applies to the BB_FEATURE_STATUSBAR stuff, |
| 480 | * much of which was blatently stolen from openssh. */ |
| 481 | |
| 482 | /*- |
| 483 | * Copyright (c) 1992, 1993 |
| 484 | * The Regents of the University of California. All rights reserved. |
| 485 | * |
| 486 | * Redistribution and use in source and binary forms, with or without |
| 487 | * modification, are permitted provided that the following conditions |
| 488 | * are met: |
| 489 | * 1. Redistributions of source code must retain the above copyright |
| 490 | * notice, this list of conditions and the following disclaimer. |
| 491 | * 2. Redistributions in binary form must reproduce the above copyright |
| 492 | * notice, this list of conditions and the following disclaimer in the |
| 493 | * documentation and/or other materials provided with the distribution. |
| 494 | * |
| 495 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 496 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 497 | * |
| 498 | * 4. Neither the name of the University nor the names of its contributors |
| 499 | * may be used to endorse or promote products derived from this software |
| 500 | * without specific prior written permission. |
| 501 | * |
| 502 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 503 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 504 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 505 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 506 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 507 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 508 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 509 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 510 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 511 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 512 | * SUCH DAMAGE. |
| 513 | * |
Matt Kraai | a9711a5 | 2001-01-03 16:15:15 +0000 | [diff] [blame^] | 514 | * $Id: wget.c,v 1.15 2001/01/03 16:15:15 kraai Exp $ |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 515 | */ |
| 516 | |
| 517 | |
| 518 | |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 519 | /* |
| 520 | Local Variables: |
| 521 | c-file-style: "linux" |
| 522 | c-basic-offset: 4 |
| 523 | tab-width: 4 |
| 524 | End: |
| 525 | */ |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 526 | |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 527 | |
| 528 | |