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