Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.23
      1 /*	$NetBSD: fetch.c,v 1.23 1998/07/10 04:39:04 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason Thorpe and Luke Mewburn.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __RCSID("$NetBSD: fetch.c,v 1.23 1998/07/10 04:39:04 thorpej Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * FTP User Program -- Command line file retrieval
     46  */
     47 
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/socket.h>
     51 
     52 #include <netinet/in.h>
     53 
     54 #include <arpa/ftp.h>
     55 #include <arpa/inet.h>
     56 
     57 #include <ctype.h>
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <netdb.h>
     61 #include <fcntl.h>
     62 #include <signal.h>
     63 #include <stdio.h>
     64 #include <stdlib.h>
     65 #include <string.h>
     66 #include <unistd.h>
     67 
     68 #include "ftp_var.h"
     69 
     70 static int	url_get __P((const char *, const char *, const char *));
     71 void    	aborthttp __P((int));
     72 
     73 
     74 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
     75 #define	HTTP_URL	"http://"	/* http URL prefix */
     76 #define FTP_PROXY	"ftp_proxy"	/* env var with ftp proxy location */
     77 #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
     78 
     79 
     80 #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
     81 
     82 jmp_buf	httpabort;
     83 
     84 /*
     85  * Retrieve URL, via the proxy in $proxyvar if necessary.
     86  * Modifies the string argument given.
     87  * Returns -1 on failure, 0 on success
     88  */
     89 static int
     90 url_get(origline, proxyenv, outfile)
     91 	const char *origline;
     92 	const char *proxyenv;
     93 	const char *outfile;
     94 {
     95 	struct sockaddr_in sin;
     96 	int i, out, isftpurl;
     97 	in_port_t port;
     98 	volatile int s;
     99 	size_t len;
    100 	char c, *cp, *ep, *portnum, *path, buf[4096];
    101 	const char *savefile;
    102 	char *line, *proxy, *host;
    103 	volatile sig_t oldintr, oldintp;
    104 	off_t hashbytes;
    105 	struct hostent *hp = NULL;
    106 	int (*closefunc) __P((FILE *));
    107 	FILE *fout;
    108 
    109 	closefunc = NULL;
    110 	fout = NULL;
    111 	s = -1;
    112 	proxy = NULL;
    113 	isftpurl = 0;
    114 
    115 #ifdef __GNUC__			/* to shut up gcc warnings */
    116 	(void)&closefunc;
    117 	(void)&fout;
    118 	(void)&proxy;
    119 	(void)&savefile;
    120 #endif
    121 
    122 	line = strdup(origline);
    123 	if (line == NULL)
    124 		errx(1, "Can't allocate memory to parse URL");
    125 	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
    126 		host = line + sizeof(HTTP_URL) - 1;
    127 	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    128 		host = line + sizeof(FTP_URL) - 1;
    129 		isftpurl = 1;
    130 	} else
    131 		errx(1, "url_get: Invalid URL '%s'", line);
    132 
    133 	path = strchr(host, '/');		/* find path */
    134 	if (EMPTYSTRING(path)) {
    135 		if (isftpurl)
    136 			goto noftpautologin;
    137 		warnx("Invalid URL (no `/' after host): %s", origline);
    138 		goto cleanup_url_get;
    139 	}
    140 	*path++ = '\0';
    141 	if (EMPTYSTRING(path)) {
    142 		if (isftpurl)
    143 			goto noftpautologin;
    144 		warnx("Invalid URL (no file after host): %s", origline);
    145 		goto cleanup_url_get;
    146 	}
    147 
    148 	if (outfile)
    149 		savefile = outfile;
    150 	else {
    151 		savefile = strrchr(path, '/');		/* find savefile */
    152 		if (savefile != NULL)
    153 			savefile++;
    154 		else
    155 			savefile = path;
    156 	}
    157 	if (EMPTYSTRING(savefile)) {
    158 		if (isftpurl)
    159 			goto noftpautologin;
    160 		warnx("Invalid URL (no file after directory): %s", origline);
    161 		goto cleanup_url_get;
    162 	}
    163 
    164 	if (proxyenv != NULL) {				/* use proxy */
    165 		proxy = strdup(proxyenv);
    166 		if (proxy == NULL)
    167 			errx(1, "Can't allocate memory for proxy URL.");
    168 		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
    169 			host = proxy + sizeof(HTTP_URL) - 1;
    170 		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
    171 			host = proxy + sizeof(FTP_URL) - 1;
    172 		else {
    173 			warnx("Malformed proxy URL: %s", proxyenv);
    174 			goto cleanup_url_get;
    175 		}
    176 		if (EMPTYSTRING(host)) {
    177 			warnx("Malformed proxy URL: %s", proxyenv);
    178 			goto cleanup_url_get;
    179 		}
    180 		*--path = '/';			/* add / back to real path */
    181 		path = strchr(host, '/');	/* remove trailing / on host */
    182 		if (! EMPTYSTRING(path))
    183 			*path++ = '\0';
    184 		path = line;
    185 	}
    186 
    187 	portnum = strchr(host, ':');			/* find portnum */
    188 	if (portnum != NULL)
    189 		*portnum++ = '\0';
    190 
    191 	if (debug)
    192 		fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
    193 		    host, portnum, path, savefile);
    194 
    195 	memset(&sin, 0, sizeof(sin));
    196 	sin.sin_family = AF_INET;
    197 
    198 	if (isdigit((unsigned char)host[0])) {
    199 		if (inet_aton(host, &sin.sin_addr) == 0) {
    200 			warnx("Invalid IP address: %s", host);
    201 			goto cleanup_url_get;
    202 		}
    203 	} else {
    204 		hp = gethostbyname(host);
    205 		if (hp == NULL) {
    206 			warnx("%s: %s", host, hstrerror(h_errno));
    207 			goto cleanup_url_get;
    208 		}
    209 		if (hp->h_addrtype != AF_INET) {
    210 			warnx("%s: not an Internet address?", host);
    211 			goto cleanup_url_get;
    212 		}
    213 		memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    214 	}
    215 
    216 	if (! EMPTYSTRING(portnum)) {
    217 		char *ep;
    218 		long nport;
    219 
    220 		nport = strtol(portnum, &ep, 10);
    221 		if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
    222 			warnx("Invalid port: %s", portnum);
    223 			goto cleanup_url_get;
    224 		}
    225 		port = htons((in_port_t)nport);
    226 	} else
    227 		port = httpport;
    228 	sin.sin_port = port;
    229 
    230 	s = socket(AF_INET, SOCK_STREAM, 0);
    231 	if (s == -1) {
    232 		warn("Can't create socket");
    233 		goto cleanup_url_get;
    234 	}
    235 
    236 	while (xconnect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    237 		if (errno == EINTR)
    238 			continue;
    239 		if (hp && hp->h_addr_list[1]) {
    240 			int oerrno = errno;
    241 			char *ia;
    242 
    243 			ia = inet_ntoa(sin.sin_addr);
    244 			errno = oerrno;
    245 			warn("connect to address %s", ia);
    246 			hp->h_addr_list++;
    247 			memcpy(&sin.sin_addr, hp->h_addr_list[0],
    248 			    (size_t)hp->h_length);
    249 			fprintf(ttyout, "Trying %s...\n",
    250 			    inet_ntoa(sin.sin_addr));
    251 			(void)close(s);
    252 			s = socket(AF_INET, SOCK_STREAM, 0);
    253 			if (s < 0) {
    254 				warn("socket");
    255 				goto cleanup_url_get;
    256 			}
    257 			continue;
    258 		}
    259 		warn("Can't connect to %s", host);
    260 		goto cleanup_url_get;
    261 	}
    262 
    263 	/*
    264 	 * Construct and send the request.  We're expecting a return
    265 	 * status of "200". Proxy requests don't want leading /.
    266 	 */
    267 	if (!proxy) {
    268 		fprintf(ttyout, "Requesting %s\n", origline);
    269 		len = snprintf(buf, sizeof(buf),
    270 		    "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
    271 	} else {
    272 		fprintf(ttyout, "Requesting %s (via %s)\n", origline, proxyenv);
    273 		len = snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n",
    274 		    path);
    275 	}
    276 	if (write(s, buf, len) < len) {
    277 		warn("Writing HTTP request");
    278 		goto cleanup_url_get;
    279 	}
    280 	memset(buf, 0, sizeof(buf));
    281 	for (cp = buf; cp < buf + sizeof(buf); ) {
    282 		if (read(s, cp, 1) != 1)
    283 			goto improper;
    284 		if (*cp == '\r')
    285 			continue;
    286 		if (*cp == '\n')
    287 			break;
    288 		cp++;
    289 	}
    290 	buf[sizeof(buf) - 1] = '\0';		/* sanity */
    291 	cp = strchr(buf, ' ');
    292 	if (cp == NULL)
    293 		goto improper;
    294 	else
    295 		cp++;
    296 	if (strncmp(cp, "200", 3)) {
    297 		warnx("Error retrieving file: %s", cp);
    298 		goto cleanup_url_get;
    299 	}
    300 
    301 			/* Read the rest of the header. */
    302 	memset(buf, 0, sizeof(buf));
    303 	c = '\0';
    304 	for (cp = buf; cp < buf + sizeof(buf); ) {
    305 		if (read(s, cp, 1) != 1)
    306 			goto improper;
    307 		if (*cp == '\r')
    308 			continue;
    309 		if (*cp == '\n' && c == '\n')
    310 			break;
    311 		c = *cp;
    312 		cp++;
    313 	}
    314 	buf[sizeof(buf) - 1] = '\0';		/* sanity */
    315 
    316 			/* Look for the "Content-length: " header. */
    317 #define CONTENTLEN "Content-Length: "
    318 	for (cp = buf; *cp != '\0'; cp++) {
    319 		if (tolower(*cp) == 'c' &&
    320 		    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
    321 			break;
    322 	}
    323 	if (*cp != '\0') {
    324 		cp += sizeof(CONTENTLEN) - 1;
    325 		ep = strchr(cp, '\n');
    326 		if (ep == NULL)
    327 			goto improper;
    328 		else
    329 			*ep = '\0';
    330 		filesize = strtol(cp, &ep, 10);
    331 		if (filesize < 1 || *ep != '\0')
    332 			goto improper;
    333 	} else
    334 		filesize = -1;
    335 
    336 	oldintr = oldintp = NULL;
    337 
    338 			/* Open the output file. */
    339 	if (strcmp(savefile, "-") == 0) {
    340 		fout = stdout;
    341 	} else if (*savefile == '|') {
    342 		oldintp = signal(SIGPIPE, SIG_IGN);
    343 		fout = popen(savefile + 1, "w");
    344 		if (fout == NULL) {
    345 			warn("Can't run %s", savefile + 1);
    346 			goto cleanup_url_get;
    347 		}
    348 		closefunc = pclose;
    349 	} else {
    350 		fout = fopen(savefile, "w");
    351 		if (fout == NULL) {
    352 			warn("Can't open %s", savefile);
    353 			goto cleanup_url_get;
    354 		}
    355 		closefunc = fclose;
    356 	}
    357 
    358 			/* Trap signals */
    359 	if (setjmp(httpabort)) {
    360 		if (oldintr)
    361 			(void)signal(SIGINT, oldintr);
    362 		if (oldintp)
    363 			(void)signal(SIGPIPE, oldintp);
    364 		goto cleanup_url_get;
    365 	}
    366 	oldintr = signal(SIGINT, aborthttp);
    367 
    368 	bytes = 0;
    369 	hashbytes = mark;
    370 	progressmeter(-1);
    371 
    372 	/* Finally, suck down the file. */
    373 	i = 0;
    374 	out = fileno(fout);
    375 	while ((len = read(s, buf, sizeof(buf))) > 0) {
    376 		bytes += len;
    377 		for (cp = buf; len > 0; len -= i, cp += i) {
    378 			if ((i = write(out, cp, len)) == -1) {
    379 				warn("Writing %s", savefile);
    380 				goto cleanup_url_get;
    381 			}
    382 			else if (i == 0)
    383 				break;
    384 		}
    385 		if (hash && !progress) {
    386 			while (bytes >= hashbytes) {
    387 				(void)putc('#', ttyout);
    388 				hashbytes += mark;
    389 			}
    390 			(void)fflush(ttyout);
    391 		}
    392 	}
    393 	if (hash && !progress && bytes > 0) {
    394 		if (bytes < mark)
    395 			(void)putc('#', ttyout);
    396 		(void)putc('\n', ttyout);
    397 		(void)fflush(ttyout);
    398 	}
    399 	if (len != 0) {
    400 		warn("Reading from socket");
    401 		goto cleanup_url_get;
    402 	}
    403 	progressmeter(1);
    404 	if (verbose)
    405 		fputs("Successfully retrieved file.\n", ttyout);
    406 	(void)signal(SIGINT, oldintr);
    407 	if (oldintp)
    408 		(void)signal(SIGPIPE, oldintp);
    409 
    410 	resetsockbufsize();
    411 	close(s);
    412 	if (closefunc != NULL)
    413 		(*closefunc)(fout);
    414 	if (proxy)
    415 		free(proxy);
    416 	free(line);
    417 	return (0);
    418 
    419 noftpautologin:
    420 	warnx(
    421 	    "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
    422 	goto cleanup_url_get;
    423 
    424 improper:
    425 	warnx("Improper response from %s", host);
    426 
    427 cleanup_url_get:
    428 	resetsockbufsize();
    429 	if (s != -1)
    430 		close(s);
    431 	if (closefunc != NULL && fout != NULL)
    432 		(*closefunc)(fout);
    433 	if (proxy)
    434 		free(proxy);
    435 	free(line);
    436 	return (-1);
    437 }
    438 
    439 /*
    440  * Abort a http retrieval
    441  */
    442 void
    443 aborthttp(notused)
    444 	int notused;
    445 {
    446 
    447 	alarmtimer(0);
    448 	fputs("\nhttp fetch aborted.\n", ttyout);
    449 	(void)fflush(ttyout);
    450 	longjmp(httpabort, 1);
    451 }
    452 
    453 /*
    454  * Retrieve multiple files from the command line, transferring
    455  * files of the form "host:path", "ftp://host/path" using the
    456  * ftp protocol, and files of the form "http://host/path" using
    457  * the http protocol.
    458  * If path has a trailing "/", then return (-1);
    459  * the path will be cd-ed into and the connection remains open,
    460  * and the function will return -1 (to indicate the connection
    461  * is alive).
    462  * If an error occurs the return value will be the offset+1 in
    463  * argv[] of the file that caused a problem (i.e, argv[x]
    464  * returns x+1)
    465  * Otherwise, 0 is returned if all files retrieved successfully.
    466  */
    467 int
    468 auto_fetch(argc, argv, outfile)
    469 	int argc;
    470 	char *argv[];
    471 	char *outfile;
    472 {
    473 	static char lasthost[MAXHOSTNAMELEN];
    474 	char *xargv[5];
    475 	char *cp, *line, *host, *dir, *file, *portnum;
    476 	char *user, *pass;
    477 	char *ftpproxy, *httpproxy;
    478 	int rval, xargc;
    479 	volatile int argpos;
    480 	int dirhasglob, filehasglob;
    481 	char rempath[MAXPATHLEN];
    482 
    483 #ifdef __GNUC__			/* to shut up gcc warnings */
    484 	(void)&outfile;
    485 #endif
    486 
    487 	argpos = 0;
    488 
    489 	if (setjmp(toplevel)) {
    490 		if (connected)
    491 			disconnect(0, NULL);
    492 		return (argpos + 1);
    493 	}
    494 	(void)signal(SIGINT, (sig_t)intr);
    495 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    496 
    497 	ftpproxy = getenv(FTP_PROXY);
    498 	httpproxy = getenv(HTTP_PROXY);
    499 
    500 	/*
    501 	 * Loop through as long as there's files to fetch.
    502 	 */
    503 	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
    504 		if (strchr(argv[argpos], ':') == NULL)
    505 			break;
    506 		host = dir = file = portnum = user = pass = NULL;
    507 
    508 		/*
    509 		 * We muck with the string, so we make a copy.
    510 		 */
    511 		line = strdup(argv[argpos]);
    512 		if (line == NULL)
    513 			errx(1, "Can't allocate memory for auto-fetch.");
    514 
    515 		/*
    516 		 * Try HTTP URL-style arguments first.
    517 		 */
    518 		if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
    519 			if (url_get(line, httpproxy, outfile) == -1)
    520 				rval = argpos + 1;
    521 			continue;
    522 		}
    523 
    524 		/*
    525 		 * Try FTP URL-style arguments next. If ftpproxy is
    526 		 * set, use url_get() instead of standard ftp.
    527 		 * Finally, try host:file.
    528 		 */
    529 		host = line;
    530 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    531 			if (ftpproxy) {
    532 				if (url_get(line, ftpproxy, outfile) == -1)
    533 					rval = argpos + 1;
    534 				continue;
    535 			}
    536 			host += sizeof(FTP_URL) - 1;
    537 			dir = strchr(host, '/');
    538 
    539 				/* look for [user:pass@]host[:port] */
    540 			pass = strpbrk(host, ":@/");
    541 			if (pass == NULL || *pass == '/') {
    542 				pass = NULL;
    543 				goto parsed_url;
    544 			}
    545 			if (pass == host || *pass == '@') {
    546 bad_ftp_url:
    547 				warnx("Invalid URL: %s", argv[argpos]);
    548 				rval = argpos + 1;
    549 				continue;
    550 			}
    551 			*pass++ = '\0';
    552 			cp = strpbrk(pass, ":@/");
    553 			if (cp == NULL || *cp == '/') {
    554 				portnum = pass;
    555 				pass = NULL;
    556 				goto parsed_url;
    557 			}
    558 			if (EMPTYSTRING(cp) || *cp == ':')
    559 				goto bad_ftp_url;
    560 			*cp++ = '\0';
    561 			user = host;
    562 			if (EMPTYSTRING(user))
    563 				goto bad_ftp_url;
    564 			host = cp;
    565 			portnum = strchr(host, ':');
    566 			if (portnum != NULL)
    567 				*portnum++ = '\0';
    568 		} else {			/* classic style `host:file' */
    569 			dir = strchr(host, ':');
    570 		}
    571 parsed_url:
    572 		if (EMPTYSTRING(host)) {
    573 			rval = argpos + 1;
    574 			continue;
    575 		}
    576 
    577 		/*
    578 		 * If dir is NULL, the file wasn't specified
    579 		 * (URL looked something like ftp://host)
    580 		 */
    581 		if (dir != NULL)
    582 			*dir++ = '\0';
    583 
    584 		/*
    585 		 * Extract the file and (if present) directory name.
    586 		 */
    587 		if (! EMPTYSTRING(dir)) {
    588 			cp = strrchr(dir, '/');
    589 			if (cp != NULL) {
    590 				*cp++ = '\0';
    591 				file = cp;
    592 			} else {
    593 				file = dir;
    594 				dir = NULL;
    595 			}
    596 		}
    597 		if (debug)
    598 			fprintf(ttyout,
    599 			    "user %s:%s host %s port %s dir %s file %s\n",
    600 			    user, pass, host, portnum, dir, file);
    601 
    602 		/*
    603 		 * Set up the connection if we don't have one.
    604 		 */
    605 		if (strcmp(host, lasthost) != 0) {
    606 			int oautologin;
    607 
    608 			(void)strcpy(lasthost, host);
    609 			if (connected)
    610 				disconnect(0, NULL);
    611 			xargv[0] = __progname;
    612 			xargv[1] = host;
    613 			xargv[2] = NULL;
    614 			xargc = 2;
    615 			if (! EMPTYSTRING(portnum)) {
    616 				xargv[2] = portnum;
    617 				xargv[3] = NULL;
    618 				xargc = 3;
    619 			}
    620 			oautologin = autologin;
    621 			if (user != NULL)
    622 				autologin = 0;
    623 			setpeer(xargc, xargv);
    624 			autologin = oautologin;
    625 			if ((connected == 0)
    626 			 || ((connected == 1) && !login(host, user, pass)) ) {
    627 				warnx("Can't connect or login to host `%s'",
    628 				    host);
    629 				rval = argpos + 1;
    630 				continue;
    631 			}
    632 
    633 			/* Always use binary transfers. */
    634 			setbinary(0, NULL);
    635 		}
    636 			/* cd back to '/' */
    637 		xargv[0] = "cd";
    638 		xargv[1] = "/";
    639 		xargv[2] = NULL;
    640 		cd(2, xargv);
    641 		if (! dirchange) {
    642 			rval = argpos + 1;
    643 			continue;
    644 		}
    645 
    646 		dirhasglob = filehasglob = 0;
    647 		if (doglob) {
    648 			if (! EMPTYSTRING(dir) &&
    649 			    strpbrk(dir, "*?[]{}") != NULL)
    650 				dirhasglob = 1;
    651 			if (! EMPTYSTRING(file) &&
    652 			    strpbrk(file, "*?[]{}") != NULL)
    653 				filehasglob = 1;
    654 		}
    655 
    656 		/* Change directories, if necessary. */
    657 		if (! EMPTYSTRING(dir) && !dirhasglob) {
    658 			xargv[0] = "cd";
    659 			xargv[1] = dir;
    660 			xargv[2] = NULL;
    661 			cd(2, xargv);
    662 			if (! dirchange) {
    663 				rval = argpos + 1;
    664 				continue;
    665 			}
    666 		}
    667 
    668 		if (EMPTYSTRING(file)) {
    669 			rval = -1;
    670 			continue;
    671 		}
    672 
    673 		if (!verbose)
    674 			fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "",
    675 			    file);
    676 
    677 		if (dirhasglob) {
    678 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
    679 			file = rempath;
    680 		}
    681 
    682 		/* Fetch the file(s). */
    683 		xargc = 2;
    684 		xargv[0] = "get";
    685 		xargv[1] = file;
    686 		xargv[2] = NULL;
    687 		if (dirhasglob || filehasglob) {
    688 			int ointeractive;
    689 
    690 			ointeractive = interactive;
    691 			interactive = 0;
    692 			xargv[0] = "mget";
    693 			mget(xargc, xargv);
    694 			interactive = ointeractive;
    695 		} else {
    696 			if (outfile != NULL) {
    697 				xargv[2] = outfile;
    698 				xargv[3] = NULL;
    699 				xargc++;
    700 			}
    701 			get(xargc, xargv);
    702 			if (outfile != NULL && strcmp(outfile, "-") != 0
    703 			    && outfile[0] != '|')
    704 				outfile = NULL;
    705 		}
    706 
    707 		if ((code / 100) != COMPLETE)
    708 			rval = argpos + 1;
    709 	}
    710 	if (connected && rval != -1)
    711 		disconnect(0, NULL);
    712 	return (rval);
    713 }
    714