+
++__private_extern__ void
++__unsetenv(const char *name, char **environ, malloc_zone_t *envz)
++{
++ char **p;
++ int offset;
++
++ while (__findenv(name, &offset, environ)) { /* if set multiple times */
++ /* if we malloc-ed it, free it first */
++ if (ZONE_OWNS_PTR(envz, environ[offset]))
++ free(environ[offset]);
++ for (p = &environ[offset];; ++p)
++ if (!(*p = *(p + 1)))
++ break;
++ }
++}
++
++/****************************************************************************/
+ /*
++ * _allocenvstate -- SPI that creates a new state (opaque)
++ */
++void *
++_allocenvstate(void)
++{
++ malloc_zone_t *zone;
++ zone = malloc_create_zone(1000 /* unused */, 0 /* unused */);
++ if (zone) {
++ malloc_set_zone_name(zone, "environ");
++ }
++ return (void *)zone;
++}
++
++/*
++ * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly
++ * allocated buffer, compatible with the other SPI env routines. If env
++ * is NULL, a char * array composed of a single NULL is returned. NULL
++ * is returned on error. (This isn't needed anymore, as __setenv will
++ * automatically make a copy in the zone.)
++ */
++char **
++_copyenv(char **env)
++{
++ char **p;
++ int cnt = 1;
++
++ if (env)
++ for (p = env; *p; ++p, ++cnt);
++ p = (char **)malloc((size_t)(sizeof(char *) * cnt));
++ if (!p)
++ return (NULL);
++ if (env)
++ bcopy(env, p, cnt * sizeof(char *));
++ else
++ *p = NULL;
++ return p;
++}
++
++/*
++ * _deallocenvstate -- SPI that frees all the memory associated with the state
++ * and all allocated strings, including the environment array itself if it
++ * was copied.
++ */
++int
++_deallocenvstate(void *state)
++{
++ malloc_zone_t *envz;
++
++ if (!(envz = (malloc_zone_t *)state) || envz == __zone0) {
++ errno = EINVAL;
++ return -1;
++ }
++ malloc_destroy_zone(envz);
++ return 0;
++}
++
++/*
++ * setenvp -- SPI using an arbitrary pointer to string array and an env state,
++ * created by _allocenvstate(). Initial checking is not done.
++ *
++ * Set the value of the environmental variable "name" to be
++ * "value". If rewrite is set, replace any current value.
++ */
++int
++_setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state)
++{
++ if (init__zone0(1)) return (-1);
++ return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0)));
++}
++
++/*
++ * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
++ * state, created by _allocenvstate(). Initial checking is not done.
++ *
++ * Delete environmental variable "name".
++ */
++int
++_unsetenvp(const char *name, char ***envp, void *state)
++{
++ if (init__zone0(1)) return (-1);
++ __unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0));
++ return 0;
++}
++