xref: /csrg-svn/lib/libc/stdlib/setenv.c (revision 42635)
133130Sbostic /*
233130Sbostic  * Copyright (c) 1987 Regents of the University of California.
333130Sbostic  * All rights reserved.
433130Sbostic  *
5*42635Sbostic  * %sccs.include.redist.c%
633130Sbostic  */
733130Sbostic 
833130Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*42635Sbostic static char sccsid[] = "@(#)setenv.c	5.4 (Berkeley) 06/01/90";
1033130Sbostic #endif /* LIBC_SCCS and not lint */
1133130Sbostic 
1242121Sbostic #include <stddef.h>
1342121Sbostic #include <stdlib.h>
1433130Sbostic 
1533130Sbostic /*
1633130Sbostic  * setenv --
1733130Sbostic  *	Set the value of the environmental variable "name" to be
1833130Sbostic  *	"value".  If rewrite is set, replace any current value.
1933130Sbostic  */
2033130Sbostic setenv(name, value, rewrite)
2133130Sbostic 	register char *name, *value;
2233130Sbostic 	int rewrite;
2333130Sbostic {
2433130Sbostic 	extern char **environ;
2533130Sbostic 	static int alloced;			/* if allocated space before */
2633130Sbostic 	register char *C;
2733130Sbostic 	int l_value, offset;
2842121Sbostic 	char *_findenv();
2933130Sbostic 
3033130Sbostic 	if (*value == '=')			/* no `=' in value */
3133130Sbostic 		++value;
3233130Sbostic 	l_value = strlen(value);
3333130Sbostic 	if ((C = _findenv(name, &offset))) {	/* find if already exists */
3433130Sbostic 		if (!rewrite)
3533130Sbostic 			return(0);
3633130Sbostic 		if (strlen(C) >= l_value) {	/* old larger; copy over */
3733130Sbostic 			while (*C++ = *value++);
3833130Sbostic 			return(0);
3933130Sbostic 		}
4033130Sbostic 	}
4133130Sbostic 	else {					/* create new slot */
4233130Sbostic 		register int	cnt;
4333130Sbostic 		register char	**P;
4433130Sbostic 
4533130Sbostic 		for (P = environ, cnt = 0; *P; ++P, ++cnt);
4633130Sbostic 		if (alloced) {			/* just increase size */
4733130Sbostic 			environ = (char **)realloc((char *)environ,
4842121Sbostic 			    (size_t)(sizeof(char *) * (cnt + 2)));
4933130Sbostic 			if (!environ)
5033130Sbostic 				return(-1);
5133130Sbostic 		}
5233130Sbostic 		else {				/* get new space */
5333130Sbostic 			alloced = 1;		/* copy old entries into it */
5442121Sbostic 			P = (char **)malloc((size_t)(sizeof(char *) *
5533130Sbostic 			    (cnt + 2)));
5633130Sbostic 			if (!P)
5733130Sbostic 				return(-1);
5833130Sbostic 			bcopy(environ, P, cnt * sizeof(char *));
5933130Sbostic 			environ = P;
6033130Sbostic 		}
6133130Sbostic 		environ[cnt + 1] = NULL;
6233130Sbostic 		offset = cnt;
6333130Sbostic 	}
6433130Sbostic 	for (C = name; *C && *C != '='; ++C);	/* no `=' in name */
6533130Sbostic 	if (!(environ[offset] =			/* name + `=' + value */
6642121Sbostic 	    malloc((size_t)((int)(C - name) + l_value + 2))))
6733130Sbostic 		return(-1);
6833130Sbostic 	for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
6933130Sbostic 	for (*C++ = '='; *C++ = *value++;);
7033130Sbostic 	return(0);
7133130Sbostic }
7233130Sbostic 
7333130Sbostic /*
7433130Sbostic  * unsetenv(name) --
7533130Sbostic  *	Delete environmental variable "name".
7633130Sbostic  */
7733130Sbostic void
7833130Sbostic unsetenv(name)
7933130Sbostic 	char	*name;
8033130Sbostic {
8142121Sbostic 	extern char **environ;
8242121Sbostic 	register char **P;
8342121Sbostic 	int offset;
8433130Sbostic 
8533130Sbostic 	while (_findenv(name, &offset))		/* if set multiple times */
8633130Sbostic 		for (P = &environ[offset];; ++P)
8733130Sbostic 			if (!(*P = *(P + 1)))
8833130Sbostic 				break;
8933130Sbostic }
90