#include "sys_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "mymalloc.h"
#include "msg.h"
#include "safe.h"
#include "stringops.h"
#include "dict.h"
#include "dict_env.h"
static int dict_env_update(DICT *dict, const char *name, const char *value)
{
dict->error = 0;
if (dict->flags & DICT_FLAG_FOLD_FIX) {
if (dict->fold_buf == 0)
dict->fold_buf = vstring_alloc(10);
vstring_strcpy(dict->fold_buf, name);
name = lowercase(vstring_str(dict->fold_buf));
}
if (setenv(name, value, 1))
msg_fatal("setenv: %m");
return (DICT_STAT_SUCCESS);
}
static const char *dict_env_lookup(DICT *dict, const char *name)
{
dict->error = 0;
if (dict->flags & DICT_FLAG_FOLD_FIX) {
if (dict->fold_buf == 0)
dict->fold_buf = vstring_alloc(10);
vstring_strcpy(dict->fold_buf, name);
name = lowercase(vstring_str(dict->fold_buf));
}
return (safe_getenv(name));
}
static void dict_env_close(DICT *dict)
{
if (dict->fold_buf)
vstring_free(dict->fold_buf);
dict_free(dict);
}
DICT *dict_env_open(const char *name, int unused_flags, int dict_flags)
{
DICT *dict;
dict = dict_alloc(DICT_TYPE_ENVIRON, name, sizeof(*dict));
dict->lookup = dict_env_lookup;
dict->update = dict_env_update;
dict->close = dict_env_close;
dict->flags = dict_flags | DICT_FLAG_FIXED;
if (dict_flags & DICT_FLAG_FOLD_FIX)
dict->fold_buf = vstring_alloc(10);
dict->owner.status = DICT_OWNER_TRUSTED;
return (DICT_DEBUG (dict));
}