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