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