Split setenv/unsetenv by platform

Windows apparently doesn't have unsetenv() at all, so use the WinAPI
native envar functions instead of the usual C ones.
This commit is contained in:
Lassi Kortela 2019-08-21 22:16:07 +03:00
parent 5caf337589
commit b2027fe023
4 changed files with 32 additions and 9 deletions

View File

@ -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;
}

1
c/os.h
View File

@ -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);

View File

@ -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");
}
}
}

View File

@ -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");
}
}
}