fetch.c revision 187361
111499Sjkh/*-
211499Sjkh * Copyright (c) 2000-2004 Dag-Erling Co��dan Sm��rgrav
311499Sjkh * All rights reserved.
411499Sjkh *
511499Sjkh * Redistribution and use in source and binary forms, with or without
611499Sjkh * modification, are permitted provided that the following conditions
742520Sasami * are met:
811499Sjkh * 1. Redistributions of source code must retain the above copyright
911499Sjkh *    notice, this list of conditions and the following disclaimer
1011499Sjkh *    in this position and unchanged.
1111499Sjkh * 2. Redistributions in binary form must reproduce the above copyright
1211499Sjkh *    notice, this list of conditions and the following disclaimer in the
1311499Sjkh *    documentation and/or other materials provided with the distribution.
1411499Sjkh * 3. The name of the author may not be used to endorse or promote products
1511499Sjkh *    derived from this software without specific prior written permission
1611499Sjkh *
1711499Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1811499Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1911499Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011499Sjkh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2111499Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2211499Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2311499Sjkh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2411499Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2511499Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2611499Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711499Sjkh */
2811499Sjkh
2911499Sjkh#include <sys/cdefs.h>
3011499Sjkh__FBSDID("$FreeBSD: head/usr.bin/fetch/fetch.c 187361 2009-01-17 13:34:56Z des $");
3111499Sjkh
3211499Sjkh#include <sys/param.h>
3311499Sjkh#include <sys/socket.h>
3411499Sjkh#include <sys/stat.h>
3511499Sjkh#include <sys/time.h>
3611499Sjkh
3711532Sjkh#include <ctype.h>
3811303Sjkh#include <err.h>
3911303Sjkh#include <errno.h>
4011303Sjkh#include <signal.h>
4111303Sjkh#include <stdint.h>
4211303Sjkh#include <stdio.h>
4311303Sjkh#include <stdlib.h>
4411303Sjkh#include <string.h>
4511303Sjkh#include <termios.h>
4616366Sjkh#include <unistd.h>
4717377Sjkh
4811303Sjkh#include <fetch.h>
4914738Sjkh
5014670Sjkh#define MINBUFSIZE	4096
5111303Sjkh#define TIMEOUT		120
5211303Sjkh
5311303Sjkh/* Option flags */
5411303Sjkhint	 A_flag;	/*    -A: do not follow 302 redirects */
5511593Sjkhint	 a_flag;	/*    -a: auto retry */
5611303Sjkhoff_t	 B_size;	/*    -B: buffer size */
5711303Sjkhint	 b_flag;	/*!   -b: workaround TCP bug */
5811303Sjkhchar    *c_dirname;	/*    -c: remote directory */
5924235Sjkhint	 d_flag;	/*    -d: direct connection */
6024235Sjkhint	 F_flag;	/*    -F: restart without checking mtime  */
6124235Sjkhchar	*f_filename;	/*    -f: file to fetch */
6224235Sjkhchar	*h_hostname;	/*    -h: host to fetch from */
6324235Sjkhint	 i_flag;	/*    -i: specify input file for mtime comparison */
6411479Sjkhchar	*i_filename;	/*        name of input file */
6512245Sjkhint	 l_flag;	/*    -l: link rather than copy file: URLs */
6612245Sjkhint	 m_flag;	/* -[Mm]: mirror mode */
6711303Sjkhchar	*N_filename;	/*    -N: netrc file name */
6842520Sasamiint	 n_flag;	/*    -n: do not preserve modification time */
6911303Sjkhint	 o_flag;	/*    -o: specify output file */
7021132Sobrienint	 o_directory;	/*        output file is a directory */
7111303Sjkhchar	*o_filename;	/*        name of output file */
7211303Sjkhint	 o_stdout;	/*        output file is stdout */
7341752Ssteveint	 once_flag;	/*    -1: stop at first successful file */
7411303Sjkhint	 p_flag;	/* -[Pp]: use passive FTP */
7511303Sjkhint	 R_flag;	/*    -R: don't delete partially transferred files */
7621132Sobrienint	 r_flag;	/*    -r: restart previously interrupted transfer */
7711303Sjkhoff_t	 S_size;        /*    -S: require size to match */
7841752Ssteveint	 s_flag;        /*    -s: show size, don't fetch */
7911303Sjkhlong	 T_secs;	/*    -T: transfer timeout in seconds */
8011303Sjkhint	 t_flag;	/*!   -t: workaround TCP bug */
8140281Sjkhint	 U_flag;	/*    -U: do not use high ports */
8211303Sjkhint	 v_level = 1;	/*    -v: verbosity level */
8311303Sjkhint	 v_tty;		/*        stdout is a tty */
8442005Sjkhpid_t	 pgrp;		/*        our process group */
8542520Sasamilong	 w_secs;	/*    -w: retry delay */
8611303Sjkhint	 family = PF_UNSPEC;	/* -[46]: address family to use */
8711303Sjkh
8834792Sjkhint	 sigalrm;	/* SIGALRM received */
8911303Sjkhint	 siginfo;	/* SIGINFO received */
9011303Sjkhint	 sigint;	/* SIGINT received */
9134867Sjkh
9224235Sjkhlong	 ftp_timeout = TIMEOUT;		/* default timeout for FTP transfers */
9311303Sjkhlong	 http_timeout = TIMEOUT;	/* default timeout for HTTP transfers */
9411303Sjkhchar	*buf;		/* transfer buffer */
9511303Sjkh
9611303Sjkh
9711303Sjkh/*
9841752Ssteve * Signal handler
9914670Sjkh */
10011303Sjkhstatic void
10111303Sjkhsig_handler(int sig)
10211303Sjkh{
10339595Sjkh	switch (sig) {
10411303Sjkh	case SIGALRM:
10542520Sasami		sigalrm = 1;
10641752Ssteve		break;
10741752Ssteve	case SIGINFO:
10811303Sjkh		siginfo = 1;
10911303Sjkh		break;
11011303Sjkh	case SIGINT:
11141752Ssteve		sigint = 1;
11211303Sjkh		break;
11311303Sjkh	}
11411303Sjkh}
11511303Sjkh
11625481Sjkhstruct xferstat {
11741752Ssteve	char		 name[64];
11841752Ssteve	struct timeval	 start;
11941752Ssteve	struct timeval	 last;
12041752Ssteve	off_t		 size;
12141752Ssteve	off_t		 offset;
12241752Ssteve	off_t		 rcvd;
12341752Ssteve};
12441752Ssteve
12541752Ssteve/*
12621132Sobrien * Compute and display ETA
12742520Sasami */
12825481Sjkhstatic const char *
12911303Sjkhstat_eta(struct xferstat *xs)
13040281Sjkh{
13140281Sjkh	static char str[16];
13242520Sasami	long elapsed, eta;
13340281Sjkh	off_t received, expected;
13441752Ssteve
13511303Sjkh	elapsed = xs->last.tv_sec - xs->start.tv_sec;
13611303Sjkh	received = xs->rcvd - xs->offset;
13711303Sjkh	expected = xs->size - xs->rcvd;
13811303Sjkh	eta = (long)((double)elapsed * expected / received);
13911303Sjkh	if (eta > 3600)
14011303Sjkh		snprintf(str, sizeof str, "%02ldh%02ldm",
14111303Sjkh		    eta / 3600, (eta % 3600) / 60);
14211303Sjkh	else
14311303Sjkh		snprintf(str, sizeof str, "%02ldm%02lds",
14411303Sjkh		    eta / 60, eta % 60);
14511303Sjkh	return (str);
14611303Sjkh}
14711303Sjkh
14811303Sjkh/*
14911303Sjkh * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
15011303Sjkh */
15111303Sjkhstatic const char *prefixes = " kMGTP";
15211303Sjkhstatic const char *
15311579Sjkhstat_bytes(off_t bytes)
15411303Sjkh{
15511303Sjkh	static char str[16];
15611303Sjkh	const char *prefix = prefixes;
15711303Sjkh
15811303Sjkh	while (bytes > 9999 && prefix[1] != '\0') {
15911303Sjkh		bytes /= 1024;
16016688Sjkh		prefix++;
16116688Sjkh	}
16216688Sjkh	snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix);
16316688Sjkh	return (str);
16416688Sjkh}
16516688Sjkh
16616688Sjkh/*
16716688Sjkh * Compute and display transfer rate
16816688Sjkh */
16916688Sjkhstatic const char *
17016688Sjkhstat_bps(struct xferstat *xs)
17111303Sjkh{
17214670Sjkh	static char str[16];
17311303Sjkh	double delta, bps;
17411579Sjkh
17511303Sjkh	delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
17611303Sjkh	    - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
17711303Sjkh	if (delta == 0.0) {
17811303Sjkh		snprintf(str, sizeof str, "?? Bps");
17911303Sjkh	} else {
18016688Sjkh		bps = (xs->rcvd - xs->offset) / delta;
18111303Sjkh		snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
18214670Sjkh	}
18311303Sjkh	return (str);
18411303Sjkh}
18511303Sjkh
18611303Sjkh/*
18711303Sjkh * Update the stats display
18811303Sjkh */
18911303Sjkhstatic void
19011303Sjkhstat_display(struct xferstat *xs, int force)
19111593Sjkh{
19211303Sjkh	struct timeval now;
19311303Sjkh	int ctty_pgrp;
19411303Sjkh
19511303Sjkh	/* check if we're the foreground process */
19611303Sjkh	if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
19711303Sjkh	    (pid_t)ctty_pgrp != pgrp)
19811303Sjkh		return;
19911303Sjkh
20011303Sjkh	gettimeofday(&now, NULL);
20111303Sjkh	if (!force && now.tv_sec <= xs->last.tv_sec)
20211303Sjkh		return;
20311303Sjkh	xs->last = now;
20411303Sjkh
20511528Sjkh	fprintf(stderr, "\r%-46.46s", xs->name);
20611303Sjkh	if (xs->size <= 0) {
20711303Sjkh		setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd));
20811303Sjkh		fprintf(stderr, "        %s", stat_bytes(xs->rcvd));
20911303Sjkh	} else {
21011303Sjkh		setproctitle("%s [%d%% of %s]", xs->name,
21111303Sjkh		    (int)((100.0 * xs->rcvd) / xs->size),
21211303Sjkh		    stat_bytes(xs->size));
21311303Sjkh		fprintf(stderr, "%3d%% of %s",
21411303Sjkh		    (int)((100.0 * xs->rcvd) / xs->size),
21511303Sjkh		    stat_bytes(xs->size));
21611303Sjkh	}
21711303Sjkh	fprintf(stderr, " %s", stat_bps(xs));
21811303Sjkh	if (xs->size > 0 && xs->rcvd > 0 &&
21911303Sjkh	    xs->last.tv_sec >= xs->start.tv_sec + 10)
22011303Sjkh		fprintf(stderr, " %s", stat_eta(xs));
22111303Sjkh}
22211303Sjkh
22311303Sjkh/*
22411303Sjkh * Initialize the transfer statistics
22511303Sjkh */
22611532Sjkhstatic void
22720315Sjkhstat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
22811532Sjkh{
22911532Sjkh	snprintf(xs->name, sizeof xs->name, "%s", name);
23011532Sjkh	gettimeofday(&xs->start, NULL);
23111532Sjkh	xs->last.tv_sec = xs->last.tv_usec = 0;
23220315Sjkh	xs->size = size;
23311532Sjkh	xs->offset = offset;
23411532Sjkh	xs->rcvd = offset;
23511532Sjkh	if (v_tty && v_level > 0)
23611532Sjkh		stat_display(xs, 1);
23711532Sjkh	else if (v_level > 0)
23811532Sjkh		fprintf(stderr, "%-46s", xs->name);
23911303Sjkh}
24024364Sjkh
24111303Sjkh/*
24211303Sjkh * Update the transfer statistics
24324364Sjkh */
24411303Sjkhstatic void
24511532Sjkhstat_update(struct xferstat *xs, off_t rcvd)
24611303Sjkh{
24720315Sjkh	xs->rcvd = rcvd;
24811532Sjkh	if (v_tty && v_level > 0)
24911532Sjkh		stat_display(xs, 0);
25011303Sjkh}
25111303Sjkh
25211303Sjkh/*
25311303Sjkh * Finalize the transfer statistics
25411303Sjkh */
25511303Sjkhstatic void
25611303Sjkhstat_end(struct xferstat *xs)
25711303Sjkh{
25824364Sjkh	gettimeofday(&xs->last, NULL);
25924645Sjkh	if (v_tty && v_level > 0) {
26024645Sjkh		stat_display(xs, 1);
26124645Sjkh		putc('\n', stderr);
26224645Sjkh	} else if (v_level > 0) {
26311303Sjkh		fprintf(stderr, "        %s %s\n",
26411303Sjkh		    stat_bytes(xs->size), stat_bps(xs));
26511303Sjkh	}
26611303Sjkh}
26720315Sjkh
26811303Sjkh/*
26914670Sjkh * Ask the user for authentication details
27011303Sjkh */
27120315Sjkhstatic int
27211303Sjkhquery_auth(struct url *URL)
27311303Sjkh{
27411303Sjkh	struct termios tios;
27514670Sjkh	tcflag_t saved_flags;
27611303Sjkh	int i, nopwd;
27711593Sjkh
27811303Sjkh	fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
27911303Sjkh	    URL->scheme, URL->host, URL->port);
28011303Sjkh
28111303Sjkh	fprintf(stderr, "Login: ");
28211303Sjkh	if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
28311303Sjkh		return (-1);
28411303Sjkh	for (i = strlen(URL->user); i >= 0; --i)
28511303Sjkh		if (URL->user[i] == '\r' || URL->user[i] == '\n')
28611303Sjkh			URL->user[i] = '\0';
28711303Sjkh
28811303Sjkh	fprintf(stderr, "Password: ");
28911303Sjkh	if (tcgetattr(STDIN_FILENO, &tios) == 0) {
29011303Sjkh		saved_flags = tios.c_lflag;
29111303Sjkh		tios.c_lflag &= ~ECHO;
29214738Sjkh		tios.c_lflag |= ECHONL|ICANON;
29314738Sjkh		tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
29414738Sjkh		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
29514738Sjkh		tios.c_lflag = saved_flags;
29614738Sjkh		tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
29714738Sjkh	} else {
29811303Sjkh		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
29914738Sjkh	}
30014738Sjkh	if (nopwd)
30114738Sjkh		return (-1);
30214738Sjkh	for (i = strlen(URL->pwd); i >= 0; --i)
30314738Sjkh		if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
30414738Sjkh			URL->pwd[i] = '\0';
30511303Sjkh
30611303Sjkh	return (0);
30714738Sjkh}
30811303Sjkh
30911303Sjkh/*
31011303Sjkh * Fetch a file
31111303Sjkh */
31211303Sjkhstatic int
31311303Sjkhfetch(char *URL, const char *path)
31411303Sjkh{
31511303Sjkh	struct url *url;
31611303Sjkh	struct url_stat us;
31711303Sjkh	struct stat sb, nsb;
31811303Sjkh	struct xferstat xs;
31911303Sjkh	FILE *f, *of;
32011303Sjkh	size_t size, wr;
32111303Sjkh	off_t count;
32211303Sjkh	char flags[8];
32311303Sjkh	const char *slash;
32411303Sjkh	char *tmppath;
32511303Sjkh	int r;
32611303Sjkh	unsigned timeout;
32711303Sjkh	char *ptr;
32811303Sjkh
32911303Sjkh	f = of = NULL;
33011303Sjkh	tmppath = NULL;
33111303Sjkh
33211303Sjkh	timeout = 0;
33311303Sjkh	*flags = 0;
33411303Sjkh	count = 0;
33511303Sjkh
33611303Sjkh	/* set verbosity level */
33711303Sjkh	if (v_level > 1)
33811303Sjkh		strcat(flags, "v");
33911303Sjkh	if (v_level > 2)
34011303Sjkh		fetchDebug = 1;
34111303Sjkh
34211303Sjkh	/* parse URL */
34311303Sjkh	if ((url = fetchParseURL(URL)) == NULL) {
34411593Sjkh		warnx("%s: parse error", URL);
34511593Sjkh		goto failure;
34611303Sjkh	}
34711303Sjkh
34811303Sjkh	/* if no scheme was specified, take a guess */
34911303Sjkh	if (!*url->scheme) {
35011303Sjkh		if (!*url->host)
35111303Sjkh			strcpy(url->scheme, SCHEME_FILE);
35211593Sjkh		else if (strncasecmp(url->host, "ftp.", 4) == 0)
35311593Sjkh			strcpy(url->scheme, SCHEME_FTP);
35411303Sjkh		else if (strncasecmp(url->host, "www.", 4) == 0)
35511303Sjkh			strcpy(url->scheme, SCHEME_HTTP);
35611303Sjkh	}
35711303Sjkh
35811303Sjkh	/* common flags */
35915788Sjkh	switch (family) {
36011303Sjkh	case PF_INET:
36111303Sjkh		strcat(flags, "4");
36211303Sjkh		break;
36315788Sjkh	case PF_INET6:
36415788Sjkh		strcat(flags, "6");
36515788Sjkh		break;
36611303Sjkh	}
36715788Sjkh
36815788Sjkh	/* FTP specific flags */
36911303Sjkh	if (strcmp(url->scheme, SCHEME_FTP) == 0) {
37011303Sjkh		if (p_flag)
37111303Sjkh			strcat(flags, "p");
37211303Sjkh		if (d_flag)
37315242Sjkh			strcat(flags, "d");
37415242Sjkh		if (U_flag)
37515242Sjkh			strcat(flags, "l");
37615242Sjkh		timeout = T_secs ? T_secs : ftp_timeout;
37711303Sjkh	}
37811303Sjkh
37911303Sjkh	/* HTTP specific flags */
38011303Sjkh	if (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
38111303Sjkh	    strcmp(url->scheme, SCHEME_HTTPS) == 0) {
38211303Sjkh		if (d_flag)
38311303Sjkh			strcat(flags, "d");
38411303Sjkh		if (A_flag)
38511303Sjkh			strcat(flags, "A");
38611303Sjkh		timeout = T_secs ? T_secs : http_timeout;
38711303Sjkh		if (i_flag) {
38815468Sjkh			if (stat(i_filename, &sb)) {
38911303Sjkh				warn("%s: stat()", i_filename);
39011303Sjkh				goto failure;
39111303Sjkh			}
39215468Sjkh			url->ims_time = sb.st_mtime;
39311303Sjkh			strcat(flags, "i");
39411303Sjkh		}
39511303Sjkh	}
39611303Sjkh
39711303Sjkh	/* set the protocol timeout. */
39811303Sjkh	fetchTimeout = timeout;
39911303Sjkh
40011303Sjkh	/* just print size */
40111303Sjkh	if (s_flag) {
40211303Sjkh		if (timeout)
40311303Sjkh			alarm(timeout);
40411303Sjkh		r = fetchStat(url, &us, flags);
40511303Sjkh		if (timeout)
40611303Sjkh			alarm(0);
40715242Sjkh		if (sigalrm || sigint)
40815242Sjkh			goto signal;
40911479Sjkh		if (r == -1) {
41015242Sjkh			warnx("%s", fetchLastErrString);
41116204Sjkh			goto failure;
41211520Sjkh		}
41316204Sjkh		if (us.size == -1)
41416203Sjkh			printf("Unknown\n");
41516688Sjkh		else
41615242Sjkh			printf("%jd\n", (intmax_t)us.size);
41715242Sjkh		goto success;
41815242Sjkh	}
41911479Sjkh
42015242Sjkh	/*
42115242Sjkh	 * If the -r flag was specified, we have to compare the local
42215242Sjkh	 * and remote files, so we should really do a fetchStat()
42315477Sjkh	 * first, but I know of at least one HTTP server that only
42415242Sjkh	 * sends the content size in response to GET requests, and
42515242Sjkh	 * leaves it out of replies to HEAD requests.  Also, in the
42615242Sjkh	 * (frequent) case that the local and remote files match but
42715477Sjkh	 * the local file is truncated, we have sufficient information
42815477Sjkh	 * before the compare to issue a correct request.  Therefore,
42915242Sjkh	 * we always issue a GET request as if we were sure the local
43015242Sjkh	 * file was a truncated copy of the remote file; we can drop
43115242Sjkh	 * the connection later if we change our minds.
43216688Sjkh	 */
43316204Sjkh	sb.st_size = -1;
43415242Sjkh	if (!o_stdout) {
43516204Sjkh		r = stat(path, &sb);
43616204Sjkh		if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
43716204Sjkh			url->offset = sb.st_size;
43816204Sjkh		} else if (r == -1 || !S_ISREG(sb.st_mode)) {
43916204Sjkh			/*
44022105Sjkh			 * Whatever value sb.st_size has now is either
44122105Sjkh			 * wrong (if stat(2) failed) or irrelevant (if the
44222105Sjkh			 * path does not refer to a regular file)
44322105Sjkh			 */
44422105Sjkh			sb.st_size = -1;
44522105Sjkh		}
44622105Sjkh		if (r == -1 && errno != ENOENT) {
44722105Sjkh			warnx("%s: stat()", path);
44811520Sjkh			goto failure;
44922102Sjkh		}
45015468Sjkh	}
45115242Sjkh
45211520Sjkh	/* start the transfer */
45315477Sjkh	if (timeout)
45411479Sjkh		alarm(timeout);
45515242Sjkh	f = fetchXGet(url, &us, flags);
45615242Sjkh	if (timeout)
45715242Sjkh		alarm(0);
45815242Sjkh	if (sigalrm || sigint)
45915242Sjkh		goto signal;
46015477Sjkh	if (f == NULL) {
46115242Sjkh		warnx("%s: %s", URL, fetchLastErrString);
46215477Sjkh		if (i_flag && strcmp(url->scheme, SCHEME_HTTP) == 0
46311479Sjkh		    && fetchLastErrCode == FETCH_OK
46411479Sjkh		    && strcmp(fetchLastErrString, "Not Modified") == 0) {
46515242Sjkh			/* HTTP Not Modified Response, return OK. */
46615242Sjkh			r = 0;
46715242Sjkh			goto done;
46816688Sjkh		} else
46916688Sjkh			goto failure;
47016688Sjkh	}
47116688Sjkh	if (sigint)
47216688Sjkh		goto signal;
47315242Sjkh
47415242Sjkh	/* check that size is as expected */
47511303Sjkh	if (S_size) {
47611303Sjkh		if (us.size == -1) {
47711303Sjkh			warnx("%s: size unknown", URL);
47811593Sjkh		} else if (us.size != S_size) {
47911303Sjkh			warnx("%s: size mismatch: expected %jd, actual %jd",
48015242Sjkh			    URL, (intmax_t)S_size, (intmax_t)us.size);
48115242Sjkh			goto failure;
48211479Sjkh		}
48315355Sjkh	}
48411303Sjkh
48511479Sjkh	/* symlink instead of copy */
48611479Sjkh	if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
48711479Sjkh		if (symlink(url->doc, path) == -1) {
48815355Sjkh			warn("%s: symlink()", path);
48911593Sjkh			goto failure;
49011479Sjkh		}
49111593Sjkh		goto success;
49211593Sjkh	}
49316688Sjkh
49411303Sjkh	if (us.size == -1 && !o_stdout && v_level > 0)
49511593Sjkh		warnx("%s: size of remote file is not known", URL);
49611479Sjkh	if (v_level > 1) {
49711593Sjkh		if (sb.st_size != -1)
49811593Sjkh			fprintf(stderr, "local size / mtime: %jd / %ld\n",
49911479Sjkh			    (intmax_t)sb.st_size, (long)sb.st_mtime);
50011303Sjkh		if (us.size != -1)
50111593Sjkh			fprintf(stderr, "remote size / mtime: %jd / %ld\n",
50211303Sjkh			    (intmax_t)us.size, (long)us.mtime);
50315355Sjkh	}
50415242Sjkh
50511303Sjkh	/* open output file */
50611479Sjkh	if (o_stdout) {
50711303Sjkh		/* output to stdout */
50811479Sjkh		of = stdout;
50912129Sjkh	} else if (r_flag && sb.st_size != -1) {
51024235Sjkh		/* resume mode, local file exists */
51124235Sjkh		if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
51211479Sjkh			/* no match! have to refetch */
51324235Sjkh			fclose(f);
51424311Sjkh			/* if precious, warn the user and give up */
51524311Sjkh			if (R_flag) {
51624235Sjkh				warnx("%s: local modification time "
51711479Sjkh				    "does not match remote", path);
51815242Sjkh				goto failure_keep;
51924235Sjkh			}
52015242Sjkh		} else if (us.size != -1) {
52111593Sjkh			if (us.size == sb.st_size)
52224235Sjkh				/* nothing to do */
52324235Sjkh				goto success;
52424235Sjkh			if (sb.st_size > us.size) {
52524235Sjkh				/* local file too long! */
52615242Sjkh				warnx("%s: local file (%jd bytes) is longer "
52715242Sjkh				    "than remote file (%jd bytes)", path,
52815242Sjkh				    (intmax_t)sb.st_size, (intmax_t)us.size);
52911479Sjkh				goto failure;
53011479Sjkh			}
53111479Sjkh			/* we got it, open local file */
53215242Sjkh			if ((of = fopen(path, "a")) == NULL) {
53315242Sjkh				warn("%s: fopen()", path);
53411479Sjkh				goto failure;
53516366Sjkh			}
53617404Sjkh			/* check that it didn't move under our feet */
53711479Sjkh			if (fstat(fileno(of), &nsb) == -1) {
53815242Sjkh				/* can't happen! */
53924235Sjkh				warn("%s: fstat()", path);
54024235Sjkh				goto failure;
54115242Sjkh			}
54211303Sjkh			if (nsb.st_dev != sb.st_dev ||
54311303Sjkh			    nsb.st_ino != nsb.st_ino ||
54411303Sjkh			    nsb.st_size != sb.st_size) {
54511303Sjkh				warnx("%s: file has changed", URL);
54611303Sjkh				fclose(of);
54711303Sjkh				of = NULL;
54811303Sjkh				sb = nsb;
54911303Sjkh			}
55011303Sjkh		}
55111303Sjkh	} else if (m_flag && sb.st_size != -1) {
55211303Sjkh		/* mirror mode, local file exists */
55311303Sjkh		if (sb.st_size == us.size && sb.st_mtime == us.mtime)
55411303Sjkh			goto success;
55511303Sjkh	}
55615355Sjkh
55711303Sjkh	if (of == NULL) {
55811303Sjkh		/*
55916366Sjkh		 * We don't yet have an output file; either this is a
56011303Sjkh		 * vanilla run with no special flags, or the local and
56115512Sjkh		 * remote files didn't match.
56215512Sjkh		 */
56315512Sjkh
56411303Sjkh		if (url->offset > 0) {
56511303Sjkh			/*
56611303Sjkh			 * We tried to restart a transfer, but for
56711487Sjkh			 * some reason gave up - so we have to restart
56814670Sjkh			 * from scratch if we want the whole file
56911303Sjkh			 */
57011303Sjkh			url->offset = 0;
57115242Sjkh			if ((f = fetchXGet(url, &us, flags)) == NULL) {
57211303Sjkh				warnx("%s: %s", URL, fetchLastErrString);
57315883Sjkh				goto failure;
57414738Sjkh			}
57514670Sjkh			if (sigint)
57614670Sjkh				goto signal;
57714670Sjkh		}
57814670Sjkh
57914738Sjkh		/* construct a temp file name */
58014670Sjkh		if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
58115242Sjkh			if ((slash = strrchr(path, '/')) == NULL)
58214670Sjkh				slash = path;
58314738Sjkh			else
58414670Sjkh				++slash;
58514738Sjkh			asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
58614670Sjkh			    (int)(slash - path), path, slash);
58714670Sjkh			if (tmppath != NULL) {
58820247Sjkh				mkstemps(tmppath, strlen(slash) + 1);
58914670Sjkh				of = fopen(tmppath, "w");
59015419Sjkh				chown(tmppath, sb.st_uid, sb.st_gid);
59114670Sjkh				chmod(tmppath, sb.st_mode & ALLPERMS);
59214670Sjkh			}
59314738Sjkh		}
59414738Sjkh		if (of == NULL)
59515419Sjkh			of = fopen(path, "w");
59614738Sjkh		if (of == NULL) {
59714738Sjkh			warn("%s: open()", path);
59814738Sjkh			goto failure;
59914738Sjkh		}
60014670Sjkh	}
60114670Sjkh	count = url->offset;
60240372Sjkh
60331150Sjkh	/* start the counter */
60431150Sjkh	stat_start(&xs, path, us.size, count);
60531150Sjkh
60631150Sjkh	sigalrm = siginfo = sigint = 0;
60731150Sjkh
60825481Sjkh	/* suck in the data */
60925481Sjkh	signal(SIGINFO, sig_handler);
61025481Sjkh	while (!sigint) {
61125481Sjkh		if (us.size != -1 && us.size - count < B_size &&
61214670Sjkh		    us.size - count >= 0)
61311303Sjkh			size = us.size - count;
61414670Sjkh		else
61515419Sjkh			size = B_size;
61614738Sjkh		if (siginfo) {
61711593Sjkh			stat_end(&xs);
61811303Sjkh			siginfo = 0;
619		}
620		if ((size = fread(buf, 1, size, f)) == 0) {
621			if (ferror(f) && errno == EINTR && !sigint)
622				clearerr(f);
623			else
624				break;
625		}
626		stat_update(&xs, count += size);
627		for (ptr = buf; size > 0; ptr += wr, size -= wr)
628			if ((wr = fwrite(ptr, 1, size, of)) < size) {
629				if (ferror(of) && errno == EINTR && !sigint)
630					clearerr(of);
631				else
632					break;
633			}
634		if (size != 0)
635			break;
636	}
637	if (!sigalrm)
638		sigalrm = ferror(f) && errno == ETIMEDOUT;
639	signal(SIGINFO, SIG_DFL);
640
641	stat_end(&xs);
642
643	/*
644	 * If the transfer timed out or was interrupted, we still want to
645	 * set the mtime in case the file is not removed (-r or -R) and
646	 * the user later restarts the transfer.
647	 */
648 signal:
649	/* set mtime of local file */
650	if (!n_flag && us.mtime && !o_stdout && of != NULL &&
651	    (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
652		struct timeval tv[2];
653
654		fflush(of);
655		tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
656		tv[1].tv_sec = (long)us.mtime;
657		tv[0].tv_usec = tv[1].tv_usec = 0;
658		if (utimes(tmppath ? tmppath : path, tv))
659			warn("%s: utimes()", tmppath ? tmppath : path);
660	}
661
662	/* timed out or interrupted? */
663	if (sigalrm)
664		warnx("transfer timed out");
665	if (sigint) {
666		warnx("transfer interrupted");
667		goto failure;
668	}
669
670	/* timeout / interrupt before connection completley established? */
671	if (f == NULL)
672		goto failure;
673
674	if (!sigalrm) {
675		/* check the status of our files */
676		if (ferror(f))
677			warn("%s", URL);
678		if (ferror(of))
679			warn("%s", path);
680		if (ferror(f) || ferror(of))
681			goto failure;
682	}
683
684	/* did the transfer complete normally? */
685	if (us.size != -1 && count < us.size) {
686		warnx("%s appears to be truncated: %jd/%jd bytes",
687		    path, (intmax_t)count, (intmax_t)us.size);
688		goto failure_keep;
689	}
690
691	/*
692	 * If the transfer timed out and we didn't know how much to
693	 * expect, assume the worst (i.e. we didn't get all of it)
694	 */
695	if (sigalrm && us.size == -1) {
696		warnx("%s may be truncated", path);
697		goto failure_keep;
698	}
699
700 success:
701	r = 0;
702	if (tmppath != NULL && rename(tmppath, path) == -1) {
703		warn("%s: rename()", path);
704		goto failure_keep;
705	}
706	goto done;
707 failure:
708	if (of && of != stdout && !R_flag && !r_flag)
709		if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
710			unlink(tmppath ? tmppath : path);
711	if (R_flag && tmppath != NULL && sb.st_size == -1)
712		rename(tmppath, path); /* ignore errors here */
713 failure_keep:
714	r = -1;
715	goto done;
716 done:
717	if (f)
718		fclose(f);
719	if (of && of != stdout)
720		fclose(of);
721	if (url)
722		fetchFreeURL(url);
723	if (tmppath != NULL)
724		free(tmppath);
725	return (r);
726}
727
728static void
729usage(void)
730{
731	fprintf(stderr, "%s\n%s\n%s\n%s\n",
732"usage: fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [-N file] [-o file] [-S bytes]",
733"       [-T seconds] [-w seconds] [-i file] URL ...",
734"       fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [-N file] [-o file] [-S bytes]",
735"       [-T seconds] [-w seconds] [-i file] -h host -f file [-c dir]");
736}
737
738
739/*
740 * Entry point
741 */
742int
743main(int argc, char *argv[])
744{
745	struct stat sb;
746	struct sigaction sa;
747	const char *p, *s;
748	char *end, *q;
749	int c, e, r;
750
751	while ((c = getopt(argc, argv,
752	    "146AaB:bc:dFf:Hh:i:lMmN:nPpo:qRrS:sT:tUvw:")) != -1)
753		switch (c) {
754		case '1':
755			once_flag = 1;
756			break;
757		case '4':
758			family = PF_INET;
759			break;
760		case '6':
761			family = PF_INET6;
762			break;
763		case 'A':
764			A_flag = 1;
765			break;
766		case 'a':
767			a_flag = 1;
768			break;
769		case 'B':
770			B_size = (off_t)strtol(optarg, &end, 10);
771			if (*optarg == '\0' || *end != '\0')
772				errx(1, "invalid buffer size (%s)", optarg);
773			break;
774		case 'b':
775			warnx("warning: the -b option is deprecated");
776			b_flag = 1;
777			break;
778		case 'c':
779			c_dirname = optarg;
780			break;
781		case 'd':
782			d_flag = 1;
783			break;
784		case 'F':
785			F_flag = 1;
786			break;
787		case 'f':
788			f_filename = optarg;
789			break;
790		case 'H':
791			warnx("the -H option is now implicit, "
792			    "use -U to disable");
793			break;
794		case 'h':
795			h_hostname = optarg;
796			break;
797		case 'i':
798			i_flag = 1;
799			i_filename = optarg;
800			break;
801		case 'l':
802			l_flag = 1;
803			break;
804		case 'o':
805			o_flag = 1;
806			o_filename = optarg;
807			break;
808		case 'M':
809		case 'm':
810			if (r_flag)
811				errx(1, "the -m and -r flags "
812				    "are mutually exclusive");
813			m_flag = 1;
814			break;
815		case 'N':
816			N_filename = optarg;
817			break;
818		case 'n':
819			n_flag = 1;
820			break;
821		case 'P':
822		case 'p':
823			p_flag = 1;
824			break;
825		case 'q':
826			v_level = 0;
827			break;
828		case 'R':
829			R_flag = 1;
830			break;
831		case 'r':
832			if (m_flag)
833				errx(1, "the -m and -r flags "
834				    "are mutually exclusive");
835			r_flag = 1;
836			break;
837		case 'S':
838			S_size = (off_t)strtol(optarg, &end, 10);
839			if (*optarg == '\0' || *end != '\0')
840				errx(1, "invalid size (%s)", optarg);
841			break;
842		case 's':
843			s_flag = 1;
844			break;
845		case 'T':
846			T_secs = strtol(optarg, &end, 10);
847			if (*optarg == '\0' || *end != '\0')
848				errx(1, "invalid timeout (%s)", optarg);
849			break;
850		case 't':
851			t_flag = 1;
852			warnx("warning: the -t option is deprecated");
853			break;
854		case 'U':
855			U_flag = 1;
856			break;
857		case 'v':
858			v_level++;
859			break;
860		case 'w':
861			a_flag = 1;
862			w_secs = strtol(optarg, &end, 10);
863			if (*optarg == '\0' || *end != '\0')
864				errx(1, "invalid delay (%s)", optarg);
865			break;
866		default:
867			usage();
868			exit(1);
869		}
870
871	argc -= optind;
872	argv += optind;
873
874	if (h_hostname || f_filename || c_dirname) {
875		if (!h_hostname || !f_filename || argc) {
876			usage();
877			exit(1);
878		}
879		/* XXX this is a hack. */
880		if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
881			errx(1, "invalid hostname");
882		if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
883		    c_dirname ? c_dirname : "", f_filename) == -1)
884			errx(1, "%s", strerror(ENOMEM));
885		argc++;
886	}
887
888	if (!argc) {
889		usage();
890		exit(1);
891	}
892
893	/* allocate buffer */
894	if (B_size < MINBUFSIZE)
895		B_size = MINBUFSIZE;
896	if ((buf = malloc(B_size)) == NULL)
897		errx(1, "%s", strerror(ENOMEM));
898
899	/* timeouts */
900	if ((s = getenv("FTP_TIMEOUT")) != NULL) {
901		ftp_timeout = strtol(s, &end, 10);
902		if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
903			warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
904			ftp_timeout = 0;
905		}
906	}
907	if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
908		http_timeout = strtol(s, &end, 10);
909		if (*s == '\0' || *end != '\0' || http_timeout < 0) {
910			warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
911			http_timeout = 0;
912		}
913	}
914
915	/* signal handling */
916	sa.sa_flags = 0;
917	sa.sa_handler = sig_handler;
918	sigemptyset(&sa.sa_mask);
919	sigaction(SIGALRM, &sa, NULL);
920	sa.sa_flags = SA_RESETHAND;
921	sigaction(SIGINT, &sa, NULL);
922	fetchRestartCalls = 0;
923
924	/* output file */
925	if (o_flag) {
926		if (strcmp(o_filename, "-") == 0) {
927			o_stdout = 1;
928		} else if (stat(o_filename, &sb) == -1) {
929			if (errno == ENOENT) {
930				if (argc > 1)
931					errx(1, "%s is not a directory",
932					    o_filename);
933			} else {
934				err(1, "%s", o_filename);
935			}
936		} else {
937			if (sb.st_mode & S_IFDIR)
938				o_directory = 1;
939		}
940	}
941
942	/* check if output is to a tty (for progress report) */
943	v_tty = isatty(STDERR_FILENO);
944	if (v_tty)
945		pgrp = getpgrp();
946
947	r = 0;
948
949	/* authentication */
950	if (v_tty)
951		fetchAuthMethod = query_auth;
952	if (N_filename != NULL)
953		setenv("NETRC", N_filename, 1);
954
955	while (argc) {
956		if ((p = strrchr(*argv, '/')) == NULL)
957			p = *argv;
958		else
959			p++;
960
961		if (!*p)
962			p = "fetch.out";
963
964		fetchLastErrCode = 0;
965
966		if (o_flag) {
967			if (o_stdout) {
968				e = fetch(*argv, "-");
969			} else if (o_directory) {
970				asprintf(&q, "%s/%s", o_filename, p);
971				e = fetch(*argv, q);
972				free(q);
973			} else {
974				e = fetch(*argv, o_filename);
975			}
976		} else {
977			e = fetch(*argv, p);
978		}
979
980		if (sigint)
981			kill(getpid(), SIGINT);
982
983		if (e == 0 && once_flag)
984			exit(0);
985
986		if (e) {
987			r = 1;
988			if ((fetchLastErrCode
989			    && fetchLastErrCode != FETCH_UNAVAIL
990			    && fetchLastErrCode != FETCH_MOVED
991			    && fetchLastErrCode != FETCH_URL
992			    && fetchLastErrCode != FETCH_RESOLV
993			    && fetchLastErrCode != FETCH_UNKNOWN)) {
994				if (w_secs && v_level)
995					fprintf(stderr, "Waiting %ld seconds "
996					    "before retrying\n", w_secs);
997				if (w_secs)
998					sleep(w_secs);
999				if (a_flag)
1000					continue;
1001			}
1002		}
1003
1004		argc--, argv++;
1005	}
1006
1007	exit(r);
1008}
1009