Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Fix "export -x" (and its consequences) to behave as o...



details:   https://anonhg.NetBSD.org/src/rev/ca870dea5a77
branches:  trunk
changeset: 446378:ca870dea5a77
user:      kre <kre%NetBSD.org@localhost>
date:      Mon Dec 03 06:42:25 2018 +0000

description:
Fix "export -x" (and its consequences) to behave as originally
intended (and as documented) rather than how it has been behaving
(which was not very rational.)   Since it is unlikely that anyone
is using this, the change should be mostly invisible.

While here, a couple of other minor cleanups:
        . One call of geteuid() is enough in choose_ps1()
        . Fix a typo in a comment
        . Improve appearance (whitspace changes) in find_var()

diffstat:

 bin/sh/eval.c |  13 +++++----
 bin/sh/var.c  |  76 +++++++++++++++++++++++++++++++++++-----------------------
 bin/sh/var.h  |   3 +-
 3 files changed, 55 insertions(+), 37 deletions(-)

diffs (243 lines):

diff -r ad9ce4f66c78 -r ca870dea5a77 bin/sh/eval.c
--- a/bin/sh/eval.c     Mon Dec 03 06:41:30 2018 +0000
+++ b/bin/sh/eval.c     Mon Dec 03 06:42:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.166 2018/12/03 06:40:26 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.167 2018/12/03 06:42:25 kre Exp $   */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)eval.c     8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.166 2018/12/03 06:40:26 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.167 2018/12/03 06:42:25 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -1085,7 +1085,8 @@
                                }
                                savehandler = handler;
                                handler = &jmploc;
-                               listmklocal(varlist.list, VEXPORT | VNOFUNC);
+                               listmklocal(varlist.list,
+                                   VDOEXPORT | VEXPORT | VNOFUNC);
                                forkchild(jp, cmd, mode, vforked);
                                break;
                        default:
@@ -1189,7 +1190,7 @@
                            cmdentry.lineno, cmdentry.lno_frel?" (=1)":"",
                            funclinebase));
                }
-               listmklocal(varlist.list, VEXPORT);
+               listmklocal(varlist.list, VDOEXPORT | VEXPORT);
                /* stop shell blowing its stack */
                if (++funcnest > 1000)
                        error("too many nested function calls");
@@ -1317,7 +1318,7 @@
                    (vforked ? REDIR_VFORK : 0) | REDIR_KEEP);
                if (!vforked)
                        for (sp = varlist.list ; sp ; sp = sp->next)
-                               setvareq(sp->text, VEXPORT|VSTACK);
+                               setvareq(sp->text, VDOEXPORT|VEXPORT|VSTACK);
                envp = environment();
                shellexec(argv, envp, path, cmdentry.u.index, vforked);
                break;
@@ -1579,7 +1580,7 @@
                mflag = 0;
                optschanged();
                for (sp = cmdenviron; sp; sp = sp->next)
-                       setvareq(sp->text, VEXPORT|VSTACK);
+                       setvareq(sp->text, VDOEXPORT|VEXPORT|VSTACK);
                shellexec(argv + 1, environment(), pathval(), 0, 0);
        }
        return 0;
diff -r ad9ce4f66c78 -r ca870dea5a77 bin/sh/var.c
--- a/bin/sh/var.c      Mon Dec 03 06:41:30 2018 +0000
+++ b/bin/sh/var.c      Mon Dec 03 06:42:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.70 2018/07/13 22:43:44 kre Exp $     */
+/*     $NetBSD: var.c,v 1.71 2018/12/03 06:42:25 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.70 2018/07/13 22:43:44 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.71 2018/12/03 06:42:25 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -323,15 +323,17 @@
 void
 choose_ps1(void)
 {
+       uid_t u = geteuid();
+
        if ((vps1.flags & (VTEXTFIXED|VSTACK)) == 0)
                free(vps1.text);
-       vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
+       vps1.text = strdup(u != 0 ? "PS1=$ " : "PS1=# ");
        vps1.flags &= ~(VTEXTFIXED|VSTACK);
 
        /*
         * Update PSc whenever we feel the need to update PS1
         */
-       setvarsafe("PSc", (geteuid() == 0 ? "#" : "$"), 0);
+       setvarsafe("PSc", (u == 0 ? "#" : "$"), 0);
 }
 
 /*
@@ -480,9 +482,11 @@
                vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
                if (flags & VNOEXPORT)
                        vp->flags &= ~VEXPORT;
+               if (flags & VDOEXPORT)
+                       vp->flags &= ~VNOEXPORT;
                if (vp->flags & VNOEXPORT)
                        flags &= ~VEXPORT;
-               vp->flags |= flags & ~VNOFUNC;
+               vp->flags |= flags & ~(VNOFUNC | VDOEXPORT);
                vp->text = s;
 
                /*
@@ -495,20 +499,22 @@
                INTON;
                return;
        }
-       VTRACE(DBG_VARS, ("new\n"));
        /* not found */
        if (flags & VNOSET) {
+               VTRACE(DBG_VARS, ("new noset\n"));
                if ((flags & (VTEXTFIXED|VSTACK)) == 0)
                        ckfree(s);
                return;
        }
        vp = ckmalloc(sizeof (*vp));
-       vp->flags = flags & ~(VNOFUNC|VFUNCREF);
+       vp->flags = flags & ~(VNOFUNC|VFUNCREF|VDOEXPORT);
        vp->text = s;
        vp->name_len = nlen;
+       vp->func = NULL;
        vp->next = *vpp;
-       vp->func = NULL;
        *vpp = vp;
+
+       VTRACE(DBG_VARS, ("new [%s] (%d) %#x\n", s, nlen, vp->flags));
 }
 
 
@@ -853,26 +859,32 @@
 
        res = 0;
        while ((name = *argptr++) != NULL) {
+               int len;
+
                f = flag;
-               if ((p = strchr(name, '=')) != NULL) {
-                       p++;
-               } else {
-                       vp = find_var(name, NULL, NULL);
-                       if (vp != NULL) {
-                               if (nflg)
-                                       vp->flags &= ~flag;
-                               else if (flag&VEXPORT && vp->flags&VNOEXPORT)
-                                       res = 1;
-                               else {
-                                       vp->flags |= flag;
-                                       if (flag == VNOEXPORT)
-                                               vp->flags &= ~VEXPORT;
-                               }
+
+               vp = find_var(name, NULL, &len);
+               p = name + len;
+               if (*p++ != '=')
+                       p = NULL;
+
+               if (vp != NULL) {
+                       if (nflg)
+                               vp->flags &= ~flag;
+                       else if (flag&VEXPORT && vp->flags&VNOEXPORT) {
+                               sh_warnx("%.*s: not available for export",
+                                   len, name);
+                               res = 1;
+                       } else {
+                               vp->flags |= flag;
+                               if (flag == VNOEXPORT)
+                                       vp->flags &= ~VEXPORT;
+                       }
+                       if (p == NULL)
                                continue;
-                       } else
-                               f |= VUNSET;
-               }
-               if (!nflg)
+               } 
+
+               if (!nflg || p != NULL)
                        setvar(name, p, f);
        }
        return res;
@@ -946,7 +958,10 @@
                        lvp->text = vp->text;
                        lvp->flags = vp->flags;
                        vp->flags |= VSTRFIXED|VTEXTFIXED;
-                       if (vp->flags & VNOEXPORT)
+                       if (flags & (VDOEXPORT | VUNSET))
+                               vp->flags &= ~VNOEXPORT;
+                       if (vp->flags & VNOEXPORT &&
+                           (flags & (VEXPORT|VDOEXPORT|VUNSET)) == VEXPORT)
                                flags &= ~VEXPORT;
                        if (flags & (VNOEXPORT | VUNSET))
                                vp->flags &= ~VEXPORT;
@@ -986,7 +1001,7 @@
        while ((lvp = localvars) != NULL) {
                localvars = lvp->next;
                vp = lvp->vp;
-               VTRACE(DBG_VARS, ("poplocalvar %s", vp ? vp->text : "-"));
+               VTRACE(DBG_VARS, ("poplocalvar %s\n", vp ? vp->text : "-"));
                if (vp == NULL) {       /* $- saved */
                        memcpy(optlist, lvp->text, sizeof_optlist);
                        ckfree(lvp->text);
@@ -1124,7 +1139,7 @@
  * Search for a variable.
  * 'name' may be terminated by '=' or a NUL.
  * vppp is set to the pointer to vp, or the list head if vp isn't found
- * lenp is set to the number of charactets in 'name'
+ * lenp is set to the number of characters in 'name'
  */
 
 STATIC struct var *
@@ -1138,10 +1153,11 @@
        hashval = 0;
        while (*p && *p != '=')
                hashval = 2 * hashval + (unsigned char)*p++;
+
        len = p - name;
-
        if (lenp)
                *lenp = len;
+
        vpp = &vartab[hashval % VTABSIZE];
        if (vppp)
                *vppp = vpp;
diff -r ad9ce4f66c78 -r ca870dea5a77 bin/sh/var.h
--- a/bin/sh/var.h      Mon Dec 03 06:41:30 2018 +0000
+++ b/bin/sh/var.h      Mon Dec 03 06:42:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.h,v 1.36 2017/10/28 03:59:11 kre Exp $     */
+/*     $NetBSD: var.h,v 1.37 2018/12/03 06:42:25 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -51,6 +51,7 @@
 #define VNOFUNC                0x0100  /* don't call the callback function */
 #define VFUNCREF       0x0200  /* the function is called on ref, not set */
 
+#define VDOEXPORT      0x2000  /* obey VEXPORT even if VNOEXPORT */
 #define VNOSET         0x4000  /* do not set variable - just readonly test */
 #define VNOERROR       0x8000  /* be quiet if set fails (no error msg) */
 



Home | Main Index | Thread Index | Old Index