Module Name:    src
Committed By:   kre
Date:           Mon Nov 20 21:11:37 UTC 2017

Modified Files:
        src/usr.bin/ftp: cmds.c ftp_var.h util.c

Log Message:
Issue PWD commands to the server only when we actually
need the results, not speculatively, just in case we might.

Allows operation with some broken servers that get confused
by PWD commands in some situations, and saves server round
trips in the (modern) common case of
        ftp ftp://path/name
where we never need to know the results from PWD.


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 src/usr.bin/ftp/cmds.c
cvs rdiff -u -r1.84 -r1.85 src/usr.bin/ftp/ftp_var.h
cvs rdiff -u -r1.158 -r1.159 src/usr.bin/ftp/util.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/ftp/cmds.c
diff -u src/usr.bin/ftp/cmds.c:1.137 src/usr.bin/ftp/cmds.c:1.138
--- src/usr.bin/ftp/cmds.c:1.137	Sat Feb 27 16:31:31 2016
+++ src/usr.bin/ftp/cmds.c	Mon Nov 20 21:11:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cmds.c,v 1.137 2016/02/27 16:31:31 christos Exp $	*/
+/*	$NetBSD: cmds.c,v 1.138 2017/11/20 21:11:36 kre Exp $	*/
 
 /*-
  * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
@@ -96,7 +96,7 @@
 #if 0
 static char sccsid[] = "@(#)cmds.c	8.6 (Berkeley) 10/9/94";
 #else
-__RCSID("$NetBSD: cmds.c,v 1.137 2016/02/27 16:31:31 christos Exp $");
+__RCSID("$NetBSD: cmds.c,v 1.138 2017/11/20 21:11:36 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -1158,7 +1158,8 @@ cd(int argc, char *argv[])
 	}
 	if (r == COMPLETE) {
 		dirchange = 1;
-		updateremotecwd();
+		remotecwd[0] = '\0';
+		remcwdvalid = 0;
 	}
 }
 
@@ -1544,9 +1545,9 @@ pwd(int argc, char *argv[])
 		UPRINTF("usage: %s\n", argv[0]);
 		return;
 	}
-	if (! remotecwd[0])
+	if (!remcwdvalid || remotecwd[0] == '\0')
 		updateremotecwd();
-	if (! remotecwd[0])
+	if (remotecwd[0] == '\0')
 		fprintf(ttyout, "Unable to determine remote directory\n");
 	else {
 		fprintf(ttyout, "Remote directory: %s\n", remotecwd);
@@ -2359,7 +2360,8 @@ cdup(int argc, char *argv[])
 	}
 	if (r == COMPLETE) {
 		dirchange = 1;
-		updateremotecwd();
+		remotecwd[0] = '\0';
+		remcwdvalid = 0;
 	}
 }
 

Index: src/usr.bin/ftp/ftp_var.h
diff -u src/usr.bin/ftp/ftp_var.h:1.84 src/usr.bin/ftp/ftp_var.h:1.85
--- src/usr.bin/ftp/ftp_var.h:1.84	Wed Dec 16 23:00:39 2015
+++ src/usr.bin/ftp/ftp_var.h	Mon Nov 20 21:11:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: ftp_var.h,v 1.84 2015/12/16 23:00:39 christos Exp $	*/
+/*	$NetBSD: ftp_var.h,v 1.85 2017/11/20 21:11:36 kre Exp $	*/
 
 /*-
  * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
@@ -269,6 +269,7 @@ GLOBAL	int	unix_server;	/* server is uni
 GLOBAL	int	unix_proxy;	/* proxy is unix, can use binary for ascii */
 GLOBAL	char	localcwd[MAXPATHLEN];	/* local dir */
 GLOBAL	char	remotecwd[MAXPATHLEN];	/* remote dir */
+GLOBAL	int	remcwdvalid;		/* remotecwd has been updated */
 GLOBAL	char   *username;	/* name of user logged in as. (dynamic) */
 
 GLOBAL	sa_family_t family;	/* address family to use for connections */

Index: src/usr.bin/ftp/util.c
diff -u src/usr.bin/ftp/util.c:1.158 src/usr.bin/ftp/util.c:1.159
--- src/usr.bin/ftp/util.c:1.158	Tue Feb 19 23:29:15 2013
+++ src/usr.bin/ftp/util.c	Mon Nov 20 21:11:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: util.c,v 1.158 2013/02/19 23:29:15 dsl Exp $	*/
+/*	$NetBSD: util.c,v 1.159 2017/11/20 21:11:36 kre Exp $	*/
 
 /*-
  * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.158 2013/02/19 23:29:15 dsl Exp $");
+__RCSID("$NetBSD: util.c,v 1.159 2017/11/20 21:11:36 kre Exp $");
 #endif /* not lint */
 
 /*
@@ -478,7 +478,8 @@ ftp_login(const char *host, const char *
 		}
 	}
 	updatelocalcwd();
-	updateremotecwd();
+	remotecwd[0] = '\0';
+	remcwdvalid = 0;
 
  cleanup_ftp_login:
 	FREEPTR(fuser);
@@ -835,6 +836,7 @@ updateremotecwd(void)
 	size_t	 i;
 	char	*cp;
 
+	remcwdvalid = 1;	/* whether it works or not, we are done */
 	overbose = verbose;
 	ocode = code;
 	if (ftp_debug == 0)
@@ -1174,6 +1176,8 @@ formatbuf(char *buf, size_t len, const c
 		case '/':
 		case '.':
 		case 'c':
+			if (connected && !remcwdvalid)
+				updateremotecwd();
 			p2 = connected ? remotecwd : "";
 			updirs = pdirs = 0;
 

Reply via email to