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