setenviron.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
#include "intercepts.h"
#include <fs3d.h>
/*
* put name=value in the environment
* pointer to value returned
* environ==0 is ok
*
* setenviron("N=V") add N=V
* setenviron("N") delete N
* setenviron(0) expect more (pre-fork optimization)
*
* _ always placed at the top
*/
char*
setenviron(const char* akey)
{
static char** envv; /* recorded environ */
static char** next; /* next free slot */
static char** last; /* last free slot (0) */
register char** v = environ;
register char** p = envv;
register char* s;
register char* t;
int n;
ast.env_serial++;
if (p && !v)
{
*++next = 0;
}
else if (p != v || !v)
{
if (v)
{
while (*v++);
v = environ;
}
else
n = INCREMENT;
if (!p || (last - p + 1) < n)
{
{
/*
* kick 3d initialization
*/
v = environ;
}
if (!(p = newof(p, char*, n, 0)))
return 0;
last = p + n - 1;
}
if (v && v[0] && v[0][0] == '_' && v[0][1] == '=')
*p++ = *v++;
else
*p++ = "_=";
if (!v)
*p = 0;
else
while (*p = *v++)
if (p[0][0] == '_' && p[0][1] == '=')
envv[0] = *p;
else
p++;
next = p;
p = envv;
}
{
if (!(p = newof(p, char*, n, 0)))
return 0;
last = p + n - 1;
}
if (!key)
return ok;
for (; s = *p; p++)
{
t = key;
do
{
if (!*t || *t == '=')
{
if (*s == '=')
{
if (!*t)
{
v = p++;
while (*v++ = *p++);
next--;
return ok;
}
*p = key;
}
break;
}
} while (*t++ == *s++);
}
return ok;
p = next;
*++next = 0;
*p = key;
return s + 1;
}