diff options
Diffstat (limited to '')
-rw-r--r-- | lib/env.c (renamed from libmisc/env.c) | 73 |
1 files changed, 28 insertions, 45 deletions
diff --git a/libmisc/env.c b/lib/env.c index fc6dbce..9b5fd32 100644 --- a/libmisc/env.c +++ b/lib/env.c @@ -15,9 +15,14 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + +#include "alloc.h" #include "prototypes.h" #include "defines.h" #include "shadowlog.h" +#include "string/sprintf.h" + + /* * NEWENVP_STEP must be a power of two. This is the number * of (char *) pointers to allocate at a time, to avoid using @@ -26,7 +31,6 @@ #define NEWENVP_STEP 16 size_t newenvc = 0; /*@null@*/char **newenvp = NULL; -extern char **environ; static const char *const forbid[] = { "_RLD_=", @@ -42,7 +46,7 @@ static const char *const forbid[] = { "PATH=", "SHELL=", "SHLIB_PATH=", - (char *) 0 + NULL }; /* these are allowed, but with no slashes inside @@ -51,7 +55,7 @@ static const char *const noslash[] = { "LANG=", "LANGUAGE=", "LC_", /* anything with the LC_ prefix */ - (char *) 0 + NULL }; /* @@ -59,23 +63,18 @@ static const char *const noslash[] = { */ void initenv (void) { - newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *)); + newenvp = XMALLOC(NEWENVP_STEP, char *); *newenvp = NULL; } void addenv (const char *string, /*@null@*/const char *value) { - char *cp, *newstring; - size_t i; - size_t n; + char *cp, *newstring; + size_t i, n; if (NULL != value) { - size_t len = strlen (string) + strlen (value) + 2; - int wlen; - newstring = xmalloc (len); - wlen = snprintf (newstring, len, "%s=%s", string, value); - assert (wlen == (int) len -1); + xasprintf(&newstring, "%s=%s", string, value); } else { newstring = xstrdup (string); } @@ -87,7 +86,7 @@ void addenv (const char *string, /*@null@*/const char *value) cp = strchr (newstring, '='); if (NULL == cp) { - free (newstring); + free(newstring); return; } @@ -104,7 +103,7 @@ void addenv (const char *string, /*@null@*/const char *value) } if (i < newenvc) { - free (newenvp[i]); + free(newenvp[i]); newenvp[i] = newstring; return; } @@ -127,32 +126,19 @@ void addenv (const char *string, /*@null@*/const char *value) */ if ((newenvc & (NEWENVP_STEP - 1)) == 0) { - char **__newenvp; - size_t newsize; + bool update_environ; + + update_environ = (environ == newenvp); + + newenvp = XREALLOC(newenvp, newenvc + NEWENVP_STEP, char *); /* - * If the resize operation succeeds we can - * happily go on, else print a message. + * If this is our current environment, update + * environ so that it doesn't point to some + * free memory area (realloc() could move it). */ - - newsize = (newenvc + NEWENVP_STEP) * sizeof (char *); - __newenvp = (char **) realloc (newenvp, newsize); - - if (NULL != __newenvp) { - /* - * If this is our current environment, update - * environ so that it doesn't point to some - * free memory area (realloc() could move it). - */ - if (environ == newenvp) { - environ = __newenvp; - } - newenvp = __newenvp; - } else { - (void) fputs (_("Environment overflow\n"), log_get_logfd()); - newenvc--; - free (newenvp[newenvc]); - } + if (update_environ) + environ = newenvp; } /* @@ -168,9 +154,9 @@ void addenv (const char *string, /*@null@*/const char *value) */ void set_env (int argc, char *const *argv) { - int noname = 1; - char variable[1024]; - char *cp; + int noname = 1; + char variable[1024]; + char *cp; for (; argc > 0; argc--, argv++) { if (strlen (*argv) >= sizeof variable) { @@ -179,9 +165,7 @@ void set_env (int argc, char *const *argv) cp = strchr (*argv, '='); if (NULL == cp) { - int wlen; - wlen = snprintf (variable, sizeof variable, "L%d", noname); - assert (wlen < (int) sizeof(variable)); + assert(SNPRINTF(variable, "L%d", noname) != -1); noname++; addenv (variable, *argv); } else { @@ -194,8 +178,7 @@ void set_env (int argc, char *const *argv) } if (NULL != *p) { - strncpy (variable, *argv, (size_t)(cp - *argv)); - variable[cp - *argv] = '\0'; + stpcpy(mempcpy(variable, *argv, (size_t)(cp - *argv)), ""); printf (_("You may not change $%s\n"), variable); continue; |