fix up environ array so subsequent getenv() calls will return the modified value. _envsetup() is not appropriate here because of the extra work it does which is not wanted. Reference: /n/atom/patch/applied2013/apeputenv Date: Wed Aug 21 01:33:20 CES 2013 Signed-off-by: quanstro@quanstro.net --- /sys/src/ape/lib/bsd/putenv.c Wed Aug 21 01:32:28 2013 +++ /sys/src/ape/lib/bsd/putenv.c Wed Aug 21 01:32:28 2013 @@ -1,8 +1,56 @@ +#include #include #include #include #include +extern char **environ; + +static int +envredo(char *s) +{ + char *x, **p, *q, **v, *mem; + int n, nsz, nenv, esz; + + x = strchr(s, '='); + if(x == NULL) + return -1; + nsz = x - s + 1; /* compare the var=, not just var */ + + nenv = 1; + esz = strlen(s)+1; + for(p = environ; (q = *p) != NULL; p++){ + esz += strlen(q)+1; + nenv++; + } + + /* overestimate in the case we have changed a variable. */ + v = malloc((nenv+1)*sizeof(char**) + esz); + if(v == NULL) + return -1; + mem = (char*)(v + nenv + 1); + + nenv = 0; + for(p = environ; (q = *p) != NULL; p++){ + if(strncmp(q, s, nsz) == 0) + continue; + v[nenv++] = mem; + n = strlen(q)+1; + memcpy(mem, q, n); + mem += n; + } + v[nenv++] = mem; + n = strlen(s)+1; + memcpy(mem, s, n); + + v[nenv] = NULL; + + free(environ); + environ = v; + + return 0; +} + int putenv(char *s) { @@ -26,7 +74,7 @@ if(write(f, value, n) != n) return -1; close(f); - return 0; + return envredo(s); } else return -1; }