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