Blame missing_d/setenv.c

Packit 575503
/*	$NetBSD: setenv.c,v 1.1.1.1 2009/04/12 15:33:26 christos Exp $	*/
Packit 575503
Packit 575503
#if defined(LIBC_SCCS) && !defined(lint)
Packit 575503
static const char sccsid[] = "@(#)setenv.c	8.1 (Berkeley) 6/4/93";
Packit 575503
static const char rcsid[] = "Id: setenv.c,v 1.2 2005/04/27 04:56:11 sra Exp";
Packit 575503
#endif /* LIBC_SCCS and not lint */
Packit 575503
Packit 575503
/*
Packit 575503
 * Copyright (c) 1987, 1993
Packit 575503
 *	The Regents of the University of California.  All rights reserved.
Packit 575503
 *
Packit 575503
 * Redistribution and use in source and binary forms, with or without
Packit 575503
 * modification, are permitted provided that the following conditions
Packit 575503
 * are met:
Packit 575503
 * 1. Redistributions of source code must retain the above copyright
Packit 575503
 *    notice, this list of conditions and the following disclaimer.
Packit 575503
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 575503
 *    notice, this list of conditions and the following disclaimer in the
Packit 575503
 *    documentation and/or other materials provided with the distribution.
Packit 575503
 * 3. Neither the name of the University nor the names of its contributors
Packit 575503
 *    may be used to endorse or promote products derived from this software
Packit 575503
 *    without specific prior written permission.
Packit 575503
 *
Packit 575503
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 575503
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 575503
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 575503
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 575503
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 575503
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 575503
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 575503
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 575503
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 575503
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 575503
 * SUCH DAMAGE.
Packit 575503
 */
Packit 575503
Packit 575503
/*
Packit 575503
 * Per the statement at http://opensource.org/licenses/bsd-license.php,
Packit 575503
 *
Packit 575503
 *	The advertising clause in the license appearing on BSD Unix files was
Packit 575503
 *	officially rescinded by the Director of the Office of Technology
Packit 575503
 *	Licensing of the University of California on July 22 1999. He states
Packit 575503
 *	that clause 3 is "hereby deleted in its entirety."
Packit 575503
 *
Packit 575503
 * I removed the advertising clause in the above copyright.
Packit 575503
 * The above web site points to
Packit 575503
 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change.
Packit 575503
 *
Packit 575503
 * Arnold Robbins
Packit 575503
 * 7 September 2016
Packit 575503
 */
Packit 575503
Packit 575503
/* #include "port_before.h" */
Packit 575503
Packit 575503
#include <stddef.h>
Packit 575503
#include <stdlib.h>
Packit 575503
#include <string.h>
Packit 575503
#include <errno.h>
Packit 575503
Packit 575503
/* #include "port_after.h" */
Packit 575503
Packit 575503
#define NEED_SETENV
Packit 575503
#if !defined(NEED_SETENV)
Packit 575503
int __bindcompat_setenv;
Packit 575503
#else
Packit 575503
Packit 575503
/*
Packit 575503
 * Gawk changes:
Packit 575503
 * 1. Comment out "port_before.h" and "port_after.h" includes.
Packit 575503
 * 2. Define NEED_SETENV.
Packit 575503
 * 3. Add leading "int" to setenv() for C99 compilers. Which is worse,
Packit 575503
 *    that it wasn't there in the first place, or that C99 now makes
Packit 575503
 *    such functions void, breaking 40+ years of historical practice?
Packit 575503
 * 4. Change return type of unsetenv to "int" for standards
Packit 575503
 *    conformance and check for name containing an "=" in
Packit 575503
 *    which case it return an error. (See unsetenv(3), at least on Linux.)
Packit 575503
 */
Packit 575503
Packit 575503
extern char **environ;
Packit 575503
Packit 575503
static char *findenv(const char *name, int *offset);
Packit 575503
Packit 575503
/*%
Packit 575503
 * setenv --
Packit 575503
 *	Set the value of the environmental variable "name" to be
Packit 575503
 *	"value".  If rewrite is set, replace any current value.
Packit 575503
 */
Packit 575503
int
Packit 575503
setenv(const char *name, const char *value, int rewrite)
Packit 575503
{
Packit 575503
	extern char **environ;
Packit 575503
	static int alloced;			/*%< if allocated space before */
Packit 575503
	char *c;
Packit 575503
	int l_value, offset;
Packit 575503
Packit 575503
	if (*value == '=')			/*%< no `=' in value */
Packit 575503
		++value;
Packit 575503
	l_value = strlen(value);
Packit 575503
	if ((c = findenv(name, &offset))) {	/*%< find if already exists */
Packit 575503
		if (!rewrite)
Packit 575503
			return (0);
Packit 575503
		if (strlen(c) >= l_value) {	/*%< old larger; copy over */
Packit 575503
			while (*c++ = *value++);
Packit 575503
			return (0);
Packit 575503
		}
Packit 575503
	} else {					/*%< create new slot */
Packit 575503
		int cnt;
Packit 575503
		char **p;
Packit 575503
Packit 575503
		for (p = environ, cnt = 0; *p; ++p, ++cnt);
Packit 575503
		if (alloced) {			/*%< just increase size */
Packit 575503
			environ = (char **)realloc((char *)environ,
Packit 575503
			    (size_t)(sizeof(char *) * (cnt + 2)));
Packit 575503
			if (!environ)
Packit 575503
				return (-1);
Packit 575503
		}
Packit 575503
		else {				/*%< get new space */
Packit 575503
			alloced = 1;		/*%< copy old entries into it */
Packit 575503
			p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
Packit 575503
			if (!p)
Packit 575503
				return (-1);
Packit 575503
			memcpy(p, environ, cnt * sizeof(char *));
Packit 575503
			environ = p;
Packit 575503
		}
Packit 575503
		environ[cnt + 1] = NULL;
Packit 575503
		offset = cnt;
Packit 575503
	}
Packit 575503
	for (c = (char *)name; *c && *c != '='; ++c);	/*%< no `=' in name */
Packit 575503
	if (!(environ[offset] =			/*%< name + `=' + value */
Packit 575503
	    malloc((size_t)((int)(c - name) + l_value + 2))))
Packit 575503
		return (-1);
Packit 575503
	for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
Packit 575503
	for (*c++ = '='; *c++ = *value++;);
Packit 575503
	return (0);
Packit 575503
}
Packit 575503
Packit 575503
/*%
Packit 575503
 * unsetenv(name) --
Packit 575503
 *	Delete environmental variable "name".
Packit 575503
 */
Packit 575503
int
Packit 575503
unsetenv(const char *name)
Packit 575503
{
Packit 575503
	char **p;
Packit 575503
	int offset;
Packit 575503
Packit 575503
	if (strchr(name, '=') != NULL) {
Packit 575503
		errno = EINVAL;
Packit 575503
		return -1;
Packit 575503
	}
Packit 575503
Packit 575503
	while (findenv(name, &offset))	/*%< if set multiple times */
Packit 575503
		for (p = &environ[offset];; ++p)
Packit 575503
			if (!(*p = *(p + 1)))
Packit 575503
				break;
Packit 575503
Packit 575503
	return 0;
Packit 575503
}
Packit 575503
Packit 575503
/*%
Packit 575503
 * findenv --
Packit 575503
 *	Returns pointer to value associated with name, if any, else NULL.
Packit 575503
 *	Sets offset to be the offset of the name/value combination in the
Packit 575503
 *	environmental array, for use by setenv(3) and unsetenv(3).
Packit 575503
 *	Explicitly removes '=' in argument name.
Packit 575503
 *
Packit 575503
 *	This routine *should* be a static; don't use it.
Packit 575503
 */
Packit 575503
static char *
Packit 575503
findenv(const char *name, int *offset)
Packit 575503
{
Packit 575503
	const char *np;
Packit 575503
	char **p, *c;
Packit 575503
	int len;
Packit 575503
Packit 575503
	if (name == NULL || environ == NULL)
Packit 575503
		return (NULL);
Packit 575503
	for (np = name; *np && *np != '='; ++np)
Packit 575503
		continue;
Packit 575503
	len = np - name;
Packit 575503
	for (p = environ; (c = *p) != NULL; ++p)
Packit 575503
		if (strncmp(c, name, len) == 0 && c[len] == '=') {
Packit 575503
			*offset = p - environ;
Packit 575503
			return (c + len + 1);
Packit 575503
		}
Packit 575503
	return (NULL);
Packit 575503
}
Packit 575503
#endif
Packit 575503
Packit 575503
/*! \file */