From e05d620a79575bc96155180e1efccd171452b82f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 2 Apr 2020 02:58:42 -0500 Subject: More shell plumbing. Redo of variable storage, add export. --- lib/env.c | 61 +++++++++++++++++++++++++++++++++++++++---------------------- lib/lib.h | 5 +++-- 2 files changed, 42 insertions(+), 24 deletions(-) (limited to 'lib') diff --git a/lib/env.c b/lib/env.c index 614a504c..3017c40a 100644 --- a/lib/env.c +++ b/lib/env.c @@ -34,18 +34,19 @@ void xclearenv(void) // Frees entries we set earlier. Use with libc getenv but not setenv/putenv. // if name has an equals and !val, act like putenv (name=val must be malloced!) // if !val unset name. (Name with = and val is an error) -void xsetmyenv(int *envc, char ***env, char *name, char *val) +// returns pointer to new name=value environment string, NULL if none +char *xsetenv(char *name, char *val) { unsigned i, len, ec; char *new; // If we haven't snapshot initial environment state yet, do so now. - if (!*envc) { + if (!toys.envc) { // envc is size +1 so even if env empty it's nonzero after initialization - while ((*env)[(*envc)++]); - memcpy(new = xmalloc(((*envc|0xff)+1)*sizeof(char *)), *env, - *envc*sizeof(char *)); - *env = (void *)new; + while (environ[toys.envc++]); + memcpy(new = xmalloc(((toys.envc|0xff)+1)*sizeof(char *)), environ, + toys.envc*sizeof(char *)); + environ = (void *)new; } new = strchr(name, '='); @@ -58,36 +59,31 @@ void xsetmyenv(int *envc, char ***env, char *name, char *val) if (val) new = xmprintf("%s=%s", name, val); } - ec = (*envc)-1; // compensate for size +1 above - for (i = 0; (*env)[i]; i++) { + 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, (*env)[i], len) && (*env)[i][len]=='=') { - if (i>=ec) free((*env)[i]); + 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 - *envc = ec--; - for (; new ? i