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*49994Skarels static char sccsid[] = "@(#)setenv.c 5.6 (Berkeley) 06/04/91"; 1033130Sbostic #endif /* LIBC_SCCS and not lint */ 1133130Sbostic 1242121Sbostic #include <stddef.h> 1342121Sbostic #include <stdlib.h> 1446599Sdonn #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) 2246599Sdonn register const char *name; 2346599Sdonn 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) 37*49994Skarels return (0); 3833130Sbostic if (strlen(C) >= l_value) { /* old larger; copy over */ 3933130Sbostic while (*C++ = *value++); 40*49994Skarels return (0); 4133130Sbostic } 42*49994Skarels } else { /* create new slot */ 4333130Sbostic register int cnt; 4433130Sbostic register char **P; 4533130Sbostic 4633130Sbostic for (P = environ, cnt = 0; *P; ++P, ++cnt); 4733130Sbostic if (alloced) { /* just increase size */ 4833130Sbostic environ = (char **)realloc((char *)environ, 4942121Sbostic (size_t)(sizeof(char *) * (cnt + 2))); 5033130Sbostic if (!environ) 51*49994Skarels return (-1); 5233130Sbostic } 5333130Sbostic else { /* get new space */ 5433130Sbostic alloced = 1; /* copy old entries into it */ 5542121Sbostic P = (char **)malloc((size_t)(sizeof(char *) * 5633130Sbostic (cnt + 2))); 5733130Sbostic if (!P) 58*49994Skarels return (-1); 5933130Sbostic bcopy(environ, P, cnt * sizeof(char *)); 6033130Sbostic environ = P; 6133130Sbostic } 6233130Sbostic environ[cnt + 1] = NULL; 6333130Sbostic offset = cnt; 6433130Sbostic } 6546599Sdonn for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ 6633130Sbostic if (!(environ[offset] = /* name + `=' + value */ 6742121Sbostic malloc((size_t)((int)(C - name) + l_value + 2)))) 68*49994Skarels return (-1); 69*49994Skarels for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) 70*49994Skarels ; 71*49994Skarels for (*C++ = '='; *C++ = *value++; ) 72*49994Skarels ; 73*49994Skarels return (0); 7433130Sbostic } 7533130Sbostic 7633130Sbostic /* 7733130Sbostic * unsetenv(name) -- 7833130Sbostic * Delete environmental variable "name". 7933130Sbostic */ 8033130Sbostic void 8133130Sbostic unsetenv(name) 8246599Sdonn const char *name; 8333130Sbostic { 8442121Sbostic extern char **environ; 8542121Sbostic register char **P; 8642121Sbostic int offset; 8733130Sbostic 8833130Sbostic while (_findenv(name, &offset)) /* if set multiple times */ 8933130Sbostic for (P = &environ[offset];; ++P) 9033130Sbostic if (!(*P = *(P + 1))) 9133130Sbostic break; 9233130Sbostic } 93