1*33130Sbostic /* 2*33130Sbostic * Copyright (c) 1987 Regents of the University of California. 3*33130Sbostic * All rights reserved. 4*33130Sbostic * 5*33130Sbostic * Redistribution and use in source and binary forms are permitted 6*33130Sbostic * provided that this notice is preserved and that due credit is given 7*33130Sbostic * to the University of California at Berkeley. The name of the University 8*33130Sbostic * may not be used to endorse or promote products derived from this 9*33130Sbostic * software without specific prior written permission. This software 10*33130Sbostic * is provided ``as is'' without express or implied warranty. 11*33130Sbostic */ 12*33130Sbostic 13*33130Sbostic #if defined(LIBC_SCCS) && !defined(lint) 14*33130Sbostic static char sccsid[] = "@(#)setenv.c 5.1 (Berkeley) 12/24/87"; 15*33130Sbostic #endif /* LIBC_SCCS and not lint */ 16*33130Sbostic 17*33130Sbostic #include <sys/types.h> 18*33130Sbostic #include <stdio.h> 19*33130Sbostic 20*33130Sbostic /* 21*33130Sbostic * setenv -- 22*33130Sbostic * Set the value of the environmental variable "name" to be 23*33130Sbostic * "value". If rewrite is set, replace any current value. 24*33130Sbostic */ 25*33130Sbostic setenv(name, value, rewrite) 26*33130Sbostic register char *name, *value; 27*33130Sbostic int rewrite; 28*33130Sbostic { 29*33130Sbostic extern char **environ; 30*33130Sbostic static int alloced; /* if allocated space before */ 31*33130Sbostic register char *C; 32*33130Sbostic int l_value, offset; 33*33130Sbostic char *malloc(), *realloc(), *_findenv(); 34*33130Sbostic 35*33130Sbostic if (*value == '=') /* no `=' in value */ 36*33130Sbostic ++value; 37*33130Sbostic l_value = strlen(value); 38*33130Sbostic if ((C = _findenv(name, &offset))) { /* find if already exists */ 39*33130Sbostic if (!rewrite) 40*33130Sbostic return(0); 41*33130Sbostic if (strlen(C) >= l_value) { /* old larger; copy over */ 42*33130Sbostic while (*C++ = *value++); 43*33130Sbostic return(0); 44*33130Sbostic } 45*33130Sbostic } 46*33130Sbostic else { /* create new slot */ 47*33130Sbostic register int cnt; 48*33130Sbostic register char **P; 49*33130Sbostic 50*33130Sbostic for (P = environ, cnt = 0; *P; ++P, ++cnt); 51*33130Sbostic if (alloced) { /* just increase size */ 52*33130Sbostic environ = (char **)realloc((char *)environ, 53*33130Sbostic (u_int)(sizeof(char *) * (cnt + 2))); 54*33130Sbostic if (!environ) 55*33130Sbostic return(-1); 56*33130Sbostic } 57*33130Sbostic else { /* get new space */ 58*33130Sbostic alloced = 1; /* copy old entries into it */ 59*33130Sbostic P = (char **)malloc((u_int)(sizeof(char *) * 60*33130Sbostic (cnt + 2))); 61*33130Sbostic if (!P) 62*33130Sbostic return(-1); 63*33130Sbostic bcopy(environ, P, cnt * sizeof(char *)); 64*33130Sbostic environ = P; 65*33130Sbostic } 66*33130Sbostic environ[cnt + 1] = NULL; 67*33130Sbostic offset = cnt; 68*33130Sbostic } 69*33130Sbostic for (C = name; *C && *C != '='; ++C); /* no `=' in name */ 70*33130Sbostic if (!(environ[offset] = /* name + `=' + value */ 71*33130Sbostic malloc((u_int)((int)(C - name) + l_value + 2)))) 72*33130Sbostic return(-1); 73*33130Sbostic for (C = environ[offset]; (*C = *name++) && *C != '='; ++C); 74*33130Sbostic for (*C++ = '='; *C++ = *value++;); 75*33130Sbostic return(0); 76*33130Sbostic } 77*33130Sbostic 78*33130Sbostic /* 79*33130Sbostic * unsetenv(name) -- 80*33130Sbostic * Delete environmental variable "name". 81*33130Sbostic */ 82*33130Sbostic void 83*33130Sbostic unsetenv(name) 84*33130Sbostic char *name; 85*33130Sbostic { 86*33130Sbostic extern char **environ; 87*33130Sbostic register char **P; 88*33130Sbostic int offset; 89*33130Sbostic 90*33130Sbostic while (_findenv(name, &offset)) /* if set multiple times */ 91*33130Sbostic for (P = &environ[offset];; ++P) 92*33130Sbostic if (!(*P = *(P + 1))) 93*33130Sbostic break; 94*33130Sbostic } 95