Deleted Added
sdiff udiff text old ( 125965 ) new ( 125976 )
full compact
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 $");
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 <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 */
91u_char *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];
129 long elapsed, received, expected, eta;
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 }
158 snprintf(str, sizeof str, "%4d %cB", 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;
319 u_int timeout;
320 u_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
400 printf("%lld\n", (long long)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) {
446 warnx("%s: size mismatch: expected %lld, actual %lld",
447 URL, (long long)S_size, (long long)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)
465 fprintf(stderr, "local size / mtime: %lld / %ld\n",
466 (long long)sb.st_size, (long)sb.st_mtime);
467 if (us.size != -1)
468 fprintf(stderr, "remote size / mtime: %lld / %ld\n",
469 (long long)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! */
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);
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) {
650 warnx("%s appears to be truncated: %lld/%lld bytes",
651 path, (long long)count, (long long)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 ---