diff options
| author | Nathan P. Reiner <nathan@nathanreiner.xyz> | 2022-12-16 10:00:33 +0100 |
|---|---|---|
| committer | Nathan P. Reiner <nathan@nathanreiner.xyz> | 2022-12-16 10:00:33 +0100 |
| commit | a8192ab39056e66dc8ecae1a637ecff88b49448b (patch) | |
| tree | 2458a7a0c02c29e3a3879197bc0429b4bb7acc48 /lib/env | |
| parent | f7f42b5f4fd22cd16e092de4a6f2a642b5ab8234 (diff) | |
add env initialization
Diffstat (limited to 'lib/env')
| -rw-r--r-- | lib/env/Makefile | 2 | ||||
| -rw-r--r-- | lib/env/env.c | 79 |
2 files changed, 80 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 |