summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2018-09-18 13:23:48 -0700
committerH. Peter Anvin <hpa@zytor.com>2018-09-18 13:23:48 -0700
commitf2cf87be8deb869b06ecc4be41d1b5752c0da00a (patch)
tree94b392ff82a109bc4ee2fbf5a5ff45c161eb8081
parent7f9de35cc2d0e56d1466082a1ef99418f8ef37a5 (diff)
downloadabc80sim-f2cf87be8deb869b06ecc4be41d1b5752c0da00a.tar.gz
abc80sim-f2cf87be8deb869b06ecc4be41d1b5752c0da00a.tar.xz
abc80sim-f2cf87be8deb869b06ecc4be41d1b5752c0da00a.zip
print: push_env: don't copy duplicate variables (if any)
-rw-r--r--print.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/print.c b/print.c
index 35a7f44..679a2dc 100644
--- a/print.c
+++ b/print.c
@@ -32,7 +32,7 @@ static struct temp_file *temp;
static char **push_env(const char *var, const char *val)
{
char **oldenviron = environ;
- char **newenviron;
+ char **newenviron, **oep, **nep;
size_t nenv;
size_t varlen, vallen;
char *newvar;
@@ -54,8 +54,16 @@ static char **push_env(const char *var, const char *val)
newvar[varlen] = '=';
memcpy(newvar + varlen + 1, val, vallen+1);
- newenviron[0] = newvar;
- memcpy(newenviron+1, oldenviron, (nenv+1) * sizeof(char *));
+ nep = newenviron;
+ *nep++ = newvar;
+ oep = oldenviron;
+ while (*oep) {
+ /* Don't copy a duplicate variable */
+ if (strncmp(*oep, newvar, varlen+1))
+ *nep++ = *oep;
+ oep++;
+ }
+ *nep = NULL;
environ = newenviron;
return oldenviron;