2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic const char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93";
2N/Astatic const char rcsid[] = "$Id: setenv.c,v 1.2 2005/04/27 04:56:11 sra Exp $";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A/*
2N/A * Copyright (c) 1987, 1993
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <stddef.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#if !defined(NEED_SETENV)
2N/Aint __bindcompat_setenv;
2N/A#else
2N/A
2N/Aextern char **environ;
2N/A
2N/Astatic char *findenv(const char *name, int *offset);
2N/A
2N/A/*%
2N/A * setenv --
2N/A * Set the value of the environmental variable "name" to be
2N/A * "value". If rewrite is set, replace any current value.
2N/A */
2N/Asetenv(const char *name, const char *value, int rewrite) {
2N/A extern char **environ;
2N/A static int alloced; /*%< if allocated space before */
2N/A char *c;
2N/A int l_value, offset;
2N/A
2N/A if (*value == '=') /*%< no `=' in value */
2N/A ++value;
2N/A l_value = strlen(value);
2N/A if ((c = findenv(name, &offset))) { /*%< find if already exists */
2N/A if (!rewrite)
2N/A return (0);
2N/A if (strlen(c) >= l_value) { /*%< old larger; copy over */
2N/A while (*c++ = *value++);
2N/A return (0);
2N/A }
2N/A } else { /*%< create new slot */
2N/A int cnt;
2N/A char **p;
2N/A
2N/A for (p = environ, cnt = 0; *p; ++p, ++cnt);
2N/A if (alloced) { /*%< just increase size */
2N/A environ = (char **)realloc((char *)environ,
2N/A (size_t)(sizeof(char *) * (cnt + 2)));
2N/A if (!environ)
2N/A return (-1);
2N/A }
2N/A else { /*%< get new space */
2N/A alloced = 1; /*%< copy old entries into it */
2N/A p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
2N/A if (!p)
2N/A return (-1);
2N/A memcpy(p, environ, cnt * sizeof(char *));
2N/A environ = p;
2N/A }
2N/A environ[cnt + 1] = NULL;
2N/A offset = cnt;
2N/A }
2N/A for (c = (char *)name; *c && *c != '='; ++c); /*%< no `=' in name */
2N/A if (!(environ[offset] = /*%< name + `=' + value */
2N/A malloc((size_t)((int)(c - name) + l_value + 2))))
2N/A return (-1);
2N/A for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
2N/A for (*c++ = '='; *c++ = *value++;);
2N/A return (0);
2N/A}
2N/A
2N/A/*%
2N/A * unsetenv(name) --
2N/A * Delete environmental variable "name".
2N/A */
2N/Avoid
2N/Aunsetenv(const char *name) {
2N/A char **p;
2N/A int offset;
2N/A
2N/A while (findenv(name, &offset)) /*%< if set multiple times */
2N/A for (p = &environ[offset];; ++p)
2N/A if (!(*p = *(p + 1)))
2N/A break;
2N/A}
2N/A
2N/A/*%
2N/A * findenv --
2N/A * Returns pointer to value associated with name, if any, else NULL.
2N/A * Sets offset to be the offset of the name/value combination in the
2N/A * environmental array, for use by setenv(3) and unsetenv(3).
2N/A * Explicitly removes '=' in argument name.
2N/A *
2N/A * This routine *should* be a static; don't use it.
2N/A */
2N/Astatic char *
2N/Afindenv(const char *name, int *offset) {
2N/A const char *np;
2N/A char **p, *c;
2N/A int len;
2N/A
2N/A if (name == NULL || environ == NULL)
2N/A return (NULL);
2N/A for (np = name; *np && *np != '='; ++np)
2N/A continue;
2N/A len = np - name;
2N/A for (p = environ; (c = *p) != NULL; ++p)
2N/A if (strncmp(c, name, len) == 0 && c[len] == '=') {
2N/A *offset = p - environ;
2N/A return (c + len + 1);
2N/A }
2N/A return (NULL);
2N/A}
2N/A#endif
2N/A
2N/A/*! \file */