summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorkre <kre@NetBSD.org>2019-02-09 09:38:11 +0000
committerkre <kre@NetBSD.org>2019-02-09 09:38:11 +0000
commit7a6f70915e123a13a0fa8558f057d972cf46f4d6 (patch)
treeaeb68753c941dbe78e9af22cf4c227075f9226b7 /bin
parent32c429acc2b3e9258a52507976739d2c53143a23 (diff)
DTRT when dynamically generated variables return "unset" instead of
a value. There are none which do that at the minute, so this is a NFCI change, which is just making the code correct even though nothing currently triggers any bugs.
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/var.c51
1 files changed, 35 insertions, 16 deletions
diff --git a/bin/sh/var.c b/bin/sh/var.c
index 05c49c64d47..90549b9b76f 100644
--- a/bin/sh/var.c
+++ b/bin/sh/var.c
@@ -1,4 +1,4 @@
-/* $NetBSD: var.c,v 1.76 2019/02/09 03:35:55 kre Exp $ */
+/* $NetBSD: var.c,v 1.77 2019/02/09 09:38:11 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.76 2019/02/09 03:35:55 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.77 2019/02/09 09:38:11 kre Exp $");
#endif
#endif /* not lint */
@@ -562,13 +562,19 @@ char *
lookupvar(const char *name)
{
struct var *v;
+ char *p;
v = find_var(name, NULL, NULL);
if (v == NULL || v->flags & VUNSET)
return NULL;
- if (v->rfunc && (v->flags & VFUNCREF) != 0)
- return (*v->rfunc)(v) + v->name_len + 1;
- return v->text + v->name_len + 1;
+ if (v->rfunc && (v->flags & VFUNCREF) != 0) {
+ p = (*v->rfunc)(v);
+ if (p == NULL)
+ return NULL;
+ } else
+ p = v->text;
+
+ return p + v->name_len + 1;
}
@@ -584,6 +590,7 @@ bltinlookup(const char *name, int doall)
{
struct strlist *sp;
struct var *v;
+ char *p;
for (sp = cmdenviron ; sp ; sp = sp->next) {
if (strequal(sp->text, name))
@@ -594,9 +601,15 @@ bltinlookup(const char *name, int doall)
if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
return NULL;
- if (v->rfunc && (v->flags & VFUNCREF) != 0)
- return (*v->rfunc)(v) + v->name_len + 1;
- return v->text + v->name_len + 1;
+
+ if (v->rfunc && (v->flags & VFUNCREF) != 0) {
+ p = (*v->rfunc)(v);
+ if (p == NULL)
+ return NULL;
+ } else
+ p = v->text;
+
+ return p + v->name_len + 1;
}
@@ -626,9 +639,11 @@ environment(void)
for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
for (vp = *vpp ; vp ; vp = vp->next)
if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) {
- if (vp->rfunc && (vp->flags & VFUNCREF))
- *ep++ = (*vp->rfunc)(vp);
- else
+ if (vp->rfunc && (vp->flags & VFUNCREF)) {
+ *ep = (*vp->rfunc)(vp);
+ if (*ep != NULL)
+ ep++;
+ } else
*ep++ = vp->text;
VTRACE(DBG_VARS, ("environment: %s\n", ep[-1]));
}
@@ -763,16 +778,20 @@ showvar(struct var *vp, const char *cmd, const char *xtra, int show_value)
{
const char *p;
- if (cmd)
- out1fmt("%s ", cmd);
- if (xtra)
- out1fmt("%s ", xtra);
p = vp->text;
if (vp->rfunc && (vp->flags & VFUNCREF) != 0) {
p = (*vp->rfunc)(vp);
- if (p == NULL)
+ if (p == NULL) {
+ if (!(show_value & 2))
+ return;
p = vp->text;
+ show_value = 0;
+ }
}
+ if (cmd)
+ out1fmt("%s ", cmd);
+ if (xtra)
+ out1fmt("%s ", xtra);
for ( ; *p != '=' ; p++)
out1c(*p);
if (!(vp->flags & VUNSET) && show_value) {