Simplify, KNF and plug leaks. Compile test only.
This commit is contained in:
parent
a05a0bbcb8
commit
d02f179fc6
|
@ -1,4 +1,4 @@
|
||||||
/* $NetBSD: setenv.c,v 1.24 2005/02/17 02:17:43 christos Exp $ */
|
/* $NetBSD: setenv.c,v 1.25 2005/02/17 04:16:09 enami Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1987, 1993
|
* Copyright (c) 1987, 1993
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93";
|
static char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93";
|
||||||
#else
|
#else
|
||||||
__RCSID("$NetBSD: setenv.c,v 1.24 2005/02/17 02:17:43 christos Exp $");
|
__RCSID("$NetBSD: setenv.c,v 1.25 2005/02/17 04:16:09 enami Exp $");
|
||||||
#endif
|
#endif
|
||||||
#endif /* LIBC_SCCS and not lint */
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ setenv(name, value, rewrite)
|
||||||
int rewrite;
|
int rewrite;
|
||||||
{
|
{
|
||||||
static char **saveenv; /* copy of previously allocated space */
|
static char **saveenv; /* copy of previously allocated space */
|
||||||
char *c;
|
char *c, **newenv;
|
||||||
const char *cc;
|
const char *cc;
|
||||||
size_t l_value;
|
size_t l_value;
|
||||||
int offset;
|
int offset;
|
||||||
|
@ -86,8 +86,7 @@ setenv(name, value, rewrite)
|
||||||
if (!rewrite)
|
if (!rewrite)
|
||||||
goto good;
|
goto good;
|
||||||
if (strlen(c) >= l_value) { /* old larger; copy over */
|
if (strlen(c) >= l_value) { /* old larger; copy over */
|
||||||
while ((*c++ = *value++) != '\0')
|
(void)memcpy(c, value, l_value + 1);
|
||||||
continue;
|
|
||||||
goto good;
|
goto good;
|
||||||
}
|
}
|
||||||
} else { /* create new slot */
|
} else { /* create new slot */
|
||||||
|
@ -97,10 +96,11 @@ setenv(name, value, rewrite)
|
||||||
continue;
|
continue;
|
||||||
siz = (size_t)(sizeof(char *) * (cnt + 2));
|
siz = (size_t)(sizeof(char *) * (cnt + 2));
|
||||||
if (saveenv == environ) { /* just increase size */
|
if (saveenv == environ) { /* just increase size */
|
||||||
if ((saveenv = realloc(environ, siz)) == NULL)
|
if ((newenv = realloc(saveenv, siz)) == NULL)
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
saveenv = newenv;
|
||||||
else { /* get new space */
|
} else { /* get new space */
|
||||||
|
free(saveenv);
|
||||||
if ((saveenv = malloc(siz)) == NULL)
|
if ((saveenv = malloc(siz)) == NULL)
|
||||||
goto bad;
|
goto bad;
|
||||||
(void)memcpy(saveenv, environ, cnt * sizeof(char *));
|
(void)memcpy(saveenv, environ, cnt * sizeof(char *));
|
||||||
|
@ -114,10 +114,11 @@ setenv(name, value, rewrite)
|
||||||
if (!(environ[offset] = /* name + `=' + value */
|
if (!(environ[offset] = /* name + `=' + value */
|
||||||
malloc((size_t)((int)(cc - name) + l_value + 2))))
|
malloc((size_t)((int)(cc - name) + l_value + 2))))
|
||||||
goto bad;
|
goto bad;
|
||||||
for (c = environ[offset]; (*c = *name++) && *c != '='; ++c)
|
c = environ[offset];
|
||||||
continue;
|
(void)memcpy(c, name, cc - name);
|
||||||
for (*c++ = '='; (*c++ = *value++) != '\0'; )
|
c += cc - name;
|
||||||
continue;
|
*c++ = '=';
|
||||||
|
(void)memcpy(c, value, l_value + 1);
|
||||||
good:
|
good:
|
||||||
rwlock_unlock(&__environ_lock);
|
rwlock_unlock(&__environ_lock);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue