1 #pragma ident "%Z%%M% %I% %E% SMI"
3 * Copyright (c) 1987, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
35 /* based on @(#)setenv.c 8.1 (Berkeley) 6/4/93 */
36 /* based on @(#)getenv.c 8.1 (Berkeley) 6/4/93 */
49 static char *__findenv
__P((const char *, int *));
53 * Set the value of the environmental variable "name" to be
54 * "value". If rewrite is set, replace any current value.
57 krb5_setenv(name
, value
, rewrite
)
58 register const char *name
;
59 register const char *value
;
63 return (setenv(name
, value
, rewrite
));
65 extern char **environ
;
66 static int alloced
; /* if allocated space before */
70 if (*value
== '=') /* no `=' in value */
72 l_value
= strlen(value
);
73 if ((c
= __findenv(name
, &offset
))) { /* find if already exists */
76 if (strlen(c
) >= l_value
) { /* old larger; copy over */
77 while ((*c
++ = *value
++));
80 } else { /* create new slot */
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)));
91 else { /* get new space */
92 alloced
= 1; /* copy old entries into it */
93 p
= (char **)malloc((size_t)(sizeof(char *) * (cnt
+ 2)));
96 memcpy(p
, environ
, cnt
* sizeof(char *));
99 environ
[cnt
+ 1] = NULL
;
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))))
107 for (c
= environ
[offset
]; (*c
= *name
++) && (*c
!= '='); ++c
);
108 for (*c
++ = '='; (*c
++ = *value
++););
115 * Delete environmental variable "name".
124 extern char **environ
;
128 while (__findenv(name
, &offset
)) /* if set multiple times */
129 for (p
= &environ
[offset
];; ++p
)
131 if (!(*p
= *(p
+ 1)))
138 * Returns ptr to value associated with name, if any, else NULL.
145 return (getenv(name
));
149 return (__findenv(name
, &offset
));
153 #if (!HAVE_GETENV || !HAVE_SETENV || !HAVE_UNSETENV)
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.
162 __findenv(name
, offset
)
163 register const char *name
;
166 extern char **environ
;
168 register const char *np
;
169 register char **p
, *c
;
171 if (name
== NULL
|| environ
== NULL
)
173 for (np
= name
; *np
&& *np
!= '='; ++np
)
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);