Home | History | Annotate | Line # | Download | only in ftp
ftp.c revision 1.8
      1 /*
      2  * Copyright (c) 1985, 1989, 1993, 1994
      3  *	The Regents of the University of California.  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  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)ftp.c	8.4 (Berkeley) 4/6/94";
     36 #endif /* not lint */
     37 
     38 #include <sys/param.h>
     39 #include <sys/stat.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/socket.h>
     42 #include <sys/time.h>
     43 #include <sys/file.h>
     44 
     45 #include <netinet/in.h>
     46 #include <netinet/in_systm.h>
     47 #include <netinet/ip.h>
     48 #include <arpa/inet.h>
     49 #include <arpa/ftp.h>
     50 #include <arpa/telnet.h>
     51 
     52 #include <ctype.h>
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <fcntl.h>
     56 #include <netdb.h>
     57 #include <pwd.h>
     58 #include <signal.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 #include <varargs.h>
     64 
     65 #include "ftp_var.h"
     66 
     67 extern int h_errno;
     68 
     69 struct	sockaddr_in hisctladdr;
     70 struct	sockaddr_in data_addr;
     71 int	data = -1;
     72 int	abrtflag = 0;
     73 jmp_buf	ptabort;
     74 int	ptabflg;
     75 int	ptflag = 0;
     76 struct	sockaddr_in myctladdr;
     77 off_t	restart_point = 0;
     78 
     79 
     80 FILE	*cin, *cout;
     81 
     82 char *
     83 hookup(host, port)
     84 	char *host;
     85 	int port;
     86 {
     87 	struct hostent *hp = 0;
     88 	int s, len, tos;
     89 	static char hostnamebuf[80];
     90 
     91 	memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
     92 	hisctladdr.sin_addr.s_addr = inet_addr(host);
     93 	if (hisctladdr.sin_addr.s_addr != -1) {
     94 		hisctladdr.sin_family = AF_INET;
     95 		(void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
     96 	} else {
     97 		hp = gethostbyname(host);
     98 		if (hp == NULL) {
     99 			warnx("%s: %s", host, hstrerror(h_errno));
    100 			code = -1;
    101 			return ((char *) 0);
    102 		}
    103 		hisctladdr.sin_family = hp->h_addrtype;
    104 		memmove((caddr_t)&hisctladdr.sin_addr,
    105 				hp->h_addr_list[0], hp->h_length);
    106 		(void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
    107 	}
    108 	hostname = hostnamebuf;
    109 	s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
    110 	if (s < 0) {
    111 		warn("socket");
    112 		code = -1;
    113 		return (0);
    114 	}
    115 	hisctladdr.sin_port = port;
    116 	while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
    117 		if (hp && hp->h_addr_list[1]) {
    118 			int oerrno = errno;
    119 			char *ia;
    120 
    121 			ia = inet_ntoa(hisctladdr.sin_addr);
    122 			errno = oerrno;
    123 			warn("connect to address %s", ia);
    124 			hp->h_addr_list++;
    125 			memmove((caddr_t)&hisctladdr.sin_addr,
    126 					hp->h_addr_list[0], hp->h_length);
    127 			fprintf(stdout, "Trying %s...\n",
    128 				inet_ntoa(hisctladdr.sin_addr));
    129 			(void) close(s);
    130 			s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
    131 			if (s < 0) {
    132 				warn("socket");
    133 				code = -1;
    134 				return (0);
    135 			}
    136 			continue;
    137 		}
    138 		warn("connect");
    139 		code = -1;
    140 		goto bad;
    141 	}
    142 	len = sizeof (myctladdr);
    143 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
    144 		warn("getsockname");
    145 		code = -1;
    146 		goto bad;
    147 	}
    148 #ifdef IP_TOS
    149 	tos = IPTOS_LOWDELAY;
    150 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
    151 		warn("setsockopt TOS (ignored)");
    152 #endif
    153 	cin = fdopen(s, "r");
    154 	cout = fdopen(s, "w");
    155 	if (cin == NULL || cout == NULL) {
    156 		warnx("fdopen failed.");
    157 		if (cin)
    158 			(void) fclose(cin);
    159 		if (cout)
    160 			(void) fclose(cout);
    161 		code = -1;
    162 		goto bad;
    163 	}
    164 	if (verbose)
    165 		printf("Connected to %s.\n", hostname);
    166 	if (getreply(0) > 2) { 	/* read startup message from server */
    167 		if (cin)
    168 			(void) fclose(cin);
    169 		if (cout)
    170 			(void) fclose(cout);
    171 		code = -1;
    172 		goto bad;
    173 	}
    174 #ifdef SO_OOBINLINE
    175 	{
    176 	int on = 1;
    177 
    178 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
    179 		< 0 && debug) {
    180 			warn("setsockopt");
    181 		}
    182 	}
    183 #endif /* SO_OOBINLINE */
    184 
    185 	return (hostname);
    186 bad:
    187 	(void) close(s);
    188 	return ((char *)0);
    189 }
    190 
    191 int
    192 login(host)
    193 	char *host;
    194 {
    195 	char tmp[80];
    196 	char *user, *pass, *acct;
    197 	int n, aflag = 0;
    198 
    199 	user = pass = acct = 0;
    200 	if (ruserpass(host, &user, &pass, &acct) < 0) {
    201 		code = -1;
    202 		return (0);
    203 	}
    204 	while (user == NULL) {
    205 		char *myname = getlogin();
    206 
    207 		if (myname == NULL) {
    208 			struct passwd *pp = getpwuid(getuid());
    209 
    210 			if (pp != NULL)
    211 				myname = pp->pw_name;
    212 		}
    213 		if (myname)
    214 			printf("Name (%s:%s): ", host, myname);
    215 		else
    216 			printf("Name (%s): ", host);
    217 		(void) fgets(tmp, sizeof(tmp) - 1, stdin);
    218 		tmp[strlen(tmp) - 1] = '\0';
    219 		if (*tmp == '\0')
    220 			user = myname;
    221 		else
    222 			user = tmp;
    223 	}
    224 	n = command("USER %s", user);
    225 	if (n == CONTINUE) {
    226 		if (pass == NULL)
    227 			pass = getpass("Password:");
    228 		n = command("PASS %s", pass);
    229 	}
    230 	if (n == CONTINUE) {
    231 		aflag++;
    232 		acct = getpass("Account:");
    233 		n = command("ACCT %s", acct);
    234 	}
    235 	if (n != COMPLETE) {
    236 		warnx("Login failed.");
    237 		return (0);
    238 	}
    239 	if (!aflag && acct != NULL)
    240 		(void) command("ACCT %s", acct);
    241 	if (proxy)
    242 		return (1);
    243 	for (n = 0; n < macnum; ++n) {
    244 		if (!strcmp("init", macros[n].mac_name)) {
    245 			(void) strcpy(line, "$init");
    246 			makeargv();
    247 			domacro(margc, margv);
    248 			break;
    249 		}
    250 	}
    251 	return (1);
    252 }
    253 
    254 void
    255 cmdabort()
    256 {
    257 
    258 	printf("\n");
    259 	(void) fflush(stdout);
    260 	abrtflag++;
    261 	if (ptflag)
    262 		longjmp(ptabort,1);
    263 }
    264 
    265 /*VARARGS*/
    266 int
    267 command(va_alist)
    268 va_dcl
    269 {
    270 	va_list ap;
    271 	char *fmt;
    272 	int r;
    273 	sig_t oldintr;
    274 
    275 	abrtflag = 0;
    276 	if (debug) {
    277 		printf("---> ");
    278 		va_start(ap);
    279 		fmt = va_arg(ap, char *);
    280 		if (strncmp("PASS ", fmt, 5) == 0)
    281 			printf("PASS XXXX");
    282 		else
    283 			vfprintf(stdout, fmt, ap);
    284 		va_end(ap);
    285 		printf("\n");
    286 		(void) fflush(stdout);
    287 	}
    288 	if (cout == NULL) {
    289 		warn("No control connection for command");
    290 		code = -1;
    291 		return (0);
    292 	}
    293 	oldintr = signal(SIGINT, cmdabort);
    294 	va_start(ap);
    295 	fmt = va_arg(ap, char *);
    296 	vfprintf(cout, fmt, ap);
    297 	va_end(ap);
    298 	fprintf(cout, "\r\n");
    299 	(void) fflush(cout);
    300 	cpend = 1;
    301 	r = getreply(!strcmp(fmt, "QUIT"));
    302 	if (abrtflag && oldintr != SIG_IGN)
    303 		(*oldintr)(SIGINT);
    304 	(void) signal(SIGINT, oldintr);
    305 	return (r);
    306 }
    307 
    308 char reply_string[BUFSIZ];		/* last line of previous reply */
    309 
    310 int
    311 getreply(expecteof)
    312 	int expecteof;
    313 {
    314 	int c, n;
    315 	int dig;
    316 	int originalcode = 0, continuation = 0;
    317 	sig_t oldintr;
    318 	int pflag = 0;
    319 	char *cp, *pt = pasv;
    320 
    321 	oldintr = signal(SIGINT, cmdabort);
    322 	for (;;) {
    323 		dig = n = code = 0;
    324 		cp = reply_string;
    325 		while ((c = getc(cin)) != '\n') {
    326 			if (c == IAC) {     /* handle telnet commands */
    327 				switch (c = getc(cin)) {
    328 				case WILL:
    329 				case WONT:
    330 					c = getc(cin);
    331 					fprintf(cout, "%c%c%c", IAC, DONT, c);
    332 					(void) fflush(cout);
    333 					break;
    334 				case DO:
    335 				case DONT:
    336 					c = getc(cin);
    337 					fprintf(cout, "%c%c%c", IAC, WONT, c);
    338 					(void) fflush(cout);
    339 					break;
    340 				default:
    341 					break;
    342 				}
    343 				continue;
    344 			}
    345 			dig++;
    346 			if (c == EOF) {
    347 				if (expecteof) {
    348 					(void) signal(SIGINT,oldintr);
    349 					code = 221;
    350 					return (0);
    351 				}
    352 				lostpeer();
    353 				if (verbose) {
    354 					printf("421 Service not available, remote server has closed connection\n");
    355 					(void) fflush(stdout);
    356 				}
    357 				code = 421;
    358 				return (4);
    359 			}
    360 			if (c != '\r' && (verbose > 0 ||
    361 			    (verbose > -1 && n == '5' && dig > 4))) {
    362 				if (proxflag &&
    363 				   (dig == 1 || dig == 5 && verbose == 0))
    364 					printf("%s:",hostname);
    365 				(void) putchar(c);
    366 			}
    367 			if (dig < 4 && isdigit(c))
    368 				code = code * 10 + (c - '0');
    369 			if (!pflag && code == 227)
    370 				pflag = 1;
    371 			if (dig > 4 && pflag == 1 && isdigit(c))
    372 				pflag = 2;
    373 			if (pflag == 2) {
    374 				if (c != '\r' && c != ')')
    375 					*pt++ = c;
    376 				else {
    377 					*pt = '\0';
    378 					pflag = 3;
    379 				}
    380 			}
    381 			if (dig == 4 && c == '-') {
    382 				if (continuation)
    383 					code = 0;
    384 				continuation++;
    385 			}
    386 			if (n == 0)
    387 				n = c;
    388 			if (cp < &reply_string[sizeof(reply_string) - 1])
    389 				*cp++ = c;
    390 		}
    391 		if (verbose > 0 || verbose > -1 && n == '5') {
    392 			(void) putchar(c);
    393 			(void) fflush (stdout);
    394 		}
    395 		if (continuation && code != originalcode) {
    396 			if (originalcode == 0)
    397 				originalcode = code;
    398 			continue;
    399 		}
    400 		*cp = '\0';
    401 		if (n != '1')
    402 			cpend = 0;
    403 		(void) signal(SIGINT,oldintr);
    404 		if (code == 421 || originalcode == 421)
    405 			lostpeer();
    406 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
    407 			(*oldintr)(SIGINT);
    408 		return (n - '0');
    409 	}
    410 }
    411 
    412 int
    413 empty(mask, sec)
    414 	struct fd_set *mask;
    415 	int sec;
    416 {
    417 	struct timeval t;
    418 
    419 	t.tv_sec = (long) sec;
    420 	t.tv_usec = 0;
    421 	return (select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
    422 }
    423 
    424 jmp_buf	sendabort;
    425 
    426 void
    427 abortsend()
    428 {
    429 
    430 	mflag = 0;
    431 	abrtflag = 0;
    432 	printf("\nsend aborted\nwaiting for remote to finish abort\n");
    433 	(void) fflush(stdout);
    434 	longjmp(sendabort, 1);
    435 }
    436 
    437 #define HASHBYTES 1024
    438 
    439 void
    440 sendrequest(cmd, local, remote, printnames)
    441 	char *cmd, *local, *remote;
    442 	int printnames;
    443 {
    444 	struct stat st;
    445 	struct timeval start, stop;
    446 	int c, d;
    447 	FILE *fin, *dout = 0, *popen();
    448 	int (*closefunc) __P((FILE *));
    449 	sig_t oldintr, oldintp;
    450 	long bytes = 0, hashbytes = HASHBYTES;
    451 	char *lmode, buf[BUFSIZ], *bufp;
    452 
    453 	if (verbose && printnames) {
    454 		if (local && *local != '-')
    455 			printf("local: %s ", local);
    456 		if (remote)
    457 			printf("remote: %s\n", remote);
    458 	}
    459 	if (proxy) {
    460 		proxtrans(cmd, local, remote);
    461 		return;
    462 	}
    463 	if (curtype != type)
    464 		changetype(type, 0);
    465 	closefunc = NULL;
    466 	oldintr = NULL;
    467 	oldintp = NULL;
    468 	lmode = "w";
    469 	if (setjmp(sendabort)) {
    470 		while (cpend) {
    471 			(void) getreply(0);
    472 		}
    473 		if (data >= 0) {
    474 			(void) close(data);
    475 			data = -1;
    476 		}
    477 		if (oldintr)
    478 			(void) signal(SIGINT,oldintr);
    479 		if (oldintp)
    480 			(void) signal(SIGPIPE,oldintp);
    481 		code = -1;
    482 		return;
    483 	}
    484 	oldintr = signal(SIGINT, abortsend);
    485 	if (strcmp(local, "-") == 0)
    486 		fin = stdin;
    487 	else if (*local == '|') {
    488 		oldintp = signal(SIGPIPE,SIG_IGN);
    489 		fin = popen(local + 1, "r");
    490 		if (fin == NULL) {
    491 			warn("%s", local + 1);
    492 			(void) signal(SIGINT, oldintr);
    493 			(void) signal(SIGPIPE, oldintp);
    494 			code = -1;
    495 			return;
    496 		}
    497 		closefunc = pclose;
    498 	} else {
    499 		fin = fopen(local, "r");
    500 		if (fin == NULL) {
    501 			warn("local: %s", local);
    502 			(void) signal(SIGINT, oldintr);
    503 			code = -1;
    504 			return;
    505 		}
    506 		closefunc = fclose;
    507 		if (fstat(fileno(fin), &st) < 0 ||
    508 		    (st.st_mode&S_IFMT) != S_IFREG) {
    509 			fprintf(stdout, "%s: not a plain file.\n", local);
    510 			(void) signal(SIGINT, oldintr);
    511 			fclose(fin);
    512 			code = -1;
    513 			return;
    514 		}
    515 	}
    516 	if (initconn()) {
    517 		(void) signal(SIGINT, oldintr);
    518 		if (oldintp)
    519 			(void) signal(SIGPIPE, oldintp);
    520 		code = -1;
    521 		if (closefunc != NULL)
    522 			(*closefunc)(fin);
    523 		return;
    524 	}
    525 	if (setjmp(sendabort))
    526 		goto abort;
    527 
    528 	if (restart_point &&
    529 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
    530 		int rc;
    531 
    532 		switch (curtype) {
    533 		case TYPE_A:
    534 			rc = fseek(fin, (long) restart_point, SEEK_SET);
    535 			break;
    536 		case TYPE_I:
    537 		case TYPE_L:
    538 			rc = lseek(fileno(fin), restart_point, SEEK_SET);
    539 			break;
    540 		}
    541 		if (rc < 0) {
    542 			warn("local: %s", local);
    543 			restart_point = 0;
    544 			if (closefunc != NULL)
    545 				(*closefunc)(fin);
    546 			return;
    547 		}
    548 		if (command("REST %ld", (long) restart_point)
    549 			!= CONTINUE) {
    550 			restart_point = 0;
    551 			if (closefunc != NULL)
    552 				(*closefunc)(fin);
    553 			return;
    554 		}
    555 		restart_point = 0;
    556 		lmode = "r+w";
    557 	}
    558 	if (remote) {
    559 		if (command("%s %s", cmd, remote) != PRELIM) {
    560 			(void) signal(SIGINT, oldintr);
    561 			if (oldintp)
    562 				(void) signal(SIGPIPE, oldintp);
    563 			if (closefunc != NULL)
    564 				(*closefunc)(fin);
    565 			return;
    566 		}
    567 	} else
    568 		if (command("%s", cmd) != PRELIM) {
    569 			(void) signal(SIGINT, oldintr);
    570 			if (oldintp)
    571 				(void) signal(SIGPIPE, oldintp);
    572 			if (closefunc != NULL)
    573 				(*closefunc)(fin);
    574 			return;
    575 		}
    576 	dout = dataconn(lmode);
    577 	if (dout == NULL)
    578 		goto abort;
    579 	(void) gettimeofday(&start, (struct timezone *)0);
    580 	oldintp = signal(SIGPIPE, SIG_IGN);
    581 	switch (curtype) {
    582 
    583 	case TYPE_I:
    584 	case TYPE_L:
    585 		errno = d = 0;
    586 		while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
    587 			bytes += c;
    588 			for (bufp = buf; c > 0; c -= d, bufp += d)
    589 				if ((d = write(fileno(dout), bufp, c)) <= 0)
    590 					break;
    591 			if (hash) {
    592 				while (bytes >= hashbytes) {
    593 					(void) putchar('#');
    594 					hashbytes += HASHBYTES;
    595 				}
    596 				(void) fflush(stdout);
    597 			}
    598 		}
    599 		if (hash && bytes > 0) {
    600 			if (bytes < HASHBYTES)
    601 				(void) putchar('#');
    602 			(void) putchar('\n');
    603 			(void) fflush(stdout);
    604 		}
    605 		if (c < 0)
    606 			warn("local: %s", local);
    607 		if (d < 0) {
    608 			if (errno != EPIPE)
    609 				warn("netout");
    610 			bytes = -1;
    611 		}
    612 		break;
    613 
    614 	case TYPE_A:
    615 		while ((c = getc(fin)) != EOF) {
    616 			if (c == '\n') {
    617 				while (hash && (bytes >= hashbytes)) {
    618 					(void) putchar('#');
    619 					(void) fflush(stdout);
    620 					hashbytes += HASHBYTES;
    621 				}
    622 				if (ferror(dout))
    623 					break;
    624 				(void) putc('\r', dout);
    625 				bytes++;
    626 			}
    627 			(void) putc(c, dout);
    628 			bytes++;
    629 	/*		if (c == '\r') {			  	*/
    630 	/*		(void)	putc('\0', dout);  // this violates rfc */
    631 	/*			bytes++;				*/
    632 	/*		}                          			*/
    633 		}
    634 		if (hash) {
    635 			if (bytes < hashbytes)
    636 				(void) putchar('#');
    637 			(void) putchar('\n');
    638 			(void) fflush(stdout);
    639 		}
    640 		if (ferror(fin))
    641 			warn("local: %s", local);
    642 		if (ferror(dout)) {
    643 			if (errno != EPIPE)
    644 				warn("netout");
    645 			bytes = -1;
    646 		}
    647 		break;
    648 	}
    649 	(void) gettimeofday(&stop, (struct timezone *)0);
    650 	if (closefunc != NULL)
    651 		(*closefunc)(fin);
    652 	(void) fclose(dout);
    653 	(void) getreply(0);
    654 	(void) signal(SIGINT, oldintr);
    655 	if (oldintp)
    656 		(void) signal(SIGPIPE, oldintp);
    657 	if (bytes > 0)
    658 		ptransfer("sent", bytes, &start, &stop);
    659 	return;
    660 abort:
    661 	(void) gettimeofday(&stop, (struct timezone *)0);
    662 	(void) signal(SIGINT, oldintr);
    663 	if (oldintp)
    664 		(void) signal(SIGPIPE, oldintp);
    665 	if (!cpend) {
    666 		code = -1;
    667 		return;
    668 	}
    669 	if (data >= 0) {
    670 		(void) close(data);
    671 		data = -1;
    672 	}
    673 	if (dout)
    674 		(void) fclose(dout);
    675 	(void) getreply(0);
    676 	code = -1;
    677 	if (closefunc != NULL && fin != NULL)
    678 		(*closefunc)(fin);
    679 	if (bytes > 0)
    680 		ptransfer("sent", bytes, &start, &stop);
    681 }
    682 
    683 jmp_buf	recvabort;
    684 
    685 void
    686 abortrecv()
    687 {
    688 
    689 	mflag = 0;
    690 	abrtflag = 0;
    691 	printf("\nreceive aborted\nwaiting for remote to finish abort\n");
    692 	(void) fflush(stdout);
    693 	longjmp(recvabort, 1);
    694 }
    695 
    696 void
    697 recvrequest(cmd, local, remote, lmode, printnames)
    698 	char *cmd, *local, *remote, *lmode;
    699 	int printnames;
    700 {
    701 	FILE *fout, *din = 0;
    702 	int (*closefunc) __P((FILE *));
    703 	sig_t oldintr, oldintp;
    704 	int c, d, is_retr, tcrflag, bare_lfs = 0;
    705 	static int bufsize;
    706 	static char *buf;
    707 	long bytes = 0, hashbytes = HASHBYTES;
    708 	struct timeval start, stop;
    709 	struct stat st;
    710 
    711 	is_retr = strcmp(cmd, "RETR") == 0;
    712 	if (is_retr && verbose && printnames) {
    713 		if (local && *local != '-')
    714 			printf("local: %s ", local);
    715 		if (remote)
    716 			printf("remote: %s\n", remote);
    717 	}
    718 	if (proxy && is_retr) {
    719 		proxtrans(cmd, local, remote);
    720 		return;
    721 	}
    722 	closefunc = NULL;
    723 	oldintr = NULL;
    724 	oldintp = NULL;
    725 	tcrflag = !crflag && is_retr;
    726 	if (setjmp(recvabort)) {
    727 		while (cpend) {
    728 			(void) getreply(0);
    729 		}
    730 		if (data >= 0) {
    731 			(void) close(data);
    732 			data = -1;
    733 		}
    734 		if (oldintr)
    735 			(void) signal(SIGINT, oldintr);
    736 		code = -1;
    737 		return;
    738 	}
    739 	oldintr = signal(SIGINT, abortrecv);
    740 	if (strcmp(local, "-") && *local != '|') {
    741 		if (access(local, 2) < 0) {
    742 			char *dir = strrchr(local, '/');
    743 
    744 			if (errno != ENOENT && errno != EACCES) {
    745 				warn("local: %s", local);
    746 				(void) signal(SIGINT, oldintr);
    747 				code = -1;
    748 				return;
    749 			}
    750 			if (dir != NULL)
    751 				*dir = 0;
    752 			d = access(dir ? local : ".", 2);
    753 			if (dir != NULL)
    754 				*dir = '/';
    755 			if (d < 0) {
    756 				warn("local: %s", local);
    757 				(void) signal(SIGINT, oldintr);
    758 				code = -1;
    759 				return;
    760 			}
    761 			if (!runique && errno == EACCES &&
    762 			    chmod(local, 0600) < 0) {
    763 				warn("local: %s", local);
    764 				(void) signal(SIGINT, oldintr);
    765 				(void) signal(SIGINT, oldintr);
    766 				code = -1;
    767 				return;
    768 			}
    769 			if (runique && errno == EACCES &&
    770 			   (local = gunique(local)) == NULL) {
    771 				(void) signal(SIGINT, oldintr);
    772 				code = -1;
    773 				return;
    774 			}
    775 		}
    776 		else if (runique && (local = gunique(local)) == NULL) {
    777 			(void) signal(SIGINT, oldintr);
    778 			code = -1;
    779 			return;
    780 		}
    781 	}
    782 	if (!is_retr) {
    783 		if (curtype != TYPE_A)
    784 			changetype(TYPE_A, 0);
    785 	} else if (curtype != type)
    786 		changetype(type, 0);
    787 	if (initconn()) {
    788 		(void) signal(SIGINT, oldintr);
    789 		code = -1;
    790 		return;
    791 	}
    792 	if (setjmp(recvabort))
    793 		goto abort;
    794 	if (is_retr && restart_point &&
    795 	    command("REST %ld", (long) restart_point) != CONTINUE)
    796 		return;
    797 	if (remote) {
    798 		if (command("%s %s", cmd, remote) != PRELIM) {
    799 			(void) signal(SIGINT, oldintr);
    800 			return;
    801 		}
    802 	} else {
    803 		if (command("%s", cmd) != PRELIM) {
    804 			(void) signal(SIGINT, oldintr);
    805 			return;
    806 		}
    807 	}
    808 	din = dataconn("r");
    809 	if (din == NULL)
    810 		goto abort;
    811 	if (strcmp(local, "-") == 0)
    812 		fout = stdout;
    813 	else if (*local == '|') {
    814 		oldintp = signal(SIGPIPE, SIG_IGN);
    815 		fout = popen(local + 1, "w");
    816 		if (fout == NULL) {
    817 			warn("%s", local+1);
    818 			goto abort;
    819 		}
    820 		closefunc = pclose;
    821 	} else {
    822 		fout = fopen(local, lmode);
    823 		if (fout == NULL) {
    824 			warn("local: %s", local);
    825 			goto abort;
    826 		}
    827 		closefunc = fclose;
    828 	}
    829 	if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
    830 		st.st_blksize = BUFSIZ;
    831 	if (st.st_blksize > bufsize) {
    832 		if (buf)
    833 			(void) free(buf);
    834 		buf = malloc((unsigned)st.st_blksize);
    835 		if (buf == NULL) {
    836 			warn("malloc");
    837 			bufsize = 0;
    838 			goto abort;
    839 		}
    840 		bufsize = st.st_blksize;
    841 	}
    842 	(void) gettimeofday(&start, (struct timezone *)0);
    843 	switch (curtype) {
    844 
    845 	case TYPE_I:
    846 	case TYPE_L:
    847 		if (restart_point &&
    848 		    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
    849 			warn("local: %s", local);
    850 			if (closefunc != NULL)
    851 				(*closefunc)(fout);
    852 			return;
    853 		}
    854 		errno = d = 0;
    855 		while ((c = read(fileno(din), buf, bufsize)) > 0) {
    856 			if ((d = write(fileno(fout), buf, c)) != c)
    857 				break;
    858 			bytes += c;
    859 			if (hash) {
    860 				while (bytes >= hashbytes) {
    861 					(void) putchar('#');
    862 					hashbytes += HASHBYTES;
    863 				}
    864 				(void) fflush(stdout);
    865 			}
    866 		}
    867 		if (hash && bytes > 0) {
    868 			if (bytes < HASHBYTES)
    869 				(void) putchar('#');
    870 			(void) putchar('\n');
    871 			(void) fflush(stdout);
    872 		}
    873 		if (c < 0) {
    874 			if (errno != EPIPE)
    875 				warn("netin");
    876 			bytes = -1;
    877 		}
    878 		if (d < c) {
    879 			if (d < 0)
    880 				warn("local: %s", local);
    881 			else
    882 				warnx("%s: short write", local);
    883 		}
    884 		break;
    885 
    886 	case TYPE_A:
    887 		if (restart_point) {
    888 			int i, n, ch;
    889 
    890 			if (fseek(fout, 0L, SEEK_SET) < 0)
    891 				goto done;
    892 			n = restart_point;
    893 			for (i = 0; i++ < n;) {
    894 				if ((ch = getc(fout)) == EOF)
    895 					goto done;
    896 				if (ch == '\n')
    897 					i++;
    898 			}
    899 			if (fseek(fout, 0L, SEEK_CUR) < 0) {
    900 done:
    901 				warn("local: %s", local);
    902 				if (closefunc != NULL)
    903 					(*closefunc)(fout);
    904 				return;
    905 			}
    906 		}
    907 		while ((c = getc(din)) != EOF) {
    908 			if (c == '\n')
    909 				bare_lfs++;
    910 			while (c == '\r') {
    911 				while (hash && (bytes >= hashbytes)) {
    912 					(void) putchar('#');
    913 					(void) fflush(stdout);
    914 					hashbytes += HASHBYTES;
    915 				}
    916 				bytes++;
    917 				if ((c = getc(din)) != '\n' || tcrflag) {
    918 					if (ferror(fout))
    919 						goto break2;
    920 					(void) putc('\r', fout);
    921 					if (c == '\0') {
    922 						bytes++;
    923 						goto contin2;
    924 					}
    925 					if (c == EOF)
    926 						goto contin2;
    927 				}
    928 			}
    929 			(void) putc(c, fout);
    930 			bytes++;
    931 	contin2:	;
    932 		}
    933 break2:
    934 		if (bare_lfs) {
    935 			printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
    936 			printf("File may not have transferred correctly.\n");
    937 		}
    938 		if (hash) {
    939 			if (bytes < hashbytes)
    940 				(void) putchar('#');
    941 			(void) putchar('\n');
    942 			(void) fflush(stdout);
    943 		}
    944 		if (ferror(din)) {
    945 			if (errno != EPIPE)
    946 				warn("netin");
    947 			bytes = -1;
    948 		}
    949 		if (ferror(fout))
    950 			warn("local: %s", local);
    951 		break;
    952 	}
    953 	if (closefunc != NULL)
    954 		(*closefunc)(fout);
    955 	(void) signal(SIGINT, oldintr);
    956 	if (oldintp)
    957 		(void) signal(SIGPIPE, oldintp);
    958 	(void) gettimeofday(&stop, (struct timezone *)0);
    959 	(void) fclose(din);
    960 	(void) getreply(0);
    961 	if (bytes > 0 && is_retr)
    962 		ptransfer("received", bytes, &start, &stop);
    963 	return;
    964 abort:
    965 
    966 /* abort using RFC959 recommended IP,SYNC sequence  */
    967 
    968 	(void) gettimeofday(&stop, (struct timezone *)0);
    969 	if (oldintp)
    970 		(void) signal(SIGPIPE, oldintr);
    971 	(void) signal(SIGINT, SIG_IGN);
    972 	if (!cpend) {
    973 		code = -1;
    974 		(void) signal(SIGINT, oldintr);
    975 		return;
    976 	}
    977 
    978 	abort_remote(din);
    979 	code = -1;
    980 	if (data >= 0) {
    981 		(void) close(data);
    982 		data = -1;
    983 	}
    984 	if (closefunc != NULL && fout != NULL)
    985 		(*closefunc)(fout);
    986 	if (din)
    987 		(void) fclose(din);
    988 	if (bytes > 0)
    989 		ptransfer("received", bytes, &start, &stop);
    990 	(void) signal(SIGINT, oldintr);
    991 }
    992 
    993 /*
    994  * Need to start a listen on the data channel before we send the command,
    995  * otherwise the server's connect may fail.
    996  */
    997 int
    998 initconn()
    999 {
   1000 	char *p, *a;
   1001 	int result, len, tmpno = 0;
   1002 	int on = 1;
   1003 	int a0, a1, a2, a3, p0, p1;
   1004 
   1005 	if (passivemode) {
   1006 		data = socket(AF_INET, SOCK_STREAM, 0);
   1007 		if (data < 0) {
   1008 			perror("ftp: socket");
   1009 			return(1);
   1010 		}
   1011 		if ((options & SO_DEBUG) &&
   1012 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1013 			       sizeof (on)) < 0)
   1014 			perror("ftp: setsockopt (ignored)");
   1015 		if (command("PASV") != COMPLETE) {
   1016 			printf("Passive mode refused.\n");
   1017 			goto bad;
   1018 		}
   1019 
   1020 		/*
   1021 		 * What we've got at this point is a string of comma
   1022 		 * separated one-byte unsigned integer values, separated
   1023 		 * by commas.  The first four are the an IP address. The
   1024 		 * fifth is the MSB of the port number, the sixth is the
   1025 		 * LSB.  From that we'll prepare a sockaddr_in.
   1026 		 */
   1027 
   1028 		if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",
   1029 			   &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
   1030 			printf("Passive mode address scan failure. "
   1031 			       "Shouldn't happen!\n");
   1032 			goto bad;
   1033 		}
   1034 
   1035 		bzero(&data_addr, sizeof(data_addr));
   1036 		data_addr.sin_family = AF_INET;
   1037 		a = (char *)&data_addr.sin_addr.s_addr;
   1038 		a[0] = a0 & 0xff;
   1039 		a[1] = a1 & 0xff;
   1040 		a[2] = a2 & 0xff;
   1041 		a[3] = a3 & 0xff;
   1042 		p = (char *)&data_addr.sin_port;
   1043 		p[0] = p0 & 0xff;
   1044 		p[1] = p1 & 0xff;
   1045 
   1046 		if (connect(data, (struct sockaddr *)&data_addr,
   1047 			    sizeof(data_addr)) < 0) {
   1048 			perror("ftp: connect");
   1049 			goto bad;
   1050 		}
   1051 #ifdef IP_TOS
   1052 		on = IPTOS_THROUGHPUT;
   1053 		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
   1054 			       sizeof(int)) < 0)
   1055 			perror("ftp: setsockopt TOS (ignored)");
   1056 #endif
   1057 		return(0);
   1058 	}
   1059 
   1060 noport:
   1061 	data_addr = myctladdr;
   1062 	if (sendport)
   1063 		data_addr.sin_port = 0;	/* let system pick one */
   1064 	if (data != -1)
   1065 		(void) close(data);
   1066 	data = socket(AF_INET, SOCK_STREAM, 0);
   1067 	if (data < 0) {
   1068 		warn("socket");
   1069 		if (tmpno)
   1070 			sendport = 1;
   1071 		return (1);
   1072 	}
   1073 	if (!sendport)
   1074 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
   1075 			warn("setsockopt (reuse address)");
   1076 			goto bad;
   1077 		}
   1078 	if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
   1079 		warn("bind");
   1080 		goto bad;
   1081 	}
   1082 	if (options & SO_DEBUG &&
   1083 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
   1084 		warn("setsockopt (ignored)");
   1085 	len = sizeof (data_addr);
   1086 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
   1087 		warn("getsockname");
   1088 		goto bad;
   1089 	}
   1090 	if (listen(data, 1) < 0)
   1091 		warn("listen");
   1092 	if (sendport) {
   1093 		a = (char *)&data_addr.sin_addr;
   1094 		p = (char *)&data_addr.sin_port;
   1095 #define	UC(b)	(((int)b)&0xff)
   1096 		result =
   1097 		    command("PORT %d,%d,%d,%d,%d,%d",
   1098 		      UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
   1099 		      UC(p[0]), UC(p[1]));
   1100 		if (result == ERROR && sendport == -1) {
   1101 			sendport = 0;
   1102 			tmpno = 1;
   1103 			goto noport;
   1104 		}
   1105 		return (result != COMPLETE);
   1106 	}
   1107 	if (tmpno)
   1108 		sendport = 1;
   1109 #ifdef IP_TOS
   1110 	on = IPTOS_THROUGHPUT;
   1111 	if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
   1112 		warn("setsockopt TOS (ignored)");
   1113 #endif
   1114 	return (0);
   1115 bad:
   1116 	(void) close(data), data = -1;
   1117 	if (tmpno)
   1118 		sendport = 1;
   1119 	return (1);
   1120 }
   1121 
   1122 FILE *
   1123 dataconn(lmode)
   1124 	char *lmode;
   1125 {
   1126 	struct sockaddr_in from;
   1127 	int s, fromlen = sizeof (from), tos;
   1128 
   1129 	if (passivemode)
   1130 		return (fdopen(data, lmode));
   1131 
   1132 	s = accept(data, (struct sockaddr *) &from, &fromlen);
   1133 	if (s < 0) {
   1134 		warn("accept");
   1135 		(void) close(data), data = -1;
   1136 		return (NULL);
   1137 	}
   1138 	(void) close(data);
   1139 	data = s;
   1140 #ifdef IP_TOS
   1141 	tos = IPTOS_THROUGHPUT;
   1142 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
   1143 		warn("setsockopt TOS (ignored)");
   1144 #endif
   1145 	return (fdopen(data, lmode));
   1146 }
   1147 
   1148 void
   1149 ptransfer(direction, bytes, t0, t1)
   1150 	char *direction;
   1151 	long bytes;
   1152 	struct timeval *t0, *t1;
   1153 {
   1154 	struct timeval td;
   1155 	float s;
   1156 	long bs;
   1157 
   1158 	if (verbose) {
   1159 		tvsub(&td, t1, t0);
   1160 		s = td.tv_sec + (td.tv_usec / 1000000.);
   1161 #define	nz(x)	((x) == 0 ? 1 : (x))
   1162 		bs = bytes / nz(s);
   1163 		printf("%ld bytes %s in %.3g seconds (%ld bytes/s)\n",
   1164 		    bytes, direction, s, bs);
   1165 	}
   1166 }
   1167 
   1168 /*
   1169 void
   1170 tvadd(tsum, t0)
   1171 	struct timeval *tsum, *t0;
   1172 {
   1173 
   1174 	tsum->tv_sec += t0->tv_sec;
   1175 	tsum->tv_usec += t0->tv_usec;
   1176 	if (tsum->tv_usec > 1000000)
   1177 		tsum->tv_sec++, tsum->tv_usec -= 1000000;
   1178 }
   1179 */
   1180 
   1181 void
   1182 tvsub(tdiff, t1, t0)
   1183 	struct timeval *tdiff, *t1, *t0;
   1184 {
   1185 
   1186 	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
   1187 	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
   1188 	if (tdiff->tv_usec < 0)
   1189 		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
   1190 }
   1191 
   1192 void
   1193 psabort()
   1194 {
   1195 
   1196 	abrtflag++;
   1197 }
   1198 
   1199 void
   1200 pswitch(flag)
   1201 	int flag;
   1202 {
   1203 	sig_t oldintr;
   1204 	static struct comvars {
   1205 		int connect;
   1206 		char name[MAXHOSTNAMELEN];
   1207 		struct sockaddr_in mctl;
   1208 		struct sockaddr_in hctl;
   1209 		FILE *in;
   1210 		FILE *out;
   1211 		int tpe;
   1212 		int curtpe;
   1213 		int cpnd;
   1214 		int sunqe;
   1215 		int runqe;
   1216 		int mcse;
   1217 		int ntflg;
   1218 		char nti[17];
   1219 		char nto[17];
   1220 		int mapflg;
   1221 		char mi[MAXPATHLEN];
   1222 		char mo[MAXPATHLEN];
   1223 	} proxstruct, tmpstruct;
   1224 	struct comvars *ip, *op;
   1225 
   1226 	abrtflag = 0;
   1227 	oldintr = signal(SIGINT, psabort);
   1228 	if (flag) {
   1229 		if (proxy)
   1230 			return;
   1231 		ip = &tmpstruct;
   1232 		op = &proxstruct;
   1233 		proxy++;
   1234 	} else {
   1235 		if (!proxy)
   1236 			return;
   1237 		ip = &proxstruct;
   1238 		op = &tmpstruct;
   1239 		proxy = 0;
   1240 	}
   1241 	ip->connect = connected;
   1242 	connected = op->connect;
   1243 	if (hostname) {
   1244 		(void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
   1245 		ip->name[strlen(ip->name)] = '\0';
   1246 	} else
   1247 		ip->name[0] = 0;
   1248 	hostname = op->name;
   1249 	ip->hctl = hisctladdr;
   1250 	hisctladdr = op->hctl;
   1251 	ip->mctl = myctladdr;
   1252 	myctladdr = op->mctl;
   1253 	ip->in = cin;
   1254 	cin = op->in;
   1255 	ip->out = cout;
   1256 	cout = op->out;
   1257 	ip->tpe = type;
   1258 	type = op->tpe;
   1259 	ip->curtpe = curtype;
   1260 	curtype = op->curtpe;
   1261 	ip->cpnd = cpend;
   1262 	cpend = op->cpnd;
   1263 	ip->sunqe = sunique;
   1264 	sunique = op->sunqe;
   1265 	ip->runqe = runique;
   1266 	runique = op->runqe;
   1267 	ip->mcse = mcase;
   1268 	mcase = op->mcse;
   1269 	ip->ntflg = ntflag;
   1270 	ntflag = op->ntflg;
   1271 	(void) strncpy(ip->nti, ntin, 16);
   1272 	(ip->nti)[strlen(ip->nti)] = '\0';
   1273 	(void) strcpy(ntin, op->nti);
   1274 	(void) strncpy(ip->nto, ntout, 16);
   1275 	(ip->nto)[strlen(ip->nto)] = '\0';
   1276 	(void) strcpy(ntout, op->nto);
   1277 	ip->mapflg = mapflag;
   1278 	mapflag = op->mapflg;
   1279 	(void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
   1280 	(ip->mi)[strlen(ip->mi)] = '\0';
   1281 	(void) strcpy(mapin, op->mi);
   1282 	(void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
   1283 	(ip->mo)[strlen(ip->mo)] = '\0';
   1284 	(void) strcpy(mapout, op->mo);
   1285 	(void) signal(SIGINT, oldintr);
   1286 	if (abrtflag) {
   1287 		abrtflag = 0;
   1288 		(*oldintr)(SIGINT);
   1289 	}
   1290 }
   1291 
   1292 void
   1293 abortpt()
   1294 {
   1295 
   1296 	printf("\n");
   1297 	(void) fflush(stdout);
   1298 	ptabflg++;
   1299 	mflag = 0;
   1300 	abrtflag = 0;
   1301 	longjmp(ptabort, 1);
   1302 }
   1303 
   1304 void
   1305 proxtrans(cmd, local, remote)
   1306 	char *cmd, *local, *remote;
   1307 {
   1308 	sig_t oldintr;
   1309 	int secndflag = 0, prox_type, nfnd;
   1310 	char *cmd2;
   1311 	struct fd_set mask;
   1312 
   1313 	if (strcmp(cmd, "RETR"))
   1314 		cmd2 = "RETR";
   1315 	else
   1316 		cmd2 = runique ? "STOU" : "STOR";
   1317 	if ((prox_type = type) == 0) {
   1318 		if (unix_server && unix_proxy)
   1319 			prox_type = TYPE_I;
   1320 		else
   1321 			prox_type = TYPE_A;
   1322 	}
   1323 	if (curtype != prox_type)
   1324 		changetype(prox_type, 1);
   1325 	if (command("PASV") != COMPLETE) {
   1326 		printf("proxy server does not support third party transfers.\n");
   1327 		return;
   1328 	}
   1329 	pswitch(0);
   1330 	if (!connected) {
   1331 		printf("No primary connection\n");
   1332 		pswitch(1);
   1333 		code = -1;
   1334 		return;
   1335 	}
   1336 	if (curtype != prox_type)
   1337 		changetype(prox_type, 1);
   1338 	if (command("PORT %s", pasv) != COMPLETE) {
   1339 		pswitch(1);
   1340 		return;
   1341 	}
   1342 	if (setjmp(ptabort))
   1343 		goto abort;
   1344 	oldintr = signal(SIGINT, abortpt);
   1345 	if (command("%s %s", cmd, remote) != PRELIM) {
   1346 		(void) signal(SIGINT, oldintr);
   1347 		pswitch(1);
   1348 		return;
   1349 	}
   1350 	sleep(2);
   1351 	pswitch(1);
   1352 	secndflag++;
   1353 	if (command("%s %s", cmd2, local) != PRELIM)
   1354 		goto abort;
   1355 	ptflag++;
   1356 	(void) getreply(0);
   1357 	pswitch(0);
   1358 	(void) getreply(0);
   1359 	(void) signal(SIGINT, oldintr);
   1360 	pswitch(1);
   1361 	ptflag = 0;
   1362 	printf("local: %s remote: %s\n", local, remote);
   1363 	return;
   1364 abort:
   1365 	(void) signal(SIGINT, SIG_IGN);
   1366 	ptflag = 0;
   1367 	if (strcmp(cmd, "RETR") && !proxy)
   1368 		pswitch(1);
   1369 	else if (!strcmp(cmd, "RETR") && proxy)
   1370 		pswitch(0);
   1371 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
   1372 		if (command("%s %s", cmd2, local) != PRELIM) {
   1373 			pswitch(0);
   1374 			if (cpend)
   1375 				abort_remote((FILE *) NULL);
   1376 		}
   1377 		pswitch(1);
   1378 		if (ptabflg)
   1379 			code = -1;
   1380 		(void) signal(SIGINT, oldintr);
   1381 		return;
   1382 	}
   1383 	if (cpend)
   1384 		abort_remote((FILE *) NULL);
   1385 	pswitch(!proxy);
   1386 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
   1387 		if (command("%s %s", cmd2, local) != PRELIM) {
   1388 			pswitch(0);
   1389 			if (cpend)
   1390 				abort_remote((FILE *) NULL);
   1391 			pswitch(1);
   1392 			if (ptabflg)
   1393 				code = -1;
   1394 			(void) signal(SIGINT, oldintr);
   1395 			return;
   1396 		}
   1397 	}
   1398 	if (cpend)
   1399 		abort_remote((FILE *) NULL);
   1400 	pswitch(!proxy);
   1401 	if (cpend) {
   1402 		FD_ZERO(&mask);
   1403 		FD_SET(fileno(cin), &mask);
   1404 		if ((nfnd = empty(&mask, 10)) <= 0) {
   1405 			if (nfnd < 0) {
   1406 				warn("abort");
   1407 			}
   1408 			if (ptabflg)
   1409 				code = -1;
   1410 			lostpeer();
   1411 		}
   1412 		(void) getreply(0);
   1413 		(void) getreply(0);
   1414 	}
   1415 	if (proxy)
   1416 		pswitch(0);
   1417 	pswitch(1);
   1418 	if (ptabflg)
   1419 		code = -1;
   1420 	(void) signal(SIGINT, oldintr);
   1421 }
   1422 
   1423 void
   1424 reset(argc, argv)
   1425 	int argc;
   1426 	char *argv[];
   1427 {
   1428 	struct fd_set mask;
   1429 	int nfnd = 1;
   1430 
   1431 	FD_ZERO(&mask);
   1432 	while (nfnd > 0) {
   1433 		FD_SET(fileno(cin), &mask);
   1434 		if ((nfnd = empty(&mask,0)) < 0) {
   1435 			warn("reset");
   1436 			code = -1;
   1437 			lostpeer();
   1438 		}
   1439 		else if (nfnd) {
   1440 			(void) getreply(0);
   1441 		}
   1442 	}
   1443 }
   1444 
   1445 char *
   1446 gunique(local)
   1447 	char *local;
   1448 {
   1449 	static char new[MAXPATHLEN];
   1450 	char *cp = strrchr(local, '/');
   1451 	int d, count=0;
   1452 	char ext = '1';
   1453 
   1454 	if (cp)
   1455 		*cp = '\0';
   1456 	d = access(cp ? local : ".", 2);
   1457 	if (cp)
   1458 		*cp = '/';
   1459 	if (d < 0) {
   1460 		warn("local: %s", local);
   1461 		return ((char *) 0);
   1462 	}
   1463 	(void) strcpy(new, local);
   1464 	cp = new + strlen(new);
   1465 	*cp++ = '.';
   1466 	while (!d) {
   1467 		if (++count == 100) {
   1468 			printf("runique: can't find unique file name.\n");
   1469 			return ((char *) 0);
   1470 		}
   1471 		*cp++ = ext;
   1472 		*cp = '\0';
   1473 		if (ext == '9')
   1474 			ext = '0';
   1475 		else
   1476 			ext++;
   1477 		if ((d = access(new, 0)) < 0)
   1478 			break;
   1479 		if (ext != '0')
   1480 			cp--;
   1481 		else if (*(cp - 2) == '.')
   1482 			*(cp - 1) = '1';
   1483 		else {
   1484 			*(cp - 2) = *(cp - 2) + 1;
   1485 			cp--;
   1486 		}
   1487 	}
   1488 	return (new);
   1489 }
   1490 
   1491 void
   1492 abort_remote(din)
   1493 	FILE *din;
   1494 {
   1495 	char buf[BUFSIZ];
   1496 	int nfnd;
   1497 	struct fd_set mask;
   1498 
   1499 	/*
   1500 	 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
   1501 	 * after urgent byte rather than before as is protocol now
   1502 	 */
   1503 	sprintf(buf, "%c%c%c", IAC, IP, IAC);
   1504 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
   1505 		warn("abort");
   1506 	fprintf(cout,"%cABOR\r\n", DM);
   1507 	(void) fflush(cout);
   1508 	FD_ZERO(&mask);
   1509 	FD_SET(fileno(cin), &mask);
   1510 	if (din) {
   1511 		FD_SET(fileno(din), &mask);
   1512 	}
   1513 	if ((nfnd = empty(&mask, 10)) <= 0) {
   1514 		if (nfnd < 0) {
   1515 			warn("abort");
   1516 		}
   1517 		if (ptabflg)
   1518 			code = -1;
   1519 		lostpeer();
   1520 	}
   1521 	if (din && FD_ISSET(fileno(din), &mask)) {
   1522 		while (read(fileno(din), buf, BUFSIZ) > 0)
   1523 			/* LOOP */;
   1524 	}
   1525 	if (getreply(0) == ERROR && code == 552) {
   1526 		/* 552 needed for nic style abort */
   1527 		(void) getreply(0);
   1528 	}
   1529 	(void) getreply(0);
   1530 }
   1531