diff options
| -rw-r--r-- | lib/env/Makefile | 2 | ||||
| -rw-r--r-- | lib/env/env.c | 79 | ||||
| -rw-r--r-- | lib/sys/start.S | 1 |
3 files changed, 81 insertions, 1 deletions
diff --git a/lib/env/Makefile b/lib/env/Makefile index 6158ef2..db9b5a8 100644 --- a/lib/env/Makefile +++ b/lib/env/Makefile @@ -1,3 +1,3 @@ unit_test: - gcc env.c ../cstr/cstr.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -g + 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 ./test diff --git a/lib/env/env.c b/lib/env/env.c index df8e361..1710462 100644 --- a/lib/env/env.c +++ b/lib/env/env.c @@ -1 +1,80 @@ #include "env.h" + +#include "../malloc/malloc.h" + +const char **envp; + +const 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); + const char *value; + + if (p == 0) + return ""; + + value = *p; + + while (*value != '=') ++value; + return ++value; +} + + +void setenv(const char *key, const char *value) +{ + const char **p = __getenvpair(key); + *p = value; +} + + +const char **__getenvpair(const char *key) +{ + const char **p = envp; + + while (*p) { + if (__match_env_key(*p, key)) { + return p; + } + + ++p; + } + + return 0; +} + + +u8 __match_env_key(const char *p, const char *key) +{ + while (*key && *p) { + + if (*key != *p) + return 0; + + ++p; + ++key; + } + + if (*p != '=') + return 0; + + return 1; +} + +void init_env(int argc, const char **argv, const char **envp__) +{ + envp = envp__; +} + +#ifdef ENV_UNIT_TEST + +#include "../io/io.h" + +int main(int argc, const char **argv) +{ + wstdf("HOME: %s\n", getenv("HOSTNAME")); +} + +#endif diff --git a/lib/sys/start.S b/lib/sys/start.S index 1faf691..ccbbf18 100644 --- a/lib/sys/start.S +++ b/lib/sys/start.S @@ -4,6 +4,7 @@ _start: xor %rax, %rax lea 8(%rsp, %rax, 8), %rsi lea 16(%rsp, %rdi, 8), %rdx + call init_env call main mov $0, %rdi mov $60, %rax |