fetch.c (125965) | fetch.c (125976) |
---|---|
1/*- 2 * Copyright (c) 2000 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 13 unchanged lines hidden (view full) --- 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2000 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 13 unchanged lines hidden (view full) --- 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> |
30__FBSDID("$FreeBSD: head/usr.bin/fetch/fetch.c 125965 2004-02-18 15:12:00Z des $"); | 30__FBSDID("$FreeBSD: head/usr.bin/fetch/fetch.c 125976 2004-02-18 21:37:36Z des $"); |
31 32#include <sys/param.h> 33#include <sys/socket.h> 34#include <sys/stat.h> 35#include <sys/time.h> 36 37#include <ctype.h> 38#include <err.h> 39#include <errno.h> 40#include <signal.h> | 31 32#include <sys/param.h> 33#include <sys/socket.h> 34#include <sys/stat.h> 35#include <sys/time.h> 36 37#include <ctype.h> 38#include <err.h> 39#include <errno.h> 40#include <signal.h> |
41#include <stdint.h> |
|
41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <sysexits.h> 45#include <termios.h> 46#include <unistd.h> 47 48#include <fetch.h> --- 34 unchanged lines hidden (view full) --- 83int family = PF_UNSPEC; /* -[46]: address family to use */ 84 85int sigalrm; /* SIGALRM received */ 86int siginfo; /* SIGINFO received */ 87int sigint; /* SIGINT received */ 88 89long ftp_timeout; /* default timeout for FTP transfers */ 90long http_timeout; /* default timeout for HTTP transfers */ | 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <sysexits.h> 46#include <termios.h> 47#include <unistd.h> 48 49#include <fetch.h> --- 34 unchanged lines hidden (view full) --- 84int family = PF_UNSPEC; /* -[46]: address family to use */ 85 86int sigalrm; /* SIGALRM received */ 87int siginfo; /* SIGINFO received */ 88int sigint; /* SIGINT received */ 89 90long ftp_timeout; /* default timeout for FTP transfers */ 91long http_timeout; /* default timeout for HTTP transfers */ |
91u_char *buf; /* transfer buffer */ | 92char *buf; /* transfer buffer */ |
92 93 94/* 95 * Signal handler 96 */ 97static void 98sig_handler(int sig) 99{ --- 21 unchanged lines hidden (view full) --- 121 122/* 123 * Compute and display ETA 124 */ 125static const char * 126stat_eta(struct xferstat *xs) 127{ 128 static char str[16]; | 93 94 95/* 96 * Signal handler 97 */ 98static void 99sig_handler(int sig) 100{ --- 21 unchanged lines hidden (view full) --- 122 123/* 124 * Compute and display ETA 125 */ 126static const char * 127stat_eta(struct xferstat *xs) 128{ 129 static char str[16]; |
129 long elapsed, received, expected, eta; | 130 long elapsed, eta; 131 off_t received, expected; |
130 131 elapsed = xs->last.tv_sec - xs->start.tv_sec; 132 received = xs->rcvd - xs->offset; 133 expected = xs->size - xs->rcvd; 134 eta = (long)((double)elapsed * expected / received); 135 if (eta > 3600) 136 snprintf(str, sizeof str, "%02ldh%02ldm", 137 eta / 3600, (eta % 3600) / 60); --- 12 unchanged lines hidden (view full) --- 150{ 151 static char str[16]; 152 const char *prefix = prefixes; 153 154 while (bytes > 9999 && prefix[1] != '\0') { 155 bytes /= 1024; 156 prefix++; 157 } | 132 133 elapsed = xs->last.tv_sec - xs->start.tv_sec; 134 received = xs->rcvd - xs->offset; 135 expected = xs->size - xs->rcvd; 136 eta = (long)((double)elapsed * expected / received); 137 if (eta > 3600) 138 snprintf(str, sizeof str, "%02ldh%02ldm", 139 eta / 3600, (eta % 3600) / 60); --- 12 unchanged lines hidden (view full) --- 152{ 153 static char str[16]; 154 const char *prefix = prefixes; 155 156 while (bytes > 9999 && prefix[1] != '\0') { 157 bytes /= 1024; 158 prefix++; 159 } |
158 snprintf(str, sizeof str, "%4d %cB", bytes, *prefix); | 160 snprintf(str, sizeof str, "%4ju %cB", (uintmax_t)bytes, *prefix); |
159 return (str); 160} 161 162/* 163 * Compute and display transfer rate 164 */ 165static const char * 166stat_bps(struct xferstat *xs) --- 144 unchanged lines hidden (view full) --- 311 struct xferstat xs; 312 FILE *f, *of; 313 size_t size, wr; 314 off_t count; 315 char flags[8]; 316 const char *slash; 317 char *tmppath; 318 int r; | 161 return (str); 162} 163 164/* 165 * Compute and display transfer rate 166 */ 167static const char * 168stat_bps(struct xferstat *xs) --- 144 unchanged lines hidden (view full) --- 313 struct xferstat xs; 314 FILE *f, *of; 315 size_t size, wr; 316 off_t count; 317 char flags[8]; 318 const char *slash; 319 char *tmppath; 320 int r; |
319 u_int timeout; 320 u_char *ptr; | 321 unsigned timeout; 322 char *ptr; |
321 322 f = of = NULL; 323 tmppath = NULL; 324 325 timeout = 0; 326 *flags = 0; 327 count = 0; 328 --- 63 unchanged lines hidden (view full) --- 392 goto signal; 393 if (r == -1) { 394 warnx("%s", fetchLastErrString); 395 goto failure; 396 } 397 if (us.size == -1) 398 printf("Unknown\n"); 399 else | 323 324 f = of = NULL; 325 tmppath = NULL; 326 327 timeout = 0; 328 *flags = 0; 329 count = 0; 330 --- 63 unchanged lines hidden (view full) --- 394 goto signal; 395 if (r == -1) { 396 warnx("%s", fetchLastErrString); 397 goto failure; 398 } 399 if (us.size == -1) 400 printf("Unknown\n"); 401 else |
400 printf("%lld\n", (long long)us.size); | 402 printf("%jd\n", (intmax_t)us.size); |
401 goto success; 402 } 403 404 /* 405 * If the -r flag was specified, we have to compare the local 406 * and remote files, so we should really do a fetchStat() 407 * first, but I know of at least one HTTP server that only 408 * sends the content size in response to GET requests, and --- 29 unchanged lines hidden (view full) --- 438 goto signal; 439 440 /* check that size is as expected */ 441 if (S_size) { 442 if (us.size == -1) { 443 warnx("%s: size unknown", URL); 444 goto failure; 445 } else if (us.size != S_size) { | 403 goto success; 404 } 405 406 /* 407 * If the -r flag was specified, we have to compare the local 408 * and remote files, so we should really do a fetchStat() 409 * first, but I know of at least one HTTP server that only 410 * sends the content size in response to GET requests, and --- 29 unchanged lines hidden (view full) --- 440 goto signal; 441 442 /* check that size is as expected */ 443 if (S_size) { 444 if (us.size == -1) { 445 warnx("%s: size unknown", URL); 446 goto failure; 447 } else if (us.size != S_size) { |
446 warnx("%s: size mismatch: expected %lld, actual %lld", 447 URL, (long long)S_size, (long long)us.size); | 448 warnx("%s: size mismatch: expected %jd, actual %jd", 449 URL, (intmax_t)S_size, (intmax_t)us.size); |
448 goto failure; 449 } 450 } 451 452 /* symlink instead of copy */ 453 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) { 454 if (symlink(url->doc, path) == -1) { 455 warn("%s: symlink()", path); 456 goto failure; 457 } 458 goto success; 459 } 460 461 if (us.size == -1 && !o_stdout && v_level > 0) 462 warnx("%s: size of remote file is not known", URL); 463 if (v_level > 1) { 464 if (sb.st_size != -1) | 450 goto failure; 451 } 452 } 453 454 /* symlink instead of copy */ 455 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) { 456 if (symlink(url->doc, path) == -1) { 457 warn("%s: symlink()", path); 458 goto failure; 459 } 460 goto success; 461 } 462 463 if (us.size == -1 && !o_stdout && v_level > 0) 464 warnx("%s: size of remote file is not known", URL); 465 if (v_level > 1) { 466 if (sb.st_size != -1) |
465 fprintf(stderr, "local size / mtime: %lld / %ld\n", 466 (long long)sb.st_size, (long)sb.st_mtime); | 467 fprintf(stderr, "local size / mtime: %jd / %ld\n", 468 (intmax_t)sb.st_size, (long)sb.st_mtime); |
467 if (us.size != -1) | 469 if (us.size != -1) |
468 fprintf(stderr, "remote size / mtime: %lld / %ld\n", 469 (long long)us.size, (long)us.mtime); | 470 fprintf(stderr, "remote size / mtime: %jd / %ld\n", 471 (intmax_t)us.size, (long)us.mtime); |
470 } 471 472 /* open output file */ 473 if (o_stdout) { 474 /* output to stdout */ 475 of = stdout; 476 } else if (r_flag && sb.st_size != -1) { 477 /* resume mode, local file exists */ --- 7 unchanged lines hidden (view full) --- 485 goto failure_keep; 486 } 487 } else { 488 if (us.size == sb.st_size) 489 /* nothing to do */ 490 goto success; 491 if (sb.st_size > us.size) { 492 /* local file too long! */ | 472 } 473 474 /* open output file */ 475 if (o_stdout) { 476 /* output to stdout */ 477 of = stdout; 478 } else if (r_flag && sb.st_size != -1) { 479 /* resume mode, local file exists */ --- 7 unchanged lines hidden (view full) --- 487 goto failure_keep; 488 } 489 } else { 490 if (us.size == sb.st_size) 491 /* nothing to do */ 492 goto success; 493 if (sb.st_size > us.size) { 494 /* local file too long! */ |
493 warnx("%s: local file (%lld bytes) is longer " 494 "than remote file (%lld bytes)", path, 495 (long long)sb.st_size, (long long)us.size); | 495 warnx("%s: local file (%jd bytes) is longer " 496 "than remote file (%jd bytes)", path, 497 (intmax_t)sb.st_size, (intmax_t)us.size); |
496 goto failure; 497 } 498 /* we got it, open local file */ 499 if ((of = fopen(path, "a")) == NULL) { 500 warn("%s: fopen()", path); 501 goto failure; 502 } 503 /* check that it didn't move under our feet */ --- 138 unchanged lines hidden (view full) --- 642 if (ferror(of)) 643 warn("%s", path); 644 if (ferror(f) || ferror(of)) 645 goto failure; 646 } 647 648 /* did the transfer complete normally? */ 649 if (us.size != -1 && count < us.size) { | 498 goto failure; 499 } 500 /* we got it, open local file */ 501 if ((of = fopen(path, "a")) == NULL) { 502 warn("%s: fopen()", path); 503 goto failure; 504 } 505 /* check that it didn't move under our feet */ --- 138 unchanged lines hidden (view full) --- 644 if (ferror(of)) 645 warn("%s", path); 646 if (ferror(f) || ferror(of)) 647 goto failure; 648 } 649 650 /* did the transfer complete normally? */ 651 if (us.size != -1 && count < us.size) { |
650 warnx("%s appears to be truncated: %lld/%lld bytes", 651 path, (long long)count, (long long)us.size); | 652 warnx("%s appears to be truncated: %jd/%jd bytes", 653 path, (intmax_t)count, (intmax_t)us.size); |
652 goto failure_keep; 653 } 654 655 /* 656 * If the transfer timed out and we didn't know how much to 657 * expect, assume the worst (i.e. we didn't get all of it) 658 */ 659 if (sigalrm && us.size == -1) { --- 308 unchanged lines hidden --- | 654 goto failure_keep; 655 } 656 657 /* 658 * If the transfer timed out and we didn't know how much to 659 * expect, assume the worst (i.e. we didn't get all of it) 660 */ 661 if (sigalrm && us.size == -1) { --- 308 unchanged lines hidden --- |