xref: /csrg-svn/usr.bin/ftp/ftp.c (revision 37225)
121739Sdist /*
236942Skarels  * Copyright (c) 1985, 1989 Regents of the University of California.
333737Sbostic  * All rights reserved.
433737Sbostic  *
533737Sbostic  * Redistribution and use in source and binary forms are permitted
634901Sbostic  * provided that the above copyright notice and this paragraph are
734901Sbostic  * duplicated in all such forms and that any documentation,
834901Sbostic  * advertising materials, and other materials related to such
934901Sbostic  * distribution and use acknowledge that the software was developed
1034901Sbostic  * by the University of California, Berkeley.  The name of the
1134901Sbostic  * University may not be used to endorse or promote products derived
1234901Sbostic  * from this software without specific prior written permission.
1334901Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434901Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1536935Skarels  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621739Sdist  */
1721739Sdist 
1810296Ssam #ifndef lint
19*37225Skarels static char sccsid[] = "@(#)ftp.c	5.26 (Berkeley) 03/21/89";
2033737Sbostic #endif /* not lint */
2110296Ssam 
2236940Skarels #include <sys/param.h>
2310296Ssam #include <sys/stat.h>
2410296Ssam #include <sys/ioctl.h>
2510296Ssam #include <sys/socket.h>
2613614Ssam #include <sys/time.h>
2736935Skarels #include <sys/file.h>
2810296Ssam 
2910296Ssam #include <netinet/in.h>
3012397Ssam #include <arpa/ftp.h>
3126048Sminshall #include <arpa/telnet.h>
3210296Ssam 
3310296Ssam #include <stdio.h>
3410296Ssam #include <signal.h>
3510296Ssam #include <errno.h>
3610296Ssam #include <netdb.h>
3726048Sminshall #include <fcntl.h>
3826048Sminshall #include <pwd.h>
3910296Ssam 
4036940Skarels #include "ftp_var.h"
4136940Skarels 
4210296Ssam struct	sockaddr_in hisctladdr;
4310296Ssam struct	sockaddr_in data_addr;
4410296Ssam int	data = -1;
4526048Sminshall int	abrtflag = 0;
4626048Sminshall int	ptflag = 0;
4710296Ssam int	connected;
4810296Ssam struct	sockaddr_in myctladdr;
4926496Sminshall uid_t	getuid();
50*37225Skarels off_t	restart_point = 0;
5110296Ssam 
5210296Ssam FILE	*cin, *cout;
5310296Ssam FILE	*dataconn();
5410296Ssam 
5525904Skarels char *
5610296Ssam hookup(host, port)
5710296Ssam 	char *host;
5810296Ssam 	int port;
5910296Ssam {
6025904Skarels 	register struct hostent *hp = 0;
6127687Sminshall 	int s,len;
6225904Skarels 	static char hostnamebuf[80];
6310296Ssam 
6410296Ssam 	bzero((char *)&hisctladdr, sizeof (hisctladdr));
6525904Skarels 	hisctladdr.sin_addr.s_addr = inet_addr(host);
6625904Skarels 	if (hisctladdr.sin_addr.s_addr != -1) {
6725904Skarels 		hisctladdr.sin_family = AF_INET;
6836940Skarels 		(void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
6936940Skarels 	} else {
7025100Sbloom 		hp = gethostbyname(host);
7125904Skarels 		if (hp == NULL) {
7235792Sbostic 			fprintf(stderr, "ftp: %s: ", host);
7335792Sbostic 			herror((char *)NULL);
7426048Sminshall 			code = -1;
7526048Sminshall 			return((char *) 0);
7625904Skarels 		}
7725904Skarels 		hisctladdr.sin_family = hp->h_addrtype;
7825904Skarels 		bcopy(hp->h_addr_list[0],
7925904Skarels 		    (caddr_t)&hisctladdr.sin_addr, hp->h_length);
8036940Skarels 		(void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
8110296Ssam 	}
8225904Skarels 	hostname = hostnamebuf;
8325904Skarels 	s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
8410296Ssam 	if (s < 0) {
8510296Ssam 		perror("ftp: socket");
8626048Sminshall 		code = -1;
8710296Ssam 		return (0);
8810296Ssam 	}
8910296Ssam 	hisctladdr.sin_port = port;
9026496Sminshall 	while (connect(s, &hisctladdr, sizeof (hisctladdr)) < 0) {
9125904Skarels 		if (hp && hp->h_addr_list[1]) {
9225904Skarels 			int oerrno = errno;
9325904Skarels 
9425904Skarels 			fprintf(stderr, "ftp: connect to address %s: ",
9525904Skarels 				inet_ntoa(hisctladdr.sin_addr));
9625904Skarels 			errno = oerrno;
9726496Sminshall 			perror((char *) 0);
9825904Skarels 			hp->h_addr_list++;
9925904Skarels 			bcopy(hp->h_addr_list[0],
10026048Sminshall 			     (caddr_t)&hisctladdr.sin_addr, hp->h_length);
10126496Sminshall 			fprintf(stdout, "Trying %s...\n",
10225904Skarels 				inet_ntoa(hisctladdr.sin_addr));
10326813Skarels 			(void) close(s);
10426813Skarels 			s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
10526813Skarels 			if (s < 0) {
10626813Skarels 				perror("ftp: socket");
10726813Skarels 				code = -1;
10826813Skarels 				return (0);
10926813Skarels 			}
11025904Skarels 			continue;
11125904Skarels 		}
11210296Ssam 		perror("ftp: connect");
11326048Sminshall 		code = -1;
11410296Ssam 		goto bad;
11510296Ssam 	}
11611627Ssam 	len = sizeof (myctladdr);
11711627Ssam 	if (getsockname(s, (char *)&myctladdr, &len) < 0) {
11811627Ssam 		perror("ftp: getsockname");
11926048Sminshall 		code = -1;
12010296Ssam 		goto bad;
12110296Ssam 	}
12210296Ssam 	cin = fdopen(s, "r");
12310296Ssam 	cout = fdopen(s, "w");
12411219Ssam 	if (cin == NULL || cout == NULL) {
12510296Ssam 		fprintf(stderr, "ftp: fdopen failed.\n");
12610296Ssam 		if (cin)
12726496Sminshall 			(void) fclose(cin);
12810296Ssam 		if (cout)
12926496Sminshall 			(void) fclose(cout);
13026048Sminshall 		code = -1;
13110296Ssam 		goto bad;
13210296Ssam 	}
13310296Ssam 	if (verbose)
13426067Sminshall 		printf("Connected to %s.\n", hostname);
13527687Sminshall 	if (getreply(0) > 2) { 	/* read startup message from server */
13626048Sminshall 		if (cin)
13726496Sminshall 			(void) fclose(cin);
13826048Sminshall 		if (cout)
13926496Sminshall 			(void) fclose(cout);
14026048Sminshall 		code = -1;
14126048Sminshall 		goto bad;
14226048Sminshall 	}
14327687Sminshall #ifdef SO_OOBINLINE
14427687Sminshall 	{
14527687Sminshall 	int on = 1;
14626048Sminshall 
14727687Sminshall 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on))
14827687Sminshall 		< 0 && debug) {
14927687Sminshall 			perror("ftp: setsockopt");
15027687Sminshall 		}
15127687Sminshall 	}
15227687Sminshall #endif SO_OOBINLINE
15326048Sminshall 
15425904Skarels 	return (hostname);
15510296Ssam bad:
15626496Sminshall 	(void) close(s);
15725904Skarels 	return ((char *)0);
15810296Ssam }
15910296Ssam 
16025904Skarels login(host)
16125904Skarels 	char *host;
16210296Ssam {
16326048Sminshall 	char tmp[80];
16435659Sbostic 	char *user, *pass, *acct, *getlogin(), *getpass();
16526048Sminshall 	int n, aflag = 0;
16610296Ssam 
16726048Sminshall 	user = pass = acct = 0;
16826048Sminshall 	if (ruserpass(host, &user, &pass, &acct) < 0) {
16926048Sminshall 		code = -1;
17026048Sminshall 		return(0);
17126048Sminshall 	}
17226048Sminshall 	if (user == NULL) {
17326048Sminshall 		char *myname = getlogin();
17426048Sminshall 
17526048Sminshall 		if (myname == NULL) {
17626048Sminshall 			struct passwd *pp = getpwuid(getuid());
17726048Sminshall 
17826448Slepreau 			if (pp != NULL)
17926048Sminshall 				myname = pp->pw_name;
18026048Sminshall 		}
18126048Sminshall 		printf("Name (%s:%s): ", host, myname);
18226048Sminshall 		(void) fgets(tmp, sizeof(tmp) - 1, stdin);
18326048Sminshall 		tmp[strlen(tmp) - 1] = '\0';
18426448Slepreau 		if (*tmp == '\0')
18526048Sminshall 			user = myname;
18626448Slepreau 		else
18726048Sminshall 			user = tmp;
18826048Sminshall 	}
18910296Ssam 	n = command("USER %s", user);
19026048Sminshall 	if (n == CONTINUE) {
19126448Slepreau 		if (pass == NULL)
19235659Sbostic 			pass = getpass("Password:");
19310296Ssam 		n = command("PASS %s", pass);
19426048Sminshall 	}
19510296Ssam 	if (n == CONTINUE) {
19626048Sminshall 		aflag++;
19735659Sbostic 		acct = getpass("Account:");
19810296Ssam 		n = command("ACCT %s", acct);
19910296Ssam 	}
20010296Ssam 	if (n != COMPLETE) {
20110296Ssam 		fprintf(stderr, "Login failed.\n");
20210296Ssam 		return (0);
20310296Ssam 	}
20426448Slepreau 	if (!aflag && acct != NULL)
20526048Sminshall 		(void) command("ACCT %s", acct);
20626448Slepreau 	if (proxy)
20726048Sminshall 		return(1);
20826048Sminshall 	for (n = 0; n < macnum; ++n) {
20926048Sminshall 		if (!strcmp("init", macros[n].mac_name)) {
21026496Sminshall 			(void) strcpy(line, "$init");
21126048Sminshall 			makeargv();
21226048Sminshall 			domacro(margc, margv);
21326048Sminshall 			break;
21426048Sminshall 		}
21526048Sminshall 	}
21610296Ssam 	return (1);
21710296Ssam }
21810296Ssam 
21926048Sminshall cmdabort()
22026048Sminshall {
22126048Sminshall 	extern jmp_buf ptabort;
22226048Sminshall 
22326048Sminshall 	printf("\n");
22426048Sminshall 	(void) fflush(stdout);
22526048Sminshall 	abrtflag++;
22626448Slepreau 	if (ptflag)
22726048Sminshall 		longjmp(ptabort,1);
22826048Sminshall }
22926048Sminshall 
23026496Sminshall /*VARARGS1*/
23110296Ssam command(fmt, args)
23210296Ssam 	char *fmt;
23310296Ssam {
23426048Sminshall 	int r, (*oldintr)(), cmdabort();
23510296Ssam 
23626048Sminshall 	abrtflag = 0;
23710296Ssam 	if (debug) {
23810296Ssam 		printf("---> ");
23910296Ssam 		_doprnt(fmt, &args, stdout);
24010296Ssam 		printf("\n");
24110296Ssam 		(void) fflush(stdout);
24210296Ssam 	}
24311219Ssam 	if (cout == NULL) {
24411219Ssam 		perror ("No control connection for command");
24526048Sminshall 		code = -1;
24611219Ssam 		return (0);
24711219Ssam 	}
24826048Sminshall 	oldintr = signal(SIGINT,cmdabort);
24910296Ssam 	_doprnt(fmt, &args, cout);
25010296Ssam 	fprintf(cout, "\r\n");
25110296Ssam 	(void) fflush(cout);
25226048Sminshall 	cpend = 1;
25326048Sminshall 	r = getreply(!strcmp(fmt, "QUIT"));
25426448Slepreau 	if (abrtflag && oldintr != SIG_IGN)
25526048Sminshall 		(*oldintr)();
25626048Sminshall 	(void) signal(SIGINT, oldintr);
25726048Sminshall 	return(r);
25810296Ssam }
25910296Ssam 
26036935Skarels char reply_string[BUFSIZ];
26136935Skarels 
26210296Ssam #include <ctype.h>
26310296Ssam 
26410296Ssam getreply(expecteof)
26510296Ssam 	int expecteof;
26610296Ssam {
26711219Ssam 	register int c, n;
26826048Sminshall 	register int dig;
26936935Skarels 	register char *cp;
27026048Sminshall 	int originalcode = 0, continuation = 0, (*oldintr)(), cmdabort();
27126048Sminshall 	int pflag = 0;
27226048Sminshall 	char *pt = pasv;
27310296Ssam 
27436935Skarels 	cp = reply_string;
27526048Sminshall 	oldintr = signal(SIGINT,cmdabort);
27610296Ssam 	for (;;) {
27710296Ssam 		dig = n = code = 0;
27810296Ssam 		while ((c = getc(cin)) != '\n') {
27927687Sminshall 			if (c == IAC) {     /* handle telnet commands */
28027687Sminshall 				switch (c = getc(cin)) {
28127687Sminshall 				case WILL:
28227687Sminshall 				case WONT:
28327687Sminshall 					c = getc(cin);
28436940Skarels 					fprintf(cout, "%c%c%c",IAC,DONT,c);
28527687Sminshall 					(void) fflush(cout);
28627687Sminshall 					break;
28727687Sminshall 				case DO:
28827687Sminshall 				case DONT:
28927687Sminshall 					c = getc(cin);
29036940Skarels 					fprintf(cout, "%c%c%c",IAC,WONT,c);
29127687Sminshall 					(void) fflush(cout);
29227687Sminshall 					break;
29327687Sminshall 				default:
29427687Sminshall 					break;
29527687Sminshall 				}
29627687Sminshall 				continue;
29727687Sminshall 			}
29810296Ssam 			dig++;
29910296Ssam 			if (c == EOF) {
30026048Sminshall 				if (expecteof) {
30126048Sminshall 					(void) signal(SIGINT,oldintr);
30226048Sminshall 					code = 221;
30310296Ssam 					return (0);
30426048Sminshall 				}
30510296Ssam 				lostpeer();
30626048Sminshall 				if (verbose) {
30726048Sminshall 					printf("421 Service not available, remote server has closed connection\n");
30826048Sminshall 					(void) fflush(stdout);
30926048Sminshall 				}
31033772Scsvsj 				code = 421;
31133772Scsvsj 				return(4);
31210296Ssam 			}
31326048Sminshall 			if (c != '\r' && (verbose > 0 ||
31426048Sminshall 			    (verbose > -1 && n == '5' && dig > 4))) {
31526448Slepreau 				if (proxflag &&
31626448Slepreau 				   (dig == 1 || dig == 5 && verbose == 0))
31726048Sminshall 					printf("%s:",hostname);
31826496Sminshall 				(void) putchar(c);
31926048Sminshall 			}
32010296Ssam 			if (dig < 4 && isdigit(c))
32110296Ssam 				code = code * 10 + (c - '0');
32226448Slepreau 			if (!pflag && code == 227)
32326048Sminshall 				pflag = 1;
32426448Slepreau 			if (dig > 4 && pflag == 1 && isdigit(c))
32526048Sminshall 				pflag = 2;
32626048Sminshall 			if (pflag == 2) {
32726448Slepreau 				if (c != '\r' && c != ')')
32826048Sminshall 					*pt++ = c;
32926048Sminshall 				else {
33026048Sminshall 					*pt = '\0';
33126048Sminshall 					pflag = 3;
33226048Sminshall 				}
33326048Sminshall 			}
33426048Sminshall 			if (dig == 4 && c == '-') {
33526448Slepreau 				if (continuation)
33626048Sminshall 					code = 0;
33710296Ssam 				continuation++;
33826048Sminshall 			}
33910296Ssam 			if (n == 0)
34010296Ssam 				n = c;
34136935Skarels 			*cp++ = c;
34210296Ssam 		}
34326048Sminshall 		if (verbose > 0 || verbose > -1 && n == '5') {
34426496Sminshall 			(void) putchar(c);
34511346Ssam 			(void) fflush (stdout);
34611346Ssam 		}
34710296Ssam 		if (continuation && code != originalcode) {
34810296Ssam 			if (originalcode == 0)
34910296Ssam 				originalcode = code;
35010296Ssam 			continue;
35110296Ssam 		}
35236935Skarels 		*cp = '\0';
35326448Slepreau 		if (n != '1')
35426048Sminshall 			cpend = 0;
35526048Sminshall 		(void) signal(SIGINT,oldintr);
35626448Slepreau 		if (code == 421 || originalcode == 421)
35726048Sminshall 			lostpeer();
35826448Slepreau 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
35926048Sminshall 			(*oldintr)();
36025907Smckusick 		return (n - '0');
36110296Ssam 	}
36210296Ssam }
36310296Ssam 
36426048Sminshall empty(mask, sec)
36527687Sminshall 	struct fd_set *mask;
36626048Sminshall 	int sec;
36726048Sminshall {
36826048Sminshall 	struct timeval t;
36926048Sminshall 
37026048Sminshall 	t.tv_sec = (long) sec;
37126048Sminshall 	t.tv_usec = 0;
37227687Sminshall 	return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
37326048Sminshall }
37426048Sminshall 
37510296Ssam jmp_buf	sendabort;
37610296Ssam 
37710296Ssam abortsend()
37810296Ssam {
37910296Ssam 
38026048Sminshall 	mflag = 0;
38126048Sminshall 	abrtflag = 0;
38226048Sminshall 	printf("\nsend aborted\n");
38326048Sminshall 	(void) fflush(stdout);
38410296Ssam 	longjmp(sendabort, 1);
38510296Ssam }
38610296Ssam 
38736940Skarels #define HASHBYTES 1024
38836940Skarels 
389*37225Skarels sendrequest(cmd, local, remote, printnames)
39010296Ssam 	char *cmd, *local, *remote;
391*37225Skarels 	int printnames;
39210296Ssam {
39335659Sbostic 	FILE *fin, *dout = 0, *popen();
39435659Sbostic 	int (*closefunc)(), pclose(), fclose(), (*oldintr)(), (*oldintp)();
39526048Sminshall 	int abortsend();
39636942Skarels 	char buf[BUFSIZ], *bufp;
39736940Skarels 	long bytes = 0, hashbytes = HASHBYTES;
39811346Ssam 	register int c, d;
39910296Ssam 	struct stat st;
40010296Ssam 	struct timeval start, stop;
40136935Skarels 	char *mode;
40210296Ssam 
403*37225Skarels 	if (verbose && printnames) {
404*37225Skarels 		if (local && *local != '-')
405*37225Skarels 			printf("local: %s ", local);
406*37225Skarels 		if (remote)
407*37225Skarels 			printf("remote: %s\n", remote);
408*37225Skarels 	}
40926048Sminshall 	if (proxy) {
41026048Sminshall 		proxtrans(cmd, local, remote);
41126048Sminshall 		return;
41226048Sminshall 	}
41310296Ssam 	closefunc = NULL;
41426048Sminshall 	oldintr = NULL;
41526048Sminshall 	oldintp = NULL;
41636935Skarels 	mode = "w";
41726048Sminshall 	if (setjmp(sendabort)) {
41826048Sminshall 		while (cpend) {
41926048Sminshall 			(void) getreply(0);
42026048Sminshall 		}
42126048Sminshall 		if (data >= 0) {
42226048Sminshall 			(void) close(data);
42326048Sminshall 			data = -1;
42426048Sminshall 		}
42526448Slepreau 		if (oldintr)
42626048Sminshall 			(void) signal(SIGINT,oldintr);
42726448Slepreau 		if (oldintp)
42826048Sminshall 			(void) signal(SIGPIPE,oldintp);
42926048Sminshall 		code = -1;
43026048Sminshall 		return;
43126048Sminshall 	}
43210296Ssam 	oldintr = signal(SIGINT, abortsend);
43310296Ssam 	if (strcmp(local, "-") == 0)
43410296Ssam 		fin = stdin;
43510296Ssam 	else if (*local == '|') {
43626048Sminshall 		oldintp = signal(SIGPIPE,SIG_IGN);
43735659Sbostic 		fin = popen(local + 1, "r");
43810296Ssam 		if (fin == NULL) {
43926048Sminshall 			perror(local + 1);
44026048Sminshall 			(void) signal(SIGINT, oldintr);
44126048Sminshall 			(void) signal(SIGPIPE, oldintp);
44226048Sminshall 			code = -1;
44326048Sminshall 			return;
44410296Ssam 		}
44535659Sbostic 		closefunc = pclose;
44610296Ssam 	} else {
44710296Ssam 		fin = fopen(local, "r");
44810296Ssam 		if (fin == NULL) {
44910296Ssam 			perror(local);
45026048Sminshall 			(void) signal(SIGINT, oldintr);
45126048Sminshall 			code = -1;
45226048Sminshall 			return;
45310296Ssam 		}
45410296Ssam 		closefunc = fclose;
45510296Ssam 		if (fstat(fileno(fin), &st) < 0 ||
45610296Ssam 		    (st.st_mode&S_IFMT) != S_IFREG) {
45726496Sminshall 			fprintf(stdout, "%s: not a plain file.\n", local);
45826048Sminshall 			(void) signal(SIGINT, oldintr);
45936935Skarels 			fclose(fin);
46026048Sminshall 			code = -1;
46126048Sminshall 			return;
46210296Ssam 		}
46310296Ssam 	}
46426048Sminshall 	if (initconn()) {
46526048Sminshall 		(void) signal(SIGINT, oldintr);
46626448Slepreau 		if (oldintp)
46726048Sminshall 			(void) signal(SIGPIPE, oldintp);
46826048Sminshall 		code = -1;
46936935Skarels 		if (closefunc != NULL)
47036935Skarels 			(*closefunc)(fin);
47126048Sminshall 		return;
47226048Sminshall 	}
47326448Slepreau 	if (setjmp(sendabort))
47426048Sminshall 		goto abort;
47536935Skarels 
476*37225Skarels 	if (restart_point &&
477*37225Skarels 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
478*37225Skarels 		if (fseek(fin, (long) restart_point, 0) < 0) {
479*37225Skarels 			perror(local);
480*37225Skarels 			restart_point = 0;
481*37225Skarels 			if (closefunc != NULL)
482*37225Skarels 				(*closefunc)(fin);
483*37225Skarels 			return;
484*37225Skarels 		}
485*37225Skarels 		if (command("REST %ld", (long) restart_point)
486*37225Skarels 			!= CONTINUE) {
487*37225Skarels 			restart_point = 0;
488*37225Skarels 			if (closefunc != NULL)
489*37225Skarels 				(*closefunc)(fin);
490*37225Skarels 			return;
491*37225Skarels 		}
492*37225Skarels 		restart_point = 0;
493*37225Skarels 		mode = "r+w";
494*37225Skarels 	}
49510296Ssam 	if (remote) {
49626048Sminshall 		if (command("%s %s", cmd, remote) != PRELIM) {
49726048Sminshall 			(void) signal(SIGINT, oldintr);
49826448Slepreau 			if (oldintp)
49926048Sminshall 				(void) signal(SIGPIPE, oldintp);
50036935Skarels 			if (closefunc != NULL)
50136935Skarels 				(*closefunc)(fin);
50226048Sminshall 			return;
50326048Sminshall 		}
50410296Ssam 	} else
50526048Sminshall 		if (command("%s", cmd) != PRELIM) {
50626048Sminshall 			(void) signal(SIGINT, oldintr);
50726448Slepreau 			if (oldintp)
50826048Sminshall 				(void) signal(SIGPIPE, oldintp);
50936935Skarels 			if (closefunc != NULL)
51036935Skarels 				(*closefunc)(fin);
51126048Sminshall 			return;
51226048Sminshall 		}
51336935Skarels 	dout = dataconn(mode);
51426448Slepreau 	if (dout == NULL)
51526048Sminshall 		goto abort;
51626496Sminshall 	(void) gettimeofday(&start, (struct timezone *)0);
51736935Skarels 	oldintp = signal(SIGPIPE, SIG_IGN);
51811219Ssam 	switch (type) {
51911219Ssam 
52011219Ssam 	case TYPE_I:
52111219Ssam 	case TYPE_L:
52211346Ssam 		errno = d = 0;
52336942Skarels 		while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
52411219Ssam 			bytes += c;
52536942Skarels 			for (bufp = buf; c > 0; c -= d, bufp += d)
52636942Skarels 				if ((d = write(fileno(dout), bufp, c)) <= 0)
52736942Skarels 					break;
52811651Ssam 			if (hash) {
52936940Skarels 				while (bytes >= hashbytes) {
53036940Skarels 					(void) putchar('#');
53136940Skarels 					hashbytes += HASHBYTES;
53236940Skarels 				}
53326496Sminshall 				(void) fflush(stdout);
53411651Ssam 			}
53511219Ssam 		}
53613213Ssam 		if (hash && bytes > 0) {
53736940Skarels 			if (bytes < HASHBYTES)
53836940Skarels 				(void) putchar('#');
53926496Sminshall 			(void) putchar('\n');
54026496Sminshall 			(void) fflush(stdout);
54111651Ssam 		}
54211219Ssam 		if (c < 0)
54311219Ssam 			perror(local);
54436942Skarels 		if (d <= 0) {
54536942Skarels 			if (d == 0)
54636942Skarels 				fprintf(stderr, "netout: write returned 0?\n");
54736942Skarels 			else if (errno != EPIPE)
54836935Skarels 				perror("netout");
54936935Skarels 			bytes = -1;
55036935Skarels 		}
55111219Ssam 		break;
55211219Ssam 
55311219Ssam 	case TYPE_A:
55411219Ssam 		while ((c = getc(fin)) != EOF) {
55511219Ssam 			if (c == '\n') {
55611651Ssam 				while (hash && (bytes >= hashbytes)) {
55726496Sminshall 					(void) putchar('#');
55826496Sminshall 					(void) fflush(stdout);
55936940Skarels 					hashbytes += HASHBYTES;
56011651Ssam 				}
56111219Ssam 				if (ferror(dout))
56211219Ssam 					break;
56326496Sminshall 				(void) putc('\r', dout);
56411219Ssam 				bytes++;
56511219Ssam 			}
56626496Sminshall 			(void) putc(c, dout);
56711219Ssam 			bytes++;
56826048Sminshall 	/*		if (c == '\r') {			  	*/
56926496Sminshall 	/*		(void)	putc('\0', dout);  /* this violates rfc */
57026048Sminshall 	/*			bytes++;				*/
57126048Sminshall 	/*		}                          			*/
57211219Ssam 		}
57311651Ssam 		if (hash) {
57413213Ssam 			if (bytes < hashbytes)
57526496Sminshall 				(void) putchar('#');
57626496Sminshall 			(void) putchar('\n');
57726496Sminshall 			(void) fflush(stdout);
57811651Ssam 		}
57911219Ssam 		if (ferror(fin))
58011219Ssam 			perror(local);
58136935Skarels 		if (ferror(dout)) {
58236935Skarels 			if (errno != EPIPE)
58336935Skarels 				perror("netout");
58436935Skarels 			bytes = -1;
58536935Skarels 		}
58611219Ssam 		break;
58710296Ssam 	}
58826496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
58910296Ssam 	if (closefunc != NULL)
59026048Sminshall 		(*closefunc)(fin);
59110296Ssam 	(void) fclose(dout);
59226048Sminshall 	(void) getreply(0);
59326048Sminshall 	(void) signal(SIGINT, oldintr);
59436935Skarels 	if (oldintp)
59536935Skarels 		(void) signal(SIGPIPE, oldintp);
59635699Sbostic 	if (bytes > 0)
597*37225Skarels 		ptransfer("sent", bytes, &start, &stop);
59810296Ssam 	return;
59926048Sminshall abort:
60026496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
60126048Sminshall 	(void) signal(SIGINT, oldintr);
60226448Slepreau 	if (oldintp)
60326048Sminshall 		(void) signal(SIGPIPE, oldintp);
60426048Sminshall 	if (!cpend) {
60526048Sminshall 		code = -1;
60626048Sminshall 		return;
60726048Sminshall 	}
60826048Sminshall 	if (data >= 0) {
60926048Sminshall 		(void) close(data);
61026048Sminshall 		data = -1;
61126048Sminshall 	}
61226448Slepreau 	if (dout)
61326048Sminshall 		(void) fclose(dout);
61426048Sminshall 	(void) getreply(0);
61526048Sminshall 	code = -1;
61610296Ssam 	if (closefunc != NULL && fin != NULL)
61726048Sminshall 		(*closefunc)(fin);
61835699Sbostic 	if (bytes > 0)
619*37225Skarels 		ptransfer("sent", bytes, &start, &stop);
62010296Ssam }
62110296Ssam 
62210296Ssam jmp_buf	recvabort;
62310296Ssam 
62410296Ssam abortrecv()
62510296Ssam {
62610296Ssam 
62726048Sminshall 	mflag = 0;
62826048Sminshall 	abrtflag = 0;
62926048Sminshall 	printf("\n");
63026048Sminshall 	(void) fflush(stdout);
63110296Ssam 	longjmp(recvabort, 1);
63210296Ssam }
63310296Ssam 
634*37225Skarels recvrequest(cmd, local, remote, mode, printnames)
63511651Ssam 	char *cmd, *local, *remote, *mode;
63610296Ssam {
63735659Sbostic 	FILE *fout, *din = 0, *popen();
63835659Sbostic 	int (*closefunc)(), pclose(), fclose(), (*oldintr)(), (*oldintp)();
63936935Skarels 	int abortrecv(), oldverbose, oldtype = 0, is_retr, tcrflag, nfnd;
64036944Skarels 	char *bufp, *gunique(), msg;
64136944Skarels 	static char *buf;
64236940Skarels 	static int bufsize;
64336940Skarels 	long bytes = 0, hashbytes = HASHBYTES;
64426496Sminshall 	struct fd_set mask;
64511346Ssam 	register int c, d;
64610296Ssam 	struct timeval start, stop;
64736940Skarels 	struct stat st;
64836940Skarels 	extern char *malloc();
64910296Ssam 
65036935Skarels 	is_retr = strcmp(cmd, "RETR") == 0;
651*37225Skarels 	if (is_retr && verbose && printnames) {
652*37225Skarels 		if (local && *local != '-')
653*37225Skarels 			printf("local: %s ", local);
654*37225Skarels 		if (remote)
655*37225Skarels 			printf("remote: %s\n", remote);
656*37225Skarels 	}
65736935Skarels 	if (proxy && is_retr) {
65826048Sminshall 		proxtrans(cmd, local, remote);
65926048Sminshall 		return;
66026048Sminshall 	}
66110296Ssam 	closefunc = NULL;
66226048Sminshall 	oldintr = NULL;
66326048Sminshall 	oldintp = NULL;
66436935Skarels 	tcrflag = !crflag && is_retr;
66526048Sminshall 	if (setjmp(recvabort)) {
66626048Sminshall 		while (cpend) {
66726048Sminshall 			(void) getreply(0);
66826048Sminshall 		}
66926048Sminshall 		if (data >= 0) {
67026048Sminshall 			(void) close(data);
67126048Sminshall 			data = -1;
67226048Sminshall 		}
67326448Slepreau 		if (oldintr)
67426048Sminshall 			(void) signal(SIGINT, oldintr);
67526048Sminshall 		code = -1;
67626048Sminshall 		return;
67726048Sminshall 	}
67810296Ssam 	oldintr = signal(SIGINT, abortrecv);
67926048Sminshall 	if (strcmp(local, "-") && *local != '|') {
68010296Ssam 		if (access(local, 2) < 0) {
68126048Sminshall 			char *dir = rindex(local, '/');
68210296Ssam 
68326048Sminshall 			if (errno != ENOENT && errno != EACCES) {
68410296Ssam 				perror(local);
68526048Sminshall 				(void) signal(SIGINT, oldintr);
68626048Sminshall 				code = -1;
68726048Sminshall 				return;
68810296Ssam 			}
68926048Sminshall 			if (dir != NULL)
69026048Sminshall 				*dir = 0;
69126048Sminshall 			d = access(dir ? local : ".", 2);
69226048Sminshall 			if (dir != NULL)
69326048Sminshall 				*dir = '/';
69426048Sminshall 			if (d < 0) {
69526048Sminshall 				perror(local);
69626048Sminshall 				(void) signal(SIGINT, oldintr);
69726048Sminshall 				code = -1;
69826048Sminshall 				return;
69926048Sminshall 			}
70026048Sminshall 			if (!runique && errno == EACCES &&
70136935Skarels 			    chmod(local, 0600) < 0) {
70226048Sminshall 				perror(local);
70326048Sminshall 				(void) signal(SIGINT, oldintr);
70426048Sminshall 				code = -1;
70526048Sminshall 				return;
70626048Sminshall 			}
70726048Sminshall 			if (runique && errno == EACCES &&
70826048Sminshall 			   (local = gunique(local)) == NULL) {
70926048Sminshall 				(void) signal(SIGINT, oldintr);
71026048Sminshall 				code = -1;
71126048Sminshall 				return;
71226048Sminshall 			}
71310296Ssam 		}
71426048Sminshall 		else if (runique && (local = gunique(local)) == NULL) {
71526048Sminshall 			(void) signal(SIGINT, oldintr);
71626048Sminshall 			code = -1;
71726048Sminshall 			return;
71826048Sminshall 		}
71926048Sminshall 	}
72026048Sminshall 	if (initconn()) {
72126048Sminshall 		(void) signal(SIGINT, oldintr);
72226048Sminshall 		code = -1;
72326048Sminshall 		return;
72426048Sminshall 	}
72526448Slepreau 	if (setjmp(recvabort))
72626048Sminshall 		goto abort;
72736935Skarels 	if (!is_retr) {
72836935Skarels 		if (type != TYPE_A) {
72936935Skarels 			oldtype = type;
73036935Skarels 			oldverbose = verbose;
73136935Skarels 			if (!debug)
73236935Skarels 				verbose = 0;
73336935Skarels 			setascii();
73436935Skarels 			verbose = oldverbose;
73536935Skarels 		}
736*37225Skarels 	} else if (restart_point) {
737*37225Skarels 		if (command("REST %ld", (long) restart_point) != CONTINUE)
738*37225Skarels 			return;
73926048Sminshall 	}
74010296Ssam 	if (remote) {
74126048Sminshall 		if (command("%s %s", cmd, remote) != PRELIM) {
74226048Sminshall 			(void) signal(SIGINT, oldintr);
74326048Sminshall 			if (oldtype) {
74426448Slepreau 				if (!debug)
74526048Sminshall 					verbose = 0;
74626048Sminshall 				switch (oldtype) {
74726048Sminshall 					case TYPE_I:
74826048Sminshall 						setbinary();
74926048Sminshall 						break;
75026048Sminshall 					case TYPE_E:
75126048Sminshall 						setebcdic();
75226048Sminshall 						break;
75326048Sminshall 					case TYPE_L:
75426048Sminshall 						settenex();
75526048Sminshall 						break;
75636942Skarels 				}
75726048Sminshall 				verbose = oldverbose;
75826048Sminshall 			}
75926048Sminshall 			return;
76026048Sminshall 		}
76126048Sminshall 	} else {
76226048Sminshall 		if (command("%s", cmd) != PRELIM) {
76326048Sminshall 			(void) signal(SIGINT, oldintr);
76426048Sminshall 			if (oldtype) {
76526448Slepreau 				if (!debug)
76626048Sminshall 					verbose = 0;
76726048Sminshall 				switch (oldtype) {
76826048Sminshall 					case TYPE_I:
76926048Sminshall 						setbinary();
77026048Sminshall 						break;
77126048Sminshall 					case TYPE_E:
77226048Sminshall 						setebcdic();
77326048Sminshall 						break;
77426048Sminshall 					case TYPE_L:
77526048Sminshall 						settenex();
77626048Sminshall 						break;
77736942Skarels 				}
77826048Sminshall 				verbose = oldverbose;
77926048Sminshall 			}
78026048Sminshall 			return;
78126048Sminshall 		}
78226048Sminshall 	}
78326048Sminshall 	din = dataconn("r");
78426048Sminshall 	if (din == NULL)
78526048Sminshall 		goto abort;
78626448Slepreau 	if (strcmp(local, "-") == 0)
78710296Ssam 		fout = stdout;
78810296Ssam 	else if (*local == '|') {
78926048Sminshall 		oldintp = signal(SIGPIPE, SIG_IGN);
79035659Sbostic 		fout = popen(local + 1, "w");
79126048Sminshall 		if (fout == NULL) {
79226048Sminshall 			perror(local+1);
79326048Sminshall 			goto abort;
79426048Sminshall 		}
79535659Sbostic 		closefunc = pclose;
79636940Skarels 	} else {
79711651Ssam 		fout = fopen(local, mode);
79826048Sminshall 		if (fout == NULL) {
79926048Sminshall 			perror(local);
80026048Sminshall 			goto abort;
80126048Sminshall 		}
80210296Ssam 		closefunc = fclose;
80310296Ssam 	}
80436940Skarels 	if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
80536940Skarels 		st.st_blksize = BUFSIZ;
80636940Skarels 	if (st.st_blksize > bufsize) {
80736940Skarels 		if (buf)
80836940Skarels 			(void) free(buf);
80936940Skarels 		buf = malloc(st.st_blksize);
81036940Skarels 		if (buf == NULL) {
81136940Skarels 			perror("malloc");
81236944Skarels 			bufsize = 0;
81336940Skarels 			goto abort;
81436940Skarels 		}
81536940Skarels 		bufsize = st.st_blksize;
81636940Skarels 	}
81726496Sminshall 	(void) gettimeofday(&start, (struct timezone *)0);
81811219Ssam 	switch (type) {
81911219Ssam 
82011219Ssam 	case TYPE_I:
82111219Ssam 	case TYPE_L:
822*37225Skarels 		if (restart_point &&
823*37225Skarels 		    lseek(fileno(fout), (long) restart_point, L_SET) < 0) {
824*37225Skarels 			perror(local);
825*37225Skarels 			if (closefunc != NULL)
826*37225Skarels 				(*closefunc)(fout);
827*37225Skarels 			return;
828*37225Skarels 		}
82911346Ssam 		errno = d = 0;
83036940Skarels 		while ((c = read(fileno(din), buf, bufsize)) > 0) {
83136944Skarels 			if ((d = write(fileno(fout), buf, c)) != c)
83211219Ssam 				break;
83311219Ssam 			bytes += c;
83411651Ssam 			if (hash) {
83536940Skarels 				while (bytes >= hashbytes) {
83636940Skarels 					(void) putchar('#');
83736940Skarels 					hashbytes += HASHBYTES;
83836940Skarels 				}
83926496Sminshall 				(void) fflush(stdout);
84011651Ssam 			}
84111219Ssam 		}
84213213Ssam 		if (hash && bytes > 0) {
84336940Skarels 			if (bytes < HASHBYTES)
84436940Skarels 				(void) putchar('#');
84526496Sminshall 			(void) putchar('\n');
84626496Sminshall 			(void) fflush(stdout);
84711651Ssam 		}
84836935Skarels 		if (c < 0) {
84936935Skarels 			if (errno != EPIPE)
85036935Skarels 				perror("netin");
85136935Skarels 			bytes = -1;
85236935Skarels 		}
85336942Skarels 		if (d < c) {
85436942Skarels 			if (d < 0)
85536942Skarels 				perror(local);
85636942Skarels 			else
85736942Skarels 				fprintf(stderr, "%s: short write\n", local);
85836942Skarels 		}
85911219Ssam 		break;
86011219Ssam 
86111219Ssam 	case TYPE_A:
862*37225Skarels 		if (restart_point) {
863*37225Skarels 			register int i, n, c;
864*37225Skarels 
865*37225Skarels 			if (fseek(fout, 0L, L_SET) < 0)
866*37225Skarels 				goto done;
867*37225Skarels 			n = restart_point;
868*37225Skarels 			i = 0;
869*37225Skarels 			while (i++ < n) {
870*37225Skarels 				if ((c=getc(fout)) == EOF)
871*37225Skarels 					goto done;
872*37225Skarels 				if (c == '\n')
873*37225Skarels 					i++;
874*37225Skarels 			}
875*37225Skarels 			if (fseek(fout, 0L, L_INCR) < 0) {
876*37225Skarels done:
877*37225Skarels 				perror(local);
878*37225Skarels 				if (closefunc != NULL)
879*37225Skarels 					(*closefunc)(fout);
880*37225Skarels 				return;
881*37225Skarels 			}
882*37225Skarels 		}
88311219Ssam 		while ((c = getc(din)) != EOF) {
88427749Sminshall 			while (c == '\r') {
88511651Ssam 				while (hash && (bytes >= hashbytes)) {
88626496Sminshall 					(void) putchar('#');
88726496Sminshall 					(void) fflush(stdout);
88836940Skarels 					hashbytes += HASHBYTES;
88911651Ssam 				}
89010296Ssam 				bytes++;
89126048Sminshall 				if ((c = getc(din)) != '\n' || tcrflag) {
89236940Skarels 					if (ferror(fout))
89336940Skarels 						goto break2;
89436940Skarels 					(void) putc('\r', fout);
89536942Skarels 					if (c == '\0') {
89636942Skarels 						bytes++;
89736940Skarels 						goto contin2;
89836942Skarels 					}
89936942Skarels 					if (c == EOF)
90036942Skarels 						goto contin2;
90111219Ssam 				}
90211219Ssam 			}
90336940Skarels 			(void) putc(c, fout);
90411219Ssam 			bytes++;
90536940Skarels 	contin2:	;
90610296Ssam 		}
90736940Skarels break2:
90811651Ssam 		if (hash) {
90913213Ssam 			if (bytes < hashbytes)
91026496Sminshall 				(void) putchar('#');
91126496Sminshall 			(void) putchar('\n');
91226496Sminshall 			(void) fflush(stdout);
91311651Ssam 		}
91436944Skarels 		if (ferror(din)) {
91536935Skarels 			if (errno != EPIPE)
91636944Skarels 				perror("netin");
91736935Skarels 			bytes = -1;
91836935Skarels 		}
91936940Skarels 		if (ferror(fout))
92036944Skarels 			perror(local);
92111219Ssam 		break;
92210296Ssam 	}
92326448Slepreau 	if (closefunc != NULL)
92426048Sminshall 		(*closefunc)(fout);
92526496Sminshall 	(void) signal(SIGINT, oldintr);
92626448Slepreau 	if (oldintp)
92726048Sminshall 		(void) signal(SIGPIPE, oldintp);
92826496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
92910296Ssam 	(void) fclose(din);
93026048Sminshall 	(void) getreply(0);
93136935Skarels 	if (bytes > 0 && is_retr)
932*37225Skarels 		ptransfer("received", bytes, &start, &stop);
93326048Sminshall 	if (oldtype) {
93426448Slepreau 		if (!debug)
93526048Sminshall 			verbose = 0;
93626048Sminshall 		switch (oldtype) {
93726048Sminshall 			case TYPE_I:
93826048Sminshall 				setbinary();
93926048Sminshall 				break;
94026048Sminshall 			case TYPE_E:
94126048Sminshall 				setebcdic();
94226048Sminshall 				break;
94326048Sminshall 			case TYPE_L:
94426048Sminshall 				settenex();
94526048Sminshall 				break;
94626048Sminshall 		}
94726048Sminshall 		verbose = oldverbose;
94826048Sminshall 	}
94926048Sminshall 	return;
95026048Sminshall abort:
95126048Sminshall 
95227687Sminshall /* abort using RFC959 recommended IP,SYNC sequence  */
95326048Sminshall 
95426496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
95526448Slepreau 	if (oldintp)
95626048Sminshall 		(void) signal(SIGPIPE, oldintr);
95726048Sminshall 	(void) signal(SIGINT,SIG_IGN);
95826048Sminshall 	if (oldtype) {
95926448Slepreau 		if (!debug)
96026048Sminshall 			verbose = 0;
96126048Sminshall 		switch (oldtype) {
96226048Sminshall 			case TYPE_I:
96326048Sminshall 				setbinary();
96426048Sminshall 				break;
96526048Sminshall 			case TYPE_E:
96626048Sminshall 				setebcdic();
96726048Sminshall 				break;
96826048Sminshall 			case TYPE_L:
96926048Sminshall 				settenex();
97026048Sminshall 				break;
97126048Sminshall 		}
97226048Sminshall 		verbose = oldverbose;
97326048Sminshall 	}
97426048Sminshall 	if (!cpend) {
97526048Sminshall 		code = -1;
97626048Sminshall 		(void) signal(SIGINT,oldintr);
97726048Sminshall 		return;
97826048Sminshall 	}
97926048Sminshall 
98027687Sminshall 	fprintf(cout,"%c%c",IAC,IP);
98127687Sminshall 	(void) fflush(cout);
98227687Sminshall 	msg = IAC;
98327687Sminshall /* send IAC in urgent mode instead of DM because UNIX places oob mark */
98427687Sminshall /* after urgent byte rather than before as now is protocol            */
98527687Sminshall 	if (send(fileno(cout),&msg,1,MSG_OOB) != 1) {
98627687Sminshall 		perror("abort");
98726048Sminshall 	}
98827687Sminshall 	fprintf(cout,"%cABOR\r\n",DM);
98926048Sminshall 	(void) fflush(cout);
99027687Sminshall 	FD_ZERO(&mask);
99126496Sminshall 	FD_SET(fileno(cin), &mask);
99226496Sminshall 	if (din) {
99326496Sminshall 		FD_SET(fileno(din), &mask);
99426496Sminshall 	}
99527687Sminshall 	if ((nfnd = empty(&mask,10)) <= 0) {
99627687Sminshall 		if (nfnd < 0) {
99727687Sminshall 			perror("abort");
99827687Sminshall 		}
99926048Sminshall 		code = -1;
100026048Sminshall 		lostpeer();
100126048Sminshall 	}
100226496Sminshall 	if (din && FD_ISSET(fileno(din), &mask)) {
100336940Skarels 		while ((c = read(fileno(din), buf, bufsize)) > 0)
100426448Slepreau 			;
100526496Sminshall 	}
100627687Sminshall 	if ((c = getreply(0)) == ERROR && code == 552) { /* needed for nic style abort */
100726048Sminshall 		if (data >= 0) {
100826496Sminshall 			(void) close(data);
100926048Sminshall 			data = -1;
101026048Sminshall 		}
101125907Smckusick 		(void) getreply(0);
101225907Smckusick 	}
101326048Sminshall 	(void) getreply(0);
101426048Sminshall 	code = -1;
101526048Sminshall 	if (data >= 0) {
101626048Sminshall 		(void) close(data);
101726048Sminshall 		data = -1;
101826048Sminshall 	}
101926448Slepreau 	if (closefunc != NULL && fout != NULL)
102026048Sminshall 		(*closefunc)(fout);
102126448Slepreau 	if (din)
102226048Sminshall 		(void) fclose(din);
102335699Sbostic 	if (bytes > 0)
1024*37225Skarels 		ptransfer("received", bytes, &start, &stop);
102526048Sminshall 	(void) signal(SIGINT,oldintr);
102610296Ssam }
102710296Ssam 
102810296Ssam /*
102910296Ssam  * Need to start a listen on the data channel
103010296Ssam  * before we send the command, otherwise the
103110296Ssam  * server's connect may fail.
103210296Ssam  */
103333224Sbostic int sendport = -1;
103411651Ssam 
103510296Ssam initconn()
103610296Ssam {
103710296Ssam 	register char *p, *a;
103826048Sminshall 	int result, len, tmpno = 0;
103926993Skarels 	int on = 1;
104010296Ssam 
104111651Ssam noport:
104210296Ssam 	data_addr = myctladdr;
104311651Ssam 	if (sendport)
104411651Ssam 		data_addr.sin_port = 0;	/* let system pick one */
104511651Ssam 	if (data != -1)
104611651Ssam 		(void) close (data);
104718287Sralph 	data = socket(AF_INET, SOCK_STREAM, 0);
104810296Ssam 	if (data < 0) {
104910296Ssam 		perror("ftp: socket");
105026448Slepreau 		if (tmpno)
105126048Sminshall 			sendport = 1;
105210296Ssam 		return (1);
105310296Ssam 	}
105412397Ssam 	if (!sendport)
105527687Sminshall 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
105633224Sbostic 			perror("ftp: setsockopt (reuse address)");
105712397Ssam 			goto bad;
105812397Ssam 		}
105926496Sminshall 	if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
106010296Ssam 		perror("ftp: bind");
106110296Ssam 		goto bad;
106210296Ssam 	}
106310296Ssam 	if (options & SO_DEBUG &&
106427687Sminshall 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
106510296Ssam 		perror("ftp: setsockopt (ignored)");
106611627Ssam 	len = sizeof (data_addr);
106711627Ssam 	if (getsockname(data, (char *)&data_addr, &len) < 0) {
106811627Ssam 		perror("ftp: getsockname");
106910296Ssam 		goto bad;
107010296Ssam 	}
107126448Slepreau 	if (listen(data, 1) < 0)
107210296Ssam 		perror("ftp: listen");
107311651Ssam 	if (sendport) {
107411651Ssam 		a = (char *)&data_addr.sin_addr;
107511651Ssam 		p = (char *)&data_addr.sin_port;
107610296Ssam #define	UC(b)	(((int)b)&0xff)
107711651Ssam 		result =
107811651Ssam 		    command("PORT %d,%d,%d,%d,%d,%d",
107911651Ssam 		      UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
108011651Ssam 		      UC(p[0]), UC(p[1]));
108111651Ssam 		if (result == ERROR && sendport == -1) {
108211651Ssam 			sendport = 0;
108326048Sminshall 			tmpno = 1;
108411651Ssam 			goto noport;
108511651Ssam 		}
108611651Ssam 		return (result != COMPLETE);
108711651Ssam 	}
108826448Slepreau 	if (tmpno)
108926048Sminshall 		sendport = 1;
109011651Ssam 	return (0);
109110296Ssam bad:
109210296Ssam 	(void) close(data), data = -1;
109326448Slepreau 	if (tmpno)
109426048Sminshall 		sendport = 1;
109510296Ssam 	return (1);
109610296Ssam }
109710296Ssam 
109810296Ssam FILE *
109910296Ssam dataconn(mode)
110010296Ssam 	char *mode;
110110296Ssam {
110210296Ssam 	struct sockaddr_in from;
110310296Ssam 	int s, fromlen = sizeof (from);
110410296Ssam 
110526496Sminshall 	s = accept(data, (struct sockaddr *) &from, &fromlen);
110610296Ssam 	if (s < 0) {
110710296Ssam 		perror("ftp: accept");
110810296Ssam 		(void) close(data), data = -1;
110910296Ssam 		return (NULL);
111010296Ssam 	}
111110296Ssam 	(void) close(data);
111210296Ssam 	data = s;
111310296Ssam 	return (fdopen(data, mode));
111410296Ssam }
111510296Ssam 
1116*37225Skarels ptransfer(direction, bytes, t0, t1)
1117*37225Skarels 	char *direction;
111811651Ssam 	long bytes;
111910296Ssam 	struct timeval *t0, *t1;
112010296Ssam {
112110296Ssam 	struct timeval td;
112216437Sleres 	float s, bs;
112310296Ssam 
112435699Sbostic 	if (verbose) {
112535699Sbostic 		tvsub(&td, t1, t0);
112635699Sbostic 		s = td.tv_sec + (td.tv_usec / 1000000.);
112710296Ssam #define	nz(x)	((x) == 0 ? 1 : (x))
112835699Sbostic 		bs = bytes / nz(s);
112935699Sbostic 		printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
113035699Sbostic 		    bytes, direction, s, bs / 1024.);
113135699Sbostic 	}
113210296Ssam }
113310296Ssam 
113426496Sminshall /*tvadd(tsum, t0)
113510296Ssam 	struct timeval *tsum, *t0;
113610296Ssam {
113710296Ssam 
113810296Ssam 	tsum->tv_sec += t0->tv_sec;
113910296Ssam 	tsum->tv_usec += t0->tv_usec;
114010296Ssam 	if (tsum->tv_usec > 1000000)
114110296Ssam 		tsum->tv_sec++, tsum->tv_usec -= 1000000;
114226496Sminshall } */
114310296Ssam 
114410296Ssam tvsub(tdiff, t1, t0)
114510296Ssam 	struct timeval *tdiff, *t1, *t0;
114610296Ssam {
114710296Ssam 
114810296Ssam 	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
114910296Ssam 	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
115010296Ssam 	if (tdiff->tv_usec < 0)
115110296Ssam 		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
115210296Ssam }
115326048Sminshall 
115426048Sminshall psabort()
115526048Sminshall {
115626048Sminshall 	extern int abrtflag;
115726048Sminshall 
115826048Sminshall 	abrtflag++;
115926048Sminshall }
116026048Sminshall 
116126048Sminshall pswitch(flag)
116226048Sminshall 	int flag;
116326048Sminshall {
116426048Sminshall 	extern int proxy, abrtflag;
116526048Sminshall 	int (*oldintr)();
116626048Sminshall 	static struct comvars {
116726048Sminshall 		int connect;
116828469Skarels 		char name[MAXHOSTNAMELEN];
116926048Sminshall 		struct sockaddr_in mctl;
117026048Sminshall 		struct sockaddr_in hctl;
117126048Sminshall 		FILE *in;
117226048Sminshall 		FILE *out;
117326048Sminshall 		int tpe;
117426048Sminshall 		int cpnd;
117526048Sminshall 		int sunqe;
117626048Sminshall 		int runqe;
117726048Sminshall 		int mcse;
117826048Sminshall 		int ntflg;
117926048Sminshall 		char nti[17];
118026048Sminshall 		char nto[17];
118126048Sminshall 		int mapflg;
118226048Sminshall 		char mi[MAXPATHLEN];
118326048Sminshall 		char mo[MAXPATHLEN];
118426048Sminshall 		} proxstruct, tmpstruct;
118526048Sminshall 	struct comvars *ip, *op;
118626048Sminshall 
118726048Sminshall 	abrtflag = 0;
118826048Sminshall 	oldintr = signal(SIGINT, psabort);
118926048Sminshall 	if (flag) {
119026448Slepreau 		if (proxy)
119126048Sminshall 			return;
119226048Sminshall 		ip = &tmpstruct;
119326048Sminshall 		op = &proxstruct;
119426048Sminshall 		proxy++;
119526048Sminshall 	}
119626048Sminshall 	else {
119726448Slepreau 		if (!proxy)
119826048Sminshall 			return;
119926048Sminshall 		ip = &proxstruct;
120026048Sminshall 		op = &tmpstruct;
120126048Sminshall 		proxy = 0;
120226048Sminshall 	}
120326048Sminshall 	ip->connect = connected;
120426048Sminshall 	connected = op->connect;
120528469Skarels 	if (hostname) {
120628469Skarels 		(void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
120728469Skarels 		ip->name[strlen(ip->name)] = '\0';
120828469Skarels 	} else
120928469Skarels 		ip->name[0] = 0;
121026048Sminshall 	hostname = op->name;
121126048Sminshall 	ip->hctl = hisctladdr;
121226048Sminshall 	hisctladdr = op->hctl;
121326048Sminshall 	ip->mctl = myctladdr;
121426048Sminshall 	myctladdr = op->mctl;
121526048Sminshall 	ip->in = cin;
121626048Sminshall 	cin = op->in;
121726048Sminshall 	ip->out = cout;
121826048Sminshall 	cout = op->out;
121926048Sminshall 	ip->tpe = type;
122026048Sminshall 	type = op->tpe;
122126448Slepreau 	if (!type)
122226048Sminshall 		type = 1;
122326048Sminshall 	ip->cpnd = cpend;
122426048Sminshall 	cpend = op->cpnd;
122526048Sminshall 	ip->sunqe = sunique;
122626048Sminshall 	sunique = op->sunqe;
122726048Sminshall 	ip->runqe = runique;
122826048Sminshall 	runique = op->runqe;
122926048Sminshall 	ip->mcse = mcase;
123026048Sminshall 	mcase = op->mcse;
123126048Sminshall 	ip->ntflg = ntflag;
123226048Sminshall 	ntflag = op->ntflg;
123326496Sminshall 	(void) strncpy(ip->nti, ntin, 16);
123426048Sminshall 	(ip->nti)[strlen(ip->nti)] = '\0';
123526496Sminshall 	(void) strcpy(ntin, op->nti);
123626496Sminshall 	(void) strncpy(ip->nto, ntout, 16);
123726048Sminshall 	(ip->nto)[strlen(ip->nto)] = '\0';
123826496Sminshall 	(void) strcpy(ntout, op->nto);
123926048Sminshall 	ip->mapflg = mapflag;
124026048Sminshall 	mapflag = op->mapflg;
124126496Sminshall 	(void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
124226048Sminshall 	(ip->mi)[strlen(ip->mi)] = '\0';
124326496Sminshall 	(void) strcpy(mapin, op->mi);
124426496Sminshall 	(void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
124526048Sminshall 	(ip->mo)[strlen(ip->mo)] = '\0';
124626496Sminshall 	(void) strcpy(mapout, op->mo);
124726048Sminshall 	(void) signal(SIGINT, oldintr);
124826048Sminshall 	if (abrtflag) {
124926048Sminshall 		abrtflag = 0;
125026048Sminshall 		(*oldintr)();
125126448Slepreau 	}
125226048Sminshall }
125326048Sminshall 
125426048Sminshall jmp_buf ptabort;
125526048Sminshall int ptabflg;
125626048Sminshall 
125726048Sminshall abortpt()
125826048Sminshall {
125926048Sminshall 	printf("\n");
126026496Sminshall 	(void) fflush(stdout);
126126048Sminshall 	ptabflg++;
126226048Sminshall 	mflag = 0;
126326048Sminshall 	abrtflag = 0;
126426048Sminshall 	longjmp(ptabort, 1);
126526048Sminshall }
126626048Sminshall 
126726048Sminshall proxtrans(cmd, local, remote)
126826048Sminshall 	char *cmd, *local, *remote;
126926048Sminshall {
127027687Sminshall 	int (*oldintr)(), abortpt(), tmptype, oldtype = 0, secndflag = 0, nfnd;
127126048Sminshall 	extern jmp_buf ptabort;
127226048Sminshall 	char *cmd2;
127326496Sminshall 	struct fd_set mask;
127426048Sminshall 
127526448Slepreau 	if (strcmp(cmd, "RETR"))
127626048Sminshall 		cmd2 = "RETR";
127726448Slepreau 	else
127826048Sminshall 		cmd2 = runique ? "STOU" : "STOR";
127926048Sminshall 	if (command("PASV") != COMPLETE) {
128026048Sminshall 		printf("proxy server does not support third part transfers.\n");
128126048Sminshall 		return;
128226048Sminshall 	}
128326048Sminshall 	tmptype = type;
128426048Sminshall 	pswitch(0);
128526048Sminshall 	if (!connected) {
128626048Sminshall 		printf("No primary connection\n");
128726048Sminshall 		pswitch(1);
128826048Sminshall 		code = -1;
128926048Sminshall 		return;
129026048Sminshall 	}
129126048Sminshall 	if (type != tmptype) {
129226048Sminshall 		oldtype = type;
129326048Sminshall 		switch (tmptype) {
129426048Sminshall 			case TYPE_A:
129526048Sminshall 				setascii();
129626048Sminshall 				break;
129726048Sminshall 			case TYPE_I:
129826048Sminshall 				setbinary();
129926048Sminshall 				break;
130026048Sminshall 			case TYPE_E:
130126048Sminshall 				setebcdic();
130226048Sminshall 				break;
130326048Sminshall 			case TYPE_L:
130426048Sminshall 				settenex();
130526048Sminshall 				break;
130626048Sminshall 		}
130726048Sminshall 	}
130826048Sminshall 	if (command("PORT %s", pasv) != COMPLETE) {
130926048Sminshall 		switch (oldtype) {
131026048Sminshall 			case 0:
131126048Sminshall 				break;
131226048Sminshall 			case TYPE_A:
131326048Sminshall 				setascii();
131426048Sminshall 				break;
131526048Sminshall 			case TYPE_I:
131626048Sminshall 				setbinary();
131726048Sminshall 				break;
131826048Sminshall 			case TYPE_E:
131926048Sminshall 				setebcdic();
132026048Sminshall 				break;
132126048Sminshall 			case TYPE_L:
132226048Sminshall 				settenex();
132326048Sminshall 				break;
132426048Sminshall 		}
132526048Sminshall 		pswitch(1);
132626048Sminshall 		return;
132726048Sminshall 	}
132826448Slepreau 	if (setjmp(ptabort))
132926048Sminshall 		goto abort;
133026048Sminshall 	oldintr = signal(SIGINT, abortpt);
133126048Sminshall 	if (command("%s %s", cmd, remote) != PRELIM) {
133226048Sminshall 		(void) signal(SIGINT, oldintr);
133326048Sminshall 		switch (oldtype) {
133426048Sminshall 			case 0:
133526048Sminshall 				break;
133626048Sminshall 			case TYPE_A:
133726048Sminshall 				setascii();
133826048Sminshall 				break;
133926048Sminshall 			case TYPE_I:
134026048Sminshall 				setbinary();
134126048Sminshall 				break;
134226048Sminshall 			case TYPE_E:
134326048Sminshall 				setebcdic();
134426048Sminshall 				break;
134526048Sminshall 			case TYPE_L:
134626048Sminshall 				settenex();
134726048Sminshall 				break;
134826048Sminshall 		}
134926048Sminshall 		pswitch(1);
135026048Sminshall 		return;
135126048Sminshall 	}
135226048Sminshall 	sleep(2);
135326048Sminshall 	pswitch(1);
135426048Sminshall 	secndflag++;
135526448Slepreau 	if (command("%s %s", cmd2, local) != PRELIM)
135626048Sminshall 		goto abort;
135726048Sminshall 	ptflag++;
135826048Sminshall 	(void) getreply(0);
135926048Sminshall 	pswitch(0);
136026048Sminshall 	(void) getreply(0);
136126048Sminshall 	(void) signal(SIGINT, oldintr);
136226048Sminshall 	switch (oldtype) {
136326048Sminshall 		case 0:
136426048Sminshall 			break;
136526048Sminshall 		case TYPE_A:
136626048Sminshall 			setascii();
136726048Sminshall 			break;
136826048Sminshall 		case TYPE_I:
136926048Sminshall 			setbinary();
137026048Sminshall 			break;
137126048Sminshall 		case TYPE_E:
137226048Sminshall 			setebcdic();
137326048Sminshall 			break;
137426048Sminshall 		case TYPE_L:
137526048Sminshall 			settenex();
137626048Sminshall 			break;
137726048Sminshall 	}
137826048Sminshall 	pswitch(1);
137926048Sminshall 	ptflag = 0;
138026048Sminshall 	printf("local: %s remote: %s\n", local, remote);
138126048Sminshall 	return;
138226048Sminshall abort:
138326048Sminshall 	(void) signal(SIGINT, SIG_IGN);
138426048Sminshall 	ptflag = 0;
138526448Slepreau 	if (strcmp(cmd, "RETR") && !proxy)
138626048Sminshall 		pswitch(1);
138726448Slepreau 	else if (!strcmp(cmd, "RETR") && proxy)
138826048Sminshall 		pswitch(0);
138926048Sminshall 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
139026048Sminshall 		if (command("%s %s", cmd2, local) != PRELIM) {
139126048Sminshall 			pswitch(0);
139226048Sminshall 			switch (oldtype) {
139326048Sminshall 				case 0:
139426048Sminshall 					break;
139526048Sminshall 				case TYPE_A:
139626048Sminshall 					setascii();
139726048Sminshall 					break;
139826048Sminshall 				case TYPE_I:
139926048Sminshall 					setbinary();
140026048Sminshall 					break;
140126048Sminshall 				case TYPE_E:
140226048Sminshall 					setebcdic();
140326048Sminshall 					break;
140426048Sminshall 				case TYPE_L:
140526048Sminshall 					settenex();
140626048Sminshall 					break;
140726048Sminshall 			}
140827687Sminshall 			if (cpend) {
140926048Sminshall 				char msg[2];
141026048Sminshall 
141126048Sminshall 				fprintf(cout,"%c%c",IAC,IP);
141226048Sminshall 				(void) fflush(cout);
141326048Sminshall 				*msg = IAC;
141426048Sminshall 				*(msg+1) = DM;
141526448Slepreau 				if (send(fileno(cout),msg,2,MSG_OOB) != 2)
141626048Sminshall 					perror("abort");
141726048Sminshall 				fprintf(cout,"ABOR\r\n");
141826048Sminshall 				(void) fflush(cout);
141927687Sminshall 				FD_ZERO(&mask);
142026496Sminshall 				FD_SET(fileno(cin), &mask);
142127687Sminshall 				if ((nfnd = empty(&mask,10)) <= 0) {
142227687Sminshall 					if (nfnd < 0) {
142327687Sminshall 						perror("abort");
142427687Sminshall 					}
142526448Slepreau 					if (ptabflg)
142626048Sminshall 						code = -1;
142726048Sminshall 					lostpeer();
142826048Sminshall 				}
142926048Sminshall 				(void) getreply(0);
143026048Sminshall 				(void) getreply(0);
143126048Sminshall 			}
143226048Sminshall 		}
143326048Sminshall 		pswitch(1);
143426448Slepreau 		if (ptabflg)
143526048Sminshall 			code = -1;
143626048Sminshall 		(void) signal(SIGINT, oldintr);
143726048Sminshall 		return;
143826048Sminshall 	}
143927687Sminshall 	if (cpend) {
144026048Sminshall 		char msg[2];
144126048Sminshall 
144226048Sminshall 		fprintf(cout,"%c%c",IAC,IP);
144326048Sminshall 		(void) fflush(cout);
144426048Sminshall 		*msg = IAC;
144526048Sminshall 		*(msg+1) = DM;
144626448Slepreau 		if (send(fileno(cout),msg,2,MSG_OOB) != 2)
144726048Sminshall 			perror("abort");
144826048Sminshall 		fprintf(cout,"ABOR\r\n");
144926048Sminshall 		(void) fflush(cout);
145027687Sminshall 		FD_ZERO(&mask);
145126496Sminshall 		FD_SET(fileno(cin), &mask);
145227687Sminshall 		if ((nfnd = empty(&mask,10)) <= 0) {
145327687Sminshall 			if (nfnd < 0) {
145427687Sminshall 				perror("abort");
145527687Sminshall 			}
145626448Slepreau 			if (ptabflg)
145726048Sminshall 				code = -1;
145826048Sminshall 			lostpeer();
145926048Sminshall 		}
146026048Sminshall 		(void) getreply(0);
146126048Sminshall 		(void) getreply(0);
146226048Sminshall 	}
146326048Sminshall 	pswitch(!proxy);
146426048Sminshall 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
146526048Sminshall 		if (command("%s %s", cmd2, local) != PRELIM) {
146626048Sminshall 			pswitch(0);
146726048Sminshall 			switch (oldtype) {
146826048Sminshall 				case 0:
146926048Sminshall 					break;
147026048Sminshall 				case TYPE_A:
147126048Sminshall 					setascii();
147226048Sminshall 					break;
147326048Sminshall 				case TYPE_I:
147426048Sminshall 					setbinary();
147526048Sminshall 					break;
147626048Sminshall 				case TYPE_E:
147726048Sminshall 					setebcdic();
147826048Sminshall 					break;
147926048Sminshall 				case TYPE_L:
148026048Sminshall 					settenex();
148126048Sminshall 					break;
148226048Sminshall 			}
148327687Sminshall 			if (cpend) {
148426048Sminshall 				char msg[2];
148526048Sminshall 
148626048Sminshall 				fprintf(cout,"%c%c",IAC,IP);
148726048Sminshall 				(void) fflush(cout);
148826048Sminshall 				*msg = IAC;
148926048Sminshall 				*(msg+1) = DM;
149026448Slepreau 				if (send(fileno(cout),msg,2,MSG_OOB) != 2)
149126048Sminshall 					perror("abort");
149226048Sminshall 				fprintf(cout,"ABOR\r\n");
149326048Sminshall 				(void) fflush(cout);
149427687Sminshall 				FD_ZERO(&mask);
149526496Sminshall 				FD_SET(fileno(cin), &mask);
149627687Sminshall 				if ((nfnd = empty(&mask,10)) <= 0) {
149727687Sminshall 					if (nfnd < 0) {
149827687Sminshall 						perror("abort");
149927687Sminshall 					}
150026448Slepreau 					if (ptabflg)
150126048Sminshall 						code = -1;
150226048Sminshall 					lostpeer();
150326048Sminshall 				}
150426048Sminshall 				(void) getreply(0);
150526048Sminshall 				(void) getreply(0);
150626048Sminshall 			}
150726048Sminshall 			pswitch(1);
150826448Slepreau 			if (ptabflg)
150926048Sminshall 				code = -1;
151026048Sminshall 			(void) signal(SIGINT, oldintr);
151126048Sminshall 			return;
151226048Sminshall 		}
151326048Sminshall 	}
151427687Sminshall 	if (cpend) {
151526048Sminshall 		char msg[2];
151626048Sminshall 
151726048Sminshall 		fprintf(cout,"%c%c",IAC,IP);
151826048Sminshall 		(void) fflush(cout);
151926048Sminshall 		*msg = IAC;
152026048Sminshall 		*(msg+1) = DM;
152126448Slepreau 		if (send(fileno(cout),msg,2,MSG_OOB) != 2)
152226048Sminshall 			perror("abort");
152326048Sminshall 		fprintf(cout,"ABOR\r\n");
152426048Sminshall 		(void) fflush(cout);
152527687Sminshall 		FD_ZERO(&mask);
152626496Sminshall 		FD_SET(fileno(cin), &mask);
152727687Sminshall 		if ((nfnd = empty(&mask,10)) <= 0) {
152827687Sminshall 			if (nfnd < 0) {
152927687Sminshall 				perror("abort");
153027687Sminshall 			}
153126448Slepreau 			if (ptabflg)
153226048Sminshall 				code = -1;
153326048Sminshall 			lostpeer();
153426048Sminshall 		}
153526048Sminshall 		(void) getreply(0);
153626048Sminshall 		(void) getreply(0);
153726048Sminshall 	}
153826048Sminshall 	pswitch(!proxy);
153926048Sminshall 	if (cpend) {
154027687Sminshall 		FD_ZERO(&mask);
154126496Sminshall 		FD_SET(fileno(cin), &mask);
154227687Sminshall 		if ((nfnd = empty(&mask,10)) <= 0) {
154327687Sminshall 			if (nfnd < 0) {
154427687Sminshall 				perror("abort");
154527687Sminshall 			}
154626448Slepreau 			if (ptabflg)
154726048Sminshall 				code = -1;
154826048Sminshall 			lostpeer();
154926048Sminshall 		}
155026048Sminshall 		(void) getreply(0);
155126048Sminshall 		(void) getreply(0);
155226048Sminshall 	}
155326448Slepreau 	if (proxy)
155426048Sminshall 		pswitch(0);
155526048Sminshall 	switch (oldtype) {
155626048Sminshall 		case 0:
155726048Sminshall 			break;
155826048Sminshall 		case TYPE_A:
155926048Sminshall 			setascii();
156026048Sminshall 			break;
156126048Sminshall 		case TYPE_I:
156226048Sminshall 			setbinary();
156326048Sminshall 			break;
156426048Sminshall 		case TYPE_E:
156526048Sminshall 			setebcdic();
156626048Sminshall 			break;
156726048Sminshall 		case TYPE_L:
156826048Sminshall 			settenex();
156926048Sminshall 			break;
157026048Sminshall 	}
157126048Sminshall 	pswitch(1);
157226448Slepreau 	if (ptabflg)
157326048Sminshall 		code = -1;
157426048Sminshall 	(void) signal(SIGINT, oldintr);
157526048Sminshall }
157626048Sminshall 
157726048Sminshall reset()
157826048Sminshall {
157926496Sminshall 	struct fd_set mask;
158026496Sminshall 	int nfnd = 1;
158126048Sminshall 
158227687Sminshall 	FD_ZERO(&mask);
158330946Scsvsj 	while (nfnd > 0) {
158426496Sminshall 		FD_SET(fileno(cin), &mask);
158527687Sminshall 		if ((nfnd = empty(&mask,0)) < 0) {
158626048Sminshall 			perror("reset");
158726048Sminshall 			code = -1;
158826048Sminshall 			lostpeer();
158926048Sminshall 		}
159027687Sminshall 		else if (nfnd) {
159126048Sminshall 			(void) getreply(0);
159226496Sminshall 		}
159326048Sminshall 	}
159426048Sminshall }
159526048Sminshall 
159626048Sminshall char *
159726048Sminshall gunique(local)
159826048Sminshall 	char *local;
159926048Sminshall {
160026048Sminshall 	static char new[MAXPATHLEN];
160126048Sminshall 	char *cp = rindex(local, '/');
160226048Sminshall 	int d, count=0;
160326048Sminshall 	char ext = '1';
160426048Sminshall 
160526448Slepreau 	if (cp)
160626048Sminshall 		*cp = '\0';
160726048Sminshall 	d = access(cp ? local : ".", 2);
160826448Slepreau 	if (cp)
160926048Sminshall 		*cp = '/';
161026048Sminshall 	if (d < 0) {
161126048Sminshall 		perror(local);
161226048Sminshall 		return((char *) 0);
161326048Sminshall 	}
161426048Sminshall 	(void) strcpy(new, local);
161526048Sminshall 	cp = new + strlen(new);
161626048Sminshall 	*cp++ = '.';
161726048Sminshall 	while (!d) {
161826048Sminshall 		if (++count == 100) {
161926048Sminshall 			printf("runique: can't find unique file name.\n");
162026048Sminshall 			return((char *) 0);
162126048Sminshall 		}
162226048Sminshall 		*cp++ = ext;
162326048Sminshall 		*cp = '\0';
162426448Slepreau 		if (ext == '9')
162526048Sminshall 			ext = '0';
162626448Slepreau 		else
162726048Sminshall 			ext++;
162826448Slepreau 		if ((d = access(new, 0)) < 0)
162926048Sminshall 			break;
163026448Slepreau 		if (ext != '0')
163126048Sminshall 			cp--;
163226448Slepreau 		else if (*(cp - 2) == '.')
163326048Sminshall 			*(cp - 1) = '1';
163426048Sminshall 		else {
163526048Sminshall 			*(cp - 2) = *(cp - 2) + 1;
163626048Sminshall 			cp--;
163726048Sminshall 		}
163826048Sminshall 	}
163926048Sminshall 	return(new);
164026048Sminshall }
1641