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 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>
41#include <stdint.h>
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 */
92char *buf; /* transfer buffer */
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];
130 long elapsed, eta;
131 off_t received, expected;
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 }
160 snprintf(str, sizeof str, "%4ju %cB", (uintmax_t)bytes, *prefix);
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;
321 unsigned timeout;
322 char *ptr;
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
402 printf("%jd\n", (intmax_t)us.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) {
448 warnx("%s: size mismatch: expected %jd, actual %jd",
449 URL, (intmax_t)S_size, (intmax_t)us.size);
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)
467 fprintf(stderr, "local size / mtime: %jd / %ld\n",
468 (intmax_t)sb.st_size, (long)sb.st_mtime);
469 if (us.size != -1)
470 fprintf(stderr, "remote size / mtime: %jd / %ld\n",
471 (intmax_t)us.size, (long)us.mtime);
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! */
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);
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) {
652 warnx("%s appears to be truncated: %jd/%jd bytes",
653 path, (intmax_t)count, (intmax_t)us.size);
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 ---