Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.2
      1  1.2  lukem /*	$NetBSD: fetch.c,v 1.2 1997/02/01 10:45:00 lukem Exp $	*/
      2  1.1  lukem 
      3  1.1  lukem /*-
      4  1.1  lukem  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  lukem  * by Jason Thorpe and Luke Mewburn.
      9  1.1  lukem  *
     10  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1  lukem  * modification, are permitted provided that the following conditions
     12  1.1  lukem  * are met:
     13  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     14  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     15  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     18  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     19  1.1  lukem  *    must display the following acknowledgement:
     20  1.1  lukem  *        This product includes software developed by the NetBSD
     21  1.1  lukem  *        Foundation, Inc. and its contributors.
     22  1.1  lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  lukem  *    contributors may be used to endorse or promote products derived
     24  1.1  lukem  *    from this software without specific prior written permission.
     25  1.1  lukem  *
     26  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  1.1  lukem  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  lukem  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  lukem  */
     38  1.1  lukem 
     39  1.1  lukem #ifndef lint
     40  1.2  lukem static char rcsid[] = "$NetBSD: fetch.c,v 1.2 1997/02/01 10:45:00 lukem Exp $";
     41  1.1  lukem #endif /* not lint */
     42  1.1  lukem 
     43  1.1  lukem /*
     44  1.1  lukem  * FTP User Program -- Command line file retrieval
     45  1.1  lukem  */
     46  1.1  lukem 
     47  1.1  lukem #include <sys/types.h>
     48  1.1  lukem #include <sys/param.h>
     49  1.1  lukem #include <sys/socket.h>
     50  1.1  lukem 
     51  1.1  lukem #include <netinet/in.h>
     52  1.1  lukem 
     53  1.2  lukem #include <arpa/ftp.h>
     54  1.1  lukem #include <arpa/inet.h>
     55  1.1  lukem 
     56  1.1  lukem #include <ctype.h>
     57  1.1  lukem #include <err.h>
     58  1.1  lukem #include <netdb.h>
     59  1.1  lukem #include <fcntl.h>
     60  1.1  lukem #include <stdio.h>
     61  1.1  lukem #include <stdlib.h>
     62  1.1  lukem #include <string.h>
     63  1.1  lukem #include <unistd.h>
     64  1.1  lukem 
     65  1.1  lukem #include "ftp_var.h"
     66  1.1  lukem 
     67  1.1  lukem #define	FTP_URL		"ftp://"	/* ftp URL prefix */
     68  1.1  lukem #define	HTTP_URL	"http://"	/* http URL prefix */
     69  1.1  lukem #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
     70  1.1  lukem 
     71  1.1  lukem 
     72  1.1  lukem #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
     73  1.1  lukem 
     74  1.2  lukem jmp_buf	httpabort;
     75  1.2  lukem 
     76  1.1  lukem /*
     77  1.1  lukem  * Retrieve an http:// URL, via a proxy if necessary.
     78  1.1  lukem  * Modifies the string argument given.
     79  1.1  lukem  * Returns -1 on failure, 0 on success
     80  1.1  lukem  */
     81  1.1  lukem int
     82  1.1  lukem http_get(line)
     83  1.1  lukem 	char *line;
     84  1.1  lukem {
     85  1.1  lukem 	struct sockaddr_in sin;
     86  1.1  lukem 	int i, out, port, s;
     87  1.1  lukem 	size_t buflen, len;
     88  1.1  lukem 	char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
     89  1.2  lukem 	char *proxyenv, *proxy, *host;
     90  1.2  lukem 	sig_t oldintr;
     91  1.1  lukem 	off_t hashbytes;
     92  1.1  lukem 
     93  1.1  lukem 	s = -1;
     94  1.2  lukem 	proxy = NULL;
     95  1.1  lukem 
     96  1.1  lukem 	host = line + sizeof(HTTP_URL) - 1;
     97  1.1  lukem 	path = strchr(host, '/');		/* find path */
     98  1.1  lukem 	if (EMPTYSTRING(path))
     99  1.1  lukem 		goto cleanup_http_get;
    100  1.1  lukem 	*path++ = '\0';
    101  1.1  lukem 	if (EMPTYSTRING(path))
    102  1.1  lukem 		goto cleanup_http_get;
    103  1.1  lukem 
    104  1.1  lukem 	savefile = strrchr(path, '/');			/* find savefile */
    105  1.1  lukem 	if (savefile != NULL)
    106  1.1  lukem 		savefile++;
    107  1.1  lukem 	else
    108  1.1  lukem 		savefile = path;
    109  1.1  lukem 	if (EMPTYSTRING(savefile))
    110  1.1  lukem 		goto cleanup_http_get;
    111  1.1  lukem 
    112  1.2  lukem 	proxyenv = getenv(HTTP_PROXY);
    113  1.2  lukem 	if (proxyenv != NULL) {				/* use proxy */
    114  1.2  lukem 		if (strncmp(proxyenv, HTTP_URL, sizeof(HTTP_URL) - 1) != 0) {
    115  1.2  lukem 			warnx("Malformed proxy url: %s", proxyenv);
    116  1.1  lukem 			goto cleanup_http_get;
    117  1.1  lukem 		}
    118  1.2  lukem 		proxy = strdup(proxyenv);
    119  1.1  lukem 		if (proxy == NULL)
    120  1.1  lukem 			errx(1, "Can't allocate memory for proxy url.");
    121  1.1  lukem 		host = proxy + sizeof(HTTP_URL) - 1;
    122  1.1  lukem 		if (EMPTYSTRING(host))
    123  1.1  lukem 			goto cleanup_http_get;
    124  1.1  lukem 		*--path = '/';			/* add / back to real path */
    125  1.1  lukem 		path = strchr(host, '/');	/* remove trailing / on host */
    126  1.1  lukem 		if (! EMPTYSTRING(path))
    127  1.1  lukem 			*path++ = '\0';
    128  1.1  lukem 		path = line;
    129  1.1  lukem 	}
    130  1.1  lukem 
    131  1.1  lukem 	portnum = strchr(host, ':');			/* find portnum */
    132  1.1  lukem 	if (portnum != NULL)
    133  1.1  lukem 		*portnum++ = '\0';
    134  1.1  lukem 
    135  1.1  lukem 	if (debug)
    136  1.1  lukem 		printf("host %s, port %s, path %s, save as %s.\n",
    137  1.1  lukem 		    host, portnum, path, savefile);
    138  1.1  lukem 
    139  1.1  lukem 	memset(&sin, 0, sizeof(sin));
    140  1.1  lukem 	sin.sin_family = AF_INET;
    141  1.1  lukem 
    142  1.1  lukem 	if (isdigit(host[0])) {
    143  1.1  lukem 		if (inet_aton(host, &sin.sin_addr) == 0) {
    144  1.1  lukem 			warnx("invalid IP address: %s", host);
    145  1.1  lukem 			goto cleanup_http_get;
    146  1.1  lukem 		}
    147  1.1  lukem 	} else {
    148  1.1  lukem 		struct hostent *hp;
    149  1.1  lukem 
    150  1.1  lukem 		hp = gethostbyname(host);
    151  1.1  lukem 		if (hp == NULL) {
    152  1.2  lukem 			warnx("%s: %s", host, hstrerror(h_errno));
    153  1.1  lukem 			goto cleanup_http_get;
    154  1.1  lukem 		}
    155  1.1  lukem 		if (hp->h_addrtype != AF_INET) {
    156  1.1  lukem 			warnx("%s: not an Internet address?", host);
    157  1.1  lukem 			goto cleanup_http_get;
    158  1.1  lukem 		}
    159  1.1  lukem 		memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    160  1.1  lukem 	}
    161  1.1  lukem 
    162  1.1  lukem 	if (! EMPTYSTRING(portnum)) {
    163  1.1  lukem 		port = atoi(portnum);
    164  1.1  lukem 		if (port < 1 || (port & 0xffff) != port) {
    165  1.1  lukem 			warnx("invalid port: %s", portnum);
    166  1.1  lukem 			goto cleanup_http_get;
    167  1.1  lukem 		}
    168  1.1  lukem 		port = htons(port);
    169  1.1  lukem 	} else
    170  1.1  lukem 		port = httpport;
    171  1.1  lukem 	sin.sin_port = port;
    172  1.1  lukem 
    173  1.1  lukem 	s = socket(AF_INET, SOCK_STREAM, 0);
    174  1.1  lukem 	if (s == -1) {
    175  1.1  lukem 		warnx("Can't create socket");
    176  1.1  lukem 		goto cleanup_http_get;
    177  1.1  lukem 	}
    178  1.1  lukem 
    179  1.1  lukem 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    180  1.1  lukem 		warn("Can't connect to %s", host);
    181  1.1  lukem 		goto cleanup_http_get;
    182  1.1  lukem 	}
    183  1.1  lukem 
    184  1.1  lukem 	/*
    185  1.1  lukem 	 * Construct and send the request.  We're expecting a return
    186  1.1  lukem 	 * status of "200". Proxy requests don't want leading /.
    187  1.1  lukem 	 */
    188  1.2  lukem 	if (!proxy)
    189  1.2  lukem 		printf("Requesting %s:%d/%s\n", line, ntohs(port), path);
    190  1.2  lukem 	else
    191  1.2  lukem 		printf("Requesting %s (via %s)\n", line, proxyenv);
    192  1.1  lukem 	snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
    193  1.1  lukem 	    proxy ? "" : "/", path);
    194  1.1  lukem 	buflen = strlen(buf);
    195  1.1  lukem 	if (write(s, buf, buflen) < buflen) {
    196  1.1  lukem 		warn("write");
    197  1.1  lukem 		goto cleanup_http_get;
    198  1.1  lukem 	}
    199  1.1  lukem 	memset(buf, 0, sizeof(buf));
    200  1.1  lukem 	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
    201  1.1  lukem 		if (read(s, cp, 1) != 1)
    202  1.1  lukem 			goto improper;
    203  1.1  lukem 		if (*cp == '\n')
    204  1.1  lukem 			break;
    205  1.1  lukem 	}
    206  1.1  lukem 	buf[buflen - 1] = '\0';		/* sanity */
    207  1.1  lukem 	cp = strchr(buf, ' ');
    208  1.1  lukem 	if (cp == NULL)
    209  1.1  lukem 		goto improper;
    210  1.1  lukem 	else
    211  1.1  lukem 		cp++;
    212  1.1  lukem 	if (strncmp(cp, "200", 3)) {
    213  1.1  lukem 		warnx("Error retrieving file: %s", cp);
    214  1.1  lukem 		goto cleanup_http_get;
    215  1.1  lukem 	}
    216  1.1  lukem 
    217  1.1  lukem 	/*
    218  1.1  lukem 	 * Read the rest of the header.
    219  1.1  lukem 	 */
    220  1.1  lukem 	memset(buf, 0, sizeof(buf));
    221  1.1  lukem 	c = '\0';
    222  1.1  lukem 	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
    223  1.1  lukem 		if (read(s, cp, 1) != 1)
    224  1.1  lukem 			goto improper;
    225  1.1  lukem 		if (*cp == '\n' && c == '\n')
    226  1.1  lukem 			break;
    227  1.1  lukem 		c = *cp;
    228  1.1  lukem 	}
    229  1.1  lukem 	buf[buflen - 1] = '\0';		/* sanity */
    230  1.1  lukem 
    231  1.1  lukem 	/*
    232  1.1  lukem 	 * Look for the "Content-length: " header.
    233  1.1  lukem 	 */
    234  1.1  lukem #define CONTENTLEN "Content-Length: "
    235  1.1  lukem 	for (cp = buf; *cp != '\0'; cp++) {
    236  1.1  lukem 		if (tolower(*cp) == 'c' &&
    237  1.1  lukem 		    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
    238  1.1  lukem 			break;
    239  1.1  lukem 	}
    240  1.1  lukem 	if (*cp == '\0')
    241  1.1  lukem 		goto improper;
    242  1.1  lukem 	cp += sizeof(CONTENTLEN) - 1;
    243  1.1  lukem 	cp2 = strchr(cp, '\n');
    244  1.1  lukem 	if (cp2 == NULL)
    245  1.1  lukem 		goto improper;
    246  1.1  lukem 	else
    247  1.1  lukem 		*cp2 = '\0';
    248  1.1  lukem 	filesize = atoi(cp);
    249  1.1  lukem 	if (filesize < 1)
    250  1.1  lukem 		goto improper;
    251  1.1  lukem 
    252  1.1  lukem 	/* Open the output file. */
    253  1.1  lukem 	out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    254  1.1  lukem 	if (out < 0) {
    255  1.1  lukem 		warn("Can't open %s", savefile);
    256  1.1  lukem 		goto cleanup_http_get;
    257  1.1  lukem 	}
    258  1.1  lukem 
    259  1.2  lukem 	/* Trap signals */
    260  1.2  lukem 	oldintr = NULL;
    261  1.2  lukem 	if (setjmp(httpabort)) {
    262  1.2  lukem 		if (oldintr)
    263  1.2  lukem 			(void) signal(SIGINT, oldintr);
    264  1.2  lukem 		goto cleanup_http_get;
    265  1.2  lukem 	}
    266  1.2  lukem 	oldintr = signal(SIGINT, aborthttp);
    267  1.2  lukem 
    268  1.1  lukem 	bytes = 0;
    269  1.1  lukem 	hashbytes = mark;
    270  1.1  lukem 	progressmeter(-1);
    271  1.2  lukem 
    272  1.2  lukem 	/* Finally, suck down the file. */
    273  1.1  lukem 	i = 0;
    274  1.1  lukem 	while ((len = read(s, buf, sizeof(buf))) > 0) {
    275  1.1  lukem 		bytes += len;
    276  1.1  lukem 		for (cp = buf; len > 0; len -= i, cp += i) {
    277  1.1  lukem 			if ((i = write(out, cp, len)) == -1) {
    278  1.1  lukem 				warn("Writing %s", savefile);
    279  1.1  lukem 				goto cleanup_http_get;
    280  1.1  lukem 			}
    281  1.1  lukem 			else if (i == 0)
    282  1.1  lukem 				break;
    283  1.1  lukem 		}
    284  1.1  lukem 		if (hash && !progress) {
    285  1.1  lukem 			while (bytes >= hashbytes) {
    286  1.1  lukem 				(void) putchar('#');
    287  1.1  lukem 				hashbytes += mark;
    288  1.1  lukem 			}
    289  1.1  lukem 			(void) fflush(stdout);
    290  1.1  lukem 		}
    291  1.1  lukem 	}
    292  1.1  lukem 	if (hash && !progress && bytes > 0) {
    293  1.1  lukem 		if (bytes < mark)
    294  1.1  lukem 			(void) putchar('#');
    295  1.1  lukem 		(void) putchar('\n');
    296  1.1  lukem 		(void) fflush(stdout);
    297  1.1  lukem 	}
    298  1.1  lukem 	if (len != 0) {
    299  1.1  lukem 		warn("Reading from socket");
    300  1.1  lukem 		goto cleanup_http_get;
    301  1.1  lukem 	}
    302  1.2  lukem 	progressmeter(1);
    303  1.2  lukem 	if (verbose)
    304  1.2  lukem 		printf("Successfully retrieved file.\n");
    305  1.2  lukem 	(void) signal(SIGINT, oldintr);
    306  1.1  lukem 
    307  1.1  lukem 	close(s);
    308  1.1  lukem 	close(out);
    309  1.1  lukem 	if (proxy)
    310  1.2  lukem 		free(proxy);
    311  1.1  lukem 	return(0);
    312  1.1  lukem 
    313  1.1  lukem improper:
    314  1.1  lukem 	warnx("improper response from %s", host);
    315  1.1  lukem cleanup_http_get:
    316  1.1  lukem 	if (s != -1)
    317  1.1  lukem 		close(s);
    318  1.1  lukem 	if (proxy)
    319  1.1  lukem 		free(proxy);
    320  1.1  lukem 	return(-1);
    321  1.1  lukem }
    322  1.1  lukem 
    323  1.1  lukem /*
    324  1.2  lukem  * Abort a http retrieval
    325  1.2  lukem  */
    326  1.2  lukem void
    327  1.2  lukem aborthttp()
    328  1.2  lukem {
    329  1.2  lukem 
    330  1.2  lukem 	alarmtimer(0);
    331  1.2  lukem 	printf("\nhttp fetch aborted\n");
    332  1.2  lukem 	(void) fflush(stdout);
    333  1.2  lukem 	longjmp(httpabort, 1);
    334  1.2  lukem }
    335  1.2  lukem 
    336  1.2  lukem /*
    337  1.1  lukem  * Retrieve multiple files from the command line, transferring
    338  1.1  lukem  * files of the form "host:path", "ftp://host/path" using the
    339  1.1  lukem  * ftp protocol, and files of the form "http://host/path" using
    340  1.1  lukem  * the http protocol.
    341  1.1  lukem  * If path has a trailing "/", then return(-1);
    342  1.1  lukem  * the path will be cd-ed into and the connection remains open,
    343  1.1  lukem  * and the function will return -1 (to indicate the connection
    344  1.1  lukem  * is alive).
    345  1.1  lukem  * If an error occurs the return value will be the offset+1 in
    346  1.1  lukem  * argv[] of the file that caused a problem (i.e, argv[x]
    347  1.1  lukem  * returns x+1)
    348  1.1  lukem  * Otherwise, 0 is returned if all files retrieved successfully.
    349  1.1  lukem  */
    350  1.1  lukem int
    351  1.1  lukem auto_fetch(argc, argv)
    352  1.1  lukem 	int argc;
    353  1.1  lukem 	char *argv[];
    354  1.1  lukem {
    355  1.1  lukem 	static char lasthost[MAXHOSTNAMELEN];
    356  1.1  lukem 	char *xargv[5];
    357  1.1  lukem 	char *cp, *line, *host, *dir, *file, *portnum;
    358  1.1  lukem 	int rval, xargc, argpos;
    359  1.1  lukem 
    360  1.2  lukem 	argpos = 0;
    361  1.1  lukem 
    362  1.2  lukem 	if (setjmp(toplevel)) {
    363  1.2  lukem 		if (connected)
    364  1.2  lukem 			disconnect(0, NULL);
    365  1.2  lukem 		return(argpos + 1);
    366  1.2  lukem 	}
    367  1.1  lukem 	(void) signal(SIGINT, intr);
    368  1.1  lukem 	(void) signal(SIGPIPE, lostpeer);
    369  1.1  lukem 
    370  1.1  lukem 	/*
    371  1.1  lukem 	 * Loop through as long as there's files to fetch.
    372  1.1  lukem 	 */
    373  1.2  lukem 	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
    374  1.1  lukem 		if (strchr(argv[argpos], ':') == NULL)
    375  1.1  lukem 			break;
    376  1.1  lukem 		host = dir = file = portnum = NULL;
    377  1.1  lukem 
    378  1.1  lukem 		/*
    379  1.1  lukem 		 * We muck with the string, so we make a copy.
    380  1.1  lukem 		 */
    381  1.1  lukem 		line = strdup(argv[argpos]);
    382  1.1  lukem 		if (line == NULL)
    383  1.1  lukem 			errx(1, "Can't allocate memory for auto-fetch.");
    384  1.1  lukem 
    385  1.1  lukem 		/*
    386  1.1  lukem 		 * Try HTTP URL-style arguments first.
    387  1.1  lukem 		 */
    388  1.1  lukem 		if (strncmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
    389  1.1  lukem 			if (http_get(line) == -1)
    390  1.1  lukem 				rval = argpos + 1;
    391  1.1  lukem 			continue;
    392  1.1  lukem 		}
    393  1.1  lukem 
    394  1.1  lukem 		/*
    395  1.1  lukem 		 * Try FTP URL-style arguments next, then host:file.
    396  1.1  lukem 		 */
    397  1.1  lukem 		host = line;
    398  1.1  lukem 		if (strncmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    399  1.1  lukem 			host += sizeof(FTP_URL) - 1;
    400  1.1  lukem 			cp = strchr(host, '/');
    401  1.1  lukem 
    402  1.1  lukem 			/* Look for a port number after the host name. */
    403  1.1  lukem 			portnum = strchr(host, ':');
    404  1.1  lukem 			if (portnum != NULL)
    405  1.1  lukem 				*portnum++ = '\0';
    406  1.1  lukem 		} else				/* classic style `host:file' */
    407  1.1  lukem 			cp = strchr(host, ':');
    408  1.1  lukem 		if (EMPTYSTRING(host)) {
    409  1.1  lukem 			rval = argpos + 1;
    410  1.1  lukem 			continue;
    411  1.1  lukem 		}
    412  1.1  lukem 
    413  1.1  lukem 		/*
    414  1.1  lukem 		 * If cp is NULL, the file wasn't specified
    415  1.1  lukem 		 * (URL looked something like ftp://host)
    416  1.1  lukem 		 */
    417  1.1  lukem 		if (cp != NULL)
    418  1.1  lukem 			*cp++ = '\0';
    419  1.1  lukem 
    420  1.1  lukem 		/*
    421  1.1  lukem 		 * Extract the file and (if present) directory name.
    422  1.1  lukem 		 */
    423  1.1  lukem 		dir = cp;
    424  1.1  lukem 		if (! EMPTYSTRING(dir)) {
    425  1.1  lukem 			cp = strrchr(cp, '/');
    426  1.1  lukem 			if (cp != NULL) {
    427  1.1  lukem 				*cp++ = '\0';
    428  1.1  lukem 				file = cp;
    429  1.1  lukem 			} else {
    430  1.1  lukem 				file = dir;
    431  1.1  lukem 				dir = NULL;
    432  1.1  lukem 			}
    433  1.1  lukem 		}
    434  1.1  lukem 		if (debug)
    435  1.1  lukem 			printf("host '%s', dir '%s', file '%s'\n",
    436  1.1  lukem 			    host, dir, file);
    437  1.1  lukem 
    438  1.1  lukem 		/*
    439  1.1  lukem 		 * Set up the connection if we don't have one.
    440  1.1  lukem 		 */
    441  1.1  lukem 		if (strcmp(host, lasthost) != 0) {
    442  1.1  lukem 			strcpy(lasthost, host);
    443  1.1  lukem 			if (connected)
    444  1.1  lukem 				disconnect(0, NULL);
    445  1.1  lukem 			xargv[0] = __progname;
    446  1.1  lukem 			xargv[1] = host;
    447  1.1  lukem 			xargv[2] = NULL;
    448  1.1  lukem 			xargc = 2;
    449  1.1  lukem 			if (portnum != NULL) {
    450  1.1  lukem 				xargv[2] = portnum;
    451  1.1  lukem 				xargv[3] = NULL;
    452  1.1  lukem 				xargc = 3;
    453  1.1  lukem 			}
    454  1.1  lukem 			setpeer(xargc, xargv);
    455  1.1  lukem 			if (connected == 0) {
    456  1.2  lukem 				warnx("Can't connect to host `%s'", host);
    457  1.1  lukem 				rval = argpos + 1;
    458  1.1  lukem 				continue;
    459  1.1  lukem 			}
    460  1.1  lukem 
    461  1.1  lukem 			/* Always use binary transfers. */
    462  1.1  lukem 			setbinary(0, NULL);
    463  1.1  lukem 		}
    464  1.1  lukem 		else	/* already have connection, cd back to '/' */
    465  1.1  lukem 		{
    466  1.1  lukem 			xargv[0] = "cd";
    467  1.1  lukem 			xargv[1] = "/";
    468  1.1  lukem 			xargv[2] = NULL;
    469  1.1  lukem 			cd(2, xargv);
    470  1.1  lukem 			if (! dirchange) {
    471  1.1  lukem 				rval = argpos + 1;
    472  1.1  lukem 				continue;
    473  1.1  lukem 			}
    474  1.1  lukem 		}
    475  1.1  lukem 
    476  1.1  lukem 		/* Change directories, if necessary. */
    477  1.1  lukem 		if (! EMPTYSTRING(dir)) {
    478  1.1  lukem 			xargv[0] = "cd";
    479  1.1  lukem 			xargv[1] = dir;
    480  1.1  lukem 			xargv[2] = NULL;
    481  1.1  lukem 			cd(2, xargv);
    482  1.1  lukem 			if (! dirchange) {
    483  1.1  lukem 				rval = argpos + 1;
    484  1.1  lukem 				continue;
    485  1.1  lukem 			}
    486  1.1  lukem 		}
    487  1.1  lukem 
    488  1.1  lukem 		if (EMPTYSTRING(file)) {
    489  1.1  lukem 			rval = -1;
    490  1.1  lukem 			continue;
    491  1.1  lukem 		}
    492  1.1  lukem 
    493  1.2  lukem 		if (!verbose)
    494  1.2  lukem 			printf("Retrieving %s/%s\n", dir ? dir : "", file);
    495  1.2  lukem 
    496  1.1  lukem 		/* Fetch the file. */
    497  1.1  lukem 		xargv[0] = "get";
    498  1.1  lukem 		xargv[1] = file;
    499  1.1  lukem 		xargv[2] = NULL;
    500  1.1  lukem 		get(2, xargv);
    501  1.2  lukem 
    502  1.2  lukem 		if ((code / 100) != COMPLETE)	/* XXX: is this valid? */
    503  1.2  lukem 			rval = argpos + 1;
    504  1.1  lukem 	}
    505  1.1  lukem 	if (connected && rval != -1)
    506  1.1  lukem 		disconnect(0, NULL);
    507  1.1  lukem 	return (rval);
    508  1.1  lukem }
    509