/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2012 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped2223#include "intercepts.h"2425#include <fs3d.h>2627/*28* put name=value in the environment29* pointer to value returned30* environ==0 is ok31*32* setenviron("N=V") add N=V33* setenviron("N") delete N34* setenviron(0) expect more (pre-fork optimization)35*36* _ always placed at the top37*/3839#define INCREMENT 16 /* environ increment */4041char*42setenviron(const char* akey)43{44#undef setenviron45static char** envv; /* recorded environ */46static char** next; /* next free slot */47static char** last; /* last free slot (0) */48static char ok[] = ""; /* delete/optimization ok return*/4950char* key = (char*)akey;51register char** v = environ;52register char** p = envv;53register char* s;54register char* t;55int n;5657ast.env_serial++;58if (intercepts.intercept_setenviron)59return (*intercepts.intercept_setenviron)(akey);60if (p && !v)61{62environ = next = p;63*++next = 0;64}65else if (p != v || !v)66{67if (v)68{69while (*v++);70n = v - environ + INCREMENT;71v = environ;72}73else74n = INCREMENT;75if (!p || (last - p + 1) < n)76{77if (!p && fs3d(FS3D_TEST))78{79/*80* kick 3d initialization81*/8283close(open(".", O_RDONLY|O_cloexec));84v = environ;85}86if (!(p = newof(p, char*, n, 0)))87return 0;88last = p + n - 1;89}90envv = environ = p;91if (v && v[0] && v[0][0] == '_' && v[0][1] == '=')92*p++ = *v++;93else94*p++ = "_=";95if (!v)96*p = 0;97else98while (*p = *v++)99if (p[0][0] == '_' && p[0][1] == '=')100envv[0] = *p;101else102p++;103next = p;104p = envv;105}106else if (next == last)107{108n = last - v + INCREMENT + 1;109if (!(p = newof(p, char*, n, 0)))110return 0;111last = p + n - 1;112next = last - INCREMENT;113envv = environ = p;114}115if (!key)116return ok;117for (; s = *p; p++)118{119t = key;120do121{122if (!*t || *t == '=')123{124if (*s == '=')125{126if (!*t)127{128v = p++;129while (*v++ = *p++);130next--;131return ok;132}133*p = key;134return (s = strchr(key, '=')) ? s + 1 : (char*)0;135}136break;137}138} while (*t++ == *s++);139}140if (!(s = strchr(key, '=')))141return ok;142p = next;143*++next = 0;144*p = key;145return s + 1;146}147148149