Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Make "export VAR" work correctly ... if VAR was unset...



details:   https://anonhg.NetBSD.org/src/rev/cb0818d85f8f
branches:  trunk
changeset: 823684:cb0818d85f8f
user:      kre <kre%NetBSD.org@localhost>
date:      Wed May 03 00:39:40 2017 +0000

description:
Make "export VAR" work correctly ... if VAR was unset before this
command it should remain unset afterwards.

Previouly "export VAR" did much the same as:
        export VAR="${VAR}"
(but without the side effects if VAR had previously been VAR='~' or similar)

Also stop unset exported variables from actually making it into the
environment.  Previously this was impossible - variables get exported
in just one of 3 ways, by being imported from the environ (which means
the var is set) when -a is set, and a var is given a value (so the var
is set), or using "export" which previously always set a null string
if the var was otheriwse unset.

The same happens for "readonly"  (readonly and export use the same mechanism)
- except, once marked readonly, it is no longer possible to set the var, so
(assuming VAR is not already readonly)
        unset VAR; readonly VAR
is (now) a way to guarantee that "VAR" can never be set.

This conforms with POISX (though it is not particularly clear on this
point) and with bash and ksh93 (and also with the FreeBSD shell, though
they export unset variables that are marked for export as if set to '')

It s not clear whether
        unset VAR; readonly VAR; unset VAR; echo $?
should print 0, or non-0, so for now just leave this as it is (prints 1).

diffstat:

 bin/sh/var.c |  16 +++++++++-------
 1 files changed, 9 insertions(+), 7 deletions(-)

diffs (65 lines):

diff -r 85e57b9a67d5 -r cb0818d85f8f bin/sh/var.c
--- a/bin/sh/var.c      Wed May 03 00:23:42 2017 +0000
+++ b/bin/sh/var.c      Wed May 03 00:39:40 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.50 2017/04/29 15:12:21 kre Exp $     */
+/*     $NetBSD: var.c,v 1.51 2017/05/03 00:39:40 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: var.c,v 1.50 2017/04/29 15:12:21 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.51 2017/05/03 00:39:40 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -430,13 +430,13 @@
        nenv = 0;
        for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
                for (vp = *vpp ; vp ; vp = vp->next)
-                       if (vp->flags & VEXPORT)
+                       if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
                                nenv++;
        }
        ep = env = stalloc((nenv + 1) * sizeof *env);
        for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
                for (vp = *vpp ; vp ; vp = vp->next)
-                       if (vp->flags & VEXPORT)
+                       if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
                                *ep++ = vp->text;
        }
        *ep = NULL;
@@ -607,7 +607,7 @@
        int xflg = 0;
        int res;
        int c;
-
+       int f;
 
        while ((c = nextopt("npx")) != '\0') {
                switch (c) {
@@ -643,6 +643,7 @@
 
        res = 0;
        while ((name = *argptr++) != NULL) {
+               f = flag;
                if ((p = strchr(name, '=')) != NULL) {
                        p++;
                } else {
@@ -658,10 +659,11 @@
                                                vp->flags &= ~VEXPORT;
                                }
                                continue;
-                       }
+                       } else
+                               f |= VUNSET;
                }
                if (!nflg)
-                       setvar(name, p, flag);
+                       setvar(name, p, f);
        }
        return res;
 }



Home | Main Index | Thread Index | Old Index