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