Fixed error case + minor cleanup.
This commit is contained in:
parent
4b080b5a28
commit
ef473e636e
|
@ -17,8 +17,12 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/* 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 Malloc(type,n) ((type *) malloc(sizeof(type)*(n)))
|
||||||
#define Free(p) (free((void *)(p)))
|
|
||||||
|
|
||||||
extern char **environ;
|
extern char **environ;
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
@ -66,15 +70,14 @@ static int append_envvar(char *str, int old_envsize)
|
||||||
|
|
||||||
int putenv(char *str)
|
int putenv(char *str)
|
||||||
{
|
{
|
||||||
char **envp = environ;
|
char **envp;
|
||||||
char *equalsign = strchr(str, '=');
|
char *equalsign = strchr(str, '=');
|
||||||
int namelen;
|
int namelen;
|
||||||
char **newenv, **nenvp;
|
|
||||||
|
|
||||||
if( ! equalsign ) return 0; /* No equals sign in str! */
|
if( ! equalsign ) return 0; /* No equals sign in str! */
|
||||||
namelen = 1 + equalsign - str; /* Count the terminating =. */
|
namelen = 1 + equalsign - str; /* Count the terminating =. */
|
||||||
|
|
||||||
for(; *envp; envp++)
|
for(envp = environ; *envp; envp++)
|
||||||
if( ! memcmp(*envp, str, namelen) ) {
|
if( ! memcmp(*envp, str, namelen) ) {
|
||||||
*envp = str;
|
*envp = str;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -83,7 +86,6 @@ int putenv(char *str)
|
||||||
/* The env var wasn't defined. Copy the entire env to a new
|
/* The env var wasn't defined. Copy the entire env to a new
|
||||||
** block, and add the new definition.
|
** block, and add the new definition.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return append_envvar(str, envp-environ+1);
|
return append_envvar(str, envp-environ+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,18 +97,20 @@ int putenv(char *str)
|
||||||
** beginning with "<var>=" are deleted from environ. unsetenv
|
** beginning with "<var>=" are deleted from environ. unsetenv
|
||||||
** returns the number of occurrences it found and deleted; if
|
** returns the number of occurrences it found and deleted; if
|
||||||
** it returns 0, then the variable wasn't in environ to begin with.
|
** 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)
|
int unsetenv(const char *name)
|
||||||
{
|
{
|
||||||
char **envp, **target;
|
char **envp, **target;
|
||||||
int hits = 0;
|
int hits;
|
||||||
int slen;
|
int slen;
|
||||||
|
|
||||||
if( !name ) return;
|
if( !name ) return -1;
|
||||||
slen = strlen(name);
|
slen = strlen(name);
|
||||||
|
hits = 0;
|
||||||
target = environ;
|
target = environ;
|
||||||
|
|
||||||
for( envp=environ; *envp; envp++ )
|
for( envp=environ; *envp; envp++ )
|
||||||
if( !strncmp(*envp, name, slen) && (*envp)[slen] == '=' )
|
if( !strncmp(*envp, name, slen) && (*envp)[slen] == '=' )
|
||||||
hits++;
|
hits++;
|
||||||
|
@ -152,7 +156,6 @@ int setenv(const char *name, const char *val)
|
||||||
|
|
||||||
for( envp=environ; *envp; envp++ )
|
for( envp=environ; *envp; envp++ )
|
||||||
if( !strncmp(*envp, name, name_len) && (*envp)[name_len] == '=' ) {
|
if( !strncmp(*envp, name, name_len) && (*envp)[name_len] == '=' ) {
|
||||||
char *newval;
|
|
||||||
char *equal = name_len + *envp;
|
char *equal = name_len + *envp;
|
||||||
|
|
||||||
if( strlen(equal+1) >= val_len )
|
if( strlen(equal+1) >= val_len )
|
||||||
|
|
Loading…
Reference in New Issue