diff options
Diffstat (limited to 'src/env.c')
-rw-r--r-- | src/env.c | 90 |
1 files changed, 53 insertions, 37 deletions
@@ -1,7 +1,8 @@ -#include "str.h" -#include "alloc.h" #include "env.h" +#include "alloc.h" +#include "str.h" + /** @file env.c @author djb @@ -9,7 +10,7 @@ @brief setting up environment after fork */ -extern /*@null@*/char *env_get(char *s) +extern /*@null@*/ char *env_get(char *s) { int i; unsigned int len; @@ -17,12 +18,11 @@ extern /*@null@*/char *env_get(char *s) if (!s) return 0; len = str_len(s); for (i = 0; environ[i]; ++i) - if (str_start(environ[i],s) && (environ[i][len] == '=')) - return environ[i] + len + 1; + if (str_start(environ[i], s) && (environ[i][len] == '=')) return environ[i] + len + 1; return 0; } -extern char *env_findeq(char *s) +extern char *env_findeq(char *s) { for (; *s; ++s) if (*s == '=') return s; @@ -33,91 +33,102 @@ int env_isinit = 0; /* if env_isinit: */ static int ea; /* environ is a pointer to ea+1 char*'s. */ static int en; /* the first en of those are ALLOCATED. environ[en] is 0. */ -static void env_del(int i) { +static void env_del(int i) +{ alloc_free(environ[i]); environ[i] = environ[--en]; environ[en] = 0; } -static void env_unsetlen(char *s,int len) +static void env_unsetlen(char *s, int len) { int i; for (i = en - 1; i >= 0; --i) - if (!str_diffn(s,environ[i],len)) - if (environ[i][len] == '=') - env_del(i); + if (!str_diffn(s, environ[i], len)) + if (environ[i][len] == '=') env_del(i); } int env_unset(char *s) { - if (!env_isinit) + if (!env_isinit) if (!env_init()) return 0; - env_unsetlen(s,str_len(s)); + env_unsetlen(s, str_len(s)); return 1; } -int env_set(char *s) { +int env_set(char *s) +{ char *t; t = env_findeq(s); - if (t) env_unsetlen(s,t - s); + if (t) env_unsetlen(s, t - s); if (en == ea) { ea += 30; - if (!alloc_re(&environ,(en + 1) * sizeof(char *),(ea + 1) * sizeof(char *))) - { ea = en; return 0; } + if (!alloc_re(&environ, (en + 1) * sizeof(char *), (ea + 1) * sizeof(char *))) { + ea = en; + return 0; + } } environ[en++] = s; environ[en] = 0; return 1; } -int env_puts(char *s) { +int env_puts(char *s) +{ char *u; - if (!env_isinit) + if (!env_isinit) if (!env_init()) return 0; u = alloc(str_len(s) + 1); if (!u) return 0; - str_copy(u,s); - if (!env_set(u)) { alloc_free(u); return 0; } + str_copy(u, s); + if (!env_set(u)) { + alloc_free(u); + return 0; + } return 1; } -int env_put(char *name,char *value) { +int env_put(char *name, char *value) +{ char *ln; int len; - if (!env_isinit) + if (!env_isinit) if (!env_init()) return 0; len = str_len(name); ln = alloc(len + str_len(value) + 2); if (!ln) return 0; - str_copy(ln,name); + str_copy(ln, name); ln[len] = '='; - str_copy(ln + len + 1,value); - if (!env_set(ln)) { alloc_free(ln); return 0; } + str_copy(ln + len + 1, value); + if (!env_set(ln)) { + alloc_free(ln); + return 0; + } return 1; } -int env_init() { +int env_init() +{ char **newenviron; int i; - for (en = 0; environ[en]; ++en) - ; + for (en = 0; environ[en]; ++en); ea = en + 10; - newenviron = (char **) alloc((ea + 1) * sizeof(char *)); + newenviron = (char **)alloc((ea + 1) * sizeof(char *)); if (!newenviron) return 0; for (en = 0; environ[en]; ++en) { newenviron[en] = alloc(str_len(environ[en]) + 1); if (!newenviron[en]) { for (i = 0; i < en; ++i) alloc_free(newenviron[i]); - alloc_free(newenviron); - return 0; + alloc_free(newenviron); + return 0; } - str_copy(newenviron[en],environ[en]); + str_copy(newenviron[en], environ[en]); } newenviron[en] = 0; @@ -128,9 +139,14 @@ int env_init() { static char *null = 0; -void env_clear() { - if (env_isinit) +void env_clear() +{ + if (env_isinit) while (en) env_del(0); - else environ = &null; + else + environ = &null; +} +extern char *env_pick() +{ + return environ[0]; } -extern char *env_pick() { return environ[0]; } |