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