putenv.c revision 75584
1170613Sbms/* Copyright (C) 1991 Free Software Foundation, Inc. 2189592SbmsThis file is part of the GNU C Library. 3170613Sbms 4170613SbmsThe GNU C Library is free software; you can redistribute it and/or 5170613Sbmsmodify it under the terms of the GNU Library General Public License as 6170613Sbmspublished by the Free Software Foundation; either version 2 of the 7170613SbmsLicense, or (at your option) any later version. 8170613Sbms 9170613SbmsThe GNU C Library is distributed in the hope that it will be useful, 10170613Sbmsbut WITHOUT ANY WARRANTY; without even the implied warranty of 11170613SbmsMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12170613SbmsLibrary General Public License for more details. 13170613Sbms 14170613SbmsYou should have received a copy of the GNU Library General Public 15170613SbmsLicense along with the GNU C Library; see the file COPYING.LIB. If 16170613Sbmsnot, write to the Free Software Foundation, Inc., 675 Mass Ave, 17170613SbmsCambridge, MA 02139, USA. */ 18170613Sbms 19170613Sbms/* Hacked slightly by jjc@jclark.com for groff. */ 20170613Sbms 21170613Sbms#include <string.h> 22170613Sbms 23170613Sbms#ifdef __STDC__ 24170613Sbms#include <stddef.h> 25170613Sbmstypedef void *PTR; 26170613Sbmstypedef size_t SIZE_T; 27170613Sbms#else /* not __STDC__ */ 28170613Sbmstypedef char *PTR; 29170613Sbmstypedef int SIZE_T; 30170613Sbms#endif /* not __STDC__ */ 31170613Sbms 32170613Sbms#ifdef HAVE_STDLIB_H 33170613Sbms#include <stdlib.h> 34170613Sbms#else /* not HAVE_STDLIB_H */ 35170613SbmsPTR malloc(); 36170613Sbms#endif /* not HAVE_STDLIB_H */ 37170613Sbms 38189106Sbz#ifndef NULL 39189106Sbz#define NULL 0 40170613Sbms#endif 41170613Sbms 42170613Sbmsextern char **environ; 43170613Sbms 44170613Sbms/* Put STRING, which is of the form "NAME=VALUE", in the environment. */ 45171746Scsjp 46170613Sbmsint putenv(const char *string) 47170613Sbms{ 48189592Sbms char *name_end = strchr(string, '='); 49170613Sbms SIZE_T size; 50181803Sbz char **ep; 51189592Sbms 52189592Sbms if (name_end == NULL) 53170613Sbms { 54170613Sbms /* Remove the variable from the environment. */ 55170613Sbms size = strlen(string); 56170613Sbms for (ep = environ; *ep != NULL; ++ep) 57185571Sbz if (!strncmp(*ep, string, size) && (*ep)[size] == '=') 58170613Sbms { 59170613Sbms while (ep[1] != NULL) 60170613Sbms { 61170613Sbms ep[0] = ep[1]; 62170613Sbms ++ep; 63170613Sbms } 64170613Sbms *ep = NULL; 65185571Sbz return 0; 66170613Sbms } 67189592Sbms } 68189592Sbms 69189592Sbms size = 0; 70189592Sbms for (ep = environ; *ep != NULL; ++ep) 71170613Sbms if (!strncmp(*ep, string, name_end - string) 72170613Sbms && (*ep)[name_end - string] == '=') 73170613Sbms break; 74170613Sbms else 75170613Sbms ++size; 76170613Sbms 77170613Sbms if (*ep == NULL) 78170613Sbms { 79170613Sbms static char **last_environ = NULL; 80170613Sbms char **new_environ = (char **) malloc((size + 2) * sizeof(char *)); 81170613Sbms if (new_environ == NULL) 82189592Sbms return -1; 83189592Sbms (void) memcpy((PTR) new_environ, (PTR) environ, size * sizeof(char *)); 84170613Sbms new_environ[size] = (char *) string; 85170613Sbms new_environ[size + 1] = NULL; 86189592Sbms if (last_environ != NULL) 87189592Sbms free((PTR) last_environ); 88170613Sbms last_environ = new_environ; 89189592Sbms environ = new_environ; 90189592Sbms } 91189592Sbms else 92189592Sbms *ep = (char *) string; 93170613Sbms 94189592Sbms return 0; 95189592Sbms} 96189592Sbms