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:
parent
5caf337589
commit
b2027fe023
14
c/builtins.c
14
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)
|
static value_t fl_os_setenv(value_t *args, uint32_t nargs)
|
||||||
{
|
{
|
||||||
char *name;
|
const char *name;
|
||||||
char *val;
|
const char *value;
|
||||||
int result;
|
|
||||||
|
|
||||||
argcount("os.setenv", nargs, 2);
|
argcount("os.setenv", nargs, 2);
|
||||||
name = tostring(args[0], "os.setenv");
|
name = tostring(args[0], "os.setenv");
|
||||||
if (args[1] == FL_F) {
|
if (args[1] == FL_F) {
|
||||||
unsetenv(name);
|
value = 0;
|
||||||
} else {
|
} else {
|
||||||
val = tostring(args[1], "os.setenv");
|
value = tostring(args[1], "os.setenv");
|
||||||
result = setenv(name, val, 1);
|
|
||||||
if (result != 0) {
|
|
||||||
lerror(ArgError, "os.setenv: invalid environment variable");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
os_setenv(name, value);
|
||||||
return FL_T;
|
return FL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
c/os.h
1
c/os.h
|
@ -3,3 +3,4 @@ void get_cwd(char *buf, size_t size);
|
||||||
int set_cwd(char *buf);
|
int set_cwd(char *buf);
|
||||||
char *get_exename(char *buf, size_t size);
|
char *get_exename(char *buf, size_t size);
|
||||||
int os_path_exists(const char *path);
|
int os_path_exists(const char *path);
|
||||||
|
void os_setenv(const char *name, const char *value);
|
||||||
|
|
13
c/os_unix.c
13
c/os_unix.c
|
@ -113,3 +113,16 @@ int os_path_exists(const char *path)
|
||||||
}
|
}
|
||||||
return FL_T;
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@
|
||||||
#include "flisp.h"
|
#include "flisp.h"
|
||||||
|
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
|
||||||
|
#include "argcount.h"
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
|
|
||||||
void path_to_dirname(char *path)
|
void path_to_dirname(char *path)
|
||||||
|
@ -73,3 +75,14 @@ int os_path_exists(const char *path)
|
||||||
}
|
}
|
||||||
return FL_T;
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue