aboutsummaryrefslogtreecommitdiff
path: root/lib/env
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-08 13:19:56 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-08 13:19:56 +0100
commitd7229970f8910aa756d299f8bdceee7f4d1fbfbc (patch)
treea95ff2e6bd50578d34d6449c9fde173acc0ef59c /lib/env
parente947b1459f93f11ad91f3e920676fe1b828281e7 (diff)
add env and make smash be able to parse files with comments
Diffstat (limited to 'lib/env')
-rw-r--r--lib/env/Makefile2
-rw-r--r--lib/env/env.c55
2 files changed, 46 insertions, 11 deletions
diff --git a/lib/env/Makefile b/lib/env/Makefile
index db9b5a8..128490b 100644
--- a/lib/env/Makefile
+++ b/lib/env/Makefile
@@ -1,3 +1,3 @@
unit_test:
- gcc env.c ../io/io.c ../cstr/cstr.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -DENV_UNIT_TEST -g
+ gcc env.c ../io/io.c ../aec/aec.c ../malloc/malloc.c ../cstr/cstr.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -DENV_UNIT_TEST -g
./test
diff --git a/lib/env/env.c b/lib/env/env.c
index d52c3f3..7ceaa15 100644
--- a/lib/env/env.c
+++ b/lib/env/env.c
@@ -1,16 +1,19 @@
#include "env.h"
#include "../malloc/malloc.h"
+#include "../cstr/cstr.h"
-const char **envp;
+char **envp;
+u64 envsize = 0;
+#define ENVSIZEINC 16
-const char **__getenvpair(const char *key);
+char **__getenvpair(const char *key);
u8 __match_env_key(const char *p, const char *key);
const char *getenv(const char *key)
{
- const char **p = __getenvpair(key);
+ char **p = __getenvpair(key);
const char *value;
if (p == 0)
@@ -25,14 +28,44 @@ const char *getenv(const char *key)
void setenv(const char *key, const char *value)
{
- const char **p = __getenvpair(key);
- *p = value;
+ char **p = __getenvpair(key);
+
+ if (p == 0) {
+ p = envp;
+
+ while (*p) ++p;
+
+ if (p - envp >= envsize) {
+ if (envsize == 0) {
+ for (; *(envp + envsize); ++envsize);
+ char **nenvp = malloc(sizeof(char *) * (envsize + ENVSIZEINC));
+ p = nenvp;
+ while (p < nenvp + envsize + ENVSIZEINC) *(p++) = 0;
+ p = nenvp;
+ while (*envp) *(p++) = *(envp++);
+ envp = nenvp;
+ } else {
+ envp = realloc(envp, envsize + ENVSIZEINC);
+ }
+
+ p = envp + envsize;
+ envsize += ENVSIZEINC;
+ }
+ }
+
+ *p = malloc((cstr_length(key) + cstr_length(key)) + 2);
+ char *i = *p;
+
+ while (*key) *(i++) = *(key++);
+ *(i++) = '=';
+ while (*value) *(i++) = *(value++);
+ *i = 0;
}
-const char **__getenvpair(const char *key)
+char **__getenvpair(const char *key)
{
- const char **p = envp;
+ char **p = envp;
while (*p) {
if (__match_env_key(*p, key)) {
@@ -48,7 +81,7 @@ const char **__getenvpair(const char *key)
const char **getenvp()
{
- return envp;
+ return (const char**)envp;
}
@@ -69,7 +102,7 @@ u8 __match_env_key(const char *p, const char *key)
return 1;
}
-void init_env(int argc, const char **argv, const char **envp__)
+void init_env(int argc, const char **argv, char **envp__)
{
envp = envp__;
}
@@ -80,7 +113,9 @@ void init_env(int argc, const char **argv, const char **envp__)
int main(int argc, const char **argv)
{
- wstdf("HOME: %s\n", getenv("HOSTNAME"));
+ wstdf("TEST: %s\n", getenv("TEST"));
+ setenv("TEST", "Hello, World!");
+ wstdf("TEST: %s\n", getenv("TEST"));
}
#endif