1
1
#if defined(LIBC_SCCS) && !defined(lint)
2
2
static const char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93";
3
static const char rcsid[] = "$Id: setenv.c,v 1.1 2001/03/29 06:30:35 marka Exp $";
3
static const char rcsid[] = "$Id: setenv.c,v 1.2 2005/04/27 04:56:11 sra Exp $";
4
4
#endif /* LIBC_SCCS and not lint */
53
53
static char *findenv(const char *name, int *offset);
57
57
* Set the value of the environmental variable "name" to be
58
58
* "value". If rewrite is set, replace any current value.
60
60
setenv(const char *name, const char *value, int rewrite) {
61
61
extern char **environ;
62
static int alloced; /* if allocated space before */
62
static int alloced; /*%< if allocated space before */
64
64
int l_value, offset;
66
if (*value == '=') /* no `=' in value */
66
if (*value == '=') /*%< no `=' in value */
68
68
l_value = strlen(value);
69
if ((c = findenv(name, &offset))) { /* find if already exists */
69
if ((c = findenv(name, &offset))) { /*%< find if already exists */
72
if (strlen(c) >= l_value) { /* old larger; copy over */
72
if (strlen(c) >= l_value) { /*%< old larger; copy over */
73
73
while (*c++ = *value++);
76
} else { /* create new slot */
76
} else { /*%< create new slot */
80
80
for (p = environ, cnt = 0; *p; ++p, ++cnt);
81
if (alloced) { /* just increase size */
81
if (alloced) { /*%< just increase size */
82
82
environ = (char **)realloc((char *)environ,
83
83
(size_t)(sizeof(char *) * (cnt + 2)));
87
else { /* get new space */
88
alloced = 1; /* copy old entries into it */
87
else { /*%< get new space */
88
alloced = 1; /*%< copy old entries into it */
89
89
p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
95
95
environ[cnt + 1] = NULL;
98
for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
99
if (!(environ[offset] = /* name + `=' + value */
98
for (c = (char *)name; *c && *c != '='; ++c); /*%< no `=' in name */
99
if (!(environ[offset] = /*%< name + `=' + value */
100
100
malloc((size_t)((int)(c - name) + l_value + 2))))
102
102
for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
116
while (findenv(name, &offset)) /* if set multiple times */
116
while (findenv(name, &offset)) /*%< if set multiple times */
117
117
for (p = &environ[offset];; ++p)
118
118
if (!(*p = *(p + 1)))
124
124
* Returns pointer to value associated with name, if any, else NULL.
125
125
* Sets offset to be the offset of the name/value combination in the