diff -r c9c90315ea24 -r 6e93e5485947 librazor/util.c --- a/librazor/util.c Wed Apr 22 15:09:17 2009 +0100 +++ b/librazor/util.c Fri Jul 03 18:02:33 2009 +0100 @@ -36,6 +36,7 @@ #if HAVE_SYS_MMAN_H #include #endif +#include #include "razor.h" #include "razor-internal.h" @@ -293,3 +294,71 @@ return map; } + +void environment_init(struct environment *env) +{ + env->is_set = 0; + array_init(&env->string_pool); + array_init(&env->vars); +} + +void environment_add_variable(struct environment *env, + const char *variable, const char *value) +{ + char *s; + uint32_t *r; + assert(!env->is_set); + + s = array_add(&env->string_pool, + strlen(variable) + strlen(value) + 2); + sprintf(s, "%s=%s", variable, value); + r = array_add(&env->vars, sizeof *r); + *r = s - (char *)env->string_pool.data; +} + +void environment_set(struct environment *env) +{ + int i, count; + char *s; + uint32_t *r; + + if (!env->is_set) { + count = env->vars.size / sizeof(uint32_t); + r = (uint32_t *)env->vars.data; + for (i = 0; i < count; i++) { + s = env->string_pool.data + *r++; + putenv(s); + } + + env->is_set = 1; + } +} + +void environment_unset(struct environment *env) +{ + int i, count; + char c, *s, *t; + uint32_t *r; + + if (env->is_set) { + count = env->vars.size / sizeof(uint32_t); + r = (uint32_t *)env->vars.data; + for (i = 0; i < count; i++) { + s = env->string_pool.data + *r++; + t = strchr(s, '=') + 1; + c = *t; + *t = '\0'; + putenv(s); + *t = c; + } + + env->is_set = 0; + } +} + +void environment_release(struct environment *env) +{ + environment_unset(env); + array_release(&env->string_pool); + array_release(&env->vars); +}