xref: /netbsd-src/lib/libc/stdlib/setenv.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: setenv.c,v 1.8 1995/12/28 08:52:49 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1987 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char *sccsid = "from: @(#)setenv.c	5.6 (Berkeley) 6/4/91";
39 #else
40 static char *rcsid = "$NetBSD: setenv.c,v 1.8 1995/12/28 08:52:49 thorpej Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include <stdlib.h>
45 #include <string.h>
46 
47 /*
48  * setenv --
49  *	Set the value of the environmental variable "name" to be
50  *	"value".  If rewrite is set, replace any current value.
51  */
52 int
53 setenv(name, value, rewrite)
54 	register const char *name;
55 	register const char *value;
56 	int rewrite;
57 {
58 	extern char **environ;
59 	static int alloced;			/* if allocated space before */
60 	register char *C;
61 	int l_value, offset;
62 	char *__findenv();
63 
64 	if (*value == '=')			/* no `=' in value */
65 		++value;
66 	l_value = strlen(value);
67 	if ((C = __findenv(name, &offset))) {	/* find if already exists */
68 		if (!rewrite)
69 			return (0);
70 		if (strlen(C) >= l_value) {	/* old larger; copy over */
71 			while (*C++ = *value++);
72 			return (0);
73 		}
74 	} else {					/* create new slot */
75 		register int	cnt;
76 		register char	**P;
77 
78 		for (P = environ, cnt = 0; *P; ++P, ++cnt);
79 		if (alloced) {			/* just increase size */
80 			environ = (char **)realloc((char *)environ,
81 			    (size_t)(sizeof(char *) * (cnt + 2)));
82 			if (!environ)
83 				return (-1);
84 		}
85 		else {				/* get new space */
86 			alloced = 1;		/* copy old entries into it */
87 			P = (char **)malloc((size_t)(sizeof(char *) *
88 			    (cnt + 2)));
89 			if (!P)
90 				return (-1);
91 			bcopy(environ, P, cnt * sizeof(char *));
92 			environ = P;
93 		}
94 		environ[cnt + 1] = NULL;
95 		offset = cnt;
96 	}
97 	for (C = (char *)name; *C && *C != '='; ++C);	/* no `=' in name */
98 	if (!(environ[offset] =			/* name + `=' + value */
99 	    malloc((size_t)((int)(C - name) + l_value + 2))))
100 		return (-1);
101 	for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
102 		;
103 	for (*C++ = '='; *C++ = *value++; )
104 		;
105 	return (0);
106 }
107 
108 /*
109  * unsetenv(name) --
110  *	Delete environmental variable "name".
111  */
112 void
113 unsetenv(name)
114 	const char	*name;
115 {
116 	extern char **environ;
117 	register char **P;
118 	int offset;
119 	char *__findenv();
120 
121 	while (__findenv(name, &offset))		/* if set multiple times */
122 		for (P = &environ[offset];; ++P)
123 			if (!(*P = *(P + 1)))
124 				break;
125 }
126