1 /* This file may have been modified by DJ Delorie (Jan 1991). If so,
2 ** these modifications are Copyright (C) 1991 DJ Delorie.
6 * Copyright (c) 1987 Regents of the University of California.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that: (1) source distributions retain this entire copyright
11 * notice and comment, and (2) distributions including binaries display
12 * the following acknowledgement: ``This product includes software
13 * developed by the University of California, Berkeley and its contributors''
14 * in the documentation or other materials provided with the distribution.
15 * Neither the name of the University nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32 extern char **environ
;
34 /* Only deal with a pointer to environ, to work around subtle bugs with shared
35 libraries and/or small data systems where the user declares his own
37 static char ***p_environ
= &environ
;
39 /* _findenv_r is defined in getenv_r.c. */
40 extern char *_findenv_r (struct _reent
*, const char *, int *);
44 * Set the value of the environmental variable "name" to be
45 * "value". If rewrite is set, replace any current value.
46 * If "name" contains equal sign, -1 is returned, and errno is
51 _setenv_r (struct _reent
*reent_ptr
,
56 static int alloced
; /* if allocated space before */
60 if (strchr(name
, '='))
68 l_value
= strlen (value
);
69 if ((C
= _findenv_r (reent_ptr
, name
, &offset
)))
70 { /* find if already exists */
76 if (strlen (C
) >= l_value
)
77 { /* old larger; copy over */
78 while ((*C
++ = *value
++) != 0);
84 { /* create new slot */
88 for (P
= *p_environ
, cnt
= 0; *P
; ++P
, ++cnt
);
90 { /* just increase size */
91 *p_environ
= (char **) _realloc_r (reent_ptr
, (char *) environ
,
92 (size_t) (sizeof (char *) * (cnt
+ 2)));
100 { /* get new space */
101 alloced
= 1; /* copy old entries into it */
102 P
= (char **) _malloc_r (reent_ptr
, (size_t) (sizeof (char *) * (cnt
+ 2)));
108 memcpy((char *) P
,(char *) *p_environ
, cnt
* sizeof (char *));
111 (*p_environ
)[cnt
+ 1] = NULL
;
114 for (C
= (char *) name
; *C
&& *C
!= '='; ++C
); /* no `=' in name */
115 if (!((*p_environ
)[offset
] = /* name + `=' + value */
116 _malloc_r (reent_ptr
, (size_t) ((int) (C
- name
) + l_value
+ 2))))
121 for (C
= (*p_environ
)[offset
]; (*C
= *name
++) && *C
!= '='; ++C
);
122 for (*C
++ = '='; (*C
++ = *value
++) != 0;);
130 * _unsetenv_r(name) --
131 * Delete environmental variable "name".
134 _unsetenv_r (struct _reent
*reent_ptr
,
140 /* Name cannot be NULL, empty, or contain an equal sign. */
141 if (name
== NULL
|| name
[0] == '\0' || strchr(name
, '='))
149 while (_findenv_r (reent_ptr
, name
, &offset
)) /* if set multiple times */
151 for (P
= &(*p_environ
)[offset
];; ++P
)
152 if (!(*P
= *(P
+ 1)))