diff --git a/c/builtins.c b/c/builtins.c index 127a834..19d8379 100644 --- a/c/builtins.c +++ b/c/builtins.c @@ -391,21 +391,17 @@ static value_t fl_os_getenv(value_t *args, uint32_t nargs) static value_t fl_os_setenv(value_t *args, uint32_t nargs) { - char *name; - char *val; - int result; + const char *name; + const char *value; argcount("os.setenv", nargs, 2); name = tostring(args[0], "os.setenv"); if (args[1] == FL_F) { - unsetenv(name); + value = 0; } else { - val = tostring(args[1], "os.setenv"); - result = setenv(name, val, 1); - if (result != 0) { - lerror(ArgError, "os.setenv: invalid environment variable"); - } + value = tostring(args[1], "os.setenv"); } + os_setenv(name, value); return FL_T; } diff --git a/c/os.h b/c/os.h index 34fcfee..128e949 100644 --- a/c/os.h +++ b/c/os.h @@ -3,3 +3,4 @@ void get_cwd(char *buf, size_t size); int set_cwd(char *buf); char *get_exename(char *buf, size_t size); int os_path_exists(const char *path); +void os_setenv(const char *name, const char *value); diff --git a/c/os_unix.c b/c/os_unix.c index f810ae9..90aeffa 100644 --- a/c/os_unix.c +++ b/c/os_unix.c @@ -113,3 +113,16 @@ int os_path_exists(const char *path) } return FL_T; } + +void os_setenv(const char *name, const char *value) +{ + if (value) { + if (setenv(name, value, 1) != 0) { + lerror(ArgError, "os.setenv: cannot set environment variable"); + } + } else { + if (unsetenv(name) != 0) { + lerror(ArgError, "os.setenv: cannot unset environment variable"); + } + } +} diff --git a/c/os_windows.c b/c/os_windows.c index c378350..84b548d 100644 --- a/c/os_windows.c +++ b/c/os_windows.c @@ -32,6 +32,8 @@ #include "flisp.h" #include "error.h" + +#include "argcount.h" #include "os.h" void path_to_dirname(char *path) @@ -73,3 +75,14 @@ int os_path_exists(const char *path) } return FL_T; } + +void os_setenv(const char *name, const char *value) +{ + if (!SetEnvironmentVariable(name, value)) { + if (value) { + lerror(ArgError, "os.setenv: cannot set environment variable"); + } else { + lerror(ArgError, "os.setenv: cannot unset environment variable"); + } + } +}