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