2 * Copyright (c) 2000-2011 Dag-Erling Smørgrav
3 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/socket.h>
52 #define MINBUFSIZE 4096
56 int A_flag; /* -A: do not follow 302 redirects */
57 int a_flag; /* -a: auto retry */
58 off_t B_size; /* -B: buffer size */
59 int b_flag; /*! -b: workaround TCP bug */
60 char *c_dirname; /* -c: remote directory */
61 int d_flag; /* -d: direct connection */
62 int F_flag; /* -F: restart without checking mtime */
63 char *f_filename; /* -f: file to fetch */
64 char *h_hostname; /* -h: host to fetch from */
65 int i_flag; /* -i: specify input file for mtime comparison */
66 char *i_filename; /* name of input file */
67 int l_flag; /* -l: link rather than copy file: URLs */
68 int m_flag; /* -[Mm]: mirror mode */
69 char *N_filename; /* -N: netrc file name */
70 int n_flag; /* -n: do not preserve modification time */
71 int o_flag; /* -o: specify output file */
72 int o_directory; /* output file is a directory */
73 char *o_filename; /* name of output file */
74 int o_stdout; /* output file is stdout */
75 int once_flag; /* -1: stop at first successful file */
76 int p_flag; /* -[Pp]: use passive FTP */
77 int R_flag; /* -R: don't delete partially transferred files */
78 int r_flag; /* -r: restart previously interrupted transfer */
79 off_t S_size; /* -S: require size to match */
80 int s_flag; /* -s: show size, don't fetch */
81 long T_secs; /* -T: transfer timeout in seconds */
82 int t_flag; /*! -t: workaround TCP bug */
83 int U_flag; /* -U: do not use high ports */
84 int v_level = 1; /* -v: verbosity level */
85 int v_tty; /* stdout is a tty */
86 pid_t pgrp; /* our process group */
87 long w_secs; /* -w: retry delay */
88 int family = PF_UNSPEC; /* -[46]: address family to use */
90 int sigalrm; /* SIGALRM received */
91 int siginfo; /* SIGINFO received */
92 int sigint; /* SIGINT received */
94 long ftp_timeout = TIMEOUT; /* default timeout for FTP transfers */
95 long http_timeout = TIMEOUT; /* default timeout for HTTP transfers */
96 char *buf; /* transfer buffer */
101 OPTION_NO_FTP_PASSIVE_MODE,
103 OPTION_HTTP_USER_AGENT,
105 OPTION_SSL_ALLOW_SSL2,
106 OPTION_SSL_CA_CERT_FILE,
107 OPTION_SSL_CA_CERT_PATH,
108 OPTION_SSL_CLIENT_CERT_FILE,
109 OPTION_SSL_CLIENT_KEY_FILE,
113 OPTION_SSL_NO_VERIFY_HOSTNAME,
114 OPTION_SSL_NO_VERIFY_PEER
118 static struct option longopts[] =
120 /* mapping to single character argument */
121 { "one-file", no_argument, NULL, '1' },
122 { "ipv4-only", no_argument, NULL, '4' },
123 { "ipv6-only", no_argument, NULL, '6' },
124 { "no-redirect", no_argument, NULL, 'A' },
125 { "retry", no_argument, NULL, 'a' },
126 { "buffer-size", required_argument, NULL, 'B' },
127 /* -c not mapped, since it's deprecated */
128 { "direct", no_argument, NULL, 'd' },
129 { "force-restart", no_argument, NULL, 'F' },
130 /* -f not mapped, since it's deprecated */
131 /* -h not mapped, since it's deprecated */
132 { "if-modified-since", required_argument, NULL, 'i' },
133 { "symlink", no_argument, NULL, 'l' },
134 /* -M not mapped since it's the same as -m */
135 { "mirror", no_argument, NULL, 'm' },
136 { "netrc", required_argument, NULL, 'N' },
137 { "no-mtime", no_argument, NULL, 'n' },
138 { "output", required_argument, NULL, 'o' },
139 /* -P not mapped since it's the same as -p */
140 { "passive", no_argument, NULL, 'p' },
141 { "quiet", no_argument, NULL, 'q' },
142 { "keep-output", no_argument, NULL, 'R' },
143 { "restart", no_argument, NULL, 'r' },
144 { "require-size", required_argument, NULL, 'S' },
145 { "print-size", no_argument, NULL, 's' },
146 { "timeout", required_argument, NULL, 'T' },
147 { "passive-portrange-default", no_argument, NULL, 'T' },
148 { "verbose", no_argument, NULL, 'v' },
149 { "retry-delay", required_argument, NULL, 'w' },
151 /* options without a single character equivalent */
152 { "bind-address", required_argument, NULL, OPTION_BIND_ADDRESS },
153 { "no-passive", no_argument, NULL, OPTION_NO_FTP_PASSIVE_MODE },
154 { "referer", required_argument, NULL, OPTION_HTTP_REFERER },
155 { "user-agent", required_argument, NULL, OPTION_HTTP_USER_AGENT },
156 { "no-proxy", required_argument, NULL, OPTION_NO_PROXY },
157 { "allow-sslv2", no_argument, NULL, OPTION_SSL_ALLOW_SSL2 },
158 { "ca-cert", required_argument, NULL, OPTION_SSL_CA_CERT_FILE },
159 { "ca-path", required_argument, NULL, OPTION_SSL_CA_CERT_PATH },
160 { "cert", required_argument, NULL, OPTION_SSL_CLIENT_CERT_FILE },
161 { "key", required_argument, NULL, OPTION_SSL_CLIENT_KEY_FILE },
162 { "crl", required_argument, NULL, OPTION_SSL_CRL_FILE },
163 { "no-sslv3", no_argument, NULL, OPTION_SSL_NO_SSL3 },
164 { "no-tlsv1", no_argument, NULL, OPTION_SSL_NO_TLS1 },
165 { "no-verify-hostname", no_argument, NULL, OPTION_SSL_NO_VERIFY_HOSTNAME },
166 { "no-verify-peer", no_argument, NULL, OPTION_SSL_NO_VERIFY_PEER },
192 struct timeval start; /* start of transfer */
193 struct timeval last; /* time of last update */
194 struct timeval last2; /* time of previous last update */
195 off_t size; /* size of file per HTTP hdr */
196 off_t offset; /* starting offset in file */
197 off_t rcvd; /* bytes already received */
198 off_t lastrcvd; /* bytes received since last update */
202 * Compute and display ETA
205 stat_eta(struct xferstat *xs)
209 off_t received, expected;
211 elapsed = xs->last.tv_sec - xs->start.tv_sec;
212 received = xs->rcvd - xs->offset;
213 expected = xs->size - xs->rcvd;
214 eta = (long)((double)elapsed * expected / received);
216 snprintf(str, sizeof str, "%02ldh%02ldm",
217 eta / 3600, (eta % 3600) / 60);
219 snprintf(str, sizeof str, "%02ldm%02lds",
222 snprintf(str, sizeof str, "%02ldm%02lds",
223 elapsed / 60, elapsed % 60);
228 * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
230 static const char *prefixes = " kMGTP";
232 stat_bytes(off_t bytes)
235 const char *prefix = prefixes;
237 while (bytes > 9999 && prefix[1] != '\0') {
241 snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix);
246 * Compute and display transfer rate
249 stat_bps(struct xferstat *xs)
254 delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
255 - (xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6));
258 snprintf(str, sizeof str, "?? Bps");
260 bps = (xs->rcvd - xs->lastrcvd) / delta;
261 snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
267 * Update the stats display
270 stat_display(struct xferstat *xs, int force)
275 /* check if we're the foreground process */
276 if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
277 (pid_t)ctty_pgrp != pgrp)
280 gettimeofday(&now, NULL);
281 if (!force && now.tv_sec <= xs->last.tv_sec)
283 xs->last2 = xs->last;
286 fprintf(stderr, "\r%-46.46s", xs->name);
288 setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd));
289 fprintf(stderr, " %s", stat_bytes(xs->rcvd));
291 setproctitle("%s [%d%% of %s]", xs->name,
292 (int)((100.0 * xs->rcvd) / xs->size),
293 stat_bytes(xs->size));
294 fprintf(stderr, "%3d%% of %s",
295 (int)((100.0 * xs->rcvd) / xs->size),
296 stat_bytes(xs->size));
299 xs->lastrcvd = xs->offset;
300 xs->last2 = xs->start;
302 fprintf(stderr, " %s", stat_bps(xs));
303 if ((xs->size > 0 && xs->rcvd > 0 &&
304 xs->last.tv_sec >= xs->start.tv_sec + 3) ||
306 fprintf(stderr, " %s", stat_eta(xs));
307 xs->lastrcvd = xs->rcvd;
311 * Initialize the transfer statistics
314 stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
316 snprintf(xs->name, sizeof xs->name, "%s", name);
317 gettimeofday(&xs->start, NULL);
318 xs->last.tv_sec = xs->last.tv_usec = 0;
322 xs->lastrcvd = offset;
323 if (v_tty && v_level > 0)
325 else if (v_level > 0)
326 fprintf(stderr, "%-46s", xs->name);
330 * Update the transfer statistics
333 stat_update(struct xferstat *xs, off_t rcvd)
336 if (v_tty && v_level > 0)
341 * Finalize the transfer statistics
344 stat_end(struct xferstat *xs)
346 gettimeofday(&xs->last, NULL);
347 if (v_tty && v_level > 0) {
350 } else if (v_level > 0) {
351 fprintf(stderr, " %s %s\n",
352 stat_bytes(xs->size), stat_bps(xs));
357 * Ask the user for authentication details
360 query_auth(struct url *URL)
363 tcflag_t saved_flags;
366 fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
367 URL->scheme, URL->host, URL->port);
369 fprintf(stderr, "Login: ");
370 if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
372 for (i = strlen(URL->user); i >= 0; --i)
373 if (URL->user[i] == '\r' || URL->user[i] == '\n')
376 fprintf(stderr, "Password: ");
377 if (tcgetattr(STDIN_FILENO, &tios) == 0) {
378 saved_flags = tios.c_lflag;
379 tios.c_lflag &= ~ECHO;
380 tios.c_lflag |= ECHONL|ICANON;
381 tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
382 nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
383 tios.c_lflag = saved_flags;
384 tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
386 nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
390 for (i = strlen(URL->pwd); i >= 0; --i)
391 if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
401 fetch(char *URL, const char *path)
408 size_t size, readcnt, wr;
424 /* set verbosity level */
436 if ((url = fetchParseURL(URL)) == NULL) {
437 warnx("%s: parse error", URL);
441 /* if no scheme was specified, take a guess */
444 strcpy(url->scheme, SCHEME_FILE);
445 else if (strncasecmp(url->host, "ftp.", 4) == 0)
446 strcpy(url->scheme, SCHEME_FTP);
447 else if (strncasecmp(url->host, "www.", 4) == 0)
448 strcpy(url->scheme, SCHEME_HTTP);
461 /* FTP specific flags */
462 if (strcmp(url->scheme, SCHEME_FTP) == 0) {
469 timeout = T_secs ? T_secs : ftp_timeout;
472 /* HTTP specific flags */
473 if (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
474 strcmp(url->scheme, SCHEME_HTTPS) == 0) {
479 timeout = T_secs ? T_secs : http_timeout;
481 if (stat(i_filename, &sb)) {
482 warn("%s: stat()", i_filename);
485 url->ims_time = sb.st_mtime;
490 /* set the protocol timeout. */
491 fetchTimeout = timeout;
493 /* just print size */
497 r = fetchStat(url, &us, flags);
500 if (sigalrm || sigint)
503 warnx("%s", fetchLastErrString);
509 printf("%jd\n", (intmax_t)us.size);
514 * If the -r flag was specified, we have to compare the local
515 * and remote files, so we should really do a fetchStat()
516 * first, but I know of at least one HTTP server that only
517 * sends the content size in response to GET requests, and
518 * leaves it out of replies to HEAD requests. Also, in the
519 * (frequent) case that the local and remote files match but
520 * the local file is truncated, we have sufficient information
521 * before the compare to issue a correct request. Therefore,
522 * we always issue a GET request as if we were sure the local
523 * file was a truncated copy of the remote file; we can drop
524 * the connection later if we change our minds.
529 if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
530 url->offset = sb.st_size;
531 } else if (r == -1 || !S_ISREG(sb.st_mode)) {
533 * Whatever value sb.st_size has now is either
534 * wrong (if stat(2) failed) or irrelevant (if the
535 * path does not refer to a regular file)
539 if (r == -1 && errno != ENOENT) {
540 warnx("%s: stat()", path);
545 /* start the transfer */
548 f = fetchXGet(url, &us, flags);
551 if (sigalrm || sigint)
554 warnx("%s: %s", URL, fetchLastErrString);
555 if (i_flag && strcmp(url->scheme, SCHEME_HTTP) == 0
556 && fetchLastErrCode == FETCH_OK
557 && strcmp(fetchLastErrString, "Not Modified") == 0) {
558 /* HTTP Not Modified Response, return OK. */
567 /* check that size is as expected */
570 warnx("%s: size unknown", URL);
571 } else if (us.size != S_size) {
572 warnx("%s: size mismatch: expected %jd, actual %jd",
573 URL, (intmax_t)S_size, (intmax_t)us.size);
578 /* symlink instead of copy */
579 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
580 if (symlink(url->doc, path) == -1) {
581 warn("%s: symlink()", path);
587 if (us.size == -1 && !o_stdout && v_level > 0)
588 warnx("%s: size of remote file is not known", URL);
590 if (sb.st_size != -1)
591 fprintf(stderr, "local size / mtime: %jd / %ld\n",
592 (intmax_t)sb.st_size, (long)sb.st_mtime);
594 fprintf(stderr, "remote size / mtime: %jd / %ld\n",
595 (intmax_t)us.size, (long)us.mtime);
598 /* open output file */
600 /* output to stdout */
602 } else if (r_flag && sb.st_size != -1) {
603 /* resume mode, local file exists */
604 if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
605 /* no match! have to refetch */
607 /* if precious, warn the user and give up */
609 warnx("%s: local modification time "
610 "does not match remote", path);
613 } else if (url->offset > sb.st_size) {
614 /* gap between what we asked for and what we got */
615 warnx("%s: gap in resume mode", URL);
618 /* picked up again later */
619 } else if (us.size != -1) {
620 if (us.size == sb.st_size)
623 if (sb.st_size > us.size) {
624 /* local file too long! */
625 warnx("%s: local file (%jd bytes) is longer "
626 "than remote file (%jd bytes)", path,
627 (intmax_t)sb.st_size, (intmax_t)us.size);
630 /* we got it, open local file */
631 if ((of = fopen(path, "r+")) == NULL) {
632 warn("%s: fopen()", path);
635 /* check that it didn't move under our feet */
636 if (fstat(fileno(of), &nsb) == -1) {
638 warn("%s: fstat()", path);
641 if (nsb.st_dev != sb.st_dev ||
642 nsb.st_ino != nsb.st_ino ||
643 nsb.st_size != sb.st_size) {
644 warnx("%s: file has changed", URL);
648 /* picked up again later */
651 /* seek to where we left off */
652 if (of != NULL && fseeko(of, url->offset, SEEK_SET) != 0) {
653 warn("%s: fseeko()", path);
656 /* picked up again later */
658 } else if (m_flag && sb.st_size != -1) {
659 /* mirror mode, local file exists */
660 if (sb.st_size == us.size && sb.st_mtime == us.mtime)
666 * We don't yet have an output file; either this is a
667 * vanilla run with no special flags, or the local and
668 * remote files didn't match.
671 if (url->offset > 0) {
673 * We tried to restart a transfer, but for
674 * some reason gave up - so we have to restart
675 * from scratch if we want the whole file
678 if ((f = fetchXGet(url, &us, flags)) == NULL) {
679 warnx("%s: %s", URL, fetchLastErrString);
686 /* construct a temp file name */
687 if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
688 if ((slash = strrchr(path, '/')) == NULL)
692 asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
693 (int)(slash - path), path, slash);
694 if (tmppath != NULL) {
695 if (mkstemps(tmppath, strlen(slash) + 1) == -1) {
696 warn("%s: mkstemps()", path);
699 of = fopen(tmppath, "w");
700 chown(tmppath, sb.st_uid, sb.st_gid);
701 chmod(tmppath, sb.st_mode & ALLPERMS);
705 of = fopen(path, "w");
707 warn("%s: open()", path);
713 /* start the counter */
714 stat_start(&xs, path, us.size, count);
716 sigalrm = siginfo = sigint = 0;
718 /* suck in the data */
719 signal(SIGINFO, sig_handler);
721 if (us.size != -1 && us.size - count < B_size &&
722 us.size - count >= 0)
723 size = us.size - count;
734 if ((readcnt = fread(buf, 1, size, f)) < size) {
735 if (ferror(f) && errno == EINTR && !sigint)
737 else if (readcnt == 0)
741 stat_update(&xs, count += readcnt);
742 for (ptr = buf; readcnt > 0; ptr += wr, readcnt -= wr)
743 if ((wr = fwrite(ptr, 1, readcnt, of)) < readcnt) {
744 if (ferror(of) && errno == EINTR && !sigint)
753 sigalrm = ferror(f) && errno == ETIMEDOUT;
754 signal(SIGINFO, SIG_DFL);
759 * If the transfer timed out or was interrupted, we still want to
760 * set the mtime in case the file is not removed (-r or -R) and
761 * the user later restarts the transfer.
764 /* set mtime of local file */
765 if (!n_flag && us.mtime && !o_stdout && of != NULL &&
766 (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
767 struct timeval tv[2];
770 tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
771 tv[1].tv_sec = (long)us.mtime;
772 tv[0].tv_usec = tv[1].tv_usec = 0;
773 if (utimes(tmppath ? tmppath : path, tv))
774 warn("%s: utimes()", tmppath ? tmppath : path);
777 /* timed out or interrupted? */
779 warnx("transfer timed out");
781 warnx("transfer interrupted");
785 /* timeout / interrupt before connection completley established? */
790 /* check the status of our files */
795 if (ferror(f) || ferror(of))
799 /* did the transfer complete normally? */
800 if (us.size != -1 && count < us.size) {
801 warnx("%s appears to be truncated: %jd/%jd bytes",
802 path, (intmax_t)count, (intmax_t)us.size);
807 * If the transfer timed out and we didn't know how much to
808 * expect, assume the worst (i.e. we didn't get all of it)
810 if (sigalrm && us.size == -1) {
811 warnx("%s may be truncated", path);
817 if (tmppath != NULL && rename(tmppath, path) == -1) {
818 warn("%s: rename()", path);
823 if (of && of != stdout && !R_flag && !r_flag)
824 if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
825 unlink(tmppath ? tmppath : path);
826 if (R_flag && tmppath != NULL && sb.st_size == -1)
827 rename(tmppath, path); /* ignore errors here */
834 if (of && of != stdout)
846 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
847 "usage: fetch [-146AadFlMmnPpqRrsUv] [--allow-sslv2] [-B bytes]",
848 " [--bind-address=host] [--ca-cert=file] [--ca-path=dir] [--cert=file]",
849 " [--crl=file] [-i file] [--key=file] [-N file] [--no-passive]",
850 " [--no-proxy=list] [--no-sslv3] [--no-tlsv1] [--no-verify-hostname]",
851 " [--no-verify-peer] [-o file] [--referer=URL] [-S bytes] [-T seconds]",
852 " [--user-agent=agent-string] [-w seconds] URL ...",
853 " fetch [-146AadFlMmnPpqRrsUv] [--allow-sslv2] [-B bytes]",
854 " [--bind-address=host] [--ca-cert=file] [--ca-path=dir] [--cert=file]",
855 " [--crl=file] [-i file] [--key=file] [-N file] [--no-passive]",
856 " [--no-proxy=list] [--no-sslv3] [--no-tlsv1] [--no-verify-hostname]",
857 " [--no-verify-peer] [-o file] [--referer=URL] [-S bytes] [-T seconds]",
858 " [--user-agent=agent-string] [-w seconds] -h host -f file [-c dir]");
866 main(int argc, char *argv[])
875 while ((c = getopt_long(argc, argv,
876 "146AaB:bc:dFf:Hh:i:lMmN:nPpo:qRrS:sT:tUvw:",
877 longopts, NULL)) != -1)
895 B_size = (off_t)strtol(optarg, &end, 10);
896 if (*optarg == '\0' || *end != '\0')
897 errx(1, "invalid buffer size (%s)", optarg);
900 warnx("warning: the -b option is deprecated");
916 warnx("the -H option is now implicit, "
917 "use -U to disable");
936 errx(1, "the -m and -r flags "
937 "are mutually exclusive");
958 errx(1, "the -m and -r flags "
959 "are mutually exclusive");
963 S_size = (off_t)strtol(optarg, &end, 10);
964 if (*optarg == '\0' || *end != '\0')
965 errx(1, "invalid size (%s)", optarg);
971 T_secs = strtol(optarg, &end, 10);
972 if (*optarg == '\0' || *end != '\0')
973 errx(1, "invalid timeout (%s)", optarg);
977 warnx("warning: the -t option is deprecated");
987 w_secs = strtol(optarg, &end, 10);
988 if (*optarg == '\0' || *end != '\0')
989 errx(1, "invalid delay (%s)", optarg);
991 case OPTION_BIND_ADDRESS:
992 setenv("FETCH_BIND_ADDRESS", optarg, 1);
994 case OPTION_NO_FTP_PASSIVE_MODE:
995 setenv("FTP_PASSIVE_MODE", "no", 1);
997 case OPTION_HTTP_REFERER:
998 setenv("HTTP_REFERER", optarg, 1);
1000 case OPTION_HTTP_USER_AGENT:
1001 setenv("HTTP_USER_AGENT", optarg, 1);
1003 case OPTION_NO_PROXY:
1004 setenv("NO_PROXY", optarg, 1);
1006 case OPTION_SSL_ALLOW_SSL2:
1007 setenv("SSL_ALLOW_SSL2", "", 1);
1009 case OPTION_SSL_CA_CERT_FILE:
1010 setenv("SSL_CA_CERT_FILE", optarg, 1);
1012 case OPTION_SSL_CA_CERT_PATH:
1013 setenv("SSL_CA_CERT_PATH", optarg, 1);
1015 case OPTION_SSL_CLIENT_CERT_FILE:
1016 setenv("SSL_CLIENT_CERT_FILE", optarg, 1);
1018 case OPTION_SSL_CLIENT_KEY_FILE:
1019 setenv("SSL_CLIENT_KEY_FILE", optarg, 1);
1021 case OPTION_SSL_CRL_FILE:
1022 setenv("SSL_CLIENT_CRL_FILE", optarg, 1);
1024 case OPTION_SSL_NO_SSL3:
1025 setenv("SSL_NO_SSL3", "", 1);
1027 case OPTION_SSL_NO_TLS1:
1028 setenv("SSL_NO_TLS1", "", 1);
1030 case OPTION_SSL_NO_VERIFY_HOSTNAME:
1031 setenv("SSL_NO_VERIFY_HOSTNAME", "", 1);
1033 case OPTION_SSL_NO_VERIFY_PEER:
1034 setenv("SSL_NO_VERIFY_PEER", "", 1);
1044 if (h_hostname || f_filename || c_dirname) {
1045 if (!h_hostname || !f_filename || argc) {
1049 /* XXX this is a hack. */
1050 if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
1051 errx(1, "invalid hostname");
1052 if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
1053 c_dirname ? c_dirname : "", f_filename) == -1)
1054 errx(1, "%s", strerror(ENOMEM));
1063 /* allocate buffer */
1064 if (B_size < MINBUFSIZE)
1065 B_size = MINBUFSIZE;
1066 if ((buf = malloc(B_size)) == NULL)
1067 errx(1, "%s", strerror(ENOMEM));
1070 if ((s = getenv("FTP_TIMEOUT")) != NULL) {
1071 ftp_timeout = strtol(s, &end, 10);
1072 if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
1073 warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
1077 if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
1078 http_timeout = strtol(s, &end, 10);
1079 if (*s == '\0' || *end != '\0' || http_timeout < 0) {
1080 warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
1085 /* signal handling */
1087 sa.sa_handler = sig_handler;
1088 sigemptyset(&sa.sa_mask);
1089 sigaction(SIGALRM, &sa, NULL);
1090 sa.sa_flags = SA_RESETHAND;
1091 sigaction(SIGINT, &sa, NULL);
1092 fetchRestartCalls = 0;
1096 if (strcmp(o_filename, "-") == 0) {
1098 } else if (stat(o_filename, &sb) == -1) {
1099 if (errno == ENOENT) {
1101 errx(1, "%s is not a directory",
1104 err(1, "%s", o_filename);
1107 if (sb.st_mode & S_IFDIR)
1112 /* check if output is to a tty (for progress report) */
1113 v_tty = isatty(STDERR_FILENO);
1119 /* authentication */
1121 fetchAuthMethod = query_auth;
1122 if (N_filename != NULL)
1123 if (setenv("NETRC", N_filename, 1) == -1)
1124 err(1, "setenv: cannot set NETRC=%s", N_filename);
1127 if ((p = strrchr(*argv, '/')) == NULL)
1135 fetchLastErrCode = 0;
1139 e = fetch(*argv, "-");
1140 } else if (o_directory) {
1141 asprintf(&q, "%s/%s", o_filename, p);
1142 e = fetch(*argv, q);
1145 e = fetch(*argv, o_filename);
1148 e = fetch(*argv, p);
1152 kill(getpid(), SIGINT);
1154 if (e == 0 && once_flag)
1159 if ((fetchLastErrCode
1160 && fetchLastErrCode != FETCH_UNAVAIL
1161 && fetchLastErrCode != FETCH_MOVED
1162 && fetchLastErrCode != FETCH_URL
1163 && fetchLastErrCode != FETCH_RESOLV
1164 && fetchLastErrCode != FETCH_UNKNOWN)) {
1165 if (w_secs && v_level)
1166 fprintf(stderr, "Waiting %ld seconds "
1167 "before retrying\n", w_secs);