blob: b852a11260bfd4b0df68304d321df62e5a88faa3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/*
* src/variables.c
*
* Local variable storage and access
* CISC 301 -- Operating Systems, Project 2
*
* Copyright (C) 2025 Douglas B. Rumbaugh <dbrumbaugh@harrisburgu.edu>
*
* Distributed under the Modified BSD License
*
*/
#include "variables.h"
#include "strmap.h"
static strmap *map;
bool init_variable_store(void) {
map = strmap_create(hash_key);
return map;
}
bool add_variable(const char *key, const char *val) {
if (strmap_put(map, key, val) != STRMAP_OK) {
return false;
}
return true;
}
const char *get_variable(const char *key) {
key += (key[0] == '$');
const char *val;
strmap_status stat = strmap_get(map, key, &val);
switch (stat) {
case STRMAP_OK:
break;
case STRMAP_NOTFOUND:
val = getenv(key);
break;
default:
val = NULL;
break;
}
return (val) ? val : "";
}
bool promote_variable_to_env(const char *key) {
const char *val = get_variable(key);
if (!val) {
val = "";
}
return !setenv(key, val, 1);
}
void destroy_variable_store() {
strmap_destroy(map);
}
|