librazor/util.c
changeset 375 c903635ae422
parent 359 c9c90315ea24
child 377 5549419824b4
     1.1 --- a/librazor/util.c	Wed Apr 22 15:09:17 2009 +0100
     1.2 +++ b/librazor/util.c	Mon Jul 06 18:19:13 2009 +0100
     1.3 @@ -36,6 +36,7 @@
     1.4  #if HAVE_SYS_MMAN_H
     1.5  #include <sys/mman.h>
     1.6  #endif
     1.7 +#include <assert.h>
     1.8  
     1.9  #include "razor.h"
    1.10  #include "razor-internal.h"
    1.11 @@ -293,3 +294,71 @@
    1.12  
    1.13  	return map;
    1.14  }
    1.15 +
    1.16 +void environment_init(struct environment *env)
    1.17 +{
    1.18 +	env->is_set = 0;
    1.19 +	array_init(&env->string_pool);
    1.20 +	array_init(&env->vars);
    1.21 +}
    1.22 +
    1.23 +void environment_add_variable(struct environment *env,
    1.24 +			      const char *variable, const char *value)
    1.25 +{
    1.26 +	char *s;
    1.27 +	uint32_t *r;
    1.28 +	assert(!env->is_set);
    1.29 +
    1.30 +	s = array_add(&env->string_pool,
    1.31 +		      strlen(variable) + strlen(value) + 2);
    1.32 +	sprintf(s, "%s=%s", variable, value);
    1.33 +	r = array_add(&env->vars, sizeof *r);
    1.34 +	*r = s - (char *)env->string_pool.data;
    1.35 +}
    1.36 +
    1.37 +void environment_set(struct environment *env)
    1.38 +{
    1.39 +	int i, count;
    1.40 +	char *s;
    1.41 +        uint32_t *r;
    1.42 +
    1.43 +	if (!env->is_set) {
    1.44 +		count = env->vars.size / sizeof(uint32_t);
    1.45 +		r = (uint32_t *)env->vars.data;
    1.46 +		for (i = 0; i < count; i++) {
    1.47 +			s = env->string_pool.data + *r++;
    1.48 +			putenv(s);
    1.49 +		}
    1.50 +
    1.51 +		env->is_set = 1;
    1.52 +	}
    1.53 +}
    1.54 +
    1.55 +void environment_unset(struct environment *env)
    1.56 +{
    1.57 +	int i, count;
    1.58 +	char c, *s, *t;
    1.59 +        uint32_t *r;
    1.60 +
    1.61 +	if (env->is_set) {
    1.62 +		count = env->vars.size / sizeof(uint32_t);
    1.63 +		r = (uint32_t *)env->vars.data;
    1.64 +		for (i = 0; i < count; i++) {
    1.65 +			s = env->string_pool.data + *r++;
    1.66 +			t = strchr(s, '=') + 1;
    1.67 +			c = *t;
    1.68 +			*t = '\0';
    1.69 +			putenv(s);
    1.70 +			*t = c;
    1.71 +		}
    1.72 +
    1.73 +		env->is_set = 0;
    1.74 +	}
    1.75 +}
    1.76 +
    1.77 +void environment_release(struct environment *env)
    1.78 +{
    1.79 +	environment_unset(env);
    1.80 +	array_release(&env->string_pool);
    1.81 +	array_release(&env->vars);
    1.82 +}