133130Sbostic /* 233130Sbostic * Copyright (c) 1987 Regents of the University of California. 333130Sbostic * All rights reserved. 433130Sbostic * 542635Sbostic * %sccs.include.redist.c% 633130Sbostic */ 733130Sbostic 833130Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*54427Sbostic static char sccsid[] = "@(#)setenv.c 5.7 (Berkeley) 06/25/92"; 1033130Sbostic #endif /* LIBC_SCCS and not lint */ 1133130Sbostic 1242121Sbostic #include <stddef.h> 1342121Sbostic #include <stdlib.h> 1446599Sdonn #include <string.h> 1533130Sbostic 16*54427Sbostic char *__findenv __P((const char *, int *)); 17*54427Sbostic 1833130Sbostic /* 1933130Sbostic * setenv -- 2033130Sbostic * Set the value of the environmental variable "name" to be 2133130Sbostic * "value". If rewrite is set, replace any current value. 2233130Sbostic */ 2333130Sbostic setenv(name, value, rewrite) 2446599Sdonn register const char *name; 2546599Sdonn register const char *value; 2633130Sbostic int rewrite; 2733130Sbostic { 2833130Sbostic extern char **environ; 2933130Sbostic static int alloced; /* if allocated space before */ 30*54427Sbostic register char *c; 3133130Sbostic int l_value, offset; 3233130Sbostic 3333130Sbostic if (*value == '=') /* no `=' in value */ 3433130Sbostic ++value; 3533130Sbostic l_value = strlen(value); 36*54427Sbostic if ((c = __findenv(name, &offset))) { /* find if already exists */ 3733130Sbostic if (!rewrite) 3849994Skarels return (0); 39*54427Sbostic if (strlen(c) >= l_value) { /* old larger; copy over */ 40*54427Sbostic while (*c++ = *value++); 4149994Skarels return (0); 4233130Sbostic } 4349994Skarels } else { /* create new slot */ 44*54427Sbostic register int cnt; 45*54427Sbostic register char **p; 4633130Sbostic 47*54427Sbostic for (p = environ, cnt = 0; *p; ++p, ++cnt); 4833130Sbostic if (alloced) { /* just increase size */ 4933130Sbostic environ = (char **)realloc((char *)environ, 5042121Sbostic (size_t)(sizeof(char *) * (cnt + 2))); 5133130Sbostic if (!environ) 5249994Skarels return (-1); 5333130Sbostic } 5433130Sbostic else { /* get new space */ 5533130Sbostic alloced = 1; /* copy old entries into it */ 56*54427Sbostic p = malloc((size_t)(sizeof(char *) * (cnt + 2))); 57*54427Sbostic if (!p) 5849994Skarels return (-1); 59*54427Sbostic bcopy(environ, p, cnt * sizeof(char *)); 60*54427Sbostic environ = p; 6133130Sbostic } 6233130Sbostic environ[cnt + 1] = NULL; 6333130Sbostic offset = cnt; 6433130Sbostic } 65*54427Sbostic for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */ 6633130Sbostic if (!(environ[offset] = /* name + `=' + value */ 67*54427Sbostic malloc((size_t)((int)(c - name) + l_value + 2)))) 6849994Skarels return (-1); 69*54427Sbostic for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); 70*54427Sbostic for (*c++ = '='; *c++ = *value++;); 7149994Skarels return (0); 7233130Sbostic } 7333130Sbostic 7433130Sbostic /* 7533130Sbostic * unsetenv(name) -- 7633130Sbostic * Delete environmental variable "name". 7733130Sbostic */ 7833130Sbostic void 7933130Sbostic unsetenv(name) 80*54427Sbostic const char *name; 8133130Sbostic { 8242121Sbostic extern char **environ; 83*54427Sbostic register char **p; 8442121Sbostic int offset; 8533130Sbostic 86*54427Sbostic while (__findenv(name, &offset)) /* if set multiple times */ 87*54427Sbostic for (p = &environ[offset];; ++p) 88*54427Sbostic if (!(*p = *(p + 1))) 8933130Sbostic break; 9033130Sbostic } 91