xref: /csrg-svn/lib/libc/stdlib/setenv.c (revision 61180)
133130Sbostic /*
2*61180Sbostic  * Copyright (c) 1987, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
433130Sbostic  *
542635Sbostic  * %sccs.include.redist.c%
633130Sbostic  */
733130Sbostic 
833130Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*61180Sbostic static char sccsid[] = "@(#)setenv.c	8.1 (Berkeley) 06/04/93";
1033130Sbostic #endif /* LIBC_SCCS and not lint */
1133130Sbostic 
1242121Sbostic #include <stddef.h>
1342121Sbostic #include <stdlib.h>
1446599Sdonn #include <string.h>
1533130Sbostic 
1654427Sbostic char *__findenv __P((const char *, int *));
1754427Sbostic 
1833130Sbostic /*
1933130Sbostic  * setenv --
2033130Sbostic  *	Set the value of the environmental variable "name" to be
2133130Sbostic  *	"value".  If rewrite is set, replace any current value.
2233130Sbostic  */
setenv(name,value,rewrite)2333130Sbostic setenv(name, value, rewrite)
2446599Sdonn 	register const char *name;
2546599Sdonn 	register const char *value;
2633130Sbostic 	int rewrite;
2733130Sbostic {
2833130Sbostic 	extern char **environ;
2933130Sbostic 	static int alloced;			/* if allocated space before */
3054427Sbostic 	register char *c;
3133130Sbostic 	int l_value, offset;
3233130Sbostic 
3333130Sbostic 	if (*value == '=')			/* no `=' in value */
3433130Sbostic 		++value;
3533130Sbostic 	l_value = strlen(value);
3654427Sbostic 	if ((c = __findenv(name, &offset))) {	/* find if already exists */
3733130Sbostic 		if (!rewrite)
3849994Skarels 			return (0);
3954427Sbostic 		if (strlen(c) >= l_value) {	/* old larger; copy over */
4054427Sbostic 			while (*c++ = *value++);
4149994Skarels 			return (0);
4233130Sbostic 		}
4349994Skarels 	} else {					/* create new slot */
4454427Sbostic 		register int cnt;
4554427Sbostic 		register char **p;
4633130Sbostic 
4754427Sbostic 		for (p = environ, cnt = 0; *p; ++p, ++cnt);
4833130Sbostic 		if (alloced) {			/* just increase size */
4933130Sbostic 			environ = (char **)realloc((char *)environ,
5042121Sbostic 			    (size_t)(sizeof(char *) * (cnt + 2)));
5133130Sbostic 			if (!environ)
5249994Skarels 				return (-1);
5333130Sbostic 		}
5433130Sbostic 		else {				/* get new space */
5533130Sbostic 			alloced = 1;		/* copy old entries into it */
5654427Sbostic 			p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
5754427Sbostic 			if (!p)
5849994Skarels 				return (-1);
5954427Sbostic 			bcopy(environ, p, cnt * sizeof(char *));
6054427Sbostic 			environ = p;
6133130Sbostic 		}
6233130Sbostic 		environ[cnt + 1] = NULL;
6333130Sbostic 		offset = cnt;
6433130Sbostic 	}
6554427Sbostic 	for (c = (char *)name; *c && *c != '='; ++c);	/* no `=' in name */
6633130Sbostic 	if (!(environ[offset] =			/* name + `=' + value */
6754427Sbostic 	    malloc((size_t)((int)(c - name) + l_value + 2))))
6849994Skarels 		return (-1);
6954427Sbostic 	for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
7054427Sbostic 	for (*c++ = '='; *c++ = *value++;);
7149994Skarels 	return (0);
7233130Sbostic }
7333130Sbostic 
7433130Sbostic /*
7533130Sbostic  * unsetenv(name) --
7633130Sbostic  *	Delete environmental variable "name".
7733130Sbostic  */
7833130Sbostic void
unsetenv(name)7933130Sbostic unsetenv(name)
8054427Sbostic 	const char *name;
8133130Sbostic {
8242121Sbostic 	extern char **environ;
8354427Sbostic 	register char **p;
8442121Sbostic 	int offset;
8533130Sbostic 
8654427Sbostic 	while (__findenv(name, &offset))	/* if set multiple times */
8754427Sbostic 		for (p = &environ[offset];; ++p)
8854427Sbostic 			if (!(*p = *(p + 1)))
8933130Sbostic 				break;
9033130Sbostic }
91