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*46599Sdonn static char sccsid[] = "@(#)setenv.c 5.5 (Berkeley) 02/23/91"; 1033130Sbostic #endif /* LIBC_SCCS and not lint */ 1133130Sbostic 1242121Sbostic #include <stddef.h> 1342121Sbostic #include <stdlib.h> 14*46599Sdonn #include <string.h> 1533130Sbostic 1633130Sbostic /* 1733130Sbostic * setenv -- 1833130Sbostic * Set the value of the environmental variable "name" to be 1933130Sbostic * "value". If rewrite is set, replace any current value. 2033130Sbostic */ 2133130Sbostic setenv(name, value, rewrite) 22*46599Sdonn register const char *name; 23*46599Sdonn register const char *value; 2433130Sbostic int rewrite; 2533130Sbostic { 2633130Sbostic extern char **environ; 2733130Sbostic static int alloced; /* if allocated space before */ 2833130Sbostic register char *C; 2933130Sbostic int l_value, offset; 3042121Sbostic char *_findenv(); 3133130Sbostic 3233130Sbostic if (*value == '=') /* no `=' in value */ 3333130Sbostic ++value; 3433130Sbostic l_value = strlen(value); 3533130Sbostic if ((C = _findenv(name, &offset))) { /* find if already exists */ 3633130Sbostic if (!rewrite) 3733130Sbostic return(0); 3833130Sbostic if (strlen(C) >= l_value) { /* old larger; copy over */ 3933130Sbostic while (*C++ = *value++); 4033130Sbostic return(0); 4133130Sbostic } 4233130Sbostic } 4333130Sbostic else { /* create new slot */ 4433130Sbostic register int cnt; 4533130Sbostic register char **P; 4633130Sbostic 4733130Sbostic 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) 5233130Sbostic return(-1); 5333130Sbostic } 5433130Sbostic else { /* get new space */ 5533130Sbostic alloced = 1; /* copy old entries into it */ 5642121Sbostic P = (char **)malloc((size_t)(sizeof(char *) * 5733130Sbostic (cnt + 2))); 5833130Sbostic if (!P) 5933130Sbostic return(-1); 6033130Sbostic bcopy(environ, P, cnt * sizeof(char *)); 6133130Sbostic environ = P; 6233130Sbostic } 6333130Sbostic environ[cnt + 1] = NULL; 6433130Sbostic offset = cnt; 6533130Sbostic } 66*46599Sdonn for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ 6733130Sbostic if (!(environ[offset] = /* name + `=' + value */ 6842121Sbostic malloc((size_t)((int)(C - name) + l_value + 2)))) 6933130Sbostic return(-1); 7033130Sbostic for (C = environ[offset]; (*C = *name++) && *C != '='; ++C); 7133130Sbostic for (*C++ = '='; *C++ = *value++;); 7233130Sbostic return(0); 7333130Sbostic } 7433130Sbostic 7533130Sbostic /* 7633130Sbostic * unsetenv(name) -- 7733130Sbostic * Delete environmental variable "name". 7833130Sbostic */ 7933130Sbostic void 8033130Sbostic unsetenv(name) 81*46599Sdonn const char *name; 8233130Sbostic { 8342121Sbostic extern char **environ; 8442121Sbostic register char **P; 8542121Sbostic int offset; 8633130Sbostic 8733130Sbostic while (_findenv(name, &offset)) /* if set multiple times */ 8833130Sbostic for (P = &environ[offset];; ++P) 8933130Sbostic if (!(*P = *(P + 1))) 9033130Sbostic break; 9133130Sbostic } 92