From ef473e636ee7fb5a87600b8d333f8ee288a8d545 Mon Sep 17 00:00:00 2001 From: shivers Date: Thu, 26 Oct 1995 13:36:23 +0000 Subject: [PATCH] Fixed error case + minor cleanup. --- scsh/putenv.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/scsh/putenv.c b/scsh/putenv.c index d1044a4..d165930 100644 --- a/scsh/putenv.c +++ b/scsh/putenv.c @@ -17,8 +17,12 @@ #include +/* Don't want to include stdlib.h because it declares putenv to take +** a const string -- bogus. So we'll just declare malloc directly. +*/ +extern void *malloc(size_t size); + #define Malloc(type,n) ((type *) malloc(sizeof(type)*(n))) -#define Free(p) (free((void *)(p))) extern char **environ; /*****************************************************************************/ @@ -66,15 +70,14 @@ static int append_envvar(char *str, int old_envsize) int putenv(char *str) { - char **envp = environ; + char **envp; char *equalsign = strchr(str, '='); int namelen; - char **newenv, **nenvp; if( ! equalsign ) return 0; /* No equals sign in str! */ namelen = 1 + equalsign - str; /* Count the terminating =. */ - for(; *envp; envp++) + for(envp = environ; *envp; envp++) if( ! memcmp(*envp, str, namelen) ) { *envp = str; return 0; @@ -83,7 +86,6 @@ int putenv(char *str) /* The env var wasn't defined. Copy the entire env to a new ** block, and add the new definition. */ - return append_envvar(str, envp-environ+1); } @@ -95,18 +97,20 @@ int putenv(char *str) ** beginning with "=" are deleted from environ. unsetenv ** returns the number of occurrences it found and deleted; if ** it returns 0, then the variable wasn't in environ to begin with. -** If name is the null pointer, unsetenv returns immediately. +** If name is the null pointer, unsetenv returns -1 immediately. */ int unsetenv(const char *name) { char **envp, **target; - int hits = 0; + int hits; int slen; - if( !name ) return; + if( !name ) return -1; slen = strlen(name); + hits = 0; target = environ; + for( envp=environ; *envp; envp++ ) if( !strncmp(*envp, name, slen) && (*envp)[slen] == '=' ) hits++; @@ -152,7 +156,6 @@ int setenv(const char *name, const char *val) for( envp=environ; *envp; envp++ ) if( !strncmp(*envp, name, name_len) && (*envp)[name_len] == '=' ) { - char *newval; char *equal = name_len + *envp; if( strlen(equal+1) >= val_len )