Module Name:    src
Committed By:   kre
Date:           Mon Jan 31 16:54:28 UTC 2022

Modified Files:
        src/bin/sh: cd.c histedit.c

Log Message:
Add some comments explaining accesses to the environment via
getenv()/setenv()/unsetenv() which manipulate the envornoment
the shell was passed at entry.

These are a little odd in sh as that environment is copied into
the shell's internal variable data struct at shell startup, and
normally never accessed after that - in builtin commands (test.
printf, ...) getenv() is #defined to become an internal sh lookup
function instead, so even those never use the startup environment).

NFCI


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/bin/sh/cd.c
cvs rdiff -u -r1.57 -r1.58 src/bin/sh/histedit.c

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

Modified files:

Index: src/bin/sh/cd.c
diff -u src/bin/sh/cd.c:1.52 src/bin/sh/cd.c:1.53
--- src/bin/sh/cd.c:1.52	Tue Nov 16 16:57:15 2021
+++ src/bin/sh/cd.c	Mon Jan 31 16:54:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $	*/
+/*	$NetBSD: cd.c,v 1.53 2022/01/31 16:54:28 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)cd.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $");
+__RCSID("$NetBSD: cd.c,v 1.53 2022/01/31 16:54:28 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -404,6 +404,20 @@ getpwd(int noerror)
 		return;
 
 	if (first) {
+		/*
+		 * Note that this happens via the call from initpwd()
+		 * just above, which is called early from main() during
+		 * sh startup, so fetching PWD from the entry environment
+		 * (which is what getenv() does) is acceptable.   Here we
+		 * could use normal sh var lookup functions instead, as
+		 * the arriving environment has already been imported before
+		 * we get here, but it makes little difference.
+		 *
+		 * XXX What would be better perhaps would be to move all of
+		 * this into initpwd() instead of here, so we could get rid of
+		 * this "first" static - that function is only ever called once.
+		 * XXX Some other day.
+		 */
 		first = 0;
 		pwd = getenv("PWD");
 		if (is_curdir(pwd)) {

Index: src/bin/sh/histedit.c
diff -u src/bin/sh/histedit.c:1.57 src/bin/sh/histedit.c:1.58
--- src/bin/sh/histedit.c:1.57	Tue Sep 14 15:04:09 2021
+++ src/bin/sh/histedit.c	Mon Jan 31 16:54:28 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: histedit.c,v 1.57 2021/09/14 15:04:09 christos Exp $	*/
+/*	$NetBSD: histedit.c,v 1.58 2022/01/31 16:54:28 kre Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: histedit.c,v 1.57 2021/09/14 15:04:09 christos Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.58 2022/01/31 16:54:28 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -129,6 +129,23 @@ histedit(void)
 			if (tracefile)
 				el_err = tracefile;
 #endif
+			/*
+			 * This odd piece of code doesn't affect the shell
+			 * at all, the environment modified here is the
+			 * stuff accessed via "environ" (the incoming
+			 * envoironment to the shell) which is only ever
+			 * touched at sh startup time (long before we get
+			 * here) and ignored thereafter.
+			 *
+			 * But libedit calls getenv() to discover TERM
+			 * and that searches the "environ" environment,
+			 * not the shell's internal variable data struct,
+			 * so we need to make sure that TERM in there is
+			 * correct.
+			 *
+			 * This sequence copies TERM from the shell into
+			 * the old "environ" environment.
+			 */
 			term = lookupvar("TERM");
 			if (term)
 				setenv("TERM", term, 1);

Reply via email to