From e9396994d328606ff436b87bea3a0321bba98809 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 3 May 2020 23:15:35 -0500 Subject: Next round of shell work. --- lib/env.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'lib/env.c') diff --git a/lib/env.c b/lib/env.c index 3017c40a..70fb0def 100644 --- a/lib/env.c +++ b/lib/env.c @@ -37,49 +37,46 @@ void xclearenv(void) // returns pointer to new name=value environment string, NULL if none char *xsetenv(char *name, char *val) { - unsigned i, len, ec; + unsigned i, j = 0, len; char *new; // If we haven't snapshot initial environment state yet, do so now. if (!toys.envc) { + // envc is size +1 so even if env empty it's nonzero after initialization while (environ[toys.envc++]); - memcpy(new = xmalloc(((toys.envc|0xff)+1)*sizeof(char *)), environ, + memcpy(new = xmalloc(((toys.envc|31)+1)*sizeof(char *)), environ, toys.envc*sizeof(char *)); environ = (void *)new; } - new = strchr(name, '='); - if (new) { + if (!(new = strchr(name, '='))) { + len = strlen(name); + if (val) new = xmprintf("%s=%s", name, val); + } else { len = new-name; if (val) error_exit("xsetenv %s to %s", name, val); new = name; - } else { - len = strlen(name); - if (val) new = xmprintf("%s=%s", name, val); } - ec = toys.envc-1; // compensate for size +1 above for (i = 0; environ[i]; i++) { // Drop old entry, freeing as appropriate. Assumes no duplicates. if (!memcmp(name, environ[i], len) && environ[i][len]=='=') { - if (i>=ec) free(environ[i]); - else { - // move old entries down, add at end of old data - toys.envc = ec--; - for (; new ? i