From proff@profane.iq.org  Sun Mar  2 01:24:30 1997
Received: from profane.iq.org (profane.iq.org [203.4.184.217])
          by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id BAA00919
          for <FreeBSD-gnats-submit@freebsd.org>; Sun, 2 Mar 1997 01:21:15 -0800 (PST)
Received: (from proff@localhost)
          by profane.iq.org (8.8.4/8.8.2) id UAA07105;
          Sun, 2 Mar 1997 20:17:32 +1100 (EST)
Message-Id: <199703020917.UAA07105@profane.iq.org>
Date: Sun, 2 Mar 1997 20:17:32 +1100 (EST)
From: Julian Assange <proff@iq.org>
Reply-To: proff@iq.org
To: FreeBSD-gnats-submit@freebsd.org
Subject: sync with spiffy new netbsd/openbsd ftp client
X-Send-Pr-Version: 3.2

>Number:         2845
>Category:       bin
>Synopsis:       sync with spiffy new netbsd/openbsd ftp client
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Mar  2 01:30:02 PST 1997
>Closed-Date:    Sat Aug 23 18:11:55 MEST 1997
>Last-Modified:  Sat Aug 23 18:12:18 MEST 1997
>Originator:     Julian Assange
>Release:        FreeBSD 3.0-CURRENT i386
>Organization:
>Environment:

	

>Description:

the NetBSD/OpenBSD ftp client has undergone some significant changes (e.g local + remote
context sensive file name completion) and bug/buffer overrun fixes. This patch merges
them in.

	

>How-To-Repeat:

	

>Fix:
	
	

nb. needs the new libedit bug fixes/extensions, see my previous patch

diff --new-file -r -b -u src/usr.bin/ftp.old/Makefile src/usr.bin/ftp/Makefile
--- src/usr.bin/ftp.old/Makefile	Thu May 30 18:53:03 1996
+++ src/usr.bin/ftp/Makefile	Sun Mar  2 20:04:30 1997
@@ -1,7 +1,21 @@
-#	@(#)Makefile	8.2 (Berkeley) 4/3/94
+#	$OpenBSD: Makefile,v 1.6 1997/02/03 01:05:31 millert Exp $
+#	$NetBSD: Makefile,v 1.8 1997/01/19 14:19:02 lukem Exp $
+#	$FreeBSD$
+#	from: @(#)Makefile	8.2 (Berkeley) 4/3/94
+
+# define SMALLFTP if editing is to be disabled
+#SMALLFTP=yes
 
 PROG=	ftp
-SRCS=	cmds.c cmdtab.c ftp.c main.c ruserpass.c domacro.c
+SRCS=	cmds.c cmdtab.c domacro.c fetch.c ftp.c main.c ruserpass.c \
+	stringlist.c util.c
+.if defined(SMALLFTP)
+CFLAGS+=-DSMALLFTP
+.else
+SRCS+=	complete.c
+LDADD+=	-ledit -ltermcap
+.endif
+
 LINKS=  ${BINDIR}/ftp ${BINDIR}/pftp
 MLINKS= ftp.1 pftp.1
 
diff --new-file -r -b -u src/usr.bin/ftp.old/cmds.c src/usr.bin/ftp/cmds.c
--- src/usr.bin/ftp.old/cmds.c	Mon Dec 23 16:05:14 1996
+++ src/usr.bin/ftp/cmds.c	Sun Mar  2 19:55:35 1997
@@ -1,3 +1,6 @@
+/*	$OpenBSD: cmds.c,v 1.15 1997/02/05 04:55:12 millert Exp $	*/
+/*	$NetBSD: cmds.c,v 1.18 1997/02/01 10:44:54 lukem Exp $	*/
+
 /*
  * Copyright (c) 1985, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -32,28 +35,30 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)cmds.c	8.5 (Berkeley) 4/6/94";
+#if 0
+static char sccsid[] = "@(#)cmds.c	8.6 (Berkeley) 10/9/94";
+static char rcsid[] = "$OpenBSD: cmds.c,v 1.15 1997/02/05 04:55:12 millert Exp $";
+#else
+static char rcsid[] = "$FreeBSD$";
+#endif
 #endif /* not lint */
 
 /*
  * FTP User Program -- Command Routines.
  */
-#include <sys/param.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
 #include <arpa/ftp.h>
 
 #include <ctype.h>
 #include <err.h>
 #include <glob.h>
 #include <netdb.h>
-#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <unistd.h>
 
 #include "ftp_var.h"
@@ -63,147 +68,6 @@
 char   *mname;
 char   *home = "/";
 
-/*
- * `Another' gets another argument, and stores the new argc and argv.
- * It reverts to the top level (via main.c's intr()) on EOF/error.
- *
- * Returns false if no new arguments have been added.
- */
-int
-another(pargc, pargv, prompt)
-	int *pargc;
-	char ***pargv;
-	char *prompt;
-{
-	int len = strlen(line), ret;
-
-	if (len >= sizeof(line) - 3) {
-		printf("sorry, arguments too long\n");
-		intr();
-	}
-	printf("(%s) ", prompt);
-	line[len++] = ' ';
-	if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
-		intr();
-	len += strlen(&line[len]);
-	if (len > 0 && line[len - 1] == '\n')
-		line[len - 1] = '\0';
-	makeargv();
-	ret = margc > *pargc;
-	*pargc = margc;
-	*pargv = margv;
-	return (ret);
-}
-
-/*
- * Connect to peer server and
- * auto-login, if possible.
- */
-void
-setpeer(argc, argv)
-	int argc;
-	char *argv[];
-{
-	char *host;
-	short port;
-
-	if (connected) {
-		printf("Already connected to %s, use close first.\n",
-			hostname);
-		code = -1;
-		return;
-	}
-	if (argc < 2)
-		(void) another(&argc, &argv, "to");
-	if (argc < 2 || argc > 3) {
-		printf("usage: %s host-name [port]\n", argv[0]);
-		code = -1;
-		return;
-	}
-	port = sp->s_port;
-	if (argc > 2) {
-		port = atoi(argv[2]);
-		if (port <= 0) {
-			printf("%s: bad port number-- %s\n", argv[1], argv[2]);
-			printf ("usage: %s host-name [port]\n", argv[0]);
-			code = -1;
-			return;
-		}
-		port = htons(port);
-	}
-	host = hookup(argv[1], port);
-	if (host) {
-		int overbose;
-
-		connected = 1;
-		/*
-		 * Set up defaults for FTP.
-		 */
-		(void) strcpy(typename, "ascii"), type = TYPE_A;
-		curtype = TYPE_A;
-		(void) strcpy(formname, "non-print"), form = FORM_N;
-		(void) strcpy(modename, "stream"), mode = MODE_S;
-		(void) strcpy(structname, "file"), stru = STRU_F;
-		(void) strcpy(bytename, "8"), bytesize = 8;
-		if (autologin)
-			(void) login(argv[1]);
-
-#if defined(unix) && NBBY == 8
-/*
- * this ifdef is to keep someone form "porting" this to an incompatible
- * system and not checking this out. This way they have to think about it.
- */
-		overbose = verbose;
-		if (debug == 0)
-			verbose = -1;
-		if (command("SYST") == COMPLETE && overbose) {
-			char *cp, c;
-			cp = strchr(reply_string+4, ' ');
-			if (cp == NULL)
-				cp = strchr(reply_string+4, '\r');
-			if (cp) {
-				if (cp[-1] == '.')
-					cp--;
-				c = *cp;
-				*cp = '\0';
-			}
-
-			printf("Remote system type is %s.\n",
-				reply_string+4);
-			if (cp)
-				*cp = c;
-		}
-		if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
-			if (proxy)
-				unix_proxy = 1;
-			else
-				unix_server = 1;
-			/*
-			 * Set type to 0 (not specified by user),
-			 * meaning binary by default, but don't bother
-			 * telling server.  We can use binary
-			 * for text files unless changed by the user.
-			 */
-			type = 0;
-			(void) strcpy(typename, "binary");
-			if (overbose)
-			    printf("Using %s mode to transfer files.\n",
-				typename);
-		} else {
-			if (proxy)
-				unix_proxy = 0;
-			else
-				unix_server = 0;
-			if (overbose &&
-			    !strncmp(reply_string, "215 TOPS20", 10))
-				printf(
-"Remember to set tenex mode when transfering binary files from this machine.\n");
-		}
-		verbose = overbose;
-#endif /* unix */
-	}
-}
-
 struct	types {
 	char	*t_name;
 	char	*t_mode;
@@ -238,7 +102,7 @@
 			printf("%s%s", sep, p->t_name);
 			sep = " | ";
 		}
-		printf(" ]\n");
+		puts(" ]");
 		code = -1;
 		return;
 	}
@@ -256,11 +120,11 @@
 		return;
 	}
 	if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
-		comret = command ("TYPE %s %s", p->t_mode, p->t_arg);
+		comret = command("TYPE %s %s", p->t_mode, p->t_arg);
 	else
 		comret = command("TYPE %s", p->t_mode);
 	if (comret == COMPLETE) {
-		(void) strcpy(typename, p->t_name);
+		(void)strcpy(typename, p->t_name);
 		curtype = type = p->t_type;
 	}
 }
@@ -312,7 +176,7 @@
 void
 setbinary(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 
 	stype[1] = "binary";
@@ -408,9 +272,9 @@
 	}
 	if (argc < 2 && !another(&argc, &argv, "local-file"))
 		goto usage;
-	if (argc < 3 && !another(&argc, &argv, "remote-file")) {
+	if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) {
 usage:
-		printf("usage: %s local-file remote-file\n", argv[0]);
+		printf("usage: %s local-file [ remote-file ]\n", argv[0]);
 		code = -1;
 		return;
 	}
@@ -444,7 +308,7 @@
 void
 mput(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	int i;
 	sig_t oldintr;
@@ -459,12 +323,12 @@
 	mname = argv[0];
 	mflag = 1;
 	oldintr = signal(SIGINT, mabort);
-	(void) setjmp(jabort);
+	(void)setjmp(jabort);
 	if (proxy) {
 		char *cp, *tp2, tmpbuf[MAXPATHLEN];
 
-		while ((cp = remglob(argv,0)) != NULL) {
-			if (*cp == 0) {
+		while ((cp = remglob(argv, 0)) != NULL) {
+			if (*cp == '\0') {
 				mflag = 0;
 				continue;
 			}
@@ -498,19 +362,19 @@
 				if (!mflag && fromatty) {
 					ointer = interactive;
 					interactive = 1;
-					if (confirm("Continue with","mput")) {
+					if (confirm("Continue with", "mput")) {
 						mflag++;
 					}
 					interactive = ointer;
 				}
 			}
 		}
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
 		mflag = 0;
 		return;
 	}
 	for (i = 1; i < argc; i++) {
-		char **cpp, **gargs;
+		char **cpp;
 		glob_t gl;
 		int flags;
 
@@ -523,7 +387,7 @@
 				if (!mflag && fromatty) {
 					ointer = interactive;
 					interactive = 1;
-					if (confirm("Continue with","mput")) {
+					if (confirm("Continue with", "mput")) {
 						mflag++;
 					}
 					interactive = ointer;
@@ -548,7 +412,7 @@
 				if (!mflag && fromatty) {
 					ointer = interactive;
 					interactive = 1;
-					if (confirm("Continue with","mput")) {
+					if (confirm("Continue with", "mput")) {
 						mflag++;
 					}
 					interactive = ointer;
@@ -557,7 +421,7 @@
 		}
 		globfree(&gl);
 	}
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
 	mflag = 0;
 }
 
@@ -567,7 +431,7 @@
 	char *argv[];
 {
 
-	(void) getit(argc, argv, 1, "r+w");
+	(void)getit(argc, argv, 1, "r+w");
 }
 
 void
@@ -576,7 +440,7 @@
 	char *argv[];
 {
 
-	(void) getit(argc, argv, 0, restart_point ? "r+w" : "w" );
+	(void)getit(argc, argv, 0, restart_point ? "r+w" : "w" );
 }
 
 /*
@@ -586,8 +450,8 @@
 getit(argc, argv, restartit, mode)
 	int argc;
 	char *argv[];
-	char *mode;
 	int restartit;
+	const char *mode;
 {
 	int loc = 0;
 	char *oldargv1, *oldargv2;
@@ -599,7 +463,7 @@
 	}
 	if (argc < 2 && !another(&argc, &argv, "remote-file"))
 		goto usage;
-	if (argc < 3 && !another(&argc, &argv, "local-file")) {
+	if ((argc < 3 && !another(&argc, &argv, "local-file")) || argc > 3) {
 usage:
 		printf("usage: %s remote-file [ local-file ]\n", argv[0]);
 		code = -1;
@@ -647,38 +511,13 @@
 			restart_point = stbuf.st_size;
 		} else {
 			if (ret == 0) {
-				int overbose;
+				time_t mtime;
 
-				overbose = verbose;
-				if (debug == 0)
-					verbose = -1;
-				if (command("MDTM %s", argv[1]) == COMPLETE) {
-					int yy, mo, day, hour, min, sec;
-					struct tm *tm;
-					verbose = overbose;
-					sscanf(reply_string,
-					    "%*s %04d%02d%02d%02d%02d%02d",
-					    &yy, &mo, &day, &hour, &min, &sec);
-					tm = gmtime(&stbuf.st_mtime);
-					tm->tm_mon++;
-					if (tm->tm_year > yy%100)
-						return (1);
-					if ((tm->tm_year == yy%100 &&
-					    tm->tm_mon > mo) ||
-					   (tm->tm_mon == mo &&
-					    tm->tm_mday > day) ||
-					   (tm->tm_mday == day &&
-					    tm->tm_hour > hour) ||
-					   (tm->tm_hour == hour &&
-					    tm->tm_min > min) ||
-					   (tm->tm_min == min &&
-					    tm->tm_sec > sec))
-						return (1);
-				} else {
-					printf("%s\n", reply_string);
-					verbose = overbose;
+				mtime = remotemodtime(argv[1], 0);
+				if (mtime == -1)
 					return (0);
-				}
+				if (stbuf.st_mtime >= mtime)
+					return (1);
 			}
 		}
 	}
@@ -694,21 +533,26 @@
 mabort(signo)
 	int signo;
 {
-	int ointer;
+	int ointer, oconf;
 
-	printf("\n");
-	(void) fflush(stdout);
+	alarmtimer(0);
+	putchar('\n');
+	(void)fflush(stdout);
 	if (mflag && fromatty) {
 		ointer = interactive;
+		oconf = confirmrest;
 		interactive = 1;
+		confirmrest = 0;
 		if (confirm("Continue with", mname)) {
 			interactive = ointer;
-			longjmp(jabort,0);
+			confirmrest = oconf;
+			longjmp(jabort, 0);
 		}
 		interactive = ointer;
+		confirmrest = oconf;
 	}
 	mflag = 0;
-	longjmp(jabort,0);
+	longjmp(jabort, 0);
 }
 
 /*
@@ -717,7 +561,7 @@
 void
 mget(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	sig_t oldintr;
 	int ch, ointer;
@@ -731,8 +575,8 @@
 	mname = argv[0];
 	mflag = 1;
 	oldintr = signal(SIGINT, mabort);
-	(void) setjmp(jabort);
-	while ((cp = remglob(argv,proxy)) != NULL) {
+	(void)setjmp(jabort);
+	while ((cp = remglob(argv, proxy)) != NULL) {
 		if (*cp == '\0') {
 			mflag = 0;
 			continue;
@@ -740,7 +584,7 @@
 		if (mflag && confirm(argv[0], cp)) {
 			tp = cp;
 			if (mcase) {
-				for (tp2 = tmpbuf; ch = *tp++;)
+				for (tp2 = tmpbuf; (ch = *tp++) != NULL; )
 					*tp2++ = isupper(ch) ? tolower(ch) : ch;
 				*tp2 = '\0';
 				tp = tmpbuf;
@@ -756,79 +600,18 @@
 			if (!mflag && fromatty) {
 				ointer = interactive;
 				interactive = 1;
-				if (confirm("Continue with","mget")) {
+				if (confirm("Continue with", "mget")) {
 					mflag++;
 				}
 				interactive = ointer;
 			}
 		}
 	}
-	(void) signal(SIGINT,oldintr);
+	(void)signal(SIGINT, oldintr);
 	mflag = 0;
 }
 
 char *
-remglob(argv,doswitch)
-	char *argv[];
-	int doswitch;
-{
-	char temp[16];
-	static char buf[MAXPATHLEN];
-	static FILE *ftemp = NULL;
-	static char **args;
-	int oldverbose, oldhash;
-	char *cp, *mode;
-
-	if (!mflag) {
-		if (!doglob) {
-			args = NULL;
-		}
-		else {
-			if (ftemp) {
-				(void) fclose(ftemp);
-				ftemp = NULL;
-			}
-		}
-		return (NULL);
-	}
-	if (!doglob) {
-		if (args == NULL)
-			args = argv;
-		if ((cp = *++args) == NULL)
-			args = NULL;
-		return (cp);
-	}
-	if (ftemp == NULL) {
-		(void) strcpy(temp, _PATH_TMP);
-		(void) mktemp(temp);
-		oldverbose = verbose, verbose = 0;
-		oldhash = hash, hash = 0;
-		if (doswitch) {
-			pswitch(!proxy);
-		}
-		for (mode = "w"; *++argv != NULL; mode = "a")
-			recvrequest ("NLST", temp, *argv, mode, 0);
-		if (doswitch) {
-			pswitch(!proxy);
-		}
-		verbose = oldverbose; hash = oldhash;
-		ftemp = fopen(temp, "r");
-		(void) unlink(temp);
-		if (ftemp == NULL) {
-			printf("can't find list of remote files, oops\n");
-			return (NULL);
-		}
-	}
-	if (fgets(buf, sizeof (buf), ftemp) == NULL) {
-		(void) fclose(ftemp), ftemp = NULL;
-		return (NULL);
-	}
-	if ((cp = strchr(buf, '\n')) != NULL)
-		*cp = '\0';
-	return (buf);
-}
-
-char *
 onoff(bool)
 	int bool;
 {
@@ -850,17 +633,19 @@
 	if (connected)
 		printf("Connected to %s.\n", hostname);
 	else
-		printf("Not connected.\n");
+		puts("Not connected.");
 	if (!proxy) {
 		pswitch(1);
 		if (connected) {
-			printf("Connected for proxy commands to %s.\n", hostname);
+			printf("Connected for proxy commands to %s.\n",
+			    hostname);
 		}
 		else {
-			printf("No proxy connection.\n");
+			puts("No proxy connection.");
 		}
 		pswitch(0);
 	}
+	printf("Passive mode: %s.\n", onoff(passivemode));
 	printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
 		modename, typename, formname, structname);
 	printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
@@ -868,31 +653,61 @@
 		onoff(doglob));
 	printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),
 		onoff(runique));
-	printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag));
+	printf("Preserve modification times: %s\n", onoff(preserve));
+	printf("Case: %s; CR stripping: %s\n", onoff(mcase), onoff(crflag));
 	if (ntflag) {
-		printf("Ntrans: (in) %s (out) %s\n", ntin,ntout);
+		printf("Ntrans: (in) %s (out) %s\n", ntin, ntout);
 	}
 	else {
-		printf("Ntrans: off\n");
+		puts("Ntrans: off");
 	}
 	if (mapflag) {
 		printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
 	}
 	else {
-		printf("Nmap: off\n");
+		puts("Nmap: off");
 	}
-	printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
-		onoff(hash), onoff(sendport));
+	printf("Hash mark printing: %s; Mark count: %d; Progress bar: %s\n",
+	    onoff(hash), mark, onoff(progress));
+	printf("Use of PORT cmds: %s\n", onoff(sendport));
+#ifndef SMALLFTP
+	printf("Command line editing: %s\n", onoff(editing));
+#endif /* !SMALLFTP */
 	if (macnum > 0) {
-		printf("Macros:\n");
+		puts("Macros:");
 		for (i=0; i<macnum; i++) {
-			printf("\t%s\n",macros[i].mac_name);
+			printf("\t%s\n", macros[i].mac_name);
 		}
 	}
 	code = 0;
 }
 
 /*
+ * Toggle a variable
+ */
+int
+togglevar(argc, argv, var, mesg)
+	int   argc;
+	char *argv[];
+	int  *var;
+	const char *mesg;
+{
+	if (argc < 2) {
+		*var = !*var;
+	} else if (argc == 2 && strcasecmp(argv[1], "on") == 0) {
+		*var = 1;
+	} else if (argc == 2 && strcasecmp(argv[1], "off") == 0) {
+		*var = 0;
+	} else {
+		printf("usage: %s [ on | off ]\n", argv[0]);
+		return -1;
+	}
+	if (mesg)
+		printf("%s %s.\n", mesg, onoff(*var));
+	return (*var);
+}
+
+/*
  * Set beep on cmd completed mode.
  */
 /*VARARGS*/
@@ -902,10 +717,23 @@
 	char *argv[];
 {
 
-	bell = !bell;
-	printf("Bell mode %s.\n", onoff(bell));
-	code = bell;
+	code = togglevar(argc, argv, &bell, "Bell mode");
+}
+
+#ifndef SMALLFTP
+/*
+ * Set command line editing
+ */
+/*VARARGS*/
+void
+setedit(argc, argv)
+	int argc;
+	char *argv[];
+{
+
+	code = togglevar(argc, argv, &editing, "Editing mode");
 }
+#endif /* !SMALLFTP */
 
 /*
  * Turn on packet tracing.
@@ -917,13 +745,11 @@
 	char *argv[];
 {
 
-	trace = !trace;
-	printf("Packet tracing %s.\n", onoff(trace));
-	code = trace;
+	code = togglevar(argc, argv, &trace, "Packet tracing");
 }
 
 /*
- * Toggle hash mark printing during transfers.
+ * Toggle hash mark printing during transfers, or set hash mark bytecount.
  */
 /*VARARGS*/
 void
@@ -931,13 +757,31 @@
 	int argc;
 	char *argv[];
 {
-
+	if (argc == 1)
 	hash = !hash;
+	else if (argc != 2) {
+		printf("usage: %s [ on | off | bytecount ]\n", argv[0]);
+		code = -1;
+		return;
+	} else if (strcasecmp(argv[1], "on") == 0)
+		hash = 1;
+	else if (strcasecmp(argv[1], "off") == 0)
+		hash = 0;
+	else {
+		int nmark = atol(argv[1]);
+		if (nmark < 1) {
+			printf("%s: bad bytecount value\n", argv[1]);
+			code = -1;
+			return;
+		}
+		mark = nmark;
+		hash = 1;
+	}
 	printf("Hash mark printing %s", onoff(hash));
-	code = hash;
 	if (hash)
-		printf(" (%d bytes/hash mark)", 1024);
-	printf(".\n");
+		printf(" (%d bytes/hash mark)", mark);
+	puts(".");
+	code = hash;
 }
 
 /*
@@ -950,9 +794,7 @@
 	char *argv[];
 {
 
-	verbose = !verbose;
-	printf("Verbose mode %s.\n", onoff(verbose));
-	code = verbose;
+	code = togglevar(argc, argv, &verbose, "Verbose mode");
 }
 
 /*
@@ -965,9 +807,20 @@
 	char *argv[];
 {
 
-	sendport = !sendport;
-	printf("Use of PORT cmds %s.\n", onoff(sendport));
-	code = sendport;
+	code = togglevar(argc, argv, &sendport, "Use of PORT cmds");
+}
+
+/*
+ * Toggle transfer progress bar.
+ */
+/*VARARGS*/
+void
+setprogress(argc, argv)
+	int argc;
+	char *argv[];
+{
+
+	code = togglevar(argc, argv, &progress, "Progress bar");
 }
 
 /*
@@ -981,9 +834,7 @@
 	char *argv[];
 {
 
-	interactive = !interactive;
-	printf("Interactive mode %s.\n", onoff(interactive));
-	code = interactive;
+	code = togglevar(argc, argv, &interactive, "Interactive mode");
 }
 
 /*
@@ -997,9 +848,20 @@
 	char *argv[];
 {
 
-	doglob = !doglob;
-	printf("Globbing %s.\n", onoff(doglob));
-	code = doglob;
+	code = togglevar(argc, argv, &doglob, "Globbing");
+}
+
+/*
+ * Toggle preserving modification times on retreived files.
+ */
+/*VARARGS*/
+void
+setpreserve(argc, argv)
+	int argc;
+	char *argv[];
+{
+
+	code = togglevar(argc, argv, &preserve, "Preserve modification times");
 }
 
 /*
@@ -1014,16 +876,26 @@
 {
 	int val;
 
-	if (argc > 1) {
+	if (argc > 2) {
+		printf("usage: %s [ on | off | debuglevel ]\n", argv[0]);
+		code = -1;
+		return;
+	} else if (argc == 2) {
+		if (strcasecmp(argv[1], "on") == 0)
+			debug = 1;
+		else if (strcasecmp(argv[1], "off") == 0)
+			debug = 0;
+		else {
 		val = atoi(argv[1]);
 		if (val < 0) {
 			printf("%s: bad debugging value.\n", argv[1]);
 			code = -1;
 			return;
 		}
-	} else
-		val = !debug;
 	debug = val;
+		}
+	} else
+		debug = !debug;
 	if (debug)
 		options |= SO_DEBUG;
 	else
@@ -1041,17 +913,22 @@
 	int argc;
 	char *argv[];
 {
+	int r;
 
-	if (argc < 2 && !another(&argc, &argv, "remote-directory")) {
+	if ((argc < 2 && !another(&argc, &argv, "remote-directory")) ||
+	    argc > 2) {
 		printf("usage: %s remote-directory\n", argv[0]);
 		code = -1;
 		return;
 	}
-	if (command("CWD %s", argv[1]) == ERROR && code == 500) {
+	r = command("CWD %s", argv[1]);
+	if (r == ERROR && code == 500) {
 		if (verbose)
-			printf("CWD command not recognized, trying XCWD\n");
-		(void) command("XCWD %s", argv[1]);
+			puts("CWD command not recognized, trying XCWD");
+		r = command("XCWD %s", argv[1]);
 	}
+	if (r == COMPLETE)
+		dirchange = 1;
 }
 
 /*
@@ -1081,10 +958,10 @@
 		code = -1;
 		return;
 	}
-	if (getwd(buf) != NULL)
+	if (getcwd(buf, sizeof(buf)) != NULL)
 		printf("Local directory now %s\n", buf);
 	else
-		warnx("getwd: %s", buf);
+		warn("getcwd: %s", argv[1]);
 	code = 0;
 }
 
@@ -1097,12 +974,12 @@
 	char *argv[];
 {
 
-	if (argc < 2 && !another(&argc, &argv, "remote-file")) {
+	if ((argc < 2 && !another(&argc, &argv, "remote-file")) || argc > 2) {
 		printf("usage: %s remote-file\n", argv[0]);
 		code = -1;
 		return;
 	}
-	(void) command("DELE %s", argv[1]);
+	(void)command("DELE %s", argv[1]);
 }
 
 /*
@@ -1111,7 +988,7 @@
 void
 mdelete(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	sig_t oldintr;
 	int ointer;
@@ -1125,14 +1002,14 @@
 	mname = argv[0];
 	mflag = 1;
 	oldintr = signal(SIGINT, mabort);
-	(void) setjmp(jabort);
-	while ((cp = remglob(argv,0)) != NULL) {
+	(void)setjmp(jabort);
+	while ((cp = remglob(argv, 0)) != NULL) {
 		if (*cp == '\0') {
 			mflag = 0;
 			continue;
 		}
 		if (mflag && confirm(argv[0], cp)) {
-			(void) command("DELE %s", cp);
+			(void)command("DELE %s", cp);
 			if (!mflag && fromatty) {
 				ointer = interactive;
 				interactive = 1;
@@ -1143,7 +1020,7 @@
 			}
 		}
 	}
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
 	mflag = 0;
 }
 
@@ -1158,14 +1035,14 @@
 
 	if (argc < 2 && !another(&argc, &argv, "from-name"))
 		goto usage;
-	if (argc < 3 && !another(&argc, &argv, "to-name")) {
+	if ((argc < 3 && !another(&argc, &argv, "to-name")) || argc > 3) {
 usage:
 		printf("%s from-name to-name\n", argv[0]);
 		code = -1;
 		return;
 	}
 	if (command("RNFR %s", argv[1]) == CONTINUE)
-		(void) command("RNTO %s", argv[2]);
+		(void)command("RNTO %s", argv[2]);
 }
 
 /*
@@ -1177,7 +1054,7 @@
 	int argc;
 	char *argv[];
 {
-	char *cmd;
+	const char *cmd;
 
 	if (argc < 2)
 		argc++, argv[1] = NULL;
@@ -1188,17 +1065,21 @@
 		code = -1;
 		return;
 	}
-	cmd = argv[0][0] == 'n' ? "NLST" : "LIST";
+	cmd = strcmp(argv[0], "dir") == 0 ? "LIST" : "NLST";
 	if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
 		code = -1;
 		return;
 	}
 	if (strcmp(argv[2], "-") && *argv[2] != '|')
-		if (!globulize(&argv[2]) || !confirm("output to local-file:", argv[2])) {
+		if (!globulize(&argv[2]) || !confirm("output to local-file:",
+		    argv[2])) {
 			code = -1;
 			return;
 	}
 	recvrequest(cmd, argv[2], argv[1], "w", 0);
+
+	/* flush results in case commands are coming from a pipe */
+	fflush(stdout);
 }
 
 /*
@@ -1208,11 +1089,12 @@
 void
 mls(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	sig_t oldintr;
 	int ointer, i;
-	char *cmd, mode[1], *dest;
+	const char *cmd;
+	char mode[1], *dest;
 
 	if (argc < 2 && !another(&argc, &argv, "remote-files"))
 		goto usage;
@@ -1230,11 +1112,11 @@
 			code = -1;
 			return;
 	}
-	cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
+	cmd = strcmp(argv[0], "mls") == 0 ? "NLST" : "LIST";
 	mname = argv[0];
 	mflag = 1;
 	oldintr = signal(SIGINT, mabort);
-	(void) setjmp(jabort);
+	(void)setjmp(jabort);
 	for (i = 1; mflag && i < argc-1; ++i) {
 		*mode = (i == 1) ? 'w' : 'a';
 		recvrequest(cmd, dest, argv[i], mode, 0);
@@ -1247,7 +1129,7 @@
 			interactive = ointer;
 		}
 	}
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
 	mflag = 0;
 }
 
@@ -1258,39 +1140,40 @@
 void
 shell(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	pid_t pid;
 	sig_t old1, old2;
-	char shellnam[40], *shell, *namep;
+	char shellnam[MAXPATHLEN], *shell, *namep;
 	union wait status;
 
 	old1 = signal (SIGINT, SIG_IGN);
 	old2 = signal (SIGQUIT, SIG_IGN);
 	if ((pid = fork()) == 0) {
 		for (pid = 3; pid < 20; pid++)
-			(void) close(pid);
-		(void) signal(SIGINT, SIG_DFL);
-		(void) signal(SIGQUIT, SIG_DFL);
+			(void)close(pid);
+		(void)signal(SIGINT, SIG_DFL);
+		(void)signal(SIGQUIT, SIG_DFL);
 		shell = getenv("SHELL");
 		if (shell == NULL)
 			shell = _PATH_BSHELL;
-		namep = strrchr(shell,'/');
+		namep = strrchr(shell, '/');
 		if (namep == NULL)
 			namep = shell;
-		(void) strcpy(shellnam,"-");
-		(void) strcat(shellnam, ++namep);
+		shellnam[0] = '-';
+		(void)strncpy(shellnam + 1, ++namep, sizeof(shellnam) - 2);
+		shellnam[sizeof(shellnam) - 1] = '\0';
 		if (strcmp(namep, "sh") != 0)
 			shellnam[0] = '+';
 		if (debug) {
-			printf ("%s\n", shell);
-			(void) fflush (stdout);
+			puts(shell);
+			(void)fflush(stdout);
 		}
 		if (argc > 1) {
-			execl(shell,shellnam,"-c",altarg,(char *)0);
+			execl(shell, shellnam, "-c", altarg, (char *)0);
 		}
 		else {
-			execl(shell,shellnam,(char *)0);
+			execl(shell, shellnam, (char *)0);
 		}
 		warn("%s", shell);
 		code = -1;
@@ -1299,8 +1182,8 @@
 	if (pid > 0)
 		while (wait((int *)&status) != pid)
 			;
-	(void) signal(SIGINT, old1);
-	(void) signal(SIGQUIT, old2);
+	(void)signal(SIGINT, old1);
+	(void)signal(SIGQUIT, old2);
 	if (pid == -1) {
 		warn("%s", "Try again later");
 		code = -1;
@@ -1316,13 +1199,13 @@
 void
 user(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	char acct[80];
 	int n, aflag = 0;
 
 	if (argc < 2)
-		(void) another(&argc, &argv, "username");
+		(void)another(&argc, &argv, "username");
 	if (argc < 2 || argc > 4) {
 		printf("usage: %s username [password] [account]\n", argv[0]);
 		code = -1;
@@ -1336,8 +1219,9 @@
 	}
 	if (n == CONTINUE) {
 		if (argc < 4) {
-			printf("Account: "); (void) fflush(stdout);
-			(void) fgets(acct, sizeof(acct) - 1, stdin);
+			(void)fputs("Account: ", stdout);
+			(void)fflush(stdout);
+			(void)fgets(acct, sizeof(acct) - 1, stdin);
 			acct[strlen(acct) - 1] = '\0';
 			argv[3] = acct; argc++;
 		}
@@ -1345,16 +1229,16 @@
 		aflag++;
 	}
 	if (n != COMPLETE) {
-		fprintf(stdout, "Login failed.\n");
+		puts("Login failed.");
 		return;
 	}
 	if (!aflag && argc == 4) {
-		(void) command("ACCT %s", argv[3]);
+		(void)command("ACCT %s", argv[3]);
 	}
 }
 
 /*
- * Print working directory.
+ * Print working directory on remote machine.
  */
 /*VARARGS*/
 void
@@ -1369,13 +1253,30 @@
 	 */
 	verbose = 1;
 	if (command("PWD") == ERROR && code == 500) {
-		printf("PWD command not recognized, trying XPWD\n");
-		(void) command("XPWD");
+		puts("PWD command not recognized, trying XPWD");
+		(void)command("XPWD");
 	}
 	verbose = oldverbose;
 }
 
 /*
+ * Print working directory on local machine.
+ */
+void
+lpwd(argc, argv)
+	int argc;
+	char *argv[];
+{
+	char buf[MAXPATHLEN];
+
+	if (getcwd(buf, sizeof(buf)) != NULL)
+		printf("Local directory %s\n", buf);
+	else
+		warn("getcwd");
+	code = 0;
+}
+
+/*
  * Make a directory.
  */
 void
@@ -1384,15 +1285,16 @@
 	char *argv[];
 {
 
-	if (argc < 2 && !another(&argc, &argv, "directory-name")) {
+	if ((argc < 2 && !another(&argc, &argv, "directory-name")) ||
+	    argc > 2) {
 		printf("usage: %s directory-name\n", argv[0]);
 		code = -1;
 		return;
 	}
 	if (command("MKD %s", argv[1]) == ERROR && code == 500) {
 		if (verbose)
-			printf("MKD command not recognized, trying XMKD\n");
-		(void) command("XMKD %s", argv[1]);
+			puts("MKD command not recognized, trying XMKD");
+		(void)command("XMKD %s", argv[1]);
 	}
 }
 
@@ -1405,15 +1307,16 @@
 	char *argv[];
 {
 
-	if (argc < 2 && !another(&argc, &argv, "directory-name")) {
+	if ((argc < 2 && !another(&argc, &argv, "directory-name")) ||
+	    argc > 2) {
 		printf("usage: %s directory-name\n", argv[0]);
 		code = -1;
 		return;
 	}
 	if (command("RMD %s", argv[1]) == ERROR && code == 500) {
 		if (verbose)
-			printf("RMD command not recognized, trying XRMD\n");
-		(void) command("XRMD %s", argv[1]);
+			puts("RMD command not recognized, trying XRMD");
+		(void)command("XRMD %s", argv[1]);
 	}
 }
 
@@ -1459,20 +1362,22 @@
  */
 void
 quote1(initial, argc, argv)
-	char *initial;
+	const char *initial;
 	int argc;
-	char **argv;
+	char *argv[];
 {
 	int i, len;
 	char buf[BUFSIZ];		/* must be >= sizeof(line) */
 
-	(void) strcpy(buf, initial);
+	(void)strncpy(buf, initial, sizeof(buf) - 1);
+	buf[sizeof(buf) - 1] = '\0';
 	if (argc > 1) {
 		len = strlen(buf);
-		len += strlen(strcpy(&buf[len], argv[1]));
-		for (i = 2; i < argc; i++) {
+		len += strlen(strncpy(&buf[len], argv[1], sizeof(buf) - len - 1));
+		for (i = 2; i < argc && len < sizeof(buf); i++) {
 			buf[len++] = ' ';
-			len += strlen(strcpy(&buf[len], argv[i]));
+			len += strlen(strncpy(&buf[len], argv[i],
+			    sizeof(buf) - len) - 1);
 		}
 	}
 	if (command(buf) == PRELIM) {
@@ -1489,13 +1394,13 @@
 
 	if (argc < 2 && !another(&argc, &argv, "mode"))
 		goto usage;
-	if (argc < 3 && !another(&argc, &argv, "file-name")) {
+	if ((argc < 3 && !another(&argc, &argv, "file-name")) || argc > 3) {
 usage:
 		printf("usage: %s mode file-name\n", argv[0]);
 		code = -1;
 		return;
 	}
-	(void) command("SITE CHMOD %s %s", argv[1], argv[2]);
+	(void)command("SITE CHMOD %s %s", argv[1], argv[2]);
 }
 
 void
@@ -1506,7 +1411,7 @@
 	int oldverbose = verbose;
 
 	verbose = 1;
-	(void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
+	(void)command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
 	verbose = oldverbose;
 }
 
@@ -1518,7 +1423,7 @@
 	int oldverbose = verbose;
 
 	verbose = 1;
-	(void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
+	(void)command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
 	verbose = oldverbose;
 }
 
@@ -1533,7 +1438,7 @@
 	int oldverbose = verbose;
 
 	verbose = 1;
-	(void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
+	(void)command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
 	verbose = oldverbose;
 }
 
@@ -1567,9 +1472,9 @@
 
 	if (!connected)
 		return;
-	(void) command("QUIT");
+	(void)command("QUIT");
 	if (cout) {
-		(void) fclose(cout);
+		(void)fclose(cout);
 	}
 	cout = NULL;
 	connected = 0;
@@ -1579,89 +1484,33 @@
 	}
 }
 
-int
-confirm(cmd, file)
-	char *cmd, *file;
-{
-	char line[BUFSIZ];
-
-	if (!interactive)
-		return (1);
-	printf("%s %s? ", cmd, file);
-	(void) fflush(stdout);
-	if (fgets(line, sizeof line, stdin) == NULL)
-		return (0);
-	return (*line != 'n' && *line != 'N');
-}
-
 void
-fatal(msg)
-	char *msg;
-{
-
-	errx(1, "%s", msg);
-}
-
-/*
- * Glob a local file name specification with
- * the expectation of a single return value.
- * Can't control multiple values being expanded
- * from the expression, we return only the first.
- */
-int
-globulize(cpp)
-	char **cpp;
-{
-	glob_t gl;
-	int flags;
-
-	if (!doglob)
-		return (1);
-
-	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
-	memset(&gl, 0, sizeof(gl));
-	if (glob(*cpp, flags, NULL, &gl) ||
-	    gl.gl_pathc == 0) {
-		warnx("%s: not found", *cpp);
-		globfree(&gl);
-		return (0);
-	}
-	*cpp = strdup(gl.gl_pathv[0]);	/* XXX - wasted memory */
-	globfree(&gl);
-	return (1);
-}
-
-void
-account(argc,argv)
+account(argc, argv)
 	int argc;
-	char **argv;
+	char *argv[];
 {
-	char acct[50], *ap;
+	char *ap;
 
-	if (argc > 1) {
-		++argv;
-		--argc;
-		(void) strncpy(acct,*argv,49);
-		acct[49] = '\0';
-		while (argc > 1) {
-			--argc;
-			++argv;
-			(void) strncat(acct,*argv, 49-strlen(acct));
-		}
-		ap = acct;
+	if (argc > 2) {
+		printf("usage: %s [password]\n", argv[0]);
+		code = -1;
+		return;
 	}
-	else {
+	else if (argc == 2)
+		ap = argv[1];
+	else
 		ap = getpass("Account:");
-	}
-	(void) command("ACCT %s", ap);
+	(void)command("ACCT %s", ap);
 }
 
 jmp_buf abortprox;
 
 void
-proxabort()
+proxabort(notused)
+	int notused;
 {
 
+	alarmtimer(0);
 	if (!proxy) {
 		pswitch(1);
 	}
@@ -1672,7 +1521,7 @@
 		proxflag = 0;
 	}
 	pswitch(0);
-	longjmp(abortprox,1);
+	longjmp(abortprox, 1);
 }
 
 void
@@ -1681,6 +1530,7 @@
 	char *argv[];
 {
 	struct cmd *c;
+	int cmdpos;
 	sig_t oldintr;
 
 	if (argc < 2 && !another(&argc, &argv, "command")) {
@@ -1690,20 +1540,20 @@
 	}
 	c = getcmd(argv[1]);
 	if (c == (struct cmd *) -1) {
-		printf("?Ambiguous command\n");
-		(void) fflush(stdout);
+		puts("?Ambiguous command");
+		(void)fflush(stdout);
 		code = -1;
 		return;
 	}
 	if (c == 0) {
-		printf("?Invalid command\n");
-		(void) fflush(stdout);
+		puts("?Invalid command");
+		(void)fflush(stdout);
 		code = -1;
 		return;
 	}
 	if (!c->c_proxy) {
-		printf("?Invalid proxy command\n");
-		(void) fflush(stdout);
+		puts("?Invalid proxy command");
+		(void)fflush(stdout);
 		code = -1;
 		return;
 	}
@@ -1714,13 +1564,16 @@
 	oldintr = signal(SIGINT, proxabort);
 	pswitch(1);
 	if (c->c_conn && !connected) {
-		printf("Not connected\n");
-		(void) fflush(stdout);
+		puts("Not connected");
+		(void)fflush(stdout);
 		pswitch(0);
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
 		code = -1;
 		return;
 	}
+	cmdpos = strcspn(line, " \t");
+	if (cmdpos > 0)		/* remove leading "proxy " from input buffer */
+		memmove(line, line + cmdpos + 1, strlen(line) - cmdpos + 1);
 	(*c->c_handler)(argc-1, argv+1);
 	if (connected) {
 		proxflag = 1;
@@ -1729,7 +1582,7 @@
 		proxflag = 0;
 	}
 	pswitch(0);
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
 }
 
 void
@@ -1738,9 +1591,7 @@
 	char *argv[];
 {
 
-	mcase = !mcase;
-	printf("Case mapping %s.\n", onoff(mcase));
-	code = mcase;
+	code = togglevar(argc, argv, &mcase, "Case mapping");
 }
 
 void
@@ -1749,32 +1600,30 @@
 	char *argv[];
 {
 
-	crflag = !crflag;
-	printf("Carriage Return stripping %s.\n", onoff(crflag));
-	code = crflag;
+	code = togglevar(argc, argv, &crflag, "Carriage Return stripping");
 }
 
 void
-setntrans(argc,argv)
+setntrans(argc, argv)
 	int argc;
 	char *argv[];
 {
 	if (argc == 1) {
 		ntflag = 0;
-		printf("Ntrans off.\n");
+		puts("Ntrans off.");
 		code = ntflag;
 		return;
 	}
 	ntflag++;
 	code = ntflag;
-	(void) strncpy(ntin, argv[1], 16);
-	ntin[16] = '\0';
+	(void)strncpy(ntin, argv[1], sizeof(ntin) - 1);
+	ntin[sizeof(ntin) - 1] = '\0';
 	if (argc == 2) {
 		ntout[0] = '\0';
 		return;
 	}
-	(void) strncpy(ntout, argv[2], 16);
-	ntout[16] = '\0';
+	(void)strncpy(ntout, argv[2], sizeof(ntout) - 1);
+	ntout[sizeof(ntout) - 1] = '\0';
 }
 
 char *
@@ -1815,12 +1664,12 @@
 
 	if (argc == 1) {
 		mapflag = 0;
-		printf("Nmap off.\n");
+		puts("Nmap off.");
 		code = mapflag;
 		return;
 	}
-	if (argc < 3 && !another(&argc, &argv, "mapout")) {
-		printf("Usage: %s [mapin mapout]\n",argv[0]);
+	if ((argc < 3 && !another(&argc, &argv, "mapout")) || argc > 3) {
+		printf("Usage: %s [mapin mapout]\n", argv[0]);
 		code = -1;
 		return;
 	}
@@ -1834,10 +1683,10 @@
 		cp = strchr(altarg, ' ');
 	}
 	*cp = '\0';
-	(void) strncpy(mapin, altarg, MAXPATHLEN - 1);
+	(void)strncpy(mapin, altarg, MAXPATHLEN - 1);
 	while (*++cp == ' ')
 		continue;
-	(void) strncpy(mapout, cp, MAXPATHLEN - 1);
+	(void)strncpy(mapout, cp, MAXPATHLEN - 1);
 }
 
 char *
@@ -1950,7 +1799,7 @@
 						}
 					}
 					if (!*cp2) {
-						printf("nmap: unbalanced brackets\n");
+						puts("nmap: unbalanced brackets");
 						return (name);
 					}
 					match = 1;
@@ -1963,7 +1812,7 @@
 					      }
 					}
 					if (!*cp2) {
-						printf("nmap: unbalanced brackets\n");
+						puts("nmap: unbalanced brackets");
 						return (name);
 					}
 					break;
@@ -2011,14 +1860,34 @@
 }
 
 void
+setpassive(argc, argv)
+	int argc;
+	char *argv[];
+{
+
+	code = togglevar(argc, argv, &passivemode,
+	    verbose ? "Passive mode" : NULL);
+}
+
+/*
+ * Restrict FTP data port range to a high group of "safe" ports
+ */
+void
+setrestrict(argc, argv)
+	int argc;
+	char *argv[];
+{
+	code = togglevar(argc, argv, &restricted_data_ports,
+		verbose ? "Data port range restricted" : NULL);
+}
+
+void
 setsunique(argc, argv)
 	int argc;
 	char *argv[];
 {
 
-	sunique = !sunique;
-	printf("Store unique %s.\n", onoff(sunique));
-	code = sunique;
+	code = togglevar(argc, argv, &sunique, "Store unique");
 }
 
 void
@@ -2027,23 +1896,25 @@
 	char *argv[];
 {
 
-	runique = !runique;
-	printf("Receive unique %s.\n", onoff(runique));
-	code = runique;
+	code = togglevar(argc, argv, &runique, "Receive unique");
 }
 
-/* change directory to perent directory */
+/* change directory to parent directory */
 void
 cdup(argc, argv)
 	int argc;
 	char *argv[];
 {
+	int r;
 
-	if (command("CDUP") == ERROR && code == 500) {
+	r = command("CDUP");
+	if (r == ERROR && code == 500) {
 		if (verbose)
-			printf("CDUP command not recognized, trying XCUP\n");
-		(void) command("XCUP");
+			puts("CDUP command not recognized, trying XCUP");
+		r = command("XCUP");
 	}
+	if (r == COMPLETE)
+		dirchange = 1;
 }
 
 /* restart transfer at specific point */
@@ -2054,11 +1925,11 @@
 {
 
 	if (argc != 2)
-		printf("restart: offset not specified\n");
+		puts("restart: offset not specified");
 	else {
 		restart_point = atol(argv[1]);
-		printf("restarting at %qd. %s\n", restart_point,
-		    "execute get, put or append to initiate transfer");
+		printf("Restarting at %qd. Execute get, put or append to"
+			"initiate transfer\n", restart_point);
 	}
 }
 
@@ -2069,7 +1940,7 @@
 	char *argv[];
 {
 
-	(void) command("SYST");
+	(void)command("SYST");
 }
 
 void
@@ -2081,29 +1952,29 @@
 	int c;
 
 	if (macnum == 16) {
-		printf("Limit of 16 macros have already been defined\n");
+		puts("Limit of 16 macros have already been defined");
 		code = -1;
 		return;
 	}
-	if (argc < 2 && !another(&argc, &argv, "macro name")) {
-		printf("Usage: %s macro_name\n",argv[0]);
+	if ((argc < 2 && !another(&argc, &argv, "macro name")) || argc > 2) {
+		printf("Usage: %s macro_name\n", argv[0]);
 		code = -1;
 		return;
 	}
 	if (interactive) {
-		printf("Enter macro line by line, terminating it with a null line\n");
+		puts("Enter macro line by line, terminating it with a null line");
 	}
-	(void) strncpy(macros[macnum].mac_name, argv[1], 8);
-	if (macnum == 0) {
+	(void)strncpy(macros[macnum].mac_name, argv[1],
+	    sizeof(macros[macnum].mac_name) - 1);
+	macros[macnum].mac_name[sizeof(macros[macnum].mac_name) - 1] = '\0';
+	if (macnum == 0)
 		macros[macnum].mac_start = macbuf;
-	}
-	else {
+	else
 		macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
-	}
 	tmp = macros[macnum].mac_start;
 	while (tmp != macbuf+4096) {
 		if ((c = getchar()) == EOF) {
-			printf("macdef:end of file encountered\n");
+			puts("macdef: end of file encountered");
 			code = -1;
 			return;
 		}
@@ -2126,7 +1997,7 @@
 		while ((c = getchar()) != '\n' && c != EOF)
 			/* LOOP */;
 		if (c == EOF || getchar() == '\n') {
-			printf("Macro not defined - 4k buffer exceeded\n");
+			puts("Macro not defined - 4k buffer exceeded");
 			code = -1;
 			return;
 		}
@@ -2134,44 +2005,24 @@
 }
 
 /*
- * Start up passive mode interaction
- */
-void
-setpassive()
-{
-	passivemode = !passivemode;
-	printf("Passive mode %s.\n", onoff(passivemode));
-	code = passivemode;
-}
-
-/*
- * Restrict FTP data port range to a high group of "safe" ports
- */
-void
-setrestrict()
-{
-	restricted_data_ports = !restricted_data_ports;
-	printf("Data port range restrictions %s.\n",
-		onoff(restricted_data_ports));
-	code = restricted_data_ports;
-}
-
-/*
  * get size of file on remote machine
  */
-/*VARARGS*/
 void
 sizecmd(argc, argv)
 	int argc;
 	char *argv[];
 {
+	off_t size;
 
-	if (argc < 2 && !another(&argc, &argv, "filename")) {
+	if ((argc < 2 && !another(&argc, &argv, "filename")) || argc > 2) {
 		printf("usage: %s filename\n", argv[0]);
 		code = -1;
 		return;
 	}
-	(void) command("SIZE %s", argv[1]);
+	size = remotesize(argv[1], 1);
+	if (size != -1)
+		printf("%s\t%qd\n", argv[1], size);
+	code = size;
 }
 
 /*
@@ -2182,30 +2033,21 @@
 	int argc;
 	char *argv[];
 {
-	int overbose;
+	time_t mtime;
 
-	if (argc < 2 && !another(&argc, &argv, "filename")) {
+	if ((argc < 2 && !another(&argc, &argv, "filename")) || argc > 2) {
 		printf("usage: %s filename\n", argv[0]);
 		code = -1;
 		return;
 	}
-	overbose = verbose;
-	if (debug == 0)
-		verbose = -1;
-	if (command("MDTM %s", argv[1]) == COMPLETE) {
-		int yy, mo, day, hour, min, sec;
-		sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
-			&day, &hour, &min, &sec);
-		/* might want to print this in local time */
-		printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1],
-			mo, day, yy, hour, min, sec);
-	} else
-		printf("%s\n", reply_string);
-	verbose = overbose;
+	mtime = remotemodtime(argv[1], 1);
+	if (mtime != -1)
+		printf("%s\t%s", argv[1], asctime(localtime(&mtime)));
+	code = mtime;
 }
 
 /*
- * show status on reomte machine
+ * show status on remote machine
  */
 void
 rmtstatus(argc, argv)
@@ -2213,7 +2055,7 @@
 	char *argv[];
 {
 
-	(void) command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
+	(void)command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
 }
 
 /*
diff --new-file -r -b -u src/usr.bin/ftp.old/cmdtab.c src/usr.bin/ftp/cmdtab.c
--- src/usr.bin/ftp.old/cmdtab.c	Thu Sep 19 20:36:32 1996
+++ src/usr.bin/ftp/cmdtab.c	Sun Mar  2 19:56:02 1997
@@ -1,3 +1,6 @@
+/*	$OpenBSD: cmdtab.c,v 1.6 1997/02/03 01:05:34 millert Exp $	*/
+/*	$NetBSD: cmdtab.c,v 1.12 1997/01/19 14:19:05 lukem Exp $	*/
+
 /*
  * Copyright (c) 1985, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -32,7 +35,12 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)cmdtab.c	8.3 (Berkeley) 4/2/94";
+#if 0
+static char sccsid[] = "@(#)cmdtab.c	8.4 (Berkeley) 10/9/94";
+static char rcsid[] = "$OpenBSD: cmdtab.c,v 1.6 1997/02/03 01:05:34 millert Exp $";
+#else
+static char rcsid[] = "$FreeBSD$";
+#endif
 #endif /* not lint */
 
 #include <stdio.h>
@@ -51,19 +59,23 @@
 char	cdhelp[] =	"change remote working directory";
 char	cduphelp[] = 	"change remote working directory to parent directory";
 char	chmodhelp[] =	"change file permissions of remote file";
-char	connecthelp[] =	"connect to remote tftp";
+char	connecthelp[] =	"connect to remote ftp server";
 char	crhelp[] =	"toggle carriage return stripping on ascii gets";
-char	deletehelp[] =	"delete remote file";
 char	debughelp[] =	"toggle/set debugging mode";
+char	deletehelp[] =	"delete remote file";
 char	dirhelp[] =	"list contents of remote directory";
 char	disconhelp[] =	"terminate ftp session";
 char	domachelp[] = 	"execute macro";
+#ifndef SMALLFTP
+char	edithelp[] =	"toggle command line editing";
+#endif /* !SMALLFTP */
 char	formhelp[] =	"set file transfer format";
 char	globhelp[] =	"toggle metacharacter expansion of local file names";
-char	hashhelp[] =	"toggle printing `#' for each buffer transferred";
+char	hashhelp[] =	"toggle printing `#' marks; specify number to set size";
 char	helphelp[] =	"print local help information";
 char	idlehelp[] =	"get (set) idle timer on remote side";
 char	lcdhelp[] =	"change local working directory";
+char	lpwdhelp[] =	"print local working directory";
 char	lshelp[] =	"list contents of remote directory";
 char	macdefhelp[] =  "define a macro";
 char	mdeletehelp[] =	"delete multiple files";
@@ -71,8 +83,8 @@
 char	mgethelp[] =	"get multiple files";
 char	mkdirhelp[] =	"make directory on the remote machine";
 char	mlshelp[] =	"list contents of multiple remote directories";
-char	modtimehelp[] = "show last modification time of remote file";
 char	modehelp[] =	"set file transfer mode";
+char	modtimehelp[] = "show last modification time of remote file";
 char	mputhelp[] =	"send multiple files";
 char	newerhelp[] =	"get file if remote file is newer than local file ";
 char	nlisthelp[] =	"nlist contents of remote directory";
@@ -80,6 +92,9 @@
 char	ntranshelp[] =	"set translation table for default file name mapping";
 char	passivehelp[] = "enter passive transfer mode";
 char	porthelp[] =	"toggle use of PORT cmd for each data connection";
+char	preservehelp[] ="toggle preservation of modification time of "
+			"retreived files";
+char	progresshelp[] ="toggle transfer progress meter";
 char	prompthelp[] =	"force interactive prompting on multiple commands";
 char	proxyhelp[] =	"issue command on alternate connection";
 char	pwdhelp[] =	"print working directory on remote machine";
@@ -89,15 +104,17 @@
 char	regethelp[] =	"get file restarting at end of local file";
 char	remotehelp[] =	"get help from remote server";
 char	renamehelp[] =	"rename file";
+char	resethelp[] =	"clear queued command replies";
 char	restarthelp[]=	"restart file transfer at bytecount";
 char	restricthelp[]= "toggle restriction of data port range";
 char	rmdirhelp[] =	"remove directory on the remote machine";
 char	rmtstatushelp[]="show status of remote machine";
 char	runiquehelp[] = "toggle store unique for local files";
-char	resethelp[] =	"clear queued command replies";
 char	sendhelp[] =	"send one file";
-char	sitehelp[] =	"send site specific command to remote server\n\t\tTry \"rhelp site\" or \"site help\" for more information";
 char	shellhelp[] =	"escape to the shell";
+char	sitehelp[] =	"send site specific command to remote server\n"
+			"\t\tTry \"rhelp site\" or \"site help\" "
+			"for more information";
 char	sizecmdhelp[] = "show size of remote file";
 char	statushelp[] =	"show current status";
 char	structhelp[] =	"set file transfer structure";
@@ -110,82 +127,98 @@
 char	userhelp[] =	"send new user information";
 char	verbosehelp[] =	"toggle verbose mode";
 
+#ifdef SMALLFTP
+#define CMPL(x)
+#define CMPL0
+#else  /* !SMALLFTP */
+#define CMPL(x)	__STRING(x), 
+#define CMPL0	"",
+#endif /* !SMALLFTP */
+
 struct cmd cmdtab[] = {
-	{ "!",		shellhelp,	0,	0,	0,	shell },
-	{ "$",		domachelp,	1,	0,	0,	domacro },
-	{ "account",	accounthelp,	0,	1,	1,	account},
-	{ "append",	appendhelp,	1,	1,	1,	put },
-	{ "ascii",	asciihelp,	0,	1,	1,	setascii },
-	{ "bell",	beephelp,	0,	0,	0,	setbell },
-	{ "binary",	binaryhelp,	0,	1,	1,	setbinary },
-	{ "bye",	quithelp,	0,	0,	0,	quit },
-	{ "case",	casehelp,	0,	0,	1,	setcase },
-	{ "cd",		cdhelp,		0,	1,	1,	cd },
-	{ "cdup",	cduphelp,	0,	1,	1,	cdup },
-	{ "chmod",	chmodhelp,	0,	1,	1,	do_chmod },
-	{ "close",	disconhelp,	0,	1,	1,	disconnect },
-	{ "cr",		crhelp,		0,	0,	0,	setcr },
-	{ "delete",	deletehelp,	0,	1,	1,	delete },
-	{ "debug",	debughelp,	0,	0,	0,	setdebug },
-	{ "dir",	dirhelp,	1,	1,	1,	ls },
-	{ "disconnect",	disconhelp,	0,	1,	1,	disconnect },
-	{ "form",	formhelp,	0,	1,	1,	setform },
-	{ "ftp",	connecthelp,	0,	0,	1,	setpeer },
-	{ "get",	receivehelp,	1,	1,	1,	get },
-	{ "glob",	globhelp,	0,	0,	0,	setglob },
-	{ "hash",	hashhelp,	0,	0,	0,	sethash },
-	{ "help",	helphelp,	0,	0,	1,	help },
-	{ "idle",	idlehelp,	0,	1,	1,	idle },
-	{ "image",	binaryhelp,	0,	1,	1,	setbinary },
-	{ "lcd",	lcdhelp,	0,	0,	0,	lcd },
-	{ "ls",		lshelp,		1,	1,	1,	ls },
-	{ "macdef",	macdefhelp,	0,	0,	0,	macdef },
-	{ "mdelete",	mdeletehelp,	1,	1,	1,	mdelete },
-	{ "mdir",	mdirhelp,	1,	1,	1,	mls },
-	{ "mget",	mgethelp,	1,	1,	1,	mget },
-	{ "mkdir",	mkdirhelp,	0,	1,	1,	makedir },
-	{ "mls",	mlshelp,	1,	1,	1,	mls },
-	{ "mode",	modehelp,	0,	1,	1,	setftmode },
-	{ "modtime",	modtimehelp,	0,	1,	1,	modtime },
-	{ "mput",	mputhelp,	1,	1,	1,	mput },
-	{ "newer",	newerhelp,	1,	1,	1,	newer },
-	{ "nmap",	nmaphelp,	0,	0,	1,	setnmap },
-	{ "nlist",	nlisthelp,	1,	1,	1,	ls },
-	{ "ntrans",	ntranshelp,	0,	0,	1,	setntrans },
-	{ "open",	connecthelp,	0,	0,	1,	setpeer },
-	{ "prompt",	prompthelp,	0,	0,	0,	setprompt },
-	{ "passive",	passivehelp,	0,	0,	0,	setpassive },
-	{ "proxy",	proxyhelp,	0,	0,	1,	doproxy },
-	{ "sendport",	porthelp,	0,	0,	0,	setport },
-	{ "put",	sendhelp,	1,	1,	1,	put },
-	{ "pwd",	pwdhelp,	0,	1,	1,	pwd },
-	{ "quit",	quithelp,	0,	0,	0,	quit },
-	{ "quote",	quotehelp,	1,	1,	1,	quote },
-	{ "recv",	receivehelp,	1,	1,	1,	get },
-	{ "reget",	regethelp,	1,	1,	1,	reget },
-	{ "rstatus",	rmtstatushelp,	0,	1,	1,	rmtstatus },
-	{ "rhelp",	remotehelp,	0,	1,	1,	rmthelp },
-	{ "rename",	renamehelp,	0,	1,	1,	renamefile },
-	{ "reset",	resethelp,	0,	1,	1,	reset },
-	{ "restart",	restarthelp,	1,	1,	1,	restart },
-	{ "restrict",	restricthelp,	0,	0,	0,	setrestrict },
-	{ "rmdir",	rmdirhelp,	0,	1,	1,	removedir },
-	{ "runique",	runiquehelp,	0,	0,	1,	setrunique },
-	{ "send",	sendhelp,	1,	1,	1,	put },
-	{ "site",	sitehelp,	0,	1,	1,	site },
-	{ "size",	sizecmdhelp,	1,	1,	1,	sizecmd },
-	{ "status",	statushelp,	0,	0,	1,	status },
-	{ "struct",	structhelp,	0,	1,	1,	setstruct },
-	{ "system",	systemhelp,	0,	1,	1,	syst },
-	{ "sunique",	suniquehelp,	0,	0,	1,	setsunique },
-	{ "tenex",	tenexhelp,	0,	1,	1,	settenex },
-	{ "trace",	tracehelp,	0,	0,	0,	settrace },
-	{ "type",	typehelp,	0,	1,	1,	settype },
-	{ "user",	userhelp,	0,	1,	1,	user },
-	{ "umask",	umaskhelp,	0,	1,	1,	do_umask },
-	{ "verbose",	verbosehelp,	0,	0,	0,	setverbose },
-	{ "?",		helphelp,	0,	0,	1,	help },
+	{ "!",		shellhelp,	0, 0, 0, CMPL0		shell },
+	{ "$",		domachelp,	1, 0, 0, CMPL0		domacro },
+	{ "account",	accounthelp,	0, 1, 1, CMPL0		account},
+	{ "append",	appendhelp,	1, 1, 1, CMPL(lr)	put },
+	{ "ascii",	asciihelp,	0, 1, 1, CMPL0		setascii },
+	{ "bell",	beephelp,	0, 0, 0, CMPL0		setbell },
+	{ "binary",	binaryhelp,	0, 1, 1, CMPL0		setbinary },
+	{ "bye",	quithelp,	0, 0, 0, CMPL0		quit },
+	{ "case",	casehelp,	0, 0, 1, CMPL0		setcase },
+	{ "cd",		cdhelp,		0, 1, 1, CMPL(r)	cd },
+	{ "cdup",	cduphelp,	0, 1, 1, CMPL0		cdup },
+	{ "chmod",	chmodhelp,	0, 1, 1, CMPL(nr)	do_chmod },
+	{ "close",	disconhelp,	0, 1, 1, CMPL0		disconnect },
+	{ "cr",		crhelp,		0, 0, 0, CMPL0		setcr },
+	{ "debug",	debughelp,	0, 0, 0, CMPL0		setdebug },
+	{ "delete",	deletehelp,	0, 1, 1, CMPL(r)	delete },
+	{ "dir",	dirhelp,	1, 1, 1, CMPL(rl)	ls },
+	{ "disconnect",	disconhelp,	0, 1, 1, CMPL0		disconnect },
+#ifndef SMALLFTP
+	{ "edit",	edithelp,	0, 0, 0, CMPL0		setedit },
+#endif /* !SMALLFTP */
+	{ "exit",	quithelp,	0, 0, 0, CMPL0		quit },
+	{ "form",	formhelp,	0, 1, 1, CMPL0		setform },
+	{ "ftp",	connecthelp,	0, 0, 1, CMPL0		setpeer },
+	{ "get",	receivehelp,	1, 1, 1, CMPL(rl)	get },
+	{ "glob",	globhelp,	0, 0, 0, CMPL0		setglob },
+	{ "hash",	hashhelp,	0, 0, 0, CMPL0		sethash },
+	{ "help",	helphelp,	0, 0, 1, CMPL(C)	help },
+	{ "idle",	idlehelp,	0, 1, 1, CMPL0		idle },
+	{ "image",	binaryhelp,	0, 1, 1, CMPL0		setbinary },
+	{ "lcd",	lcdhelp,	0, 0, 0, CMPL(l)	lcd },
+	{ "lpwd",	lpwdhelp,	0, 0, 0, CMPL0		lpwd },
+	{ "ls",		lshelp,		1, 1, 1, CMPL(rl)	ls },
+	{ "macdef",	macdefhelp,	0, 0, 0, CMPL0		macdef },
+	{ "mdelete",	mdeletehelp,	1, 1, 1, CMPL(R)	mdelete },
+	{ "mdir",	mdirhelp,	1, 1, 1, CMPL(R)	mls },
+	{ "mget",	mgethelp,	1, 1, 1, CMPL(R)	mget },
+	{ "mkdir",	mkdirhelp,	0, 1, 1, CMPL(r)	makedir },
+	{ "mls",	mlshelp,	1, 1, 1, CMPL(R)	mls },
+	{ "mode",	modehelp,	0, 1, 1, CMPL0		setftmode },
+	{ "modtime",	modtimehelp,	0, 1, 1, CMPL(r)	modtime },
+	{ "mput",	mputhelp,	1, 1, 1, CMPL(L)	mput },
+	{ "msend",	mputhelp,	1, 1, 1, CMPL(L)	mput },
+	{ "newer",	newerhelp,	1, 1, 1, CMPL(r)	newer },
+	{ "nlist",	nlisthelp,	1, 1, 1, CMPL(rl)	ls },
+	{ "nmap",	nmaphelp,	0, 0, 1, CMPL0		setnmap },
+	{ "ntrans",	ntranshelp,	0, 0, 1, CMPL0		setntrans },
+	{ "open",	connecthelp,	0, 0, 1, CMPL0		setpeer },
+	{ "passive",	passivehelp,	0, 0, 0, CMPL0		setpassive },
+	{ "preserve",	preservehelp,	0, 0, 0, CMPL0		setpreserve },
+	{ "progress",	progresshelp,	0, 0, 0, CMPL0		setprogress },
+	{ "prompt",	prompthelp,	0, 0, 0, CMPL0		setprompt },
+	{ "proxy",	proxyhelp,	0, 0, 1, CMPL(c)	doproxy },
+	{ "put",	sendhelp,	1, 1, 1, CMPL(lr)	put },
+	{ "pwd",	pwdhelp,	0, 1, 1, CMPL0		pwd },
+	{ "quit",	quithelp,	0, 0, 0, CMPL0		quit },
+	{ "quote",	quotehelp,	1, 1, 1, CMPL0		quote },
+	{ "recv",	receivehelp,	1, 1, 1, CMPL(rl)	get },
+	{ "reget",	regethelp,	1, 1, 1, CMPL(rl)	reget },
+	{ "rename",	renamehelp,	0, 1, 1, CMPL(rr)	renamefile },
+	{ "reset",	resethelp,	0, 1, 1, CMPL0		reset },
+	{ "restart",	restarthelp,	1, 1, 1, CMPL0		restart },
+	{ "restrict",	restricthelp,	0, 0, 0, CMPL0		setrestrict },
+	{ "rhelp",	remotehelp,	0, 1, 1, CMPL0		rmthelp },
+	{ "rmdir",	rmdirhelp,	0, 1, 1, CMPL(r)	removedir },
+	{ "rstatus",	rmtstatushelp,	0, 1, 1, CMPL(r)	rmtstatus },
+	{ "runique",	runiquehelp,	0, 0, 1, CMPL0		setrunique },
+	{ "send",	sendhelp,	1, 1, 1, CMPL(lr)	put },
+	{ "sendport",	porthelp,	0, 0, 0, CMPL0		setport },
+	{ "site",	sitehelp,	0, 1, 1, CMPL0		site },
+	{ "size",	sizecmdhelp,	1, 1, 1, CMPL(r)	sizecmd },
+	{ "status",	statushelp,	0, 0, 1, CMPL0		status },
+	{ "struct",	structhelp,	0, 1, 1, CMPL0		setstruct },
+	{ "sunique",	suniquehelp,	0, 0, 1, CMPL0		setsunique },
+	{ "system",	systemhelp,	0, 1, 1, CMPL0		syst },
+	{ "tenex",	tenexhelp,	0, 1, 1, CMPL0		settenex },
+	{ "trace",	tracehelp,	0, 0, 0, CMPL0		settrace },
+	{ "type",	typehelp,	0, 1, 1, CMPL0		settype },
+	{ "umask",	umaskhelp,	0, 1, 1, CMPL0		do_umask },
+	{ "user",	userhelp,	0, 1, 1, CMPL0		user },
+	{ "verbose",	verbosehelp,	0, 0, 0, CMPL0		setverbose },
+	{ "?",		helphelp,	0, 0, 1, CMPL(C)	help },
 	{ 0 },
 };
 
-int	NCMDS = (sizeof (cmdtab) / sizeof (cmdtab[0])) - 1;
+int	NCMDS = (sizeof(cmdtab) / sizeof(cmdtab[0])) - 1;
diff --new-file -r -b -u src/usr.bin/ftp.old/complete.c src/usr.bin/ftp/complete.c
--- src/usr.bin/ftp.old/complete.c	Thu Jan  1 10:00:00 1970
+++ src/usr.bin/ftp/complete.c	Sun Mar  2 19:56:33 1997
@@ -0,0 +1,370 @@
+/*	$OpenBSD: complete.c,v 1.3 1997/02/05 04:55:14 millert Exp $	*/
+/*	$NetBSD: complete.c,v 1.2 1997/02/01 10:44:57 lukem Exp $	*/
+
+/*-
+ * Copyright (c) 1997 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char rcsid[] = "$OpenBSD: complete.c,v 1.3 1997/02/05 04:55:14 millert Exp $";
+#endif
+static char rcsid[] = "$FreeBSD$";
+#endif /* not lint */
+
+/*
+ * FTP user program - command and file completion routines
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <err.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ftp_var.h"
+
+static int
+comparstr(a, b)
+	const void *a, *b;
+{
+	return strcmp(*(char **)a, *(char **)b);
+}
+
+/*
+ * Determine if complete is ambiguous. If unique, insert.
+ * If no choices, error. If unambiguous prefix, insert that.
+ * Otherwise, list choices. words is assumed to be filtered
+ * to only contain possible choices.
+ * Args:
+ *	word	word which started the match
+ *	list	list by default
+ *	words	stringlist containing possible matches
+ */
+static unsigned char
+complete_ambiguous(word, list, words)
+	char *word;
+	int list;
+	StringList *words;
+{
+	char insertstr[MAXPATHLEN];
+	char *lastmatch;
+	int i, j, matchlen, wordlen;
+
+	wordlen = strlen(word);
+	if (words->sl_cur == 0)
+		return CC_ERROR;	/* no choices available */
+
+	if (words->sl_cur == 1) {	/* only once choice available */
+		strncpy(insertstr, words->sl_str[0], MAXPATHLEN);
+		insertstr[MAXPATHLEN-1] = '\0';
+		if (el_insertstr(el, insertstr + wordlen) == -1)
+			return CC_ERROR;
+		else
+			return CC_REFRESH;
+	}
+
+	if (!list) {
+		matchlen = 0;
+		lastmatch = words->sl_str[0];
+		matchlen = strlen(lastmatch);
+		for (i = 1 ; i < words->sl_cur ; i++) {
+			for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
+				if (lastmatch[j] != words->sl_str[i][j])
+					break;
+			if (j < matchlen)
+				matchlen = j;
+		}
+		if (matchlen > wordlen) {
+			strncpy(insertstr, lastmatch, matchlen);
+			insertstr[matchlen] = '\0';
+			if (el_insertstr(el, insertstr + wordlen) == -1)
+				return CC_ERROR;
+			else	
+					/*
+					 * XXX: really want CC_REFRESH_BEEP
+					 */
+				return CC_REFRESH;
+		}
+	}
+
+	putchar('\n');
+	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
+	list_vertical(words);
+	return CC_REDISPLAY;
+}
+
+/*
+ * Complete a command
+ */
+static unsigned char
+complete_command(word, list)
+	char *word;
+	int list;
+{
+	struct cmd *c;
+	StringList *words;
+	int wordlen;
+	unsigned char rv;
+
+	words = sl_init();
+	wordlen = strlen(word);
+
+	for (c = cmdtab; c->c_name != NULL; c++) {
+		if (wordlen > strlen(c->c_name))
+			continue;
+		if (strncmp(word, c->c_name, wordlen) == 0)
+			sl_add(words, c->c_name);
+	}
+
+	rv = complete_ambiguous(word, list, words);
+	sl_free(words, 0);
+	return rv;
+}
+
+/*
+ * Complete a local file
+ */
+static unsigned char
+complete_local(word, list)
+	char *word;
+	int list;
+{
+	StringList *words;
+	char dir[MAXPATHLEN];
+	char *file;
+	DIR *dd;
+	struct dirent *dp;
+	unsigned char rv;
+
+	if ((file = strrchr(word, '/')) == NULL) {
+		strcpy(dir, ".");
+		file = word;
+	} else {
+		if (file == word)
+			strcpy(dir, "/");
+		else {
+			strncpy(dir, word, file - word);
+			dir[file - word] = '\0';
+		}
+		++file;
+	}
+
+	if ((dd = opendir(dir)) == NULL)
+		return CC_ERROR;
+
+	words = sl_init();
+
+	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
+		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
+			continue;
+		if (strlen(file) > dp->d_namlen)
+			continue;
+		if (strncmp(file, dp->d_name, strlen(file)) == 0) {
+			char *tcp;
+
+			tcp = strdup(dp->d_name);
+			if (tcp == NULL)
+				errx(1, "Can't allocate memory for local dir");
+			sl_add(words, tcp);
+		}
+	}
+	closedir(dd);
+
+	rv = complete_ambiguous(file, list, words);
+	sl_free(words, 1);
+	return rv;
+}
+
+/*
+ * Complete a remote file
+ */
+static unsigned char
+complete_remote(word, list)
+	char *word;
+	int list;
+{
+	static StringList *dirlist;
+	static char	 lastdir[MAXPATHLEN];
+	static int	 ftpdslashbug;
+	StringList	*words;
+	char		 dir[MAXPATHLEN];
+	char		*file, *cp;
+	int		 i, offset;
+	unsigned char	 rv;
+
+	char *dummyargv[] = { "complete", dir, NULL };
+
+	offset = 0;
+	if ((file = strrchr(word, '/')) == NULL) {
+		strcpy(dir, ".");
+		file = word;
+	} else {
+		if (file == word)
+			strcpy(dir, "/");
+		else {
+			offset = file - word;
+			strncpy(dir, word, offset);
+			dir[offset] = '\0';
+			offset++;
+		}
+		file++;
+	}
+
+	if (dirchange || strcmp(dir, lastdir) != 0) {	/* dir not cached */
+		if (dirlist != NULL)
+			sl_free(dirlist, 1);
+		dirlist = sl_init();
+
+		ftpdslashbug = 0;
+		mflag = 1;
+		while ((cp = remglob(dummyargv, 0)) != NULL) {
+			char *tcp;
+
+			if (!mflag)
+				continue;
+			if (*cp == '\0') {
+				mflag = 0;
+				continue;
+			}
+			/*
+			 * Work around ftpd(1) bug, which puts a // instead
+			 * of / in front of each filename returned by "NLST /".
+			 * Without this, remote completes of / look ugly.
+			 */
+			if (dir[0] == '/' && dir[1] == '\0' &&
+			    cp[0] == '/' && cp[1] == '/') {
+				cp++;
+				ftpdslashbug = 1;
+			}
+			tcp = strdup(cp);
+			if (tcp == NULL)
+				errx(1, "Can't allocate memory for remote dir");
+			sl_add(dirlist, tcp);
+		}
+		strcpy(lastdir, dir);
+		dirchange = 0;
+	}
+
+	words = sl_init();
+	for (i = 0; i < dirlist->sl_cur; i++) {
+		cp = dirlist->sl_str[i];
+		if (strlen(word) > strlen(cp))
+			continue;
+		if (strncmp(word, cp, strlen(word)) == 0)
+			sl_add(words, cp + offset + ftpdslashbug);
+	}
+	rv = complete_ambiguous(file, list, words);
+	sl_free(words, 0);
+	return rv;
+}
+
+/*
+ * Generic complete routine
+ */
+unsigned char
+complete(el, ch)
+	EditLine *el;
+	int ch;
+{
+	static char word[FTPBUFLEN];
+	static int lastc_argc, lastc_argo;
+
+	struct cmd *c;
+	const LineInfo *lf;
+	int len, celems, dolist;
+
+	lf = el_line(el);
+	len = lf->lastchar - lf->buffer;
+	if (len >= sizeof(line))
+		return CC_ERROR;
+	strncpy(line, lf->buffer, len);
+	line[len] = '\0';
+	cursor_pos = line + (lf->cursor - lf->buffer);
+	lastc_argc = cursor_argc;	/* remember last cursor pos */
+	lastc_argo = cursor_argo;
+	makeargv();			/* build argc/argv of current line */
+
+	if (cursor_argo >= sizeof(word))
+		return CC_ERROR;
+
+	dolist = 0;
+			/* if cursor and word is same, list alternatives */
+	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
+	    && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
+		dolist = 1;
+	else
+	    strncpy(word, margv[cursor_argc], cursor_argo);
+	word[cursor_argo] = '\0';
+
+	if (cursor_argc == 0)
+		return complete_command(word, dolist);
+
+	c = getcmd(margv[0]);
+	if (c == (struct cmd *)-1 || c == 0)
+		return CC_ERROR;
+	celems = strlen(c->c_complete);
+
+		/* check for 'continuation' completes (which are uppercase) */
+	if ((cursor_argc > celems) && (celems > 0)
+	    && isupper(c->c_complete[celems-1]))
+		cursor_argc = celems;
+
+	if (cursor_argc > celems)
+		return CC_ERROR;
+
+	switch (c->c_complete[cursor_argc - 1]) {
+		case 'l':			/* local complete */
+		case 'L':
+			return complete_local(word, dolist);
+		case 'r':			/* remote complete */
+		case 'R':
+			if (!connected) {
+				puts("\nMust be connected to complete");
+				return CC_REDISPLAY;
+			}
+			return complete_remote(word, dolist);
+		case 'c':			/* command complete */
+		case 'C':
+			return complete_command(word, dolist);
+		case 'n':			/* no complete */
+		default:
+			return CC_ERROR;
+	}
+
+	return CC_ERROR;
+}
diff --new-file -r -b -u src/usr.bin/ftp.old/domacro.c src/usr.bin/ftp/domacro.c
--- src/usr.bin/ftp.old/domacro.c	Fri May 27 22:31:27 1994
+++ src/usr.bin/ftp/domacro.c	Sun Mar  2 19:56:56 1997
@@ -1,3 +1,6 @@
+/*	$OpenBSD: domacro.c,v 1.4 1997/02/03 01:05:36 millert Exp $	*/
+/*	$NetBSD: domacro.c,v 1.8 1997/01/19 14:19:08 lukem Exp $	*/
+
 /*
  * Copyright (c) 1985, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -32,13 +35,18 @@
  */
 
 #ifndef lint
+#if 0
 static char sccsid[] = "@(#)domacro.c	8.3 (Berkeley) 4/2/94";
+static char rcsid[] = "$OpenBSD: domacro.c,v 1.4 1997/02/03 01:05:36 millert Exp $";
+#else
+static char rcsid[] = "$FreeBSD$";
+#endif
 #endif /* not lint */
 
 #include <ctype.h>
 #include <signal.h>
 #include <stdio.h>
-#include <strings.h>
+#include <string.h>
 
 #include "ftp_var.h"
 
@@ -66,7 +74,7 @@
 		code = -1;
 		return;
 	}
-	(void) strcpy(line2, line);
+	(void)strcpy(line2, line);
 TOP:
 	cp1 = macros[i].mac_start;
 	while (cp1 != macros[i].mac_end) {
@@ -87,7 +95,7 @@
 				    }
 				    cp1--;
 				    if (argc - 2 >= j) {
-					(void) strcpy(cp2, argv[j+1]);
+					(void)strcpy(cp2, argv[j+1]);
 					cp2 += strlen(argv[j+1]);
 				    }
 				    break;
@@ -96,7 +104,7 @@
 					loopflg = 1;
 					cp1++;
 					if (count < argc) {
-					   (void) strcpy(cp2, argv[count]);
+					   (void)strcpy(cp2, argv[count]);
 					   cp2 += strlen(argv[count]);
 					}
 					break;
@@ -114,26 +122,26 @@
 		makeargv();
 		c = getcmd(margv[0]);
 		if (c == (struct cmd *)-1) {
-			printf("?Ambiguous command\n");
+			puts("?Ambiguous command");
 			code = -1;
 		}
 		else if (c == 0) {
-			printf("?Invalid command\n");
+			puts("?Invalid command");
 			code = -1;
 		}
 		else if (c->c_conn && !connected) {
-			printf("Not connected.\n");
+			puts("Not connected.");
 			code = -1;
 		}
 		else {
 			if (verbose) {
-				printf("%s\n",line);
+				puts(line);
 			}
 			(*c->c_handler)(margc, margv);
 			if (bell && c->c_bell) {
-				(void) putchar('\007');
+				(void)putchar('\007');
 			}
-			(void) strcpy(line, line2);
+			(void)strcpy(line, line2);
 			makeargv();
 			argc = margc;
 			argv = margv;
diff --new-file -r -b -u src/usr.bin/ftp.old/extern.h src/usr.bin/ftp/extern.h
--- src/usr.bin/ftp.old/extern.h	Sun Aug  6 07:07:10 1995
+++ src/usr.bin/ftp/extern.h	Sun Mar  2 19:59:05 1997
@@ -1,3 +1,7 @@
+/*	$NetBSD: extern.h,v 1.11 1997/02/01 10:44:58 lukem Exp $	*/
+/*	$OpenBSD: extern.h,v 1.8 1997/02/05 04:55:15 millert Exp $	*/
+/*	$FreeBSD$	*/
+
 /*-
  * Copyright (c) 1994 The Regents of the University of California.
  * All rights reserved.
@@ -30,27 +34,32 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	@(#)extern.h	8.2 (Berkeley) 4/3/94
+ *	@(#)extern.h	8.3 (Berkeley) 10/9/94
  */
 
-struct timeval;
 struct fd_set;
 
 void    abort_remote __P((FILE *));
-void    abortpt __P(());
-void    abortrecv __P(());
-void    abortsend __P(());
+void    abortpt __P((int));
+void    abortrecv __P((int));
+void    abortsend __P((int));
+void    aborthttp __P((int));
 void	account __P((int, char **));
-int	another __P((int *, char ***, char *));
+void	alarmtimer __P((int));
+int	another __P((int *, char ***, const char *));
+int	auto_fetch __P((int, char **));
 void	blkfree __P((char **));
 void	cd __P((int, char **));
 void	cdup __P((int, char **));
 void	changetype __P((int, int));
-void	cmdabort __P(());
+void	cmdabort __P((int));
 void	cmdscanner __P((int));
 int	command __P(());
-int	confirm __P((char *, char *));
-FILE   *dataconn __P((char *));
+#ifndef SMALLFTP
+unsigned char complete __P((EditLine *, int));
+#endif /* !SMALLFTP */
+int	confirm __P((const char *, const char *));
+FILE   *dataconn __P((const char *));
 void	delete __P((int, char **));
 void	disconnect __P((int, char **));
 void	do_chmod __P((int, char **));
@@ -60,25 +69,26 @@
 void	doproxy __P((int, char **));
 char   *dotrans __P((char *));
 int     empty __P((struct fd_set *, int));
-void	fatal __P((char *));
 void	get __P((int, char **));
-struct cmd *getcmd __P((char *));
-int	getit __P((int, char **, int, char *));
+struct cmd *getcmd __P((const char *));
+int	getit __P((int, char **, int, const char *));
 int	getreply __P((int));
 int	globulize __P((char **));
-char   *gunique __P((char *));
+char   *gunique __P((const char *));
 void	help __P((int, char **));
-char   *hookup __P((char *, int));
+char   *hookup __P((const char *, int));
 void	idle __P((int, char **));
 int     initconn __P((void));
-void	intr __P(());
+void	intr __P((void));
+void	list_vertical __P((StringList *));
 void	lcd __P((int, char **));
-int	login __P((char *));
-void	lostpeer __P(());
+int	login __P((const char *));
+void	lostpeer __P((void));
+void	lpwd __P((int, char **));
 void	ls __P((int, char **));
 void	mabort __P((int));
 void	macdef __P((int, char **));
-void	makeargv __P((void));
+void	makeargv __P(());
 void	makedir __P((int, char **));
 void	mdelete __P((int, char **));
 void	mget __P((int, char **));
@@ -87,60 +97,72 @@
 void	mput __P((int, char **));
 char   *onoff __P((int));
 void	newer __P((int, char **));
-void	proxabort __P(());
-void    proxtrans __P((char *, char *, char *));
-void    psabort __P(());
+void    progressmeter __P((int));
+char   *prompt __P((void));
+void	proxabort __P((int));
+void    proxtrans __P((const char *, const char *, const char *));
+void    psabort __P((int));
+void	psummary __P((int));
 void    pswitch __P((int));
-void    ptransfer __P((char *, long, struct timeval *, struct timeval *));
+void    ptransfer __P((int));
 void	put __P((int, char **));
 void	pwd __P((int, char **));
 void	quit __P((int, char **));
 void	quote __P((int, char **));
-void	quote1 __P((char *, int, char **));
-void    recvrequest __P((char *, char *, char *, char *, int));
+void	quote1 __P((const char *, int, char **));
+void    recvrequest __P((const char *, const char *, const char *,
+	    const char *, int));
 void	reget __P((int, char **));
 char   *remglob __P((char **, int));
+off_t	remotesize __P((const char *, int));
+time_t	remotemodtime __P((const char *, int));
 void	removedir __P((int, char **));
 void	renamefile __P((int, char **));
 void    reset __P((int, char **));
 void	restart __P((int, char **));
 void	rmthelp __P((int, char **));
 void	rmtstatus __P((int, char **));
-int	ruserpass __P((char *, char **, char **, char **));
-void    sendrequest __P((char *, char *, char *, int));
+int	ruserpass __P((const char *, char **, char **, char **));
+void    sendrequest __P((const char *, const char *, const char *, int));
 void	setascii __P((int, char **));
 void	setbell __P((int, char **));
 void	setbinary __P((int, char **));
 void	setcase __P((int, char **));
 void	setcr __P((int, char **));
 void	setdebug __P((int, char **));
+void	setedit __P((int, char **));
 void	setform __P((int, char **));
 void	setftmode __P((int, char **));
 void	setglob __P((int, char **));
 void	sethash __P((int, char **));
 void	setnmap __P((int, char **));
 void	setntrans __P((int, char **));
-void	setpassive __P(());
+void	setpassive __P((int, char **));
 void	setpeer __P((int, char **));
 void	setport __P((int, char **));
+void	setpreserve __P((int, char **));
+void	setprogress __P((int, char **));
 void	setprompt __P((int, char **));
-void	setrestrict __P(());
+void	setrestrict __P((int, char **));
 void	setrunique __P((int, char **));
 void	setstruct __P((int, char **));
 void	setsunique __P((int, char **));
 void	settenex __P((int, char **));
 void	settrace __P((int, char **));
+void	setttywidth __P((int));
 void	settype __P((int, char **));
 void	setverbose __P((int, char **));
 void	shell __P((int, char **));
 void	site __P((int, char **));
 void	sizecmd __P((int, char **));
-char   *slurpstring __P((void));
+char   *slurpstring __P(());
 void	status __P((int, char **));
 void	syst __P((int, char **));
-void    tvsub __P((struct timeval *, struct timeval *, struct timeval *));
+int	togglevar __P((int, char **, int *, const char *));
+void	usage __P((void));
 void	user __P((int, char **));
 
+
 extern jmp_buf	abortprox;
 extern int	abrtflag;
 extern struct	cmd cmdtab[];
@@ -152,3 +174,6 @@
 extern char	reply_string[];
 extern off_t	restart_point;
 extern int	NCMDS;
+
+extern char *__progname;		/* from crt0.o */
+
diff --new-file -r -b -u src/usr.bin/ftp.old/fetch.c src/usr.bin/ftp/fetch.c
--- src/usr.bin/ftp.old/fetch.c	Thu Jan  1 10:00:00 1970
+++ src/usr.bin/ftp/fetch.c	Sun Mar  2 19:57:12 1997
@@ -0,0 +1,514 @@
+/*	$OpenBSD: fetch.c,v 1.3 1997/02/05 04:55:16 millert Exp $	*/
+/*	$NetBSD: fetch.c,v 1.2 1997/02/01 10:45:00 lukem Exp $	*/
+
+/*-
+ * Copyright (c) 1997 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason Thorpe and Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char rcsid[] = "$OpenBSD: fetch.c,v 1.3 1997/02/05 04:55:16 millert Exp $";
+#endif
+static char rcsid[] = "$FreeBSD$";
+#endif /* not lint */
+
+/*
+ * FTP User Program -- Command line file retrieval
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+
+#include <arpa/ftp.h>
+#include <arpa/inet.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "ftp_var.h"
+
+#define	FTP_URL		"ftp://"	/* ftp URL prefix */
+#define	HTTP_URL	"http://"	/* http URL prefix */
+#define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
+
+
+#define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
+
+jmp_buf	httpabort;
+
+/*
+ * Retrieve an http:// URL, via a proxy if necessary.
+ * Modifies the string argument given.
+ * Returns -1 on failure, 0 on success
+ */
+int
+http_get(line)
+	char *line;
+{
+	struct sockaddr_in sin;
+	int i, out, port, s;
+	size_t buflen, len;
+	char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
+	char *proxyenv, *proxy, *host;
+	sig_t oldintr;
+	off_t hashbytes;
+
+	s = -1;
+	proxy = NULL;
+
+	host = line + sizeof(HTTP_URL) - 1;
+	path = strchr(host, '/');		/* find path */
+	if (EMPTYSTRING(path))
+		goto cleanup_http_get;
+	*path++ = '\0';
+	if (EMPTYSTRING(path))
+		goto cleanup_http_get;
+
+	savefile = strrchr(path, '/');			/* find savefile */
+	if (savefile != NULL)
+		savefile++;
+	else
+		savefile = path;
+	if (EMPTYSTRING(savefile))
+		goto cleanup_http_get;
+
+	proxyenv = getenv(HTTP_PROXY);
+	if (proxyenv != NULL) {				/* use proxy */
+		if (strncmp(proxyenv, HTTP_URL, sizeof(HTTP_URL) - 1) != 0) {
+			warnx("Malformed proxy url: %s", proxyenv);
+			goto cleanup_http_get;
+		}
+		proxy = strdup(proxyenv);
+		if (proxy == NULL)
+			errx(1, "Can't allocate memory for proxy url.");
+		host = proxy + sizeof(HTTP_URL) - 1;
+		if (EMPTYSTRING(host))
+			goto cleanup_http_get;
+		*--path = '/';			/* add / back to real path */
+		path = strchr(host, '/');	/* remove trailing / on host */
+		if (! EMPTYSTRING(path))
+			*path++ = '\0';
+		path = line;
+	}
+
+	portnum = strchr(host, ':');			/* find portnum */
+	if (portnum != NULL)
+		*portnum++ = '\0';
+
+	if (debug)
+		printf("host %s, port %s, path %s, save as %s.\n",
+		    host, portnum, path, savefile);
+
+	memset(&sin, 0, sizeof(sin));
+	sin.sin_family = AF_INET;
+
+	if (isdigit(host[0])) {
+		if (inet_aton(host, &sin.sin_addr) == 0) {
+			warnx("invalid IP address: %s", host);
+			goto cleanup_http_get;
+		}
+	} else {
+		struct hostent *hp;
+
+		hp = gethostbyname(host);
+		if (hp == NULL) {
+			warnx("%s: %s", host, hstrerror(h_errno));
+			goto cleanup_http_get;
+		}
+		if (hp->h_addrtype != AF_INET) {
+			warnx("%s: not an Internet address?", host);
+			goto cleanup_http_get;
+		}
+		memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
+	}
+
+	if (! EMPTYSTRING(portnum)) {
+		port = atoi(portnum);
+		if (port < 1 || (port & 0xffff) != port) {
+			warnx("invalid port: %s", portnum);
+			goto cleanup_http_get;
+		}
+		port = htons(port);
+	} else
+		port = httpport;
+	sin.sin_port = port;
+
+	s = socket(AF_INET, SOCK_STREAM, 0);
+	if (s == -1) {
+		warnx("Can't create socket");
+		goto cleanup_http_get;
+	}
+
+	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
+		warn("Can't connect to %s", host);
+		goto cleanup_http_get;
+	}
+
+	/*
+	 * Construct and send the request.  We're expecting a return
+	 * status of "200". Proxy requests don't want leading /.
+	 */
+	if (!proxy)
+		printf("Requesting %s:%d/%s\n", line, ntohs(port), path);
+	else
+		printf("Requesting %s (via %s)\n", line, proxyenv);
+	snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
+	    proxy ? "" : "/", path);
+	buflen = strlen(buf);
+	if (write(s, buf, buflen) < buflen) {
+		warn("write");
+		goto cleanup_http_get;
+	}
+	memset(buf, 0, sizeof(buf));
+	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
+		if (read(s, cp, 1) != 1)
+			goto improper;
+		if (*cp == '\n')
+			break;
+	}
+	buf[buflen - 1] = '\0';		/* sanity */
+	cp = strchr(buf, ' ');
+	if (cp == NULL)
+		goto improper;
+	else
+		cp++;
+	if (strncmp(cp, "200", 3)) {
+		warnx("Error retrieving file: %s", cp);
+		goto cleanup_http_get;
+	}
+
+	/*
+	 * Read the rest of the header.
+	 */
+	memset(buf, 0, sizeof(buf));
+	c = '\0';
+	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
+		if (read(s, cp, 1) != 1)
+			goto improper;
+		if (*cp == '\n' && c == '\n')
+			break;
+		c = *cp;
+	}
+	buf[buflen - 1] = '\0';		/* sanity */
+
+	/*
+	 * Look for the "Content-length: " header.
+	 */
+#define CONTENTLEN "Content-Length: "
+	for (cp = buf; *cp != '\0'; cp++) {
+		if (tolower(*cp) == 'c' &&
+		    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
+			break;
+	}
+	if (*cp == '\0')
+		goto improper;
+	cp += sizeof(CONTENTLEN) - 1;
+	cp2 = strchr(cp, '\n');
+	if (cp2 == NULL)
+		goto improper;
+	else
+		*cp2 = '\0';
+	filesize = atoi(cp);
+	if (filesize < 1)
+		goto improper;
+
+	/* Open the output file. */
+	out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
+	if (out < 0) {
+		warn("Can't open %s", savefile);
+		goto cleanup_http_get;
+	}
+
+	/* Trap signals */
+	oldintr = NULL;
+	if (setjmp(httpabort)) {
+		if (oldintr)
+			(void)signal(SIGINT, oldintr);
+		goto cleanup_http_get;
+	}
+	oldintr = signal(SIGINT, aborthttp);
+
+	bytes = 0;
+	hashbytes = mark;
+	progressmeter(-1);
+
+	/* Finally, suck down the file. */
+	i = 0;
+	while ((len = read(s, buf, sizeof(buf))) > 0) {
+		bytes += len;
+		for (cp = buf; len > 0; len -= i, cp += i) {
+			if ((i = write(out, cp, len)) == -1) {
+				warn("Writing %s", savefile);
+				goto cleanup_http_get;
+			}
+			else if (i == 0)
+				break;
+		}
+		if (hash && !progress) {
+			while (bytes >= hashbytes) {
+				(void)putchar('#');
+				hashbytes += mark;
+			}
+			(void)fflush(stdout);
+		}
+	}
+	if (hash && !progress && bytes > 0) {
+		if (bytes < mark)
+			(void)putchar('#');
+		(void)putchar('\n');
+		(void)fflush(stdout);
+	}
+	if (len != 0) {
+		warn("Reading from socket");
+		goto cleanup_http_get;
+	}
+	progressmeter(1);
+	if (verbose)
+		puts("Successfully retrieved file.");
+	(void)signal(SIGINT, oldintr);
+
+	close(s);
+	close(out);
+	if (proxy)
+		free(proxy);
+	return (0);
+
+improper:
+	warnx("improper response from %s", host);
+cleanup_http_get:
+	if (s != -1)
+		close(s);
+	if (proxy)
+		free(proxy);
+	return (-1);
+}
+
+/*
+ * Abort a http retrieval
+ */
+void
+aborthttp(notused)
+	int notused;
+{
+
+	alarmtimer(0);
+	puts("\nhttp fetch aborted");
+	(void)fflush(stdout);
+	longjmp(httpabort, 1);
+}
+
+/*
+ * Retrieve multiple files from the command line, transferring
+ * files of the form "host:path", "ftp://host/path" using the
+ * ftp protocol, and files of the form "http://host/path" using
+ * the http protocol.
+ * If path has a trailing "/", then return (-1);
+ * the path will be cd-ed into and the connection remains open,
+ * and the function will return -1 (to indicate the connection
+ * is alive).
+ * If an error occurs the return value will be the offset+1 in
+ * argv[] of the file that caused a problem (i.e, argv[x]
+ * returns x+1)
+ * Otherwise, 0 is returned if all files retrieved successfully.
+ */
+int
+auto_fetch(argc, argv)
+	int argc;
+	char *argv[];
+{
+	static char lasthost[MAXHOSTNAMELEN];
+	char *xargv[5];
+	char *cp, *line, *host, *dir, *file, *portnum;
+	int rval, xargc, argpos;
+
+	argpos = 0;
+
+	if (setjmp(toplevel)) {
+		if (connected)
+			disconnect(0, NULL);
+		return (argpos + 1);
+	}
+	(void)signal(SIGINT, (sig_t)intr);
+	(void)signal(SIGPIPE, (sig_t)lostpeer);
+
+	/*
+	 * Loop through as long as there's files to fetch.
+	 */
+	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
+		if (strchr(argv[argpos], ':') == NULL)
+			break;
+		host = dir = file = portnum = NULL;
+
+		/*
+		 * We muck with the string, so we make a copy.
+		 */
+		line = strdup(argv[argpos]);
+		if (line == NULL)
+			errx(1, "Can't allocate memory for auto-fetch.");
+
+		/*
+		 * Try HTTP URL-style arguments first.
+		 */
+		if (strncmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
+			if (http_get(line) == -1)
+				rval = argpos + 1;
+			continue;
+		}
+
+		/*
+		 * Try FTP URL-style arguments next, then host:file.
+		 */
+		host = line;
+		if (strncmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
+			host += sizeof(FTP_URL) - 1;
+			cp = strchr(host, '/');
+
+			/* Look for a port number after the host name. */
+			portnum = strchr(host, ':');
+			if (portnum != NULL)
+				*portnum++ = '\0';
+		} else				/* classic style `host:file' */
+			cp = strchr(host, ':');
+		if (EMPTYSTRING(host)) {
+			rval = argpos + 1;
+			continue;
+		}
+
+		/*
+		 * If cp is NULL, the file wasn't specified
+		 * (URL looked something like ftp://host)
+		 */
+		if (cp != NULL)
+			*cp++ = '\0';
+
+		/*
+		 * Extract the file and (if present) directory name.
+		 */
+		dir = cp;
+		if (! EMPTYSTRING(dir)) {
+			cp = strrchr(cp, '/');
+			if (cp != NULL) {
+				*cp++ = '\0';
+				file = cp;
+			} else {
+				file = dir;
+				dir = NULL;
+			}
+		}
+		if (debug)
+			printf("host '%s', dir '%s', file '%s'\n",
+			    host, dir, file);
+
+		/*
+		 * Set up the connection if we don't have one.
+		 */
+		if (strcmp(host, lasthost) != 0) {
+			strcpy(lasthost, host);
+			if (connected)
+				disconnect(0, NULL);
+			xargv[0] = __progname;
+			xargv[1] = host;
+			xargv[2] = NULL;
+			xargc = 2;
+			if (portnum != NULL) {
+				xargv[2] = portnum;
+				xargv[3] = NULL;
+				xargc = 3;
+			}
+			setpeer(xargc, xargv);
+			if (connected == 0) {
+				warnx("Can't connect to host `%s'", host);
+				rval = argpos + 1;
+				continue;
+			}
+
+			/* Always use binary transfers. */
+			setbinary(0, NULL);
+		}
+		else	/* already have connection, cd back to '/' */
+		{
+			xargv[0] = "cd";
+			xargv[1] = "/";
+			xargv[2] = NULL;
+			cd(2, xargv);
+			if (! dirchange) {
+				rval = argpos + 1;
+				continue;
+			}
+		}
+
+		/* Change directories, if necessary. */
+		if (! EMPTYSTRING(dir)) {
+			xargv[0] = "cd";
+			xargv[1] = dir;
+			xargv[2] = NULL;
+			cd(2, xargv);
+			if (! dirchange) {
+				rval = argpos + 1;
+				continue;
+			}
+		}
+
+		if (EMPTYSTRING(file)) {
+			rval = -1;
+			continue;
+		}
+
+		if (!verbose)
+			printf("Retrieving %s/%s\n", dir ? dir : "", file);
+
+		/* Fetch the file. */
+		xargv[0] = "get";
+		xargv[1] = file;
+		xargv[2] = NULL;
+		get(2, xargv);
+
+		if ((code / 100) != COMPLETE)	/* XXX: is this valid? */
+			rval = argpos + 1;
+	}
+	if (connected && rval != -1)
+		disconnect(0, NULL);
+	return (rval);
+}
diff --new-file -r -b -u src/usr.bin/ftp.old/ftp.1 src/usr.bin/ftp/ftp.1
--- src/usr.bin/ftp.old/ftp.1	Wed Jan 31 04:34:58 1996
+++ src/usr.bin/ftp/ftp.1	Sun Mar  2 15:49:47 1997
@@ -1,3 +1,6 @@
+.\" 	$OpenBSD: ftp.1,v 1.8 1997/02/18 18:04:28 kstailey Exp $
+.\" 	$NetBSD: ftp.1,v 1.17 1997/02/01 10:45:01 lukem Exp $
+.\"
 .\" Copyright (c) 1985, 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
 .\"
@@ -29,9 +32,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\"	@(#)ftp.1	8.2 (Berkeley) 12/30/93
+.\"	@(#)ftp.1	8.3 (Berkeley) 10/9/94
 .\"
-.Dd December 30, 1993
+.Dd January 20, 1997
 .Dt FTP 1
 .Os BSD 4.2
 .Sh NAME
@@ -40,15 +43,27 @@
 .Tn ARPANET
 file transfer program
 .Sh SYNOPSIS
-.Nm ftp
-.Op Fl v
+.Nm
+.Op Fl a
 .Op Fl d
+.Op Fl e
+.Op Fl g
 .Op Fl i
 .Op Fl n
-.Op Fl U
 .Op Fl p
-.Op Fl g
-.Op Ar host
+.Op Fl P Ar port
+.Op Fl r Ar seconds
+.Op Fl t
+.Op Fl v
+.Op Fl U
+.Op Fl V
+.Op Ar host Op Ar port
+.Nm ftp
+ftp://\fIhost\fR[:\fIport\fR]/\fIfile\fR[/]
+.Nm ftp
+http://\fIhost\fR[:\fIport\fR]/\fIfile\fR
+.Nm ftp
+\fIhost\fR:\fI/path/file\fR[/]
 .Sh DESCRIPTION
 .Nm Ftp
 is the user interface to the
@@ -57,55 +72,81 @@
 The program allows a user to transfer files to and from a
 remote network site.
 .Pp
+The latter three usage formats will fetch a file using either the
+HTTP or FTP protocols into the current directory.
+This is ideal for scripts.
+.Pp
 Options may be specified at the command line, or to the
 command interpreter.
-.Bl -tag -width flag
-.It Fl v
-Verbose option forces
-.Nm ftp
-to show all responses from the remote server, as well
-as report on data transfer statistics.
+.Bl -tag -width "port    "
+.It Fl a
+Causes
+.Nm
+to bypass normal login procedure, and use an anonymous login instead.
+.It Fl d
+Enables debugging.
+.It Fl e
+Suppress the use of editline(3).  Useful for Emacs ange-ftp.
+.It Fl g
+Disables file name globbing.
+.It Fl i
+Turns off interactive prompting during
+multiple file transfers.
 .It Fl n
 Restrains
-.Nm ftp
-from attempting \*(Lqauto-login\*(Rq upon initial connection.
+.Nm
+from attempting
+.Dq auto-login
+upon initial connection.
 If auto-login is enabled,
-.Nm ftp
+.Nm
 will check the
 .Pa .netrc
 (see below) file in the user's home directory for an entry describing
 an account on the remote machine.
 If no entry exists,
-.Nm ftp
+.Nm
 will prompt for the remote machine login name (default is the user
 identity on the local machine), and, if necessary, prompt for a password
 and an account with which to login.
-.It Fl i
-Turns off interactive prompting during
-multiple file transfers.
-.It Fl d
-Enables debugging.
-.It Fl g
-Disables file name globbing.
-.It Fl U
-Disable data port range restrictions.
 .It Fl p
 Enable passive mode operation for use behind connection filtering firewalls.
+.It Fl P Ar port
+Sets the port number to
+.Ar port .
+.It Fl r Ar number
+Retry to connect if failed, pausing for
+.Ar number
+of seconds.
+.It Fl t
+Enables packet tracing.
+.lt Fl U
+Disable port data range restrictions.
+.It Fl v
+Enable verbose mode.
+This is the default if input is from a terminal.
+Forces
+.Nm
+to show all responses from the remote server, as well
+as report on data transfer statistics.
+.It Fl V
+Disable verbose mode, overriding the default of enabled when input
+is from a terminal.
 .El
 .Pp
 The client host with which
-.Nm ftp
+.Nm
 is to communicate may be specified on the command line.
 If this is done,
-.Nm ftp
+.Nm
 will immediately attempt to establish a connection to an
 .Tn FTP
 server on that host; otherwise,
-.Nm ftp
+.Nm
 will enter its command interpreter and await instructions
 from the user.
 When
-.Nm ftp
+.Nm
 is awaiting commands from the user the prompt
 .Ql ftp>
 is provided to the user.
@@ -222,37 +263,49 @@
 .Ar debug-value
 is specified it is used to set the debugging level.
 When debugging is on,
-.Nm ftp
-prints each command sent to the remote machine, preceded
-by the string
+.Nm
+prints each command sent to the remote machine,
+preceded by the string
 .Ql \-\->
-.It Xo
-.Ic dir
-.Op Ar remote-directory
-.Op Ar local-file
-.Xc
-Print a listing of the directory contents in the
-directory,
-.Ar remote-directory  ,
-and, optionally, placing the output in
-.Ar local-file  .
+.It Ic dir Op Ar remote-directory Op Ar local-file
+Print a listing of the contents of a
+directory on the remote machine.
+The listing includes any system-dependent information that the server
+chooses to include; for example, most
+.Ux
+systems will produce
+output from the command
+.Ql ls \-l .
+(See also
+.Ic ls . )
+If
+.Ar remote-directory
+is left unspecified, the current working directory is used.
 If interactive prompting is on,
-.Nm ftp
+.Nm
 will prompt the user to verify that the last argument is indeed the
 target local file for receiving
 .Ic dir
 output.
-If no directory is specified, the current working
-directory on the remote machine is used.
-If no local
-file is specified, or
+If no local file is specified, or if
 .Ar local-file
 is
-.Fl  ,
-output comes to the terminal.
+.Sq Fl ,
+the output is sent to the terminal.
 .It Ic disconnect
 A synonym for
-.Ar close  .
+.Ic close .
+.It Ic edit
+Toggle command line editing, and context sensitive command and file
+completion.
+This is automatically enabled if input is from a terminal, and
+disabled otherwise.
+.It Ic exit
+A synonym for
+.Ic bye .
+.It Ic ftp Ar host Op Ar port
+A synonym for
+.Ic open .
 .It Ic form Ar format
 Set the file transfer
 .Ic form
@@ -314,15 +367,18 @@
 transferring a
 .Xr tar 1
 archive of the subtree (in binary mode).
-.It Ic hash
+.It Ic hash Op Ar size
 Toggle hash-sign (``#'') printing for each data block
 transferred.
-The size of a data block is 1024 bytes.
+The size of a data block defaults to 1024 bytes.
+This can be changed by specifying
+.Ar size
+in bytes.
 .It Ic help Op Ar command
 Print an informative message about the meaning of
 .Ar command  .
 If no argument is given,
-.Nm ftp
+.Nm
 prints a list of the known commands.
 .It Ic idle Op Ar seconds
 Set the inactivity timer on the remote server to
@@ -337,36 +393,26 @@
 no
 .Ar directory
 is specified, the user's home directory is used.
-.It Xo
-.Ic \&ls
-.Op Ar remote-directory
-.Op Ar local-file
-.Xc
-Print a listing of the contents of a
+.It Ic lpwd
+Print the working directory on the local machine.
+.It Ic \&ls Op Ar remote-directory Op Ar local-file
+Print a list of the files in a
 directory on the remote machine.
-The listing includes any system-dependent information that the server
-chooses to include; for example, most
-.Ux
-systems will produce
-output from the command
-.Ql ls \-l .
-(See also
-.Ic nlist . )
 If
 .Ar remote-directory
 is left unspecified, the current working directory is used.
 If interactive prompting is on,
-.Nm ftp
+.Nm
 will prompt the user to verify that the last argument is indeed the
 target local file for receiving
-.Ic \&ls
+.Ic ls
 output.
 If no local file is specified, or if
 .Ar local-file
 is
-.Sq Fl ,
+.Fl  ,
 the output is sent to the terminal.
-.It Ic macdefNs Ar macro-name
+.It Ic macdef Ar macro-name
 Define a macro.
 Subsequent lines are stored as the macro
 .Ar macro-name  ;
@@ -397,7 +443,7 @@
 .Ic dir  ,
 except multiple remote files may be specified.
 If interactive prompting is on,
-.Nm ftp
+.Nm
 will prompt the user to verify that the last argument is indeed the
 target local file for receiving
 .Ic mdir
@@ -427,13 +473,13 @@
 Make a directory on the remote machine.
 .It Ic mls Ar remote-files local-file
 Like
-.Ic nlist  ,
+.Ic ls  ,
 except multiple remote files may be specified,
 and the
 .Ar local-file
 must be specified.
 If interactive prompting is on,
-.Nm ftp
+.Nm
 will prompt the user to verify that the last argument is indeed the
 target local file for receiving
 .Ic mls
@@ -459,6 +505,9 @@
 and
 .Ic nmap
 settings.
+.It Ic msend Ar local-files
+A synonym for
+.Ic mput .
 .It Ic newer Ar file-name
 Get the file only if the modification time of the remote file is more
 recent that the file on the current system.
@@ -467,27 +516,9 @@
 .Ic newer  .
 Otherwise, this command is identical to
 .Ar get  .
-.It Xo
-.Ic nlist
-.Op Ar remote-directory
-.Op Ar local-file
-.Xc
-Print a  list of the files in a
-directory on the remote machine.
-If
-.Ar remote-directory
-is left unspecified, the current working directory is used.
-If interactive prompting is on,
-.Nm ftp
-will prompt the user to verify that the last argument is indeed the
-target local file for receiving
-.Ic nlist
-output.
-If no local file is specified, or if
-.Ar local-file
-is
-.Fl  ,
-the output is sent to the terminal.
+.It Ic nlist Op Ar remote-directory Op Ar local-file
+A synonym for
+.Ic ls .
 .It Ic nmap Op Ar inpattern outpattern
 Set or unset the filename mapping mechanism.
 If no arguments are specified, the filename mapping mechanism is unset.
@@ -595,18 +626,43 @@
 server.
 An optional port number may be supplied,
 in which case,
-.Nm ftp
+.Nm
 will attempt to contact an
 .Tn FTP
 server at that port.
 If the
 .Ic auto-login
 option is on (default),
-.Nm ftp
+.Nm
 will also attempt to automatically log the user in to
 the
 .Tn FTP
 server (see below).
+.It Ic passive
+Toggle passive mode.  If passive mode is turned on
+(default is off), the ftp client will
+send a
+.Dv PASV
+command for all data connections instead of the usual
+.Dv PORT
+command.  The
+.Dv PASV
+command requests that the remote server open a port for the data connection
+and return the address of that port.  The remote server listens on that
+port and the client connects to it.  When using the more traditional
+.Dv PORT
+command, the client listens on a port and sends that address to the remote
+server, who connects back to it.  Passive mode is useful when using
+.Nm
+through a gateway router or host that controls the directionality of
+traffic.
+(Note that though ftp servers are required to support the
+.Dv PASV
+command by RFC 1123, some do not.)
+.It Ic preserve
+Toggle preservation of modification times on retrieved files.
+.It Ic progress
+Toggle display of transfer progress bar.
 .It Ic prompt
 Toggle interactive prompting.
 Interactive prompting
@@ -619,6 +675,29 @@
 will transfer all files, and any
 .Ic mdelete
 will delete all files.
+.Pp
+When prompting is on, the following commands are available at a prompt:
+.Bl -tag -width 2n -offset indent
+.It Ic n
+Do not transfer the file.
+.It Ic a
+Answer
+.Sq yes
+to the current file, and automatically answer
+.Sq yes
+to any remaining files for the current command.
+.It Ic p
+Answer
+.Sq yes
+to the current file, and turn off prompt mode
+(as is
+.Dq prompt off
+had been given).
+.El
+.Pp
+Any other reponse will answer
+.Sq yes
+to the current file.
 .It Ic proxy Ar ftp-command
 Execute an ftp command on a secondary control connection.
 This command allows simultaneous connection to two remote ftp
@@ -679,7 +758,8 @@
 .Tn FTP
 server.
 .It Ic recv Ar remote-file Op Ar local-file
-A synonym for get.
+A synonym for
+.Ic get .
 .It Ic reget Ar remote-file Op Ar local-file
 Reget acts like get, except that if
 .Ar local-file
@@ -702,18 +782,14 @@
 If a
 .Ar command-name
 is specified it is supplied to the server as well.
-.It Ic remotestatus Op Ar file-name
+.It Ic rstatus Op Ar file-name
 With no arguments, show status of remote machine.
 If
 .Ar file-name
 is specified, show status of
 .Ar file-name
 on remote machine.
-.It Xo
-.Ic rename
-.Op Ar from
-.Op Ar to
-.Xc
+.It Ic rename Op Ar from Op Ar to
 Rename the file
 .Ar from
 on the remote machine, to the file
@@ -769,13 +845,14 @@
 (see below).
 The default value is off.
 .It Ic send Ar local-file Op Ar remote-file
-A synonym for put.
+A synonym for
+.Ic put .
 .It Ic sendport
 Toggle the use of
 .Dv PORT
 commands.
 By default,
-.Nm ftp
+.Nm
 will attempt to use a
 .Dv PORT
 command when establishing
@@ -787,7 +864,7 @@
 If the
 .Dv PORT
 command fails,
-.Nm ftp
+.Nm
 will use the default data port.
 When the use of
 .Dv PORT
@@ -853,8 +930,7 @@
 is omitted, the current umask is printed.
 .It Xo
 .Ic user Ar user-name
-.Op Ar password
-.Op Ar account
+.Op Ar password Op Ar account
 .Xc
 Identify yourself to the remote
 .Tn FTP
@@ -862,7 +938,7 @@
 If the
 .Ar password
 is not specified and the server requires it,
-.Nm ftp
+.Nm
 will prompt the user for it (after disabling local echo).
 If an
 .Ar account
@@ -877,7 +953,7 @@
 is completed if the remote server did not require it
 for logging in.
 Unless
-.Nm ftp
+.Nm
 is invoked with \*(Lqauto-login\*(Rq disabled, this
 process is done automatically on initial connection to
 the
@@ -895,11 +971,63 @@
 By default,
 verbose is on.
 .It Ic ? Op Ar command
-A synonym for help.
+A synonym for
+.Ic help .
 .El
 .Pp
 Command arguments which have embedded spaces may be quoted with
 quote `"' marks.
+.Pp
+Commands which toggle settings can take an explicit
+.Ic on
+or
+.Ic off
+argument to force the setting appropriately.
+.Pp
+If
+.Nm
+receives a
+.Dv SIGINFO
+(see the
+.Dq status
+argument of
+.Xr stty 1 )
+signal whilst a transfer is in progress, the current transfer rate
+statistics will be written to the standard error output, in the
+same format as the standard completion message.
+.Sh AUTO-FETCHING FILES
+In addition to standard commands, this version of
+.Nm
+supports an auto-fetch feature.
+To enable auto-fetch, simply pass the list of hostnames/files
+on the command line.
+.Pp
+The following formats are valid syntax for an auto-fetch element:
+.Bl -tag -width "http://host[:port]/file"
+.It host:/file 
+.Dq Classic
+ftp format
+.It ftp://host[:port]/file
+FTP URL, using the ftp protocol.
+.It http://host[:port]/file
+HTTP URL, using the http protocol.
+If
+.Ev http_proxy
+is defined, it is used as a URL to an HTTP proxy server.
+.El
+.Pp
+If a classic format or a ftp URL format has a trailing
+.Sq / ,
+then
+.Nm
+will connect to the site and
+.Ic cd
+to the directory given as the path, and leave the user in interactive
+mode ready for further input.
+.Pp
+If successive auto-fetch ftp elements refer to the same host, then
+the connection is maintained between transfers, reducing overhead on
+connection creation and deletion.
 .Sh ABORTING A FILE TRANSFER
 To abort a file transfer, use the terminal interrupt key
 (usually Ctrl-C).
@@ -919,18 +1047,18 @@
 sending the requested file.
 .Pp
 The terminal interrupt key sequence will be ignored when
-.Nm ftp
+.Nm
 has completed any local processing and is awaiting a reply
 from the remote server.
 A long delay in this mode may result from the ABOR processing described
 above, or from unexpected behavior by the remote server, including
 violations of the ftp protocol.
 If the delay results from unexpected remote server behavior, the local
-.Nm ftp
+.Nm
 program must be killed by hand.
 .Sh FILE NAMING CONVENTIONS
 Files specified as arguments to
-.Nm ftp
+.Nm
 commands are processed according to the following rules.
 .Bl -enum
 .It
@@ -946,7 +1074,7 @@
 .Sq \&| ,
 the
 remainder of the argument is interpreted as a shell command.
-.Nm Ftp
+.Nm
 then forks a shell, using
 .Xr popen 3
 with the argument supplied, and reads (writes) from the stdout
@@ -955,7 +1083,7 @@
 must be quoted; e.g.
 \*(Lq" ls -lt"\*(Rq.
 A particularly
-useful example of this mechanism is: \*(Lqdir more\*(Rq.
+useful example of this mechanism is: \*(Lqdir \&|more\*(Rq.
 .It
 Failing the above checks, if ``globbing'' is enabled,
 local file names are expanded
@@ -965,7 +1093,7 @@
 .Ic glob
 command.
 If the
-.Nm ftp
+.Nm
 command expects a single local file (.e.g.
 .Ic put  ) ,
 only the first filename generated by the "globbing" operation is used.
@@ -1010,13 +1138,13 @@
 and
 .Tn PDP Ns -20's
 mostly).
-.Nm Ftp
+.Nm
 supports the ascii and image types of file transfer,
 plus local byte size 8 for
 .Ic tenex
 mode transfers.
 .Pp
-.Nm Ftp
+.Nm
 supports only the default values for the remaining
 file transfer parameters:
 .Ic mode  ,
@@ -1040,7 +1168,7 @@
 file for a
 .Ic machine
 token that matches the remote machine specified on the
-.Nm ftp
+.Nm
 command line or as an
 .Ic open
 command argument.
@@ -1092,7 +1220,7 @@
 file for any user other
 than
 .Ar anonymous  ,
-.Nm ftp
+.Nm
 will abort the auto-login process if the
 .Pa .netrc
 is readable by
@@ -1107,7 +1235,7 @@
 .It Ic macdef Ar name
 Define a macro.
 This token functions like the
-.Nm ftp
+.Nm
 .Ic macdef
 command functions.
 A macro is defined with the specified name; its contents begin with the
@@ -1120,22 +1248,53 @@
 is defined, it is automatically executed as the last step in the
 auto-login process.
 .El
+.Sh COMMAND LINE EDITING
+.Nm
+supports interactive command line editing, via the 
+.Xr editline 3
+library.
+It is enabled with the
+.Ic edit
+command, and is enabled by default.
+Previous lines can be recalled and edited with the arrow keys,
+and other GNU Emacs-style editing keys may be used as well.
+.Pp
+The
+.Xr editline 3
+library is configured with a
+.Pa .editrc
+file - refer to
+.Xr editrc 5
+for more information.
+.Pp
+An extra key binding is available to
+.Nm
+to provide context sensitive command and filename completion
+(including remote file completion).
+To use this, bind a key to the
+.Xr editline 3
+command
+.Ic ftp-complete .
+By default, this is bound to the TAB key.
 .Sh ENVIRONMENT
-.Nm Ftp
+.Nm
 utilizes the following environment variables.
-.Bl -tag -width Fl
+.Bl -tag -width "http_proxy"
 .It Ev HOME
 For default location of a
 .Pa .netrc
 file, if one exists.
 .It Ev SHELL
 For default shell.
+.It Ev http_proxy
+URL of HTTP proxy to use when making HTTP requests.
 .El
 .Sh SEE ALSO
+.Xr editrc 5 ,
 .Xr ftpd 8
 .Sh HISTORY
 The
-.Nm ftp
+.Nm
 command appeared in
 .Bx 4.2 .
 .Sh BUGS
diff --new-file -r -b -u src/usr.bin/ftp.old/ftp.c src/usr.bin/ftp/ftp.c
--- src/usr.bin/ftp.old/ftp.c	Fri Aug 23 19:33:32 1996
+++ src/usr.bin/ftp/ftp.c	Sun Mar  2 19:57:27 1997
@@ -1,3 +1,6 @@
+/*	$OpenBSD: ftp.c,v 1.12 1997/02/05 04:55:18 millert Exp $	*/
+/*	$NetBSD: ftp.c,v 1.22 1997/02/01 10:45:03 lukem Exp $	*/
+
 /*
  * Copyright (c) 1985, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -32,15 +35,17 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)ftp.c	8.4 (Berkeley) 4/6/94";
+#if 0
+static char sccsid[] = "@(#)ftp.c	8.6 (Berkeley) 10/27/94";
+static char rcsid[] = "$OpenBSD: ftp.c,v 1.12 1997/02/05 04:55:18 millert Exp $";
+#else
+static char rcsid[] = "$FreeBSD$";
+#endif
 #endif /* not lint */
 
-#include <sys/param.h>
+#include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/ioctl.h>
 #include <sys/socket.h>
-#include <sys/time.h>
-#include <sys/file.h>
 
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
@@ -52,20 +57,17 @@
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <netdb.h>
 #include <pwd.h>
-#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <utime.h>
 #include <varargs.h>
 
 #include "ftp_var.h"
 
-extern int h_errno;
-
 struct	sockaddr_in hisctladdr;
 struct	sockaddr_in data_addr;
 int	data = -1;
@@ -81,18 +83,18 @@
 
 char *
 hookup(host, port)
-	char *host;
+	const char *host;
 	int port;
 {
 	struct hostent *hp = 0;
 	int s, len, tos;
-	static char hostnamebuf[80];
+	static char hostnamebuf[MAXHOSTNAMELEN];
 
-	memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
-	hisctladdr.sin_addr.s_addr = inet_addr(host);
-	if (hisctladdr.sin_addr.s_addr != -1) {
+	memset((void *)&hisctladdr, 0, sizeof(hisctladdr));
+	if (inet_aton(host, &hisctladdr.sin_addr) != 0) {
 		hisctladdr.sin_family = AF_INET;
-		(void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
+		(void)strncpy(hostnamebuf, host, sizeof(hostnamebuf) - 1);
+		hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
 	} else {
 		hp = gethostbyname(host);
 		if (hp == NULL) {
@@ -101,9 +103,9 @@
 			return ((char *) 0);
 		}
 		hisctladdr.sin_family = hp->h_addrtype;
-		memmove((caddr_t)&hisctladdr.sin_addr,
-				hp->h_addr_list[0], hp->h_length);
-		(void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
+		memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0], hp->h_length);
+		(void)strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf) - 1);
+		hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
 	}
 	hostname = hostnamebuf;
 	s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
@@ -113,7 +115,8 @@
 		return (0);
 	}
 	hisctladdr.sin_port = port;
-	while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
+	while (connect(s, (struct sockaddr *)&hisctladdr,
+			sizeof(hisctladdr)) < 0) {
 		if (hp && hp->h_addr_list[1]) {
 			int oerrno = errno;
 			char *ia;
@@ -122,11 +125,10 @@
 			errno = oerrno;
 			warn("connect to address %s", ia);
 			hp->h_addr_list++;
-			memmove((caddr_t)&hisctladdr.sin_addr,
-					hp->h_addr_list[0], hp->h_length);
-			fprintf(stdout, "Trying %s...\n",
-				inet_ntoa(hisctladdr.sin_addr));
-			(void) close(s);
+			memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0],
+			    hp->h_length);
+			printf("Trying %s...\n", inet_ntoa(hisctladdr.sin_addr));
+			(void)close(s);
 			s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
 			if (s < 0) {
 				warn("socket");
@@ -139,7 +141,7 @@
 		code = -1;
 		goto bad;
 	}
-	len = sizeof (myctladdr);
+	len = sizeof(myctladdr);
 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
 		warn("getsockname");
 		code = -1;
@@ -155,9 +157,9 @@
 	if (cin == NULL || cout == NULL) {
 		warnx("fdopen failed.");
 		if (cin)
-			(void) fclose(cin);
+			(void)fclose(cin);
 		if (cout)
-			(void) fclose(cout);
+			(void)fclose(cout);
 		code = -1;
 		goto bad;
 	}
@@ -165,9 +167,9 @@
 		printf("Connected to %s.\n", hostname);
 	if (getreply(0) > 2) { 	/* read startup message from server */
 		if (cin)
-			(void) fclose(cin);
+			(void)fclose(cin);
 		if (cout)
-			(void) fclose(cout);
+			(void)fclose(cout);
 		code = -1;
 		goto bad;
 	}
@@ -184,23 +186,56 @@
 
 	return (hostname);
 bad:
-	(void) close(s);
+	(void)close(s);
 	return ((char *)0);
 }
 
 int
 login(host)
-	char *host;
+	const char *host;
 {
 	char tmp[80];
 	char *user, *pass, *acct;
+	char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN];	/* "user@hostname" */
+	char hostname[MAXHOSTNAMELEN];
 	int n, aflag = 0;
 
-	user = pass = acct = 0;
+	user = pass = acct = NULL;
 	if (ruserpass(host, &user, &pass, &acct) < 0) {
 		code = -1;
 		return (0);
 	}
+
+	/*
+	 * Set up arguments for an anonymous FTP session, if necessary.
+	 */
+	if ((user == NULL || pass == NULL) && anonftp) {
+		memset(anonpass, 0, sizeof(anonpass));
+		memset(hostname, 0, sizeof(hostname));
+
+		/*
+		 * Set up anonymous login password.
+		 */
+		user = getlogin();
+		gethostname(hostname, MAXHOSTNAMELEN);
+#ifndef DONT_CHEAT_ANONPASS
+		/*
+		 * Every anonymous FTP server I've encountered
+		 * will accept the string "username@", and will
+		 * append the hostname itself.  We do this by default
+		 * since many servers are picky about not having
+		 * a FQDN in the anonymous password. - thorpej@netbsd.org
+		 */
+		snprintf(anonpass, sizeof(anonpass) - 1, "%s@",
+		    user);
+#else
+		snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s",
+		    user, hp->h_name);
+#endif
+		pass = anonpass;
+		user = "anonymous";
+	}
+
 	while (user == NULL) {
 		char *myname = getlogin();
 
@@ -214,7 +249,7 @@
 			printf("Name (%s:%s): ", host, myname);
 		else
 			printf("Name (%s): ", host);
-		(void) fgets(tmp, sizeof(tmp) - 1, stdin);
+		(void)fgets(tmp, sizeof(tmp) - 1, stdin);
 		tmp[strlen(tmp) - 1] = '\0';
 		if (*tmp == '\0')
 			user = myname;
@@ -237,12 +272,12 @@
 		return (0);
 	}
 	if (!aflag && acct != NULL)
-		(void) command("ACCT %s", acct);
+		(void)command("ACCT %s", acct);
 	if (proxy)
 		return (1);
 	for (n = 0; n < macnum; ++n) {
 		if (!strcmp("init", macros[n].mac_name)) {
-			(void) strcpy(line, "$init");
+			(void)strcpy(line, "$init");
 			makeargv();
 			domacro(margc, margv);
 			break;
@@ -252,14 +287,16 @@
 }
 
 void
-cmdabort()
+cmdabort(notused)
+	int notused;
 {
 
-	printf("\n");
-	(void) fflush(stdout);
+	alarmtimer(0);
+	putchar('\n');
+	(void)fflush(stdout);
 	abrtflag++;
 	if (ptflag)
-		longjmp(ptabort,1);
+		longjmp(ptabort, 1);
 }
 
 /*VARARGS*/
@@ -274,16 +311,18 @@
 
 	abrtflag = 0;
 	if (debug) {
-		printf("---> ");
+		fputs("---> ", stdout);
 		va_start(ap);
 		fmt = va_arg(ap, char *);
 		if (strncmp("PASS ", fmt, 5) == 0)
-			printf("PASS XXXX");
+			fputs("PASS XXXX", stdout);
+		else if (strncmp("ACCT ", fmt, 5) == 0)
+			fputs("ACCT XXXX", stdout);
 		else
-			vfprintf(stdout, fmt, ap);
+			vprintf(fmt, ap);
 		va_end(ap);
-		printf("\n");
-		(void) fflush(stdout);
+		putchar('\n');
+		(void)fflush(stdout);
 	}
 	if (cout == NULL) {
 		warn("No control connection for command");
@@ -295,23 +334,24 @@
 	fmt = va_arg(ap, char *);
 	vfprintf(cout, fmt, ap);
 	va_end(ap);
-	fprintf(cout, "\r\n");
-	(void) fflush(cout);
+	fputs("\r\n", cout);
+	(void)fflush(cout);
 	cpend = 1;
 	r = getreply(!strcmp(fmt, "QUIT"));
 	if (abrtflag && oldintr != SIG_IGN)
 		(*oldintr)(SIGINT);
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
 	return (r);
 }
 
-char reply_string[BUFSIZ];		/* last line of previous reply */
+char reply_string[BUFSIZ];		/* first line of previous reply */
 
 int
 getreply(expecteof)
 	int expecteof;
 {
-	int c, n;
+	char current_line[BUFSIZ];	/* last line of previous reply */
+	int c, n, line;
 	int dig;
 	int originalcode = 0, continuation = 0;
 	sig_t oldintr;
@@ -319,9 +359,9 @@
 	char *cp, *pt = pasv;
 
 	oldintr = signal(SIGINT, cmdabort);
-	for (;;) {
+	for (line = 0 ;; line++) {
 		dig = n = code = 0;
-		cp = reply_string;
+		cp = current_line;
 		while ((c = getc(cin)) != '\n') {
 			if (c == IAC) {     /* handle telnet commands */
 				switch (c = getc(cin)) {
@@ -329,13 +369,13 @@
 				case WONT:
 					c = getc(cin);
 					fprintf(cout, "%c%c%c", IAC, DONT, c);
-					(void) fflush(cout);
+					(void)fflush(cout);
 					break;
 				case DO:
 				case DONT:
 					c = getc(cin);
 					fprintf(cout, "%c%c%c", IAC, WONT, c);
-					(void) fflush(cout);
+					(void)fflush(cout);
 					break;
 				default:
 					break;
@@ -345,24 +385,26 @@
 			dig++;
 			if (c == EOF) {
 				if (expecteof) {
-					(void) signal(SIGINT,oldintr);
+					(void)signal(SIGINT, oldintr);
 					code = 221;
 					return (0);
 				}
 				lostpeer();
 				if (verbose) {
-					printf("421 Service not available, remote server has closed connection\n");
-					(void) fflush(stdout);
+					puts(
+"421 Service not available, remote server has closed connection");
+					(void)fflush(stdout);
 				}
 				code = 421;
 				return (4);
 			}
 			if (c != '\r' && (verbose > 0 ||
-			    (verbose > -1 && n == '5' && dig > 4))) {
+			    (verbose > -1 && n == '5' && dig > 4)) &&
+			    (n < '5' || !retry_connect)) {
 				if (proxflag &&
-				   (dig == 1 || dig == 5 && verbose == 0))
-					printf("%s:",hostname);
-				(void) putchar(c);
+				   (dig == 1 || (dig == 5 && verbose == 0)))
+					printf("%s:", hostname);
+				(void)putchar(c);
 			}
 			if (dig < 4 && isdigit(c))
 				code = code * 10 + (c - '0');
@@ -385,12 +427,22 @@
 			}
 			if (n == 0)
 				n = c;
-			if (cp < &reply_string[sizeof(reply_string) - 1])
+			if (cp < &current_line[sizeof(current_line) - 1])
 				*cp++ = c;
 		}
-		if (verbose > 0 || verbose > -1 && n == '5') {
-			(void) putchar(c);
-			(void) fflush (stdout);
+		if (verbose > 0 || ((verbose > -1 && n == '5') &&
+		    (n < '5' || !retry_connect))) {
+			(void)putchar(c);
+			(void)fflush (stdout);
+		}
+		if (line == 0) {
+			size_t len = cp - current_line;
+
+			if (len > sizeof(reply_string))
+				len = sizeof(reply_string);
+
+			(void)strncpy(reply_string, current_line, len);
+			reply_string[len] = '\0';
 		}
 		if (continuation && code != originalcode) {
 			if (originalcode == 0)
@@ -400,7 +452,7 @@
 		*cp = '\0';
 		if (n != '1')
 			cpend = 0;
-		(void) signal(SIGINT,oldintr);
+		(void)signal(SIGINT, oldintr);
 		if (code == 421 || originalcode == 421)
 			lostpeer();
 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
@@ -424,32 +476,35 @@
 jmp_buf	sendabort;
 
 void
-abortsend()
+abortsend(notused)
+	int notused;
 {
 
+	alarmtimer(0);
 	mflag = 0;
 	abrtflag = 0;
-	printf("\nsend aborted\nwaiting for remote to finish abort\n");
-	(void) fflush(stdout);
+	puts("\nsend aborted\nwaiting for remote to finish abort");
+	(void)fflush(stdout);
 	longjmp(sendabort, 1);
 }
 
-#define HASHBYTES 1024
-
 void
 sendrequest(cmd, local, remote, printnames)
-	char *cmd, *local, *remote;
+	const char *cmd, *local, *remote;
 	int printnames;
 {
 	struct stat st;
-	struct timeval start, stop;
 	int c, d;
-	FILE *fin, *dout = 0, *popen();
+	FILE *fin, *dout = 0;
 	int (*closefunc) __P((FILE *));
-	sig_t oldintr, oldintp;
-	long bytes = 0, hashbytes = HASHBYTES;
+	sig_t oldinti, oldintr, oldintp;
+	off_t hashbytes;
 	char *lmode, buf[BUFSIZ], *bufp;
 
+	hashbytes = mark;
+	direction = "sent";
+	bytes = 0;
+	filesize = -1;
 	if (verbose && printnames) {
 		if (local && *local != '-')
 			printf("local: %s ", local);
@@ -465,32 +520,37 @@
 	closefunc = NULL;
 	oldintr = NULL;
 	oldintp = NULL;
+	oldinti = NULL;
 	lmode = "w";
 	if (setjmp(sendabort)) {
 		while (cpend) {
-			(void) getreply(0);
+			(void)getreply(0);
 		}
 		if (data >= 0) {
-			(void) close(data);
+			(void)close(data);
 			data = -1;
 		}
 		if (oldintr)
-			(void) signal(SIGINT,oldintr);
+			(void)signal(SIGINT, oldintr);
 		if (oldintp)
-			(void) signal(SIGPIPE,oldintp);
+			(void)signal(SIGPIPE, oldintp);
+		if (oldinti)
+			(void)signal(SIGINFO, oldinti);
 		code = -1;
 		return;
 	}
 	oldintr = signal(SIGINT, abortsend);
+	oldinti = signal(SIGINFO, psummary);
 	if (strcmp(local, "-") == 0)
 		fin = stdin;
 	else if (*local == '|') {
-		oldintp = signal(SIGPIPE,SIG_IGN);
+		oldintp = signal(SIGPIPE, SIG_IGN);
 		fin = popen(local + 1, "r");
 		if (fin == NULL) {
 			warn("%s", local + 1);
-			(void) signal(SIGINT, oldintr);
-			(void) signal(SIGPIPE, oldintp);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGPIPE, oldintp);
+			(void)signal(SIGINFO, oldinti);
 			code = -1;
 			return;
 		}
@@ -499,24 +559,28 @@
 		fin = fopen(local, "r");
 		if (fin == NULL) {
 			warn("local: %s", local);
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			code = -1;
 			return;
 		}
 		closefunc = fclose;
 		if (fstat(fileno(fin), &st) < 0 ||
 		    (st.st_mode&S_IFMT) != S_IFREG) {
-			fprintf(stdout, "%s: not a plain file.\n", local);
-			(void) signal(SIGINT, oldintr);
+			printf("%s: not a plain file.\n", local);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			fclose(fin);
 			code = -1;
 			return;
 		}
+		filesize = st.st_size;
 	}
 	if (initconn()) {
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
+		(void)signal(SIGINFO, oldinti);
 		if (oldintp)
-			(void) signal(SIGPIPE, oldintp);
+			(void)signal(SIGPIPE, oldintp);
 		code = -1;
 		if (closefunc != NULL)
 			(*closefunc)(fin);
@@ -529,6 +593,7 @@
 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
 		int rc;
 
+		rc = -1;
 		switch (curtype) {
 		case TYPE_A:
 			rc = fseek(fin, (long) restart_point, SEEK_SET);
@@ -557,18 +622,20 @@
 	}
 	if (remote) {
 		if (command("%s %s", cmd, remote) != PRELIM) {
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			if (oldintp)
-				(void) signal(SIGPIPE, oldintp);
+				(void)signal(SIGPIPE, oldintp);
 			if (closefunc != NULL)
 				(*closefunc)(fin);
 			return;
 		}
 	} else
 		if (command("%s", cmd) != PRELIM) {
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			if (oldintp)
-				(void) signal(SIGPIPE, oldintp);
+				(void)signal(SIGPIPE, oldintp);
 			if (closefunc != NULL)
 				(*closefunc)(fin);
 			return;
@@ -576,31 +643,31 @@
 	dout = dataconn(lmode);
 	if (dout == NULL)
 		goto abort;
-	(void) gettimeofday(&start, (struct timezone *)0);
+	progressmeter(-1);
 	oldintp = signal(SIGPIPE, SIG_IGN);
 	switch (curtype) {
 
 	case TYPE_I:
 	case TYPE_L:
 		errno = d = 0;
-		while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
+		while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
 			bytes += c;
 			for (bufp = buf; c > 0; c -= d, bufp += d)
 				if ((d = write(fileno(dout), bufp, c)) <= 0)
 					break;
-			if (hash) {
+			if (hash && (!progress || filesize < 0) ) {
 				while (bytes >= hashbytes) {
-					(void) putchar('#');
-					hashbytes += HASHBYTES;
+					(void)putchar('#');
+					hashbytes += mark;
 				}
-				(void) fflush(stdout);
+				(void)fflush(stdout);
 			}
 		}
-		if (hash && bytes > 0) {
-			if (bytes < HASHBYTES)
-				(void) putchar('#');
-			(void) putchar('\n');
-			(void) fflush(stdout);
+		if (hash && (!progress || filesize < 0) && bytes > 0) {
+			if (bytes < mark)
+				(void)putchar('#');
+			(void)putchar('\n');
+			(void)fflush(stdout);
 		}
 		if (c < 0)
 			warn("local: %s", local);
@@ -614,28 +681,31 @@
 	case TYPE_A:
 		while ((c = getc(fin)) != EOF) {
 			if (c == '\n') {
-				while (hash && (bytes >= hashbytes)) {
-					(void) putchar('#');
-					(void) fflush(stdout);
-					hashbytes += HASHBYTES;
+				while (hash && (!progress || filesize < 0) &&
+				    (bytes >= hashbytes)) {
+					(void)putchar('#');
+					(void)fflush(stdout);
+					hashbytes += mark;
 				}
 				if (ferror(dout))
 					break;
-				(void) putc('\r', dout);
+				(void)putc('\r', dout);
 				bytes++;
 			}
-			(void) putc(c, dout);
+			(void)putc(c, dout);
 			bytes++;
-	/*		if (c == '\r') {			  	*/
-	/*		(void)	putc('\0', dout);  // this violates rfc */
-	/*			bytes++;				*/
-	/*		}                          			*/
+#if 0	/* this violates RFC */
+			if (c == '\r') {
+				(void)putc('\0', dout);
+				bytes++;
+			}
+#endif
 		}
-		if (hash) {
+		if (hash && (!progress || filesize < 0)) {
 			if (bytes < hashbytes)
-				(void) putchar('#');
-			(void) putchar('\n');
-			(void) fflush(stdout);
+				(void)putchar('#');
+			(void)putchar('\n');
+			(void)fflush(stdout);
 		}
 		if (ferror(fin))
 			warn("local: %s", local);
@@ -646,68 +716,75 @@
 		}
 		break;
 	}
-	(void) gettimeofday(&stop, (struct timezone *)0);
+	progressmeter(1);
 	if (closefunc != NULL)
 		(*closefunc)(fin);
-	(void) fclose(dout);
-	(void) getreply(0);
-	(void) signal(SIGINT, oldintr);
+	(void)fclose(dout);
+	(void)getreply(0);
+	(void)signal(SIGINT, oldintr);
+	(void)signal(SIGINFO, oldinti);
 	if (oldintp)
-		(void) signal(SIGPIPE, oldintp);
+		(void)signal(SIGPIPE, oldintp);
 	if (bytes > 0)
-		ptransfer("sent", bytes, &start, &stop);
+		ptransfer(0);
 	return;
 abort:
-	(void) gettimeofday(&stop, (struct timezone *)0);
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
+	(void)signal(SIGINFO, oldinti);
 	if (oldintp)
-		(void) signal(SIGPIPE, oldintp);
+		(void)signal(SIGPIPE, oldintp);
 	if (!cpend) {
 		code = -1;
 		return;
 	}
 	if (data >= 0) {
-		(void) close(data);
+		(void)close(data);
 		data = -1;
 	}
 	if (dout)
-		(void) fclose(dout);
-	(void) getreply(0);
+		(void)fclose(dout);
+	(void)getreply(0);
 	code = -1;
 	if (closefunc != NULL && fin != NULL)
 		(*closefunc)(fin);
 	if (bytes > 0)
-		ptransfer("sent", bytes, &start, &stop);
+		ptransfer(0);
 }
 
 jmp_buf	recvabort;
 
 void
-abortrecv()
+abortrecv(notused)
+	int notused;
 {
 
+	alarmtimer(0);
 	mflag = 0;
 	abrtflag = 0;
-	printf("\nreceive aborted\nwaiting for remote to finish abort\n");
-	(void) fflush(stdout);
+	puts("\nreceive aborted\nwaiting for remote to finish abort");
+	(void)fflush(stdout);
 	longjmp(recvabort, 1);
 }
 
 void
 recvrequest(cmd, local, remote, lmode, printnames)
-	char *cmd, *local, *remote, *lmode;
+	const char *cmd, *local, *remote, *lmode;
 	int printnames;
 {
 	FILE *fout, *din = 0;
 	int (*closefunc) __P((FILE *));
-	sig_t oldintr, oldintp;
+	sig_t oldinti, oldintr, oldintp;
 	int c, d, is_retr, tcrflag, bare_lfs = 0;
 	static int bufsize;
 	static char *buf;
-	long bytes = 0, hashbytes = HASHBYTES;
-	struct timeval start, stop;
+	off_t hashbytes;
 	struct stat st;
+	time_t mtime;
 
+	hashbytes = mark;
+	direction = "received";
+	bytes = 0;
+	filesize = -1;
 	is_retr = strcmp(cmd, "RETR") == 0;
 	if (is_retr && verbose && printnames) {
 		if (local && *local != '-')
@@ -725,25 +802,29 @@
 	tcrflag = !crflag && is_retr;
 	if (setjmp(recvabort)) {
 		while (cpend) {
-			(void) getreply(0);
+			(void)getreply(0);
 		}
 		if (data >= 0) {
-			(void) close(data);
+			(void)close(data);
 			data = -1;
 		}
 		if (oldintr)
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+		if (oldinti)
+			(void)signal(SIGINFO, oldinti);
 		code = -1;
 		return;
 	}
 	oldintr = signal(SIGINT, abortrecv);
+	oldinti = signal(SIGINFO, psummary);
 	if (strcmp(local, "-") && *local != '|') {
 		if (access(local, 2) < 0) {
 			char *dir = strrchr(local, '/');
 
 			if (errno != ENOENT && errno != EACCES) {
 				warn("local: %s", local);
-				(void) signal(SIGINT, oldintr);
+				(void)signal(SIGINT, oldintr);
+				(void)signal(SIGINFO, oldinti);
 				code = -1;
 				return;
 			}
@@ -754,27 +835,30 @@
 				*dir = '/';
 			if (d < 0) {
 				warn("local: %s", local);
-				(void) signal(SIGINT, oldintr);
+				(void)signal(SIGINT, oldintr);
+				(void)signal(SIGINFO, oldinti);
 				code = -1;
 				return;
 			}
 			if (!runique && errno == EACCES &&
 			    chmod(local, 0600) < 0) {
 				warn("local: %s", local);
-				(void) signal(SIGINT, oldintr);
-				(void) signal(SIGINT, oldintr);
+				(void)signal(SIGINT, oldintr);
+				(void)signal(SIGINFO, oldinti);
 				code = -1;
 				return;
 			}
 			if (runique && errno == EACCES &&
 			   (local = gunique(local)) == NULL) {
-				(void) signal(SIGINT, oldintr);
+				(void)signal(SIGINT, oldintr);
+				(void)signal(SIGINFO, oldinti);
 				code = -1;
 				return;
 			}
 		}
 		else if (runique && (local = gunique(local)) == NULL) {
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			code = -1;
 			return;
 		}
@@ -782,10 +866,14 @@
 	if (!is_retr) {
 		if (curtype != TYPE_A)
 			changetype(TYPE_A, 0);
-	} else if (curtype != type)
+	} else {
+		if (curtype != type)
 		changetype(type, 0);
+		filesize = remotesize(remote, 0);
+	}
 	if (initconn()) {
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
+		(void)signal(SIGINFO, oldinti);
 		code = -1;
 		return;
 	}
@@ -796,12 +884,14 @@
 		return;
 	if (remote) {
 		if (command("%s %s", cmd, remote) != PRELIM) {
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			return;
 		}
 	} else {
 		if (command("%s", cmd) != PRELIM) {
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
+			(void)signal(SIGINFO, oldinti);
 			return;
 		}
 	}
@@ -830,7 +920,7 @@
 		st.st_blksize = BUFSIZ;
 	if (st.st_blksize > bufsize) {
 		if (buf)
-			(void) free(buf);
+			(void)free(buf);
 		buf = malloc((unsigned)st.st_blksize);
 		if (buf == NULL) {
 			warn("malloc");
@@ -839,7 +929,7 @@
 		}
 		bufsize = st.st_blksize;
 	}
-	(void) gettimeofday(&start, (struct timezone *)0);
+	progressmeter(-1);
 	switch (curtype) {
 
 	case TYPE_I:
@@ -856,19 +946,19 @@
 			if ((d = write(fileno(fout), buf, c)) != c)
 				break;
 			bytes += c;
-			if (hash) {
+			if (hash && (!progress || filesize < 0)) {
 				while (bytes >= hashbytes) {
-					(void) putchar('#');
-					hashbytes += HASHBYTES;
+					(void)putchar('#');
+					hashbytes += mark;
 				}
-				(void) fflush(stdout);
+				(void)fflush(stdout);
 			}
 		}
-		if (hash && bytes > 0) {
-			if (bytes < HASHBYTES)
-				(void) putchar('#');
-			(void) putchar('\n');
-			(void) fflush(stdout);
+		if (hash && (!progress || filesize < 0) && bytes > 0) {
+			if (bytes < mark)
+				(void)putchar('#');
+			(void)putchar('\n');
+			(void)fflush(stdout);
 		}
 		if (c < 0) {
 			if (errno != EPIPE)
@@ -908,16 +998,17 @@
 			if (c == '\n')
 				bare_lfs++;
 			while (c == '\r') {
-				while (hash && (bytes >= hashbytes)) {
-					(void) putchar('#');
-					(void) fflush(stdout);
-					hashbytes += HASHBYTES;
+				while (hash && (!progress || filesize < 0) &&
+				    (bytes >= hashbytes)) {
+					(void)putchar('#');
+					(void)fflush(stdout);
+					hashbytes += mark;
 				}
 				bytes++;
 				if ((c = getc(din)) != '\n' || tcrflag) {
 					if (ferror(fout))
 						goto break2;
-					(void) putc('\r', fout);
+					(void)putc('\r', fout);
 					if (c == '\0') {
 						bytes++;
 						goto contin2;
@@ -926,20 +1017,21 @@
 						goto contin2;
 				}
 			}
-			(void) putc(c, fout);
+			(void)putc(c, fout);
 			bytes++;
 	contin2:	;
 		}
 break2:
 		if (bare_lfs) {
-			printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
-			printf("File may not have transferred correctly.\n");
+			printf("WARNING! %d bare linefeeds received in ASCII mode\n",
+			    bare_lfs);
+			puts("File may not have transferred correctly.");
 		}
-		if (hash) {
+		if (hash && (!progress || filesize < 0)) {
 			if (bytes < hashbytes)
-				(void) putchar('#');
-			(void) putchar('\n');
-			(void) fflush(stdout);
+				(void)putchar('#');
+			(void)putchar('\n');
+			(void)fflush(stdout);
 		}
 		if (ferror(din)) {
 			if (errno != EPIPE)
@@ -950,44 +1042,60 @@
 			warn("local: %s", local);
 		break;
 	}
+	progressmeter(1);
 	if (closefunc != NULL)
 		(*closefunc)(fout);
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
+	(void)signal(SIGINFO, oldinti);
 	if (oldintp)
-		(void) signal(SIGPIPE, oldintp);
-	(void) gettimeofday(&stop, (struct timezone *)0);
-	(void) fclose(din);
-	(void) getreply(0);
-	if (bytes > 0 && is_retr)
-		ptransfer("received", bytes, &start, &stop);
+		(void)signal(SIGPIPE, oldintp);
+	(void)fclose(din);
+	(void)getreply(0);
+	if (bytes >= 0 && is_retr) {
+		if (bytes > 0)
+			ptransfer(0);
+		if (preserve && (closefunc == fclose)) {
+			mtime = remotemodtime(remote, 0);
+			if (mtime != -1) {
+				struct utimbuf ut;
+
+				ut.actime = time(NULL);
+				ut.modtime = mtime;
+				if (utime(local, &ut) == -1)
+					printf("Can't change modification time on %s to %s",
+					    local, asctime(localtime(&mtime)));
+			}
+		}
+	}
 	return;
 abort:
 
 /* abort using RFC959 recommended IP,SYNC sequence  */
 
-	(void) gettimeofday(&stop, (struct timezone *)0);
 	if (oldintp)
-		(void) signal(SIGPIPE, oldintr);
-	(void) signal(SIGINT, SIG_IGN);
+		(void)signal(SIGPIPE, oldintp);
+	(void)signal(SIGINT, SIG_IGN);
 	if (!cpend) {
 		code = -1;
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
+		(void)signal(SIGINFO, oldinti);
 		return;
 	}
 
 	abort_remote(din);
 	code = -1;
 	if (data >= 0) {
-		(void) close(data);
+		(void)close(data);
 		data = -1;
 	}
 	if (closefunc != NULL && fout != NULL)
 		(*closefunc)(fout);
 	if (din)
-		(void) fclose(din);
+		(void)fclose(din);
 	if (bytes > 0)
-		ptransfer("received", bytes, &start, &stop);
-	(void) signal(SIGINT, oldintr);
+		ptransfer(0);
+	(void)signal(SIGINT, oldintr);
+	(void)signal(SIGINFO, oldinti);
 }
 
 /*
@@ -1000,58 +1108,56 @@
 	char *p, *a;
 	int result, len, tmpno = 0;
 	int on = 1;
-	int tos, ports;
-	u_long a1,a2,a3,a4,p1,p2;
+	int a0, a1, a2, a3, p0, p1;
+	int ports;
 
 	if (passivemode) {
 		data = socket(AF_INET, SOCK_STREAM, 0);
 		if (data < 0) {
-			perror("ftp: socket");
-			return(1);
+			warn("socket");
+			return (1);
 		}
-		if (options & SO_DEBUG &&
+		if ((options & SO_DEBUG) &&
 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
-			       sizeof (on)) < 0)
-			perror("ftp: setsockopt (ignored)");
+			       sizeof(on)) < 0)
+			warn("setsockopt (ignored)");
 		if (command("PASV") != COMPLETE) {
-			printf("Passive mode refused.\n");
-			close(data);
-			return(1);
+			puts("Passive mode refused.");
+			goto bad;
 		}
 
 		/*
-		 * What we've got at this point is a string of comma separated
-		 * one-byte unsigned integer values, separated by commas.
-		 * The first four are the an IP address. The fifth is the MSB
-		 * of the port number, the sixth is the LSB. From that we'll
-		 * prepare a sockaddr_in.
+		 * What we've got at this point is a string of comma
+		 * separated one-byte unsigned integer values.
+		 * The first four are the an IP address. The fifth is
+		 * the MSB of the port number, the sixth is the LSB.
+		 * From that we'll prepare a sockaddr_in.
 		 */
 
-		if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2)
-		    != 6) {
-			printf("Passive mode address scan failure. Shouldn't happen!\n");
-			close(data);
-			return(1);
-		};
+		if (sscanf(pasv, "%d,%d,%d,%d,%d,%d",
+			   &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
+			puts("Passive mode address scan failure. Shouldn't happen!");
+			goto bad;
+		}
 
+		memset(&data_addr, 0, sizeof(data_addr));
 		data_addr.sin_family = AF_INET;
-		data_addr.sin_addr.s_addr = htonl((a1 << 24) | (a2 << 16) |
-						  (a3 << 8) | a4);
-		data_addr.sin_port = htons((p1 << 8) | p2);
-
-		if (connect(data, (struct sockaddr *) &data_addr,
-		    sizeof(data_addr))<0) {
-			perror("ftp: connect");
-			close(data);
-			return(1);
+		data_addr.sin_addr.s_addr = htonl((a0 << 24) | (a1 << 16) |
+						  (a2 << 8) | a3);
+		data_addr.sin_port = htons((p0 << 8) | p1);
+
+		if (connect(data, (struct sockaddr *)&data_addr,
+			    sizeof(data_addr)) < 0) {
+			warn("connect");
+			goto bad;
 		}
 #ifdef IP_TOS
-		tos = IPTOS_THROUGHPUT;
-		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos,
-		    sizeof(tos)) < 0)
-			perror("ftp: setsockopt TOS (ignored)");
+		on = IPTOS_THROUGHPUT;
+		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
+			       sizeof(int)) < 0)
+			warn("setsockopt TOS (ignored)");
 #endif
-		return(0);
+		return (0);
 	}
 
 noport:
@@ -1059,7 +1165,7 @@
 	if (sendport)
 		data_addr.sin_port = 0;	/* let system pick one */
 	if (data != -1)
-		(void) close(data);
+		(void)close(data);
 	data = socket(AF_INET, SOCK_STREAM, 0);
 	if (data < 0) {
 		warn("socket");
@@ -1069,7 +1175,7 @@
 	}
 	if (!sendport)
 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
-			       sizeof (on)) < 0) {
+				sizeof(on)) < 0) {
 			warn("setsockopt (reuse address)");
 			goto bad;
 		}
@@ -1079,14 +1185,16 @@
 		       sizeof (ports)) < 0)
 		warn("setsockopt PORTRANGE (ignored)");
 #endif
-	if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
+ 
+	if (bind(data, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) {
 		warn("bind");
 		goto bad;
 	}
 	if (options & SO_DEBUG &&
-	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
+	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
+			sizeof(on)) < 0)
 		warn("setsockopt (ignored)");
-	len = sizeof (data_addr);
+	len = sizeof(data_addr);
 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
 		warn("getsockname");
 		goto bad;
@@ -1117,7 +1225,7 @@
 #endif
 	return (0);
 bad:
-	(void) close(data), data = -1;
+	(void)close(data), data = -1;
 	if (tmpno)
 		sendport = 1;
 	return (1);
@@ -1125,10 +1233,10 @@
 
 FILE *
 dataconn(lmode)
-	char *lmode;
+	const char *lmode;
 {
 	struct sockaddr_in from;
-	int s, fromlen = sizeof (from), tos;
+	int s, fromlen = sizeof(from), tos;
 
 	if (passivemode)
 	    return (fdopen(data, lmode));
@@ -1136,10 +1244,10 @@
 	s = accept(data, (struct sockaddr *) &from, &fromlen);
 	if (s < 0) {
 		warn("accept");
-		(void) close(data), data = -1;
+		(void)close(data), data = -1;
 		return (NULL);
 	}
-	(void) close(data);
+	(void)close(data);
 	data = s;
 #ifdef IP_TOS
 	tos = IPTOS_THROUGHPUT;
@@ -1150,56 +1258,20 @@
 }
 
 void
-ptransfer(direction, bytes, t0, t1)
-	char *direction;
-	long bytes;
-	struct timeval *t0, *t1;
+psummary(notused)
+	int notused;
 {
-	struct timeval td;
-	float s, bs;
 
-	if (verbose) {
-		tvsub(&td, t1, t0);
-		s = td.tv_sec + (td.tv_usec / 1000000.);
-#define	nz(x)	((x) == 0 ? 1 : (x))
-		bs = bytes / nz(s);
-		if(bs > ( 1024 * 1024 ))
-			printf("%ld bytes %s in %.2f seconds (%.2f Meg/s)\n",
-				bytes, direction, s, bs / (1024. * 1024.));
-		else
-			printf("%ld bytes %s in %.2f seconds (%.2f Kbytes/s)\n",
-				bytes, direction, s, bs / 1024.);
-	}
-}
-
-/*
-void
-tvadd(tsum, t0)
-	struct timeval *tsum, *t0;
-{
-
-	tsum->tv_sec += t0->tv_sec;
-	tsum->tv_usec += t0->tv_usec;
-	if (tsum->tv_usec > 1000000)
-		tsum->tv_sec++, tsum->tv_usec -= 1000000;
-}
-*/
-
-void
-tvsub(tdiff, t1, t0)
-	struct timeval *tdiff, *t1, *t0;
-{
-
-	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
-	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
-	if (tdiff->tv_usec < 0)
-		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
+	if (bytes > 0)
+		ptransfer(1);
 }
 
 void
-psabort()
+psabort(notused)
+	int notused;
 {
 
+	alarmtimer(0);
 	abrtflag++;
 }
 
@@ -1248,10 +1320,10 @@
 	ip->connect = connected;
 	connected = op->connect;
 	if (hostname) {
-		(void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
-		ip->name[strlen(ip->name)] = '\0';
+		(void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
+		ip->name[sizeof(ip->name) - 1] = '\0';
 	} else
-		ip->name[0] = 0;
+		ip->name[0] = '\0';
 	hostname = op->name;
 	ip->hctl = hisctladdr;
 	hisctladdr = op->hctl;
@@ -1275,21 +1347,21 @@
 	mcase = op->mcse;
 	ip->ntflg = ntflag;
 	ntflag = op->ntflg;
-	(void) strncpy(ip->nti, ntin, 16);
-	(ip->nti)[strlen(ip->nti)] = '\0';
-	(void) strcpy(ntin, op->nti);
-	(void) strncpy(ip->nto, ntout, 16);
-	(ip->nto)[strlen(ip->nto)] = '\0';
-	(void) strcpy(ntout, op->nto);
+	(void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
+	(ip->nti)[sizeof(ip->nti) - 1] = '\0';
+	(void)strcpy(ntin, op->nti);
+	(void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
+	(ip->nto)[sizeof(ip->nto) - 1] = '\0';
+	(void)strcpy(ntout, op->nto);
 	ip->mapflg = mapflag;
 	mapflag = op->mapflg;
-	(void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
-	(ip->mi)[strlen(ip->mi)] = '\0';
-	(void) strcpy(mapin, op->mi);
-	(void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
-	(ip->mo)[strlen(ip->mo)] = '\0';
-	(void) strcpy(mapout, op->mo);
-	(void) signal(SIGINT, oldintr);
+	(void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
+	(ip->mi)[sizeof(ip->mi) - 1] = '\0';
+	(void)strcpy(mapin, op->mi);
+	(void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
+	(ip->mo)[sizeof(ip->mo) - 1] = '\0';
+	(void)strcpy(mapout, op->mo);
+	(void)signal(SIGINT, oldintr);
 	if (abrtflag) {
 		abrtflag = 0;
 		(*oldintr)(SIGINT);
@@ -1297,11 +1369,13 @@
 }
 
 void
-abortpt()
+abortpt(notused)
+	int notused;
 {
 
-	printf("\n");
-	(void) fflush(stdout);
+	alarmtimer(0);
+	putchar('\n');
+	(void)fflush(stdout);
 	ptabflg++;
 	mflag = 0;
 	abrtflag = 0;
@@ -1310,7 +1384,7 @@
 
 void
 proxtrans(cmd, local, remote)
-	char *cmd, *local, *remote;
+	const char *cmd, *local, *remote;
 {
 	sig_t oldintr;
 	int secndflag = 0, prox_type, nfnd;
@@ -1330,12 +1404,12 @@
 	if (curtype != prox_type)
 		changetype(prox_type, 1);
 	if (command("PASV") != COMPLETE) {
-		printf("proxy server does not support third party transfers.\n");
+		puts("proxy server does not support third party transfers.");
 		return;
 	}
 	pswitch(0);
 	if (!connected) {
-		printf("No primary connection\n");
+		puts("No primary connection");
 		pswitch(1);
 		code = -1;
 		return;
@@ -1350,7 +1424,7 @@
 		goto abort;
 	oldintr = signal(SIGINT, abortpt);
 	if (command("%s %s", cmd, remote) != PRELIM) {
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
 		pswitch(1);
 		return;
 	}
@@ -1360,16 +1434,16 @@
 	if (command("%s %s", cmd2, local) != PRELIM)
 		goto abort;
 	ptflag++;
-	(void) getreply(0);
+	(void)getreply(0);
 	pswitch(0);
-	(void) getreply(0);
-	(void) signal(SIGINT, oldintr);
+	(void)getreply(0);
+	(void)signal(SIGINT, oldintr);
 	pswitch(1);
 	ptflag = 0;
 	printf("local: %s remote: %s\n", local, remote);
 	return;
 abort:
-	(void) signal(SIGINT, SIG_IGN);
+	(void)signal(SIGINT, SIG_IGN);
 	ptflag = 0;
 	if (strcmp(cmd, "RETR") && !proxy)
 		pswitch(1);
@@ -1384,7 +1458,7 @@
 		pswitch(1);
 		if (ptabflg)
 			code = -1;
-		(void) signal(SIGINT, oldintr);
+		(void)signal(SIGINT, oldintr);
 		return;
 	}
 	if (cpend)
@@ -1398,7 +1472,7 @@
 			pswitch(1);
 			if (ptabflg)
 				code = -1;
-			(void) signal(SIGINT, oldintr);
+			(void)signal(SIGINT, oldintr);
 			return;
 		}
 	}
@@ -1416,15 +1490,15 @@
 				code = -1;
 			lostpeer();
 		}
-		(void) getreply(0);
-		(void) getreply(0);
+		(void)getreply(0);
+		(void)getreply(0);
 	}
 	if (proxy)
 		pswitch(0);
 	pswitch(1);
 	if (ptabflg)
 		code = -1;
-	(void) signal(SIGINT, oldintr);
+	(void)signal(SIGINT, oldintr);
 }
 
 void
@@ -1438,20 +1512,20 @@
 	FD_ZERO(&mask);
 	while (nfnd > 0) {
 		FD_SET(fileno(cin), &mask);
-		if ((nfnd = empty(&mask,0)) < 0) {
+		if ((nfnd = empty(&mask, 0)) < 0) {
 			warn("reset");
 			code = -1;
 			lostpeer();
 		}
 		else if (nfnd) {
-			(void) getreply(0);
+			(void)getreply(0);
 		}
 	}
 }
 
 char *
 gunique(local)
-	char *local;
+	const char *local;
 {
 	static char new[MAXPATHLEN];
 	char *cp = strrchr(local, '/');
@@ -1467,12 +1541,12 @@
 		warn("local: %s", local);
 		return ((char *) 0);
 	}
-	(void) strcpy(new, local);
+	(void)strcpy(new, local);
 	cp = new + strlen(new);
 	*cp++ = '.';
 	while (!d) {
 		if (++count == 100) {
-			printf("runique: can't find unique file name.\n");
+			puts("runique: can't find unique file name.");
 			return ((char *) 0);
 		}
 		*cp++ = ext;
@@ -1510,8 +1584,8 @@
 	sprintf(buf, "%c%c%c", IAC, IP, IAC);
 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
 		warn("abort");
-	fprintf(cout,"%cABOR\r\n", DM);
-	(void) fflush(cout);
+	fprintf(cout, "%cABOR\r\n", DM);
+	(void)fflush(cout);
 	FD_ZERO(&mask);
 	FD_SET(fileno(cin), &mask);
 	if (din) {
@@ -1531,7 +1605,7 @@
 	}
 	if (getreply(0) == ERROR && code == 552) {
 		/* 552 needed for nic style abort */
-		(void) getreply(0);
+		(void)getreply(0);
 	}
-	(void) getreply(0);
+	(void)getreply(0);
 }
diff --new-file -r -b -u src/usr.bin/ftp.old/ftp_var.h src/usr.bin/ftp/ftp_var.h
--- src/usr.bin/ftp.old/ftp_var.h	Sun Aug  6 07:07:11 1995
+++ src/usr.bin/ftp/ftp_var.h	Sun Mar  2 19:59:17 1997
@@ -1,3 +1,7 @@
+/*	$OpenBSD: ftp_var.h,v 1.8 1997/02/18 18:04:30 kstailey Exp $	*/
+/*	$NetBSD: ftp_var.h,v 1.13 1997/02/01 10:45:05 lukem Exp $	*/
+/*	$FreeBSD$	*/
+
 /*
  * Copyright (c) 1985, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -30,7 +34,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	@(#)ftp_var.h	8.3 (Berkeley) 4/2/94
+ *	@(#)ftp_var.h	8.4 (Berkeley) 10/9/94
  */
 
 /*
@@ -40,18 +44,34 @@
 #include <sys/param.h>
 #include <setjmp.h>
 
+#ifndef SMALLFTP
+#include <histedit.h>
+#endif /* !SMALLFTP */
+
+#include "stringlist.h"
 #include "extern.h"
 
+#define HASHBYTES	1024
+#define FTPBUFLEN	MAXPATHLEN + 200
+
+#define STALLTIME	5	/* # of seconds of no xfer before "stalling" */
+
+#define	FTP_PORT	21	/* default if getservbyname("ftp/tcp") fails */
+#define	HTTP_PORT	80	/* default if getservbyname("http/tcp") fails */
+
 /*
  * Options and other state info.
  */
 int	trace;			/* trace packets exchanged */
 int	hash;			/* print # for each buffer transferred */
+int	mark;			/* number of bytes between hashes */
 int	sendport;		/* use PORT cmd for each data connection */
 int	verbose;		/* print messages coming back from server */
 int	connected;		/* connected to server */
 int	fromatty;		/* input is from a terminal */
+int	use_editline;		/* use the editline(3) routine for input */
 int	interactive;		/* interactively prompt on m* cmds */
+int	confirmrest;		/* confirm rest of current m* cmd */
 int	debug;			/* debugging level */
 int	bell;			/* ring bell on cmd completion */
 int	doglob;			/* glob local file names */
@@ -63,6 +83,8 @@
 int	mcase;			/* map upper to lower case for mget names */
 int	ntflag;			/* use ntin ntout tables for name translation */
 int	mapflag;		/* use mapin mapout templates on file names */
+int	preserve;		/* preserve modification time on files */
+int	progress;		/* display transfer progress bar */
 int	code;			/* return/reply code for ftp command */
 int	crflag;			/* if 1, strip car. rets. on ascii gets */
 char	pasv[64];		/* passive port for proxy data connection */
@@ -84,21 +106,39 @@
 int	mode;			/* file transfer mode */
 char	bytename[32];		/* local byte size in ascii */
 int	bytesize;		/* local byte size in binary */
+int	anonftp;		/* automatic anonymous login */
+int	dirchange;		/* remote directory changed by cd command */
+int	retry_connect;		/* retry connect if failed */
+int	ttywidth;		/* width of tty */
+
+#ifndef SMALLFTP
+int	  editing;		/* command line editing enabled */
+EditLine *el;			/* editline(3) status structure */
+History  *hist;			/* editline(3) history structure */
+char	 *cursor_pos;		/* cursor position we're looking for */
+int	  cursor_argc;		/* location of cursor in margv */
+int	  cursor_argo;		/* offset of cursor in margv[cursor_argc] */
+#endif /* !SMALLFTP */
+
+off_t	bytes;			/* current # of bytes read */
+off_t	filesize;		/* size of file being transferred */
+char   *direction;		/* direction transfer is occurring */
 
 char	*hostname;		/* name of host connected to */
 int	unix_server;		/* server is unix, can use binary for ascii */
 int	unix_proxy;		/* proxy is unix, can use binary for ascii */
-
-struct	servent *sp;		/* service spec for tcp/ftp */
+int	ftpport;		/* port number to use for ftp connections */
+int	httpport;		/* port number to use for http connections */
 
 jmp_buf	toplevel;		/* non-local goto stuff for cmd scanner */
 
-char	line[200];		/* input line buffer */
+char	line[FTPBUFLEN];	/* input line buffer */
 char	*stringbase;		/* current scan point in line buffer */
-char	argbuf[200];		/* argument storage buffer */
+char	argbuf[FTPBUFLEN];	/* argument storage buffer */
 char	*argbase;		/* current storage point in arg buffer */
+StringList *marg_sl;		/* stringlist containing margv */
 int	margc;			/* count of arguments on input line */
-char	*margv[20];		/* args parsed from input line */
+#define margv (marg_sl->sl_str)	/* args parsed from input line */
 int     cpend;                  /* flag: if != 0, then pending server reply */
 int	mflag;			/* flag: if != 0, then active multi command */
 
@@ -113,6 +153,9 @@
 	char	c_bell;		/* give bell when command completes */
 	char	c_conn;		/* must be connected to use command */
 	char	c_proxy;	/* proxy server may execute */
+#ifndef SMALLFTP
+	char	*c_complete;	/* context sensitive completion list */
+#endif /* !SMALLFTP */
 	void	(*c_handler) __P((int, char **)); /* function to call */
 };
 
diff --new-file -r -b -u src/usr.bin/ftp.old/main.c src/usr.bin/ftp/main.c
--- src/usr.bin/ftp.old/main.c	Thu Nov 21 14:16:47 1996
+++ src/usr.bin/ftp/main.c	Sun Mar  2 19:57:42 1997
@@ -1,3 +1,6 @@
+/*	$OpenBSD: main.c,v 1.21 1997/02/18 18:04:31 kstailey Exp $	*/
+/*	$NetBSD: main.c,v 1.17 1997/02/01 10:45:07 lukem Exp $	*/
+
 /*
  * Copyright (c) 1985, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -38,27 +41,28 @@
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 4/3/94";
+#if 0
+static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 10/9/94";
+static char rcsid[] = "$OpenBSD: main.c,v 1.21 1997/02/18 18:04:31 kstailey Exp $";
+#else
+static char rcsid[] = "$FreeBSD$";
+#endif
 #endif /* not lint */
 
 /*
  * FTP User Program -- Command Interface.
  */
-/*#include <sys/ioctl.h>*/
 #include <sys/types.h>
 #include <sys/socket.h>
 
-#include <arpa/ftp.h>
-
 #include <ctype.h>
 #include <err.h>
 #include <netdb.h>
 #include <pwd.h>
-#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "ftp_var.h"
 
@@ -67,35 +71,57 @@
 	int argc;
 	char *argv[];
 {
-	int ch, top;
+	struct servent *sp;
+	int ch, top, port, rval;
 	struct passwd *pw = NULL;
 	char *cp, homedir[MAXPATHLEN];
-	struct servent sp_default;
 
 	sp = getservbyname("ftp", "tcp");
-	if (sp == 0) {
-		sp = &sp_default;
-		memset(sp, 0, sizeof *sp);
-		sp->s_port = htons(21);
-	}
+	if (sp == 0)
+		ftpport = htons(FTP_PORT);	/* good fallback */
+	else
+		ftpport = sp->s_port;
+	sp = getservbyname("http", "tcp");
+	if (sp == 0)
+		httpport = htons(HTTP_PORT);	/* good fallback */
+	else
+		httpport = sp->s_port;
 	doglob = 1;
 	interactive = 1;
 	autologin = 1;
 	passivemode = 0;
 	restricted_data_ports = 1;
+	preserve = 1;
+	verbose = 0;
+	progress = 0;
+	mark = HASHBYTES;
+	marg_sl = sl_init();
+	use_editline = 1;	/* use editline if possible */
 
 	cp = strrchr(argv[0], '/');
-	cp = (cp == NULL) ? argv[0] : cp+1;
+	cp = (cp == NULL) ? argv[0] : cp + 1;
 	if (getenv("FTP_PASSIVE_MODE") || strcmp(cp, "pftp") == 0)
 	    passivemode = 1;
 
-	while ((ch = getopt(argc, argv, "dginptvU")) != EOF) {
+	fromatty = isatty(fileno(stdin));
+	if (fromatty)
+		verbose = 1;		/* verbose if from a tty */
+
+	while ((ch = getopt(argc, argv, "adeginpPr:tUvV")) != -1) {
 		switch (ch) {
+		case 'a':
+			anonftp = 1;
+			break;
+
 		case 'd':
 			options |= SO_DEBUG;
 			debug++;
 			break;
 
+		case 'e':
+			use_editline = 0;
+			break;
+
 		case 'g':
 			doglob = 0;
 			break;
@@ -112,30 +138,40 @@
 			passivemode = 1;
 			break;
 
+		case 'P':
+			port = atoi(optarg);
+			if (port <= 0)
+				warnx("bad port number: %s (ignored)", optarg);
+			else
+				ftpport = htons(port);
+			break;
+
+		case 'r':
+			if (isdigit(*optarg))
+				retry_connect = atoi(optarg);
+			else
+				errx(1, "-r requires numeric argument");
+			break;
+
 		case 't':
-			trace++;
+			trace = 1;
 			break;
 
 		case 'v':
-			verbose++;
+			verbose = 1;
 			break;
 
-		case 'U':
-			restricted_data_ports = 0;
+		case 'V':
+			verbose = 0;
 			break;
 
 		default:
-			(void)fprintf(stderr,
-				"usage: ftp [-dginptv] [host [port]]\n");
-			exit(1);
+			usage();
 		}
 	}
 	argc -= optind;
 	argv += optind;
 
-	fromatty = isatty(fileno(stdin));
-	if (fromatty)
-		verbose++;
 	cpend = 0;	/* no pending replies */
 	proxy = 0;	/* proxy not active */
 	crflag = 1;	/* strip c.r. on ascii gets */
@@ -151,27 +187,69 @@
 		pw = getpwuid(getuid());
 	if (pw != NULL) {
 		home = homedir;
-		(void) strcpy(home, pw->pw_dir);
+		(void)strcpy(home, pw->pw_dir);
 	}
+
+#ifndef SMALLFTP
+	editing = 0;			/* command line editing off */
+	if (fromatty && use_editline) {
+		editing = 1;		/* editing mode on if a tty */
+		el = el_init(__progname, stdin, stdout); /* init editline */
+
+		hist = history_init();		/* init the builtin history */
+		history(hist, H_EVENT, 100);	/* remember 100 events */
+		el_set(el, EL_HIST, history, hist);	/* use history */
+
+		el_set(el, EL_EDITOR, "emacs");	/* default editor is emacs */
+		el_set(el, EL_PROMPT, prompt);	/* set the prompt function */
+
+		/* add local file completion, bind to TAB */
+		el_set(el, EL_ADDFN, "ftp-complete",
+		    "Context sensitive argument completion",
+		    complete);
+		el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
+
+		el_source(el, NULL);	/* read ~/.editrc */
+	}
+#endif /* !SMALLFTP */
+
+	setttywidth(0);
+	(void)signal(SIGWINCH, setttywidth);
+
 	if (argc > 0) {
+		if (strchr(argv[0], ':') != NULL) {
+			anonftp = 1;	/* Handle "automatic" transfers. */
+			rval = auto_fetch(argc, argv);
+			if (rval >= 0)		/* -1 == connected and cd-ed */
+				exit(rval);
+		} else {
 		char *xargv[5];
-		extern char *__progname;
 
 		if (setjmp(toplevel))
 			exit(0);
-		(void) signal(SIGINT, intr);
-		(void) signal(SIGPIPE, lostpeer);
+			(void)signal(SIGINT, (sig_t)intr);
+			(void)signal(SIGPIPE, (sig_t)lostpeer);
 		xargv[0] = __progname;
 		xargv[1] = argv[0];
 		xargv[2] = argv[1];
 		xargv[3] = argv[2];
 		xargv[4] = NULL;
+			do {
 		setpeer(argc+1, xargv);
+				if (!retry_connect)
+					break;
+				if (!connected) {
+					macnum = 0;
+					puts("Retrying...");
+					sleep(retry_connect);
+				}
+			} while (!connected);
+		}
 	}
 	top = setjmp(toplevel) == 0;
 	if (top) {
-		(void) signal(SIGINT, intr);
-		(void) signal(SIGPIPE, lostpeer);
+		(void)signal(SIGINT, (sig_t)intr);
+		(void)signal(SIGPIPE, (sig_t)lostpeer);
 	}
 	for (;;) {
 		cmdscanner(top);
@@ -183,6 +261,7 @@
 intr()
 {
 
+	alarmtimer(0);
 	longjmp(toplevel, 1);
 }
 
@@ -190,15 +269,16 @@
 lostpeer()
 {
 
+	alarmtimer(0);
 	if (connected) {
 		if (cout != NULL) {
-			(void) shutdown(fileno(cout), 1+1);
-			(void) fclose(cout);
+			(void)shutdown(fileno(cout), 1+1);
+			(void)fclose(cout);
 			cout = NULL;
 		}
 		if (data >= 0) {
-			(void) shutdown(data, 1+1);
-			(void) close(data);
+			(void)shutdown(data, 1+1);
+			(void)close(data);
 			data = -1;
 		}
 		connected = 0;
@@ -206,8 +286,8 @@
 	pswitch(1);
 	if (connected) {
 		if (cout != NULL) {
-			(void) shutdown(fileno(cout), 1+1);
-			(void) fclose(cout);
+			(void)shutdown(fileno(cout), 1+1);
+			(void)fclose(cout);
 			cout = NULL;
 		}
 		connected = 0;
@@ -217,25 +297,13 @@
 }
 
 /*
+ * Generate a prompt
+ */
 char *
-tail(filename)
-	char *filename;
+prompt()
 {
-	char *s;
-
-	while (*filename) {
-		s = strrchr(filename, '/');
-		if (s == NULL)
-			break;
-		if (s[1])
-			return (s + 1);
-		if (s == filename)
-			break;	XXX
-		*s = '\0';
-	}
-	return (filename);
+	return ("ftp> ");
 }
-*/
 
 /*
  * Command parser.
@@ -245,69 +313,107 @@
 	int top;
 {
 	struct cmd *c;
-	int l;
+	int num;
 
-	if (!top)
-		(void) putchar('\n');
+	if (!top 
+#ifndef SMALLFTP
+	    && !editing
+#endif /* !SMALLFTP */
+	    )
+		(void)putchar('\n');
 	for (;;) {
+#ifndef SMALLFTP
+		if (!editing) {
+#endif /* !SMALLFTP */
 		if (fromatty) {
-			printf("ftp> ");
-			(void) fflush(stdout);
+				fputs(prompt(), stdout);
+				(void)fflush(stdout);
 		}
-		if (fgets(line, sizeof line, stdin) == NULL)
+			if (fgets(line, sizeof(line), stdin) == NULL)
 			quit(0, 0);
-		l = strlen(line);
-		if (l == 0)
+			num = strlen(line);
+			if (num == 0)
 			break;
-		if (line[--l] == '\n') {
-			if (l == 0)
+			if (line[--num] == '\n') {
+				if (num == 0)
 				break;
-			line[l] = '\0';
-		} else if (l == sizeof(line) - 2) {
-			printf("sorry, input line too long\n");
-			while ((l = getchar()) != '\n' && l != EOF)
+				line[num] = '\0';
+			} else if (num == sizeof(line) - 2) {
+				puts("sorry, input line too long");
+				while ((num = getchar()) != '\n' && num != EOF)
 				/* void */;
 			break;
 		} /* else it was a line without a newline */
+#ifndef SMALLFTP
+		} else {
+			const char *buf;
+			cursor_pos = NULL;
+
+			if ((buf = el_gets(el, &num)) == NULL || num == 0)
+				quit(0, 0);
+			if (line[--num] == '\n') {
+				if (num == 0)
+					break;
+			} else if (num >= sizeof(line)) {
+				puts("sorry, input line too long");
+				break;
+			}
+			memcpy(line, buf, num);
+			line[num] = '\0';
+			history(hist, H_ENTER, buf);
+		}
+#endif /* !SMALLFTP */
+
 		makeargv();
-		if (margc == 0) {
+		if (margc == 0)
 			continue;
-		}
+#if 0 && !defined(SMALLFTP)	/* XXX: don't want el_parse */
+		/*
+		 * el_parse returns -1 to signal that it's not been handled
+		 * internally.
+		 */
+		if (el_parse(el, margc, margv) != -1)
+			continue;
+#endif /* !SMALLFTP */
 		c = getcmd(margv[0]);
 		if (c == (struct cmd *)-1) {
-			printf("?Ambiguous command\n");
+			puts("?Ambiguous command");
 			continue;
 		}
 		if (c == 0) {
-			printf("?Invalid command\n");
+			puts("?Invalid command");
 			continue;
 		}
 		if (c->c_conn && !connected) {
-			printf("Not connected.\n");
+			puts("Not connected.");
 			continue;
 		}
+		confirmrest = 0;
 		(*c->c_handler)(margc, margv);
 		if (bell && c->c_bell)
-			(void) putchar('\007');
+			(void)putchar('\007');
 		if (c->c_handler != help)
 			break;
 	}
-	(void) signal(SIGINT, intr);
-	(void) signal(SIGPIPE, lostpeer);
+	(void)signal(SIGINT, (sig_t)intr);
+	(void)signal(SIGPIPE, (sig_t)lostpeer);
 }
 
 struct cmd *
 getcmd(name)
-	char *name;
+	const char *name;
 {
-	char *p, *q;
+	const char *p, *q;
 	struct cmd *c, *found;
 	int nmatches, longest;
 
+	if (name == NULL)
+		return (0);
+
 	longest = 0;
 	nmatches = 0;
 	found = 0;
-	for (c = cmdtab; p = c->c_name; c++) {
+	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
 		for (q = name; *q == *p++; q++)
 			if (*q == 0)		/* exact match? */
 				return (c);
@@ -334,17 +440,41 @@
 void
 makeargv()
 {
-	char **argp;
+	char *argp;
 
-	margc = 0;
-	argp = margv;
 	stringbase = line;		/* scan from first of buffer */
 	argbase = argbuf;		/* store from first of buffer */
 	slrflag = 0;
-	while (*argp++ = slurpstring())
-		margc++;
+	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
+	for (margc = 0; ; margc++) {
+		argp = slurpstring();
+		sl_add(marg_sl, argp);
+		if (argp == NULL)
+			break;
+	}
+#ifndef SMALLFTP
+	if (cursor_pos == line) {
+		cursor_argc = 0;
+		cursor_argo = 0;
+	} else if (cursor_pos != NULL) {
+		cursor_argc = margc;
+		cursor_argo = strlen(margv[margc-1]);
+	}
+#endif /* !SMALLFTP */
 }
 
+#ifdef SMALLFTP
+#define INC_CHKCURSOR(x)	(x)++
+#else  /* !SMALLFTP */
+#define INC_CHKCURSOR(x)	{ (x)++ ; \
+				if (x == cursor_pos) { \
+					cursor_argc = margc; \
+					cursor_argo = ap-argbase; \
+					cursor_pos = NULL; \
+				} }
+						
+#endif /* !SMALLFTP */
+
 /*
  * Parse string into argbuf;
  * implemented with FSM to
@@ -362,7 +492,7 @@
 		switch (slrflag) {	/* and $ as token for macro invoke */
 			case 0:
 				slrflag++;
-				stringbase++;
+				INC_CHKCURSOR(stringbase);
 				return ((*sb == '!') ? "!" : "$");
 				/* NOTREACHED */
 			case 1:
@@ -382,7 +512,8 @@
 
 	case ' ':
 	case '\t':
-		sb++; goto S0;
+		INC_CHKCURSOR(sb);
+		goto S0;
 
 	default:
 		switch (slrflag) {
@@ -408,13 +539,17 @@
 		goto OUT;	/* end of token */
 
 	case '\\':
-		sb++; goto S2;	/* slurp next character */
+		INC_CHKCURSOR(sb);
+		goto S2;	/* slurp next character */
 
 	case '"':
-		sb++; goto S3;	/* slurp quoted string */
+		INC_CHKCURSOR(sb);
+		goto S3;	/* slurp quoted string */
 
 	default:
-		*ap++ = *sb++;	/* add character to token */
+		*ap = *sb;	/* add character to token */
+		ap++;
+		INC_CHKCURSOR(sb);
 		got_one = 1;
 		goto S1;
 	}
@@ -426,7 +561,9 @@
 		goto OUT;
 
 	default:
-		*ap++ = *sb++;
+		*ap = *sb;
+		ap++;
+		INC_CHKCURSOR(sb);
 		got_one = 1;
 		goto S1;
 	}
@@ -438,10 +575,13 @@
 		goto OUT;
 
 	case '"':
-		sb++; goto S1;
+		INC_CHKCURSOR(sb);
+		goto S1;
 
 	default:
-		*ap++ = *sb++;
+		*ap = *sb;
+		ap++;
+		INC_CHKCURSOR(sb);
 		got_one = 1;
 		goto S3;
 	}
@@ -468,8 +608,6 @@
 	return ((char *)0);
 }
 
-#define HELPINDENT ((int) sizeof ("directory"))
-
 /*
  * Help command.
  * Call each command handler with argc == 0 and argv[0] == name.
@@ -482,47 +620,24 @@
 	struct cmd *c;
 
 	if (argc == 1) {
-		int i, j, w, k;
-		int columns, width = 0, lines;
+		StringList *buf;
 
-		printf("Commands may be abbreviated.  Commands are:\n\n");
-		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
-			int len = strlen(c->c_name);
-
-			if (len > width)
-				width = len;
-		}
-		width = (width + 8) &~ 7;
-		columns = 80 / width;
-		if (columns == 0)
-			columns = 1;
-		lines = (NCMDS + columns - 1) / columns;
-		for (i = 0; i < lines; i++) {
-			for (j = 0; j < columns; j++) {
-				c = cmdtab + j * lines + i;
-				if (c->c_name && (!proxy || c->c_proxy)) {
-					printf("%s", c->c_name);
-				}
-				else if (c->c_name) {
-					for (k=0; k < strlen(c->c_name); k++) {
-						(void) putchar(' ');
-					}
-				}
-				if (c + lines >= &cmdtab[NCMDS]) {
-					printf("\n");
-					break;
-				}
-				w = strlen(c->c_name);
-				while (w < width) {
-					w = (w + 8) &~ 7;
-					(void) putchar('\t');
-				}
-			}
-		}
+		buf = sl_init();
+		printf("%sommands may be abbreviated.  Commands are:\n\n",
+		    proxy ? "Proxy c" : "C");
+		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
+			if (c->c_name && (!proxy || c->c_proxy))
+				sl_add(buf, c->c_name);
+		list_vertical(buf);
+		sl_free(buf, 0);
 		return;
 	}
+
+#define HELPINDENT ((int) sizeof("disconnect"))
+
 	while (--argc > 0) {
 		char *arg;
+
 		arg = *++argv;
 		c = getcmd(arg);
 		if (c == (struct cmd *)-1)
@@ -533,4 +648,16 @@
 			printf("%-*s\t%s\n", HELPINDENT,
 				c->c_name, c->c_help);
 	}
+}
+
+void
+usage()
+{
+	(void)fprintf(stderr,
+	    "usage: %s [-adginprtUvV] [host [port]]\n"
+	    "       %s host:path[/]\n"
+	    "       %s ftp://host[:port]/path[/]\n"
+	    "       %s http://host[:port]/file\n",
+	    __progname, __progname, __progname, __progname);
+	exit(1);
 }
diff --new-file -r -b -u src/usr.bin/ftp.old/pathnames.h src/usr.bin/ftp/pathnames.h
--- src/usr.bin/ftp.old/pathnames.h	Fri May 27 22:31:25 1994
+++ src/usr.bin/ftp/pathnames.h	Sun Mar  2 19:59:25 1997
@@ -1,3 +1,7 @@
+/*	$OpenBSD: pathnames.h,v 1.5 1997/02/03 01:05:43 millert Exp $	*/
+/*	$NetBSD: pathnames.h,v 1.7 1997/01/09 20:19:40 tls Exp $	*/
+/*	$FreeBSD$	*/
+
 /*
  * Copyright (c) 1989, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -35,5 +39,4 @@
 
 #include <paths.h>
 
-#undef _PATH_TMP
-#define	_PATH_TMP	"/tmp/ftpXXXXXX"
+#define	TMPFILE		"ftpXXXXXX"
diff --new-file -r -b -u src/usr.bin/ftp.old/ruserpass.c src/usr.bin/ftp/ruserpass.c
--- src/usr.bin/ftp.old/ruserpass.c	Tue May 30 21:06:17 1995
+++ src/usr.bin/ftp/ruserpass.c	Sun Mar  2 19:57:53 1997
@@ -1,3 +1,6 @@
+/*	$OpenBSD: ruserpass.c,v 1.5 1997/02/03 01:05:44 millert Exp $	*/
+/*	$NetBSD: ruserpass.c,v 1.11 1997/01/19 14:19:16 lukem Exp $	*/
+
 /*
  * Copyright (c) 1985, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -32,7 +35,12 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)ruserpass.c	8.3 (Berkeley) 4/2/94";
+#if 0
+static char sccsid[] = "@(#)ruserpass.c	8.4 (Berkeley) 4/27/95";
+static char rcsid[] = "$OpenBSD: ruserpass.c,v 1.5 1997/02/03 01:05:44 millert Exp $";
+#else
+static char rcsid[] = "$FreeBSD$";
+#endif
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -77,7 +85,8 @@
 
 int
 ruserpass(host, aname, apass, aacct)
-	char *host, **aname, **apass, **aacct;
+	const char *host;
+	char **aname, **apass, **aacct;
 {
 	char *hdir, buf[BUFSIZ], *tmp;
 	char myname[MAXHOSTNAMELEN], *mydomain;
@@ -87,7 +96,12 @@
 	hdir = getenv("HOME");
 	if (hdir == NULL)
 		hdir = ".";
-	(void) sprintf(buf, "%s/.netrc", hdir);
+	if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
+		(void)sprintf(buf, "%s/.netrc", hdir);
+	} else {
+		warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
+		return (0);
+	}
 	cfile = fopen(buf, "r");
 	if (cfile == NULL) {
 		if (errno != ENOENT)
@@ -136,8 +150,9 @@
 		case LOGIN:
 			if (token())
 				if (*aname == 0) {
-					*aname = malloc((unsigned) strlen(tokval) + 1);
-					(void) strcpy(*aname, tokval);
+					*aname = malloc((unsigned)
+					    strlen(tokval) + 1);
+					(void)strcpy(*aname, tokval);
 				} else {
 					if (strcmp(*aname, tokval))
 						goto next;
@@ -153,7 +168,7 @@
 			}
 			if (token() && *apass == 0) {
 				*apass = malloc((unsigned) strlen(tokval) + 1);
-				(void) strcpy(*apass, tokval);
+				(void)strcpy(*apass, tokval);
 			}
 			break;
 		case ACCOUNT:
@@ -165,21 +180,24 @@
 			}
 			if (token() && *aacct == 0) {
 				*aacct = malloc((unsigned) strlen(tokval) + 1);
-				(void) strcpy(*aacct, tokval);
+				(void)strcpy(*aacct, tokval);
 			}
 			break;
 		case MACDEF:
 			if (proxy) {
-				(void) fclose(cfile);
+				(void)fclose(cfile);
 				return (0);
 			}
-			while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
+			while ((c=getc(cfile)) != EOF)
+				if (c != ' ' && c != '\t')
+					break;
 			if (c == EOF || c == '\n') {
-				printf("Missing macdef name argument.\n");
+				puts("Missing macdef name argument.");
 				goto bad;
 			}
 			if (macnum == 16) {
-				printf("Limit of 16 macros have already been defined\n");
+				puts(
+"Limit of 16 macros have already been defined");
 				goto bad;
 			}
 			tmp = macros[macnum].mac_name;
@@ -189,7 +207,8 @@
 				*tmp++ = c;
 			}
 			if (c == EOF) {
-				printf("Macro definition missing null line terminator.\n");
+				puts(
+"Macro definition missing null line terminator.");
 				goto bad;
 			}
 			*tmp = '\0';
@@ -197,19 +216,22 @@
 				while ((c=getc(cfile)) != EOF && c != '\n');
 			}
 			if (c == EOF) {
-				printf("Macro definition missing null line terminator.\n");
+				puts(
+"Macro definition missing null line terminator.");
 				goto bad;
 			}
 			if (macnum == 0) {
 				macros[macnum].mac_start = macbuf;
 			}
 			else {
-				macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
+				macros[macnum].mac_start =
+				    macros[macnum-1].mac_end + 1;
 			}
 			tmp = macros[macnum].mac_start;
 			while (tmp != macbuf + 4096) {
 				if ((c=getc(cfile)) == EOF) {
-				printf("Macro definition missing null line terminator.\n");
+				puts(
+"Macro definition missing null line terminator.");
 					goto bad;
 				}
 				*tmp = c;
@@ -223,7 +245,7 @@
 				tmp++;
 			}
 			if (tmp == macbuf + 4096) {
-				printf("4K macro buffer exceeded\n");
+				puts("4K macro buffer exceeded");
 				goto bad;
 			}
 			break;
@@ -234,10 +256,10 @@
 		goto done;
 	}
 done:
-	(void) fclose(cfile);
+	(void)fclose(cfile);
 	return (0);
 bad:
-	(void) fclose(cfile);
+	(void)fclose(cfile);
 	return (-1);
 }
 
diff --new-file -r -b -u src/usr.bin/ftp.old/stringlist.c src/usr.bin/ftp/stringlist.c
--- src/usr.bin/ftp.old/stringlist.c	Thu Jan  1 10:00:00 1970
+++ src/usr.bin/ftp/stringlist.c	Sun Mar  2 19:58:12 1997
@@ -0,0 +1,125 @@
+/*	$OpenBSD: stringlist.c,v 1.1 1997/02/03 01:05:44 millert Exp $	*/
+/*	$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $	*/
+
+/*
+ * Copyright (c) 1994 Christos Zoulas
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by Christos Zoulas.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char *rcsid = "$OpenBSD: stringlist.c,v 1.1 1997/02/03 01:05:44 millert Exp $";
+#endif
+static char rcsid[] = "$FreeBSD$";
+#endif /* LIBC_SCCS and not lint */
+
+#include <stdio.h>
+#include <string.h>
+#include <err.h>
+#include <stdlib.h>
+
+#include "stringlist.h"
+
+#define _SL_CHUNKSIZE	20
+
+/*
+ * sl_init(): Initialize a string list
+ */
+StringList *
+sl_init()
+{
+	StringList *sl = malloc(sizeof(StringList));
+	if (sl == NULL)
+		err(1, "stringlist: %m");
+
+	sl->sl_cur = 0;
+	sl->sl_max = _SL_CHUNKSIZE;
+	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
+	if (sl->sl_str == NULL)
+		err(1, "stringlist: %m");
+	return sl;
+}
+
+
+/*
+ * sl_add(): Add an item to the string list
+ */
+void
+sl_add(sl, name)
+	StringList *sl;
+	char *name;
+{
+	if (sl->sl_cur == sl->sl_max - 1) {
+		sl->sl_max += _SL_CHUNKSIZE;
+		sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *));
+		if (sl->sl_str == NULL)
+			err(1, "stringlist: %m");
+	}
+	sl->sl_str[sl->sl_cur++] = name;
+}
+
+
+/*
+ * sl_free(): Free a stringlist
+ */
+void
+sl_free(sl, all)
+	StringList *sl;
+	int all;
+{
+	size_t i;
+
+	if (sl == NULL)
+		return;
+	if (sl->sl_str) {
+		if (all)
+			for (i = 0; i < sl->sl_cur; i++)
+				free(sl->sl_str[i]);
+		free(sl->sl_str);
+	}
+	free(sl);
+}
+
+
+/*
+ * sl_find(): Find a name in the string list
+ */
+char *
+sl_find(sl, name)
+	StringList *sl;
+	char *name;
+{
+	size_t i;
+
+	for (i = 0; i < sl->sl_cur; i++)
+		if (strcmp(sl->sl_str[i], name) == 0)
+			return sl->sl_str[i];
+
+	return NULL;
+}
diff --new-file -r -b -u src/usr.bin/ftp.old/stringlist.h src/usr.bin/ftp/stringlist.h
--- src/usr.bin/ftp.old/stringlist.h	Thu Jan  1 10:00:00 1970
+++ src/usr.bin/ftp/stringlist.h	Sun Mar  2 19:59:36 1997
@@ -0,0 +1,58 @@
+/*	$OpenBSD: stringlist.h,v 1.1 1997/02/03 01:05:45 millert Exp $	*/
+/*	$NetBSD: stringlist.h,v 1.2 1997/01/17 06:11:36 lukem Exp $	*/
+/*	$FreeBSD$	*/
+
+/*
+ * Copyright (c) 1994 Christos Zoulas
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by Christos Zoulas.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _STRINGLIST_H
+#define _STRINGLIST_H 
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+/*
+ * Simple string list
+ */
+typedef struct _stringlist {
+	char	**sl_str;
+	size_t	  sl_max;
+	size_t	  sl_cur;
+} StringList;
+
+__BEGIN_DECLS
+StringList	*sl_init	__P((void));
+void		 sl_add		__P((StringList *, char *));
+void		 sl_free	__P((StringList *, int));
+char		*sl_find	__P((StringList *, char *));
+__END_DECLS
+
+#endif /* _STRINGLIST_H */
diff --new-file -r -b -u src/usr.bin/ftp.old/util.c src/usr.bin/ftp/util.c
--- src/usr.bin/ftp.old/util.c	Thu Jan  1 10:00:00 1970
+++ src/usr.bin/ftp/util.c	Sun Mar  2 19:58:24 1997
@@ -0,0 +1,638 @@
+/*	$OpenBSD: util.c,v 1.3 1997/02/05 04:55:21 millert Exp $	*/
+/*	$NetBSD: util.c,v 1.4 1997/02/01 11:26:34 lukem Exp $	*/
+
+/*
+ * Copyright (c) 1985, 1989, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char rcsid[] = "$OpenBSD: util.c,v 1.3 1997/02/05 04:55:21 millert Exp $";
+#endif
+static char rcsid[] = "$FreeBSD$";
+#endif /* not lint */
+
+/*
+ * FTP User Program -- Misc support routines
+ */
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <arpa/ftp.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <glob.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "ftp_var.h"
+#include "pathnames.h"
+
+/*
+ * Connect to peer server and
+ * auto-login, if possible.
+ */
+void
+setpeer(argc, argv)
+	int argc;
+	char *argv[];
+{
+	char *host;
+	short port;
+
+	if (connected) {
+		printf("Already connected to %s, use close first.\n",
+		    hostname);
+		code = -1;
+		return;
+	}
+	if (argc < 2)
+		(void)another(&argc, &argv, "to");
+	if (argc < 2 || argc > 3) {
+		printf("usage: %s host-name [port]\n", argv[0]);
+		code = -1;
+		return;
+	}
+	port = ftpport;
+	if (argc > 2) {
+		port = atoi(argv[2]);
+		if (port <= 0) {
+			printf("%s: bad port number-- %s\n", argv[1], argv[2]);
+			printf ("usage: %s host-name [port]\n", argv[0]);
+			code = -1;
+			return;
+		}
+		port = htons(port);
+	}
+	host = hookup(argv[1], port);
+	if (host) {
+		int overbose;
+
+		connected = 1;
+		/*
+		 * Set up defaults for FTP.
+		 */
+		(void)strcpy(typename, "ascii"), type = TYPE_A;
+		curtype = TYPE_A;
+		(void)strcpy(formname, "non-print"), form = FORM_N;
+		(void)strcpy(modename, "stream"), mode = MODE_S;
+		(void)strcpy(structname, "file"), stru = STRU_F;
+		(void)strcpy(bytename, "8"), bytesize = 8;
+		if (autologin)
+			(void)login(argv[1]);
+
+#if (defined(unix) || defined(BSD)) && NBBY == 8
+/*
+ * this ifdef is to keep someone form "porting" this to an incompatible
+ * system and not checking this out. This way they have to think about it.
+ */
+		overbose = verbose;
+		if (debug == 0)
+			verbose = -1;
+		if (command("SYST") == COMPLETE && overbose) {
+			char *cp, c;
+			c = 0;
+			cp = strchr(reply_string+4, ' ');
+			if (cp == NULL)
+				cp = strchr(reply_string+4, '\r');
+			if (cp) {
+				if (cp[-1] == '.')
+					cp--;
+				c = *cp;
+				*cp = '\0';
+			}
+
+			printf("Remote system type is %s.\n", reply_string + 4);
+			if (cp)
+				*cp = c;
+		}
+		if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
+			if (proxy)
+				unix_proxy = 1;
+			else
+				unix_server = 1;
+			/*
+			 * Set type to 0 (not specified by user),
+			 * meaning binary by default, but don't bother
+			 * telling server.  We can use binary
+			 * for text files unless changed by the user.
+			 */
+			type = 0;
+			(void)strcpy(typename, "binary");
+			if (overbose)
+			    printf("Using %s mode to transfer files.\n",
+				typename);
+		} else {
+			if (proxy)
+				unix_proxy = 0;
+			else
+				unix_server = 0;
+			if (overbose &&
+			    !strncmp(reply_string, "215 TOPS20", 10))
+				puts(
+"Remember to set tenex mode when transferring binary files from this machine.");
+		}
+		verbose = overbose;
+#endif /* unix || BSD */
+	}
+}
+
+/*
+ * `Another' gets another argument, and stores the new argc and argv.
+ * It reverts to the top level (via main.c's intr()) on EOF/error.
+ *
+ * Returns false if no new arguments have been added.
+ */
+int
+another(pargc, pargv, prompt)
+	int *pargc;
+	char ***pargv;
+	const char *prompt;
+{
+	int len = strlen(line), ret;
+
+	if (len >= sizeof(line) - 3) {
+		puts("sorry, arguments too long");
+		intr();
+	}
+	printf("(%s) ", prompt);
+	line[len++] = ' ';
+	if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
+		intr();
+	len += strlen(&line[len]);
+	if (len > 0 && line[len - 1] == '\n')
+		line[len - 1] = '\0';
+	makeargv();
+	ret = margc > *pargc;
+	*pargc = margc;
+	*pargv = margv;
+	return (ret);
+}
+
+char *
+remglob(argv, doswitch)
+        char *argv[];
+        int doswitch;
+{
+        char temp[MAXPATHLEN];
+        static char buf[MAXPATHLEN];
+        static FILE *ftemp = NULL;
+        static char **args;
+        int oldverbose, oldhash, fd;
+        char *cp, *mode;
+
+        if (!mflag) {
+                if (!doglob) {
+                        args = NULL;
+                }
+                else {
+                        if (ftemp) {
+                                (void)fclose(ftemp);
+                                ftemp = NULL;
+                        }
+                }
+                return (NULL);
+        }
+        if (!doglob) {
+                if (args == NULL)
+                        args = argv;
+                if ((cp = *++args) == NULL)
+                        args = NULL;
+                return (cp);
+        }
+        if (ftemp == NULL) {
+		int len;
+
+		if ((cp = getenv("TMPDIR")) == NULL)
+		    cp = _PATH_TMP;
+		len = strlen(cp);
+		if (len + sizeof(TMPFILE) + (cp[len-1] != '/') > sizeof(temp)) {
+			warnx("unable to create temporary file: %s",
+			    strerror(ENAMETOOLONG));
+			return (NULL);
+		}
+
+		(void)strcpy(temp, cp);
+		if (temp[len-1] != '/')
+			temp[len++] = '/';
+		(void)strcpy(&temp[len], TMPFILE);
+;
+                if ((fd = mkstemp(temp)) < 0) {
+                        warn("unable to create temporary file %s", temp);
+                        return (NULL);
+                }
+                close(fd);
+                oldverbose = verbose, verbose = 0;
+                oldhash = hash, hash = 0;
+                if (doswitch) {
+                        pswitch(!proxy);
+                }
+                for (mode = "w"; *++argv != NULL; mode = "a")
+                        recvrequest ("NLST", temp, *argv, mode, 0);
+                if (doswitch) {
+                        pswitch(!proxy);
+                }
+                verbose = oldverbose; hash = oldhash;
+                ftemp = fopen(temp, "r");
+                (void)unlink(temp);
+                if (ftemp == NULL) {
+                        puts("can't find list of remote files, oops");
+                        return (NULL);
+                }
+        }
+        if (fgets(buf, sizeof(buf), ftemp) == NULL) {
+                (void)fclose(ftemp), ftemp = NULL;
+                return (NULL);
+        }
+        if ((cp = strchr(buf, '\n')) != NULL)
+                *cp = '\0';
+        return (buf);
+}
+
+int
+confirm(cmd, file)
+	const char *cmd, *file;
+{
+	char line[BUFSIZ];
+
+	if (!interactive || confirmrest)
+		return (1);
+	printf("%s %s? ", cmd, file);
+	(void)fflush(stdout);
+	if (fgets(line, sizeof(line), stdin) == NULL)
+		return (0);
+	switch (tolower(*line)) {
+		case 'n':
+			return (0);
+		case 'p':
+			interactive = 0;
+			puts("Interactive mode: off");
+			break;
+		case 'a':
+			confirmrest = 1;
+			printf("Prompting off for duration of %s\n", cmd);
+			break;
+	}
+	return (1);
+}
+
+/*
+ * Glob a local file name specification with
+ * the expectation of a single return value.
+ * Can't control multiple values being expanded
+ * from the expression, we return only the first.
+ */
+int
+globulize(cpp)
+	char **cpp;
+{
+	glob_t gl;
+	int flags;
+
+	if (!doglob)
+		return (1);
+
+	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
+	memset(&gl, 0, sizeof(gl));
+	if (glob(*cpp, flags, NULL, &gl) ||
+	    gl.gl_pathc == 0) {
+		warnx("%s: not found", *cpp);
+		globfree(&gl);
+		return (0);
+	}
+	*cpp = strdup(gl.gl_pathv[0]);	/* XXX - wasted memory */
+	globfree(&gl);
+	return (1);
+}
+
+/*
+ * determine size of remote file
+ */
+off_t
+remotesize(file, noisy)
+	const char *file;
+	int noisy;
+{
+	int overbose;
+	off_t size;
+
+	overbose = verbose;
+	size = -1;
+	if (debug == 0)
+		verbose = -1;
+	if (command("SIZE %s", file) == COMPLETE)
+		sscanf(reply_string, "%*s %qd", &size);
+	else if (noisy && debug == 0)
+		printf("%s\n", reply_string);
+	verbose = overbose;
+	return (size);
+}
+
+/*
+ * determine last modification time (in GMT) of remote file
+ */
+time_t
+remotemodtime(file, noisy)
+	const char *file;
+	int noisy;
+{
+	int overbose;
+	time_t rtime;
+
+	overbose = verbose;
+	rtime = -1;
+	if (debug == 0)
+		verbose = -1;
+	if (command("MDTM %s", file) == COMPLETE) {
+		struct tm timebuf;
+		int yy, mo, day, hour, min, sec;
+		sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
+			&day, &hour, &min, &sec);
+		memset(&timebuf, 0, sizeof(timebuf));
+		timebuf.tm_sec = sec;
+		timebuf.tm_min = min;
+		timebuf.tm_hour = hour;
+		timebuf.tm_mday = day;
+		timebuf.tm_mon = mo - 1;
+		timebuf.tm_year = yy - 1900;
+		timebuf.tm_isdst = -1;
+		rtime = mktime(&timebuf);
+		if (rtime == -1 && (noisy || debug != 0))
+			printf("Can't convert %s to a time\n", reply_string);
+		else
+			rtime += timebuf.tm_gmtoff;	/* conv. local -> GMT */
+	} else if (noisy && debug == 0)
+		puts(reply_string);
+	verbose = overbose;
+	return (rtime);
+}
+
+void
+updateprogressmeter()
+{
+
+	progressmeter(0);
+}
+
+/*
+ * Display a transfer progress bar if progress is non-zero.
+ * SIGALRM is hijacked for use by this function.
+ * - Before the transfer, set filesize to size of file (or -1 if unknown),
+ *   and call with flag = -1. This starts the once per second timer,
+ *   and a call to updateprogressmeter() upon SIGALRM.
+ * - During the transfer, updateprogressmeter will call progressmeter
+ *   with flag = 0
+ * - After the transfer, call with flag = 1
+ */
+static struct timeval start;
+
+void
+progressmeter(flag)
+	int flag;
+{
+	/*
+	 * List of order of magnitude prefixes.
+	 * The last is `P', as 2^64 = 16384 Petabytes
+	 */
+	static const char prefixes[] = " KMGTP";
+
+	static struct timeval lastupdate;
+	static off_t lastsize;
+	struct timeval now, td, wait;
+	off_t cursize, abbrevsize;
+	double elapsed;
+	int ratio, barlength, i, remaining;
+	char buf[256];
+
+	if (flag == -1) {
+		(void)gettimeofday(&start, (struct timezone *)0);
+		lastupdate = start;
+		lastsize = restart_point;
+	}
+	(void)gettimeofday(&now, (struct timezone *)0);
+	if (!progress || filesize <= 0)
+		return;
+	cursize = bytes + restart_point;
+
+	ratio = cursize * 100 / filesize;
+	ratio = MAX(ratio, 0);
+	ratio = MIN(ratio, 100);
+	snprintf(buf, sizeof(buf), "\r%3d%% ", ratio);
+
+	barlength = ttywidth - 30;
+	if (barlength > 0) {
+		i = barlength * ratio / 100;
+		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+		    "|%.*s%*s|", i, 
+"*****************************************************************************"
+"*****************************************************************************",
+		    barlength - i, "");
+	}
+
+	i = 0;
+	abbrevsize = cursize;
+	while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
+		i++;
+		abbrevsize >>= 10;
+	}
+	snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+	    " %5qd %c%c ", abbrevsize, prefixes[i],
+	    prefixes[i] == ' ' ? ' ' : 'B');
+
+	timersub(&now, &lastupdate, &wait);
+	if (cursize > lastsize) {
+		lastupdate = now;
+		lastsize = cursize;
+		if (wait.tv_sec >= STALLTIME) {	/* fudge out stalled time */
+			start.tv_sec += wait.tv_sec;
+			start.tv_usec += wait.tv_usec;
+		}
+		wait.tv_sec = 0;
+	}
+
+	timersub(&now, &start, &td);
+	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
+
+	if (bytes <= 0 || elapsed <= 0.0) {
+		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+		    "   --:-- ETA");
+	} else if (wait.tv_sec >= STALLTIME) {
+		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+		    " - stalled -");
+	} else {
+		remaining = (int)((filesize - restart_point) /
+				  (bytes / elapsed) - elapsed);
+		i = remaining / 3600;
+		if (i)
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+			    "%2d:", i);
+		else
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+			    "   ");
+		i = remaining % 3600;
+		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+		    "%02d:%02d ETA", i / 60, i % 60);
+	}
+	(void)write(STDOUT_FILENO, buf, strlen(buf));
+
+	if (flag == -1) {
+		(void)signal(SIGALRM, updateprogressmeter);
+		alarmtimer(1);		/* set alarm timer for 1 Hz */
+	} else if (flag == 1) {
+		alarmtimer(0);
+		(void)putchar('\n');
+	}
+	fflush(stdout);
+}
+
+/*
+ * Display transfer statistics.
+ * Requires start to be initialised by progressmeter(-1),
+ * direction to be defined by xfer routines, and filesize and bytes
+ * to be updated by xfer routines
+ * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
+ * instead of STDOUT.
+ */
+void
+ptransfer(siginfo)
+	int siginfo;
+{
+	struct timeval now, td;
+	double elapsed;
+	off_t bs;
+	int meg, remaining, hh;
+	char buf[100];
+
+	if (!verbose && !siginfo)
+		return;
+
+	(void)gettimeofday(&now, (struct timezone *)0);
+	timersub(&now, &start, &td);
+	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
+	bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
+	meg = 0;
+	if (bs > (1024 * 1024))
+		meg = 1;
+	(void)snprintf(buf, sizeof(buf),
+	    "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
+	    bytes, bytes == 1 ? "" : "s", direction, elapsed,
+	    bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
+	if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0) {
+		remaining = (int)((filesize - restart_point) /
+				  (bytes / elapsed) - elapsed);
+		hh = remaining / 3600;
+		remaining %= 3600;
+		snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
+		    "  ETA: %02d:%02d:%02d\n", hh, remaining / 60,
+		    remaining % 60);
+	}
+	(void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf));
+}
+
+/*
+ * List words in stringlist, vertically arranged
+ */
+void
+list_vertical(sl)
+	StringList *sl;
+{
+	int i, j, w;
+	int columns, width, lines, items;
+	char *p;
+
+	width = items = 0;
+
+	for (i = 0 ; i < sl->sl_cur ; i++) {
+		w = strlen(sl->sl_str[i]);
+		if (w > width)
+			width = w;
+	}
+	width = (width + 8) &~ 7;
+
+	columns = ttywidth / width;
+	if (columns == 0)
+		columns = 1;
+	lines = (sl->sl_cur + columns - 1) / columns;
+	for (i = 0; i < lines; i++) {
+		for (j = 0; j < columns; j++) {
+			p = sl->sl_str[j * lines + i];
+			if (p)
+				fputs(p, stdout);
+			if (j * lines + i + lines >= sl->sl_cur) {
+				putchar('\n');
+				break;
+			}
+			w = strlen(p);
+			while (w < width) {
+				w = (w + 8) &~ 7;
+				(void)putchar('\t');
+			}
+		}
+	}
+}
+
+/*
+ * Update the global ttywidth value, using TIOCGWINSZ.
+ */
+void
+setttywidth(a)
+	int a;
+{
+	struct winsize winsize;
+
+	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
+		ttywidth = winsize.ws_col;
+	else
+		ttywidth = 80;
+}
+
+/*
+ * Set the SIGALRM interval timer for wait seconds, 0 to disable.
+ */
+
+void
+alarmtimer(wait)
+	int wait;
+{
+	struct itimerval itv;
+
+	itv.it_value.tv_sec = wait;
+	itv.it_value.tv_usec = 0;
+	itv.it_interval = itv.it_value;
+	setitimer(ITIMER_REAL, &itv, NULL);
+}
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: gpalmer 
Responsible-Changed-When: Sun Mar 2 15:53:27 PST 1997 
Responsible-Changed-Why:  
Misfiled PR 
State-Changed-From-To: open->closed 
State-Changed-By: joerg 
State-Changed-When: Sat Aug 23 18:11:55 MEST 1997 
State-Changed-Why:  

Basically handled by Mike Smith. 
>Unformatted:
