extern int putenv(char *newval)
{
    extern char **environ;

    static int firstTime = 1;
    char **ep;
    char *cp;
    int esiz;
    char *np;

    if (!(np = strchr(newval, '=')))
	return 1;
    *np = '\0';

    /* look it up */
    for (ep=environ ; *ep ; ep++)
    {
	/* this should always be true... */
	if (cp = strchr(*ep, '='))
	{
	    *cp = '\0';
	    if (!strcmp(*ep, newval))
	    {
		/* got it! */
		*cp = '=';
		break;
	    }
	    *cp = '=';
	}
	else
	{
	    *np = '=';
	    return 1;
	}
    }

    *np = '=';
    if (*ep)
    {
	/* the string was already there: just replace it with the new one */
	*ep = newval;
	return 0;
    }

    /* expand environ by one */
    for (esiz=2, ep=environ ; *ep ; ep++)
	esiz++;
    if (firstTime)
    {
	char **epp;
	char **newenv;
	if (!(newenv = malloc(esiz * sizeof(char *))))
	    return 1;
   
	for (ep=environ, epp=newenv ; *ep ;)
	    *epp++ = *ep++;
	*epp++ = newval;
	*epp = (char *) 0;
	environ = newenv;
    }
    else
    {
	if (!(environ = realloc(environ, esiz * sizeof(char *))))
	    return 1;
	environ[esiz - 2] = newval;
	environ[esiz - 1] = (char *) 0;
	firstTime = 0;
    }

    return 0;
}

