putenv.c revision 75584
175584Sru/* Copyright (C) 1991 Free Software Foundation, Inc. 275584SruThis file is part of the GNU C Library. 375584Sru 475584SruThe GNU C Library is free software; you can redistribute it and/or 575584Srumodify it under the terms of the GNU Library General Public License as 675584Srupublished by the Free Software Foundation; either version 2 of the 775584SruLicense, or (at your option) any later version. 875584Sru 975584SruThe GNU C Library is distributed in the hope that it will be useful, 1075584Srubut WITHOUT ANY WARRANTY; without even the implied warranty of 1175584SruMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1275584SruLibrary General Public License for more details. 1375584Sru 1475584SruYou should have received a copy of the GNU Library General Public 1575584SruLicense along with the GNU C Library; see the file COPYING.LIB. If 1675584Srunot, write to the Free Software Foundation, Inc., 675 Mass Ave, 1775584SruCambridge, MA 02139, USA. */ 1875584Sru 1975584Sru/* Hacked slightly by jjc@jclark.com for groff. */ 2075584Sru 2175584Sru#include <string.h> 2275584Sru 2375584Sru#ifdef __STDC__ 2475584Sru#include <stddef.h> 2575584Srutypedef void *PTR; 2675584Srutypedef size_t SIZE_T; 2775584Sru#else /* not __STDC__ */ 2875584Srutypedef char *PTR; 2975584Srutypedef int SIZE_T; 3075584Sru#endif /* not __STDC__ */ 3175584Sru 3275584Sru#ifdef HAVE_STDLIB_H 3375584Sru#include <stdlib.h> 3475584Sru#else /* not HAVE_STDLIB_H */ 3575584SruPTR malloc(); 3675584Sru#endif /* not HAVE_STDLIB_H */ 3775584Sru 3875584Sru#ifndef NULL 3975584Sru#define NULL 0 4075584Sru#endif 4175584Sru 4275584Sruextern char **environ; 4375584Sru 4475584Sru/* Put STRING, which is of the form "NAME=VALUE", in the environment. */ 4575584Sru 4675584Sruint putenv(const char *string) 4775584Sru{ 4875584Sru char *name_end = strchr(string, '='); 4975584Sru SIZE_T size; 5075584Sru char **ep; 5175584Sru 5275584Sru if (name_end == NULL) 5375584Sru { 5475584Sru /* Remove the variable from the environment. */ 5575584Sru size = strlen(string); 5675584Sru for (ep = environ; *ep != NULL; ++ep) 5775584Sru if (!strncmp(*ep, string, size) && (*ep)[size] == '=') 5875584Sru { 5975584Sru while (ep[1] != NULL) 6075584Sru { 6175584Sru ep[0] = ep[1]; 6275584Sru ++ep; 6375584Sru } 6475584Sru *ep = NULL; 6575584Sru return 0; 6675584Sru } 6775584Sru } 6875584Sru 6975584Sru size = 0; 7075584Sru for (ep = environ; *ep != NULL; ++ep) 7175584Sru if (!strncmp(*ep, string, name_end - string) 7275584Sru && (*ep)[name_end - string] == '=') 7375584Sru break; 7475584Sru else 7575584Sru ++size; 7675584Sru 7775584Sru if (*ep == NULL) 7875584Sru { 7975584Sru static char **last_environ = NULL; 8075584Sru char **new_environ = (char **) malloc((size + 2) * sizeof(char *)); 8175584Sru if (new_environ == NULL) 8275584Sru return -1; 8375584Sru (void) memcpy((PTR) new_environ, (PTR) environ, size * sizeof(char *)); 8475584Sru new_environ[size] = (char *) string; 8575584Sru new_environ[size + 1] = NULL; 8675584Sru if (last_environ != NULL) 8775584Sru free((PTR) last_environ); 8875584Sru last_environ = new_environ; 8975584Sru environ = new_environ; 9075584Sru } 9175584Sru else 9275584Sru *ep = (char *) string; 9375584Sru 9475584Sru return 0; 9575584Sru} 96