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