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