xref: /freebsd/usr.bin/fetch/fetch.c (revision 5773cccf19ef7b97e56c1101aa481c43149224da)
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
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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$");
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>
49 
50 #define MINBUFSIZE	4096
51 
52 /* Option flags */
53 int	 A_flag;	/*    -A: do not follow 302 redirects */
54 int	 a_flag;	/*    -a: auto retry */
55 off_t	 B_size;	/*    -B: buffer size */
56 int	 b_flag;	/*!   -b: workaround TCP bug */
57 char    *c_dirname;	/*    -c: remote directory */
58 int	 d_flag;	/*    -d: direct connection */
59 int	 F_flag;	/*    -F: restart without checking mtime  */
60 char	*f_filename;	/*    -f: file to fetch */
61 char	*h_hostname;	/*    -h: host to fetch from */
62 int	 l_flag;	/*    -l: link rather than copy file: URLs */
63 int	 m_flag;	/* -[Mm]: mirror mode */
64 int	 n_flag;	/*    -n: do not preserve modification time */
65 int	 o_flag;	/*    -o: specify output file */
66 int	 o_directory;	/*        output file is a directory */
67 char	*o_filename;	/*        name of output file */
68 int	 o_stdout;	/*        output file is stdout */
69 int	 once_flag;	/*    -1: stop at first successful file */
70 int	 p_flag;	/* -[Pp]: use passive FTP */
71 int	 R_flag;	/*    -R: don't delete partially transferred files */
72 int	 r_flag;	/*    -r: restart previously interrupted transfer */
73 off_t	 S_size;        /*    -S: require size to match */
74 int	 s_flag;        /*    -s: show size, don't fetch */
75 long	 T_secs = 120;	/*    -T: transfer timeout in seconds */
76 int	 t_flag;	/*!   -t: workaround TCP bug */
77 int	 U_flag;	/*    -U: do not use high ports */
78 int	 v_level = 1;	/*    -v: verbosity level */
79 int	 v_tty;		/*        stdout is a tty */
80 pid_t	 pgrp;		/*        our process group */
81 long	 w_secs;	/*    -w: retry delay */
82 int	 family = PF_UNSPEC;	/* -[46]: address family to use */
83 
84 int	 sigalrm;	/* SIGALRM received */
85 int	 siginfo;	/* SIGINFO received */
86 int	 sigint;	/* SIGINT received */
87 
88 long	 ftp_timeout;	/* default timeout for FTP transfers */
89 long	 http_timeout;	/* default timeout for HTTP transfers */
90 u_char	*buf;		/* transfer buffer */
91 
92 
93 /*
94  * Signal handler
95  */
96 static void
97 sig_handler(int sig)
98 {
99 	switch (sig) {
100 	case SIGALRM:
101 		sigalrm = 1;
102 		break;
103 	case SIGINFO:
104 		siginfo = 1;
105 		break;
106 	case SIGINT:
107 		sigint = 1;
108 		break;
109 	}
110 }
111 
112 struct xferstat {
113 	char		 name[40];
114 	struct timeval	 start;
115 	struct timeval	 end;
116 	struct timeval	 last;
117 	off_t		 size;
118 	off_t		 offset;
119 	off_t		 rcvd;
120 };
121 
122 /*
123  * Update the stats display
124  */
125 static void
126 stat_display(struct xferstat *xs, int force)
127 {
128 	struct timeval now;
129 	int ctty_pgrp;
130 
131 	if (!v_tty || !v_level)
132 		return;
133 
134 	/* check if we're the foreground process */
135 	if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
136 	    (pid_t)ctty_pgrp != pgrp)
137 		return;
138 
139 	gettimeofday(&now, NULL);
140 	if (!force && now.tv_sec <= xs->last.tv_sec)
141 		return;
142 	xs->last = now;
143 
144 	fprintf(stderr, "\rReceiving %s", xs->name);
145 	if (xs->size <= 0) {
146 		fprintf(stderr, ": %lld bytes", (long long)xs->rcvd);
147 	} else {
148 		long elapsed;
149 
150 		fprintf(stderr, " (%lld bytes): %d%%", (long long)xs->size,
151 		    (int)((100.0 * xs->rcvd) / xs->size));
152 		elapsed = xs->last.tv_sec - xs->start.tv_sec;
153 		if (elapsed > 30 && xs->rcvd > 0) {
154 			long remaining;
155 
156 			remaining = ((xs->size * elapsed) / xs->rcvd) - elapsed;
157 			fprintf(stderr, " (ETA ");
158 			if (remaining > 3600) {
159 				fprintf(stderr, "%02ld:", remaining / 3600);
160 				remaining %= 3600;
161 			}
162 			fprintf(stderr, "%02ld:%02ld)  ",
163 			    remaining / 60, remaining % 60);
164 		}
165 	}
166 }
167 
168 /*
169  * Initialize the transfer statistics
170  */
171 static void
172 stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
173 {
174 	snprintf(xs->name, sizeof xs->name, "%s", name);
175 	gettimeofday(&xs->start, NULL);
176 	xs->last.tv_sec = xs->last.tv_usec = 0;
177 	xs->end = xs->last;
178 	xs->size = size;
179 	xs->offset = offset;
180 	xs->rcvd = offset;
181 	stat_display(xs, 1);
182 }
183 
184 /*
185  * Update the transfer statistics
186  */
187 static void
188 stat_update(struct xferstat *xs, off_t rcvd)
189 {
190 	xs->rcvd = rcvd;
191 	stat_display(xs, 0);
192 }
193 
194 /*
195  * Finalize the transfer statistics
196  */
197 static void
198 stat_end(struct xferstat *xs)
199 {
200 	double delta;
201 	double bps;
202 
203 	if (!v_level)
204 		return;
205 
206 	gettimeofday(&xs->end, NULL);
207 
208 	stat_display(xs, 1);
209 	fputc('\n', stderr);
210 	delta = (xs->end.tv_sec + (xs->end.tv_usec / 1.e6))
211 	    - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
212 	fprintf(stderr, "%lld bytes transferred in %.1f seconds ",
213 	    (long long)(xs->rcvd - xs->offset), delta);
214 	bps = (xs->rcvd - xs->offset) / delta;
215 	if (bps > 1024*1024)
216 		fprintf(stderr, "(%.2f MBps)\n", bps / (1024*1024));
217 	else if (bps > 1024)
218 		fprintf(stderr, "(%.2f kBps)\n", bps / 1024);
219 	else
220 		fprintf(stderr, "(%.2f Bps)\n", bps);
221 }
222 
223 /*
224  * Ask the user for authentication details
225  */
226 static int
227 query_auth(struct url *URL)
228 {
229 	struct termios tios;
230 	tcflag_t saved_flags;
231 	int i, nopwd;
232 
233 
234 	fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
235 	    URL->scheme, URL->host, URL->port);
236 
237 	fprintf(stderr, "Login: ");
238 	if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
239 		return -1;
240 	for (i = 0; URL->user[i]; ++i)
241 		if (isspace(URL->user[i]))
242 			URL->user[i] = '\0';
243 
244 	fprintf(stderr, "Password: ");
245 	if (tcgetattr(STDIN_FILENO, &tios) == 0) {
246 		saved_flags = tios.c_lflag;
247 		tios.c_lflag &= ~ECHO;
248 		tios.c_lflag |= ECHONL|ICANON;
249 		tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
250 		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
251 		tios.c_lflag = saved_flags;
252 		tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
253 	} else {
254 		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
255 	}
256 	if (nopwd)
257 		return -1;
258 
259 	for (i = 0; URL->pwd[i]; ++i)
260 		if (isspace(URL->pwd[i]))
261 			URL->pwd[i] = '\0';
262 	return 0;
263 }
264 
265 /*
266  * Fetch a file
267  */
268 static int
269 fetch(char *URL, const char *path)
270 {
271 	struct url *url;
272 	struct url_stat us;
273 	struct stat sb, nsb;
274 	struct xferstat xs;
275 	FILE *f, *of;
276 	size_t size, wr;
277 	off_t count;
278 	char flags[8];
279 	const char *slash;
280 	char *tmppath;
281 	int r;
282 	u_int timeout;
283 	u_char *ptr;
284 
285 	f = of = NULL;
286 	tmppath = NULL;
287 
288 	/* parse URL */
289 	if ((url = fetchParseURL(URL)) == NULL) {
290 		warnx("%s: parse error", URL);
291 		goto failure;
292 	}
293 
294 	/* if no scheme was specified, take a guess */
295 	if (!*url->scheme) {
296 		if (!*url->host)
297 			strcpy(url->scheme, SCHEME_FILE);
298 		else if (strncasecmp(url->host, "ftp.", 4) == 0)
299 			strcpy(url->scheme, SCHEME_FTP);
300 		else if (strncasecmp(url->host, "www.", 4) == 0)
301 			strcpy(url->scheme, SCHEME_HTTP);
302 	}
303 
304 	timeout = 0;
305 	*flags = 0;
306 	count = 0;
307 
308 	/* common flags */
309 	if (v_level > 1)
310 		strcat(flags, "v");
311 	if (v_level > 2)
312 		fetchDebug = 1;
313 	switch (family) {
314 	case PF_INET:
315 		strcat(flags, "4");
316 		break;
317 	case PF_INET6:
318 		strcat(flags, "6");
319 		break;
320 	}
321 
322 	/* FTP specific flags */
323 	if (strcmp(url->scheme, "ftp") == 0) {
324 		if (p_flag)
325 			strcat(flags, "p");
326 		if (d_flag)
327 			strcat(flags, "d");
328 		if (U_flag)
329 			strcat(flags, "l");
330 		timeout = T_secs ? T_secs : ftp_timeout;
331 	}
332 
333 	/* HTTP specific flags */
334 	if (strcmp(url->scheme, "http") == 0) {
335 		if (d_flag)
336 			strcat(flags, "d");
337 		if (A_flag)
338 			strcat(flags, "A");
339 		timeout = T_secs ? T_secs : http_timeout;
340 	}
341 
342 	/* set the protocol timeout. */
343 	fetchTimeout = timeout;
344 
345 	/* just print size */
346 	if (s_flag) {
347 		if (timeout)
348 			alarm(timeout);
349 		r = fetchStat(url, &us, flags);
350 		if (timeout)
351 			alarm(0);
352 		if (sigalrm || sigint)
353 			goto signal;
354 		if (r == -1) {
355 			warnx("%s", fetchLastErrString);
356 			goto failure;
357 		}
358 		if (us.size == -1)
359 			printf("Unknown\n");
360 		else
361 			printf("%lld\n", (long long)us.size);
362 		goto success;
363 	}
364 
365 	/*
366 	 * If the -r flag was specified, we have to compare the local
367 	 * and remote files, so we should really do a fetchStat()
368 	 * first, but I know of at least one HTTP server that only
369 	 * sends the content size in response to GET requests, and
370 	 * leaves it out of replies to HEAD requests.  Also, in the
371 	 * (frequent) case that the local and remote files match but
372 	 * the local file is truncated, we have sufficient information
373 	 * before the compare to issue a correct request.  Therefore,
374 	 * we always issue a GET request as if we were sure the local
375 	 * file was a truncated copy of the remote file; we can drop
376 	 * the connection later if we change our minds.
377 	 */
378 	sb.st_size = -1;
379 	if (!o_stdout && stat(path, &sb) == -1 && errno != ENOENT) {
380 		warnx("%s: stat()", path);
381 		goto failure;
382 	}
383 	if (!o_stdout && r_flag && S_ISREG(sb.st_mode))
384 		url->offset = sb.st_size;
385 
386 	/* start the transfer */
387 	if (timeout)
388 		alarm(timeout);
389 	f = fetchXGet(url, &us, flags);
390 	if (timeout)
391 		alarm(0);
392 	if (sigalrm || sigint)
393 		goto signal;
394 	if (f == NULL) {
395 		warnx("%s: %s", path, fetchLastErrString);
396 		goto failure;
397 	}
398 	if (sigint)
399 		goto signal;
400 
401 	/* check that size is as expected */
402 	if (S_size) {
403 		if (us.size == -1) {
404 			warnx("%s: size unknown", path);
405 			goto failure;
406 		} else if (us.size != S_size) {
407 			warnx("%s: size mismatch: expected %lld, actual %lld",
408 			    path, (long long)S_size, (long long)us.size);
409 			goto failure;
410 		}
411 	}
412 
413 	/* symlink instead of copy */
414 	if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
415 		if (symlink(url->doc, path) == -1) {
416 			warn("%s: symlink()", path);
417 			goto failure;
418 		}
419 		goto success;
420 	}
421 
422 	if (us.size == -1 && !o_stdout && v_level > 0)
423 		warnx("%s: size of remote file is not known", path);
424 	if (v_level > 1) {
425 		if (sb.st_size != -1)
426 			fprintf(stderr, "local size / mtime: %lld / %ld\n",
427 			    (long long)sb.st_size, (long)sb.st_mtime);
428 		if (us.size != -1)
429 			fprintf(stderr, "remote size / mtime: %lld / %ld\n",
430 			    (long long)us.size, (long)us.mtime);
431 	}
432 
433 	/* open output file */
434 	if (o_stdout) {
435 		/* output to stdout */
436 		of = stdout;
437 	} else if (r_flag && sb.st_size != -1) {
438 		/* resume mode, local file exists */
439 		if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
440 			/* no match! have to refetch */
441 			fclose(f);
442 			/* if precious, warn the user and give up */
443 			if (R_flag) {
444 				warnx("%s: local modification time "
445 				    "does not match remote", path);
446 				goto failure_keep;
447 			}
448 		} else {
449 			if (us.size == sb.st_size)
450 				/* nothing to do */
451 				goto success;
452 			if (sb.st_size > us.size) {
453 				/* local file too long! */
454 				warnx("%s: local file (%lld bytes) is longer "
455 				    "than remote file (%lld bytes)", path,
456 				    (long long)sb.st_size, (long long)us.size);
457 				goto failure;
458 			}
459 			/* we got it, open local file */
460 			if ((of = fopen(path, "a")) == NULL) {
461 				warn("%s: fopen()", path);
462 				goto failure;
463 			}
464 			/* check that it didn't move under our feet */
465 			if (fstat(fileno(of), &nsb) == -1) {
466 				/* can't happen! */
467 				warn("%s: fstat()", path);
468 				goto failure;
469 			}
470 			if (nsb.st_dev != sb.st_dev ||
471 			    nsb.st_ino != nsb.st_ino ||
472 			    nsb.st_size != sb.st_size) {
473 				warnx("%s: file has changed", path);
474 				fclose(of);
475 				of = NULL;
476 				sb = nsb;
477 			}
478 		}
479 	} else if (m_flag && sb.st_size != -1) {
480 		/* mirror mode, local file exists */
481 		if (sb.st_size == us.size && sb.st_mtime == us.mtime)
482 			goto success;
483 	}
484 
485 	if (of == NULL) {
486 		/*
487 		 * We don't yet have an output file; either this is a
488 		 * vanilla run with no special flags, or the local and
489 		 * remote files didn't match.
490 		 */
491 
492 		if (url->offset != 0) {
493 			/*
494 			 * We tried to restart a transfer, but for
495 			 * some reason gave up - so we have to restart
496 			 * from scratch if we want the whole file
497 			 */
498 			url->offset = 0;
499 			if ((f = fetchXGet(url, &us, flags)) == NULL) {
500 				warnx("%s: %s", path, fetchLastErrString);
501 				goto failure;
502 			}
503 			if (sigint)
504 				goto signal;
505 		}
506 
507 		/* construct a temp file name */
508 		if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
509 			if ((slash = strrchr(path, '/')) == NULL)
510 				slash = path;
511 			else
512 				++slash;
513 			asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
514 			    (int)(slash - path), path, slash);
515 			if (tmppath != NULL) {
516 				mkstemps(tmppath, strlen(slash) + 1);
517 				of = fopen(tmppath, "w");
518 			}
519 		}
520 		if (of == NULL)
521 			of = fopen(path, "w");
522 		if (of == NULL) {
523 			warn("%s: open()", path);
524 			goto failure;
525 		}
526 	}
527 	count = url->offset;
528 
529 	/* start the counter */
530 	stat_start(&xs, path, us.size, count);
531 
532 	sigalrm = siginfo = sigint = 0;
533 
534 	/* suck in the data */
535 	signal(SIGINFO, sig_handler);
536 	while (!sigint) {
537 		if (us.size != -1 && us.size - count < B_size)
538 			size = us.size - count;
539 		else
540 			size = B_size;
541 		if (siginfo) {
542 			stat_end(&xs);
543 			siginfo = 0;
544 		}
545 		if ((size = fread(buf, 1, size, f)) == 0) {
546 			if (ferror(f) && errno == EINTR && !sigint)
547 				clearerr(f);
548 			else
549 				break;
550 		}
551 		stat_update(&xs, count += size);
552 		for (ptr = buf; size > 0; ptr += wr, size -= wr)
553 			if ((wr = fwrite(ptr, 1, size, of)) < size) {
554 				if (ferror(of) && errno == EINTR && !sigint)
555 					clearerr(of);
556 				else
557 					break;
558 			}
559 		if (size != 0)
560 			break;
561 	}
562 	if (!sigalrm)
563 		sigalrm = ferror(f) && errno == ETIMEDOUT;
564 	signal(SIGINFO, SIG_DFL);
565 
566 	stat_end(&xs);
567 
568 	/*
569 	 * If the transfer timed out or was interrupted, we still want to
570 	 * set the mtime in case the file is not removed (-r or -R) and
571 	 * the user later restarts the transfer.
572 	 */
573  signal:
574 	/* set mtime of local file */
575 	if (!n_flag && us.mtime && !o_stdout && of != NULL &&
576 	    (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
577 		struct timeval tv[2];
578 
579 		fflush(of);
580 		tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
581 		tv[1].tv_sec = (long)us.mtime;
582 		tv[0].tv_usec = tv[1].tv_usec = 0;
583 		if (utimes(tmppath ? tmppath : path, tv))
584 			warn("%s: utimes()", tmppath ? tmppath : path);
585 	}
586 
587 	/* timed out or interrupted? */
588 	if (sigalrm)
589 		warnx("transfer timed out");
590 	if (sigint) {
591 		warnx("transfer interrupted");
592 		goto failure;
593 	}
594 
595 	/* timeout / interrupt before connection completley established? */
596 	if (f == NULL)
597 		goto failure;
598 
599 	if (!sigalrm) {
600 		/* check the status of our files */
601 		if (ferror(f))
602 			warn("%s", URL);
603 		if (ferror(of))
604 			warn("%s", path);
605 		if (ferror(f) || ferror(of))
606 			goto failure;
607 	}
608 
609 	/* did the transfer complete normally? */
610 	if (us.size != -1 && count < us.size) {
611 		warnx("%s appears to be truncated: %lld/%lld bytes",
612 		    path, (long long)count, (long long)us.size);
613 		goto failure_keep;
614 	}
615 
616 	/*
617 	 * If the transfer timed out and we didn't know how much to
618 	 * expect, assume the worst (i.e. we didn't get all of it)
619 	 */
620 	if (sigalrm && us.size == -1) {
621 		warnx("%s may be truncated", path);
622 		goto failure_keep;
623 	}
624 
625  success:
626 	r = 0;
627 	if (tmppath != NULL && rename(tmppath, path) == -1) {
628 		warn("%s: rename()", path);
629 		goto failure_keep;
630 	}
631 	goto done;
632  failure:
633 	if (of && of != stdout && !R_flag && !r_flag)
634 		if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
635 			unlink(tmppath ? tmppath : path);
636 	if (R_flag && tmppath != NULL && sb.st_size == -1)
637 		rename(tmppath, path); /* ignore errors here */
638  failure_keep:
639 	r = -1;
640 	goto done;
641  done:
642 	if (f)
643 		fclose(f);
644 	if (of && of != stdout)
645 		fclose(of);
646 	if (url)
647 		fetchFreeURL(url);
648 	if (tmppath != NULL)
649 		free(tmppath);
650 	return r;
651 }
652 
653 static void
654 usage(void)
655 {
656 	fprintf(stderr, "%s\n%s\n%s\n",
657 	    "usage: fetch [-146AFMPRUadlmnpqrsv] [-o outputfile] [-S bytes]",
658 	    "             [-B bytes] [-T seconds] [-w seconds]",
659 	    "             [-h host -f file [-c dir] | URL ...]");
660 }
661 
662 
663 /*
664  * Entry point
665  */
666 int
667 main(int argc, char *argv[])
668 {
669 	struct stat sb;
670 	struct sigaction sa;
671 	const char *p, *s;
672 	char *end, *q;
673 	int c, e, r;
674 
675 	while ((c = getopt(argc, argv,
676 	    "146AaB:bc:dFf:Hh:lMmnPpo:qRrS:sT:tUvw:")) != -1)
677 		switch (c) {
678 		case '1':
679 			once_flag = 1;
680 			break;
681 		case '4':
682 			family = PF_INET;
683 			break;
684 		case '6':
685 			family = PF_INET6;
686 			break;
687 		case 'A':
688 			A_flag = 1;
689 			break;
690 		case 'a':
691 			a_flag = 1;
692 			break;
693 		case 'B':
694 			B_size = (off_t)strtol(optarg, &end, 10);
695 			if (*optarg == '\0' || *end != '\0')
696 				errx(1, "invalid buffer size (%s)", optarg);
697 			break;
698 		case 'b':
699 			warnx("warning: the -b option is deprecated");
700 			b_flag = 1;
701 			break;
702 		case 'c':
703 			c_dirname = optarg;
704 			break;
705 		case 'd':
706 			d_flag = 1;
707 			break;
708 		case 'F':
709 			F_flag = 1;
710 			break;
711 		case 'f':
712 			f_filename = optarg;
713 			break;
714 		case 'H':
715 			warnx("the -H option is now implicit, "
716 			    "use -U to disable");
717 			break;
718 		case 'h':
719 			h_hostname = optarg;
720 			break;
721 		case 'l':
722 			l_flag = 1;
723 			break;
724 		case 'o':
725 			o_flag = 1;
726 			o_filename = optarg;
727 			break;
728 		case 'M':
729 		case 'm':
730 			if (r_flag)
731 				errx(1, "the -m and -r flags "
732 				    "are mutually exclusive");
733 			m_flag = 1;
734 			break;
735 		case 'n':
736 			n_flag = 1;
737 			break;
738 		case 'P':
739 		case 'p':
740 			p_flag = 1;
741 			break;
742 		case 'q':
743 			v_level = 0;
744 			break;
745 		case 'R':
746 			R_flag = 1;
747 			break;
748 		case 'r':
749 			if (m_flag)
750 				errx(1, "the -m and -r flags "
751 				    "are mutually exclusive");
752 			r_flag = 1;
753 			break;
754 		case 'S':
755 			S_size = (off_t)strtol(optarg, &end, 10);
756 			if (*optarg == '\0' || *end != '\0')
757 				errx(1, "invalid size (%s)", optarg);
758 			break;
759 		case 's':
760 			s_flag = 1;
761 			break;
762 		case 'T':
763 			T_secs = strtol(optarg, &end, 10);
764 			if (*optarg == '\0' || *end != '\0')
765 				errx(1, "invalid timeout (%s)", optarg);
766 			break;
767 		case 't':
768 			t_flag = 1;
769 			warnx("warning: the -t option is deprecated");
770 			break;
771 		case 'U':
772 			U_flag = 1;
773 			break;
774 		case 'v':
775 			v_level++;
776 			break;
777 		case 'w':
778 			a_flag = 1;
779 			w_secs = strtol(optarg, &end, 10);
780 			if (*optarg == '\0' || *end != '\0')
781 				errx(1, "invalid delay (%s)", optarg);
782 			break;
783 		default:
784 			usage();
785 			exit(EX_USAGE);
786 		}
787 
788 	argc -= optind;
789 	argv += optind;
790 
791 	if (h_hostname || f_filename || c_dirname) {
792 		if (!h_hostname || !f_filename || argc) {
793 			usage();
794 			exit(EX_USAGE);
795 		}
796 		/* XXX this is a hack. */
797 		if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
798 			errx(1, "invalid hostname");
799 		if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
800 		    c_dirname ? c_dirname : "", f_filename) == -1)
801 			errx(1, "%s", strerror(ENOMEM));
802 		argc++;
803 	}
804 
805 	if (!argc) {
806 		usage();
807 		exit(EX_USAGE);
808 	}
809 
810 	/* allocate buffer */
811 	if (B_size < MINBUFSIZE)
812 		B_size = MINBUFSIZE;
813 	if ((buf = malloc(B_size)) == NULL)
814 		errx(1, "%s", strerror(ENOMEM));
815 
816 	/* timeouts */
817 	if ((s = getenv("FTP_TIMEOUT")) != NULL) {
818 		ftp_timeout = strtol(s, &end, 10);
819 		if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
820 			warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
821 			ftp_timeout = 0;
822 		}
823 	}
824 	if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
825 		http_timeout = strtol(s, &end, 10);
826 		if (*s == '\0' || *end != '\0' || http_timeout < 0) {
827 			warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
828 			http_timeout = 0;
829 		}
830 	}
831 
832 	/* signal handling */
833 	sa.sa_flags = 0;
834 	sa.sa_handler = sig_handler;
835 	sigemptyset(&sa.sa_mask);
836 	sigaction(SIGALRM, &sa, NULL);
837 	sa.sa_flags = SA_RESETHAND;
838 	sigaction(SIGINT, &sa, NULL);
839 	fetchRestartCalls = 0;
840 
841 	/* output file */
842 	if (o_flag) {
843 		if (strcmp(o_filename, "-") == 0) {
844 			o_stdout = 1;
845 		} else if (stat(o_filename, &sb) == -1) {
846 			if (errno == ENOENT) {
847 				if (argc > 1)
848 					errx(EX_USAGE, "%s is not a directory",
849 					    o_filename);
850 			} else {
851 				err(EX_IOERR, "%s", o_filename);
852 			}
853 		} else {
854 			if (sb.st_mode & S_IFDIR)
855 				o_directory = 1;
856 		}
857 	}
858 
859 	/* check if output is to a tty (for progress report) */
860 	v_tty = isatty(STDERR_FILENO);
861 	if (v_tty)
862 		pgrp = getpgrp();
863 
864 	r = 0;
865 
866 	/* authentication */
867 	if (v_tty)
868 		fetchAuthMethod = query_auth;
869 
870 	while (argc) {
871 		if ((p = strrchr(*argv, '/')) == NULL)
872 			p = *argv;
873 		else
874 			p++;
875 
876 		if (!*p)
877 			p = "fetch.out";
878 
879 		fetchLastErrCode = 0;
880 
881 		if (o_flag) {
882 			if (o_stdout) {
883 				e = fetch(*argv, "-");
884 			} else if (o_directory) {
885 				asprintf(&q, "%s/%s", o_filename, p);
886 				e = fetch(*argv, q);
887 				free(q);
888 			} else {
889 				e = fetch(*argv, o_filename);
890 			}
891 		} else {
892 			e = fetch(*argv, p);
893 		}
894 
895 		if (sigint)
896 			kill(getpid(), SIGINT);
897 
898 		if (e == 0 && once_flag)
899 			exit(0);
900 
901 		if (e) {
902 			r = 1;
903 			if ((fetchLastErrCode
904 			    && fetchLastErrCode != FETCH_UNAVAIL
905 			    && fetchLastErrCode != FETCH_MOVED
906 			    && fetchLastErrCode != FETCH_URL
907 			    && fetchLastErrCode != FETCH_RESOLV
908 			    && fetchLastErrCode != FETCH_UNKNOWN)) {
909 				if (w_secs && v_level)
910 					fprintf(stderr, "Waiting %ld seconds "
911 					    "before retrying\n", w_secs);
912 				if (w_secs)
913 					sleep(w_secs);
914 				if (a_flag)
915 					continue;
916 			}
917 		}
918 
919 		argc--, argv++;
920 	}
921 
922 	exit(r);
923 }
924