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