Commit f054649a authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

lua: setenv function

Because lua has os.getenv, but there's no setenv and we need it to run
external programs.
parent 58389d05
......@@ -518,6 +518,16 @@ static int lua_stat(lua_State *L) {
return 2;
}
static int lua_setenv(lua_State *L) {
const char *name = luaL_checkstring(L, 1);
const char *value = luaL_checkstring(L, 2);
int result = setenv(name, value, 1);
if (result) {
return luaL_error(L, "Failed to set env %s = %s", name, value, strerror(errno));
}
return 0;
}
struct injected_func {
int (*func)(lua_State *);
const char *name;
......@@ -533,7 +543,8 @@ static const struct injected_func injected_funcs[] = {
{ lua_mkdir, "mkdir" },
{ lua_move, "move" },
{ lua_ls, "ls" },
{ lua_stat, "stat" }
{ lua_stat, "stat" },
{ lua_setenv, "setenv" }
/*
* Note: watch_cancel is not provided, because it would be hell to
* manage the dynamically allocated memory correctly and there doesn't
......
......@@ -172,3 +172,7 @@ DataDumper(variable)::
It is a third-party code from
`http://lua-users.org/wiki/DataDumper`. Note that some data
(userdata, for example) can't be represented this way.
setenv(name, value)::
Set the environment variable with the given name to the given value.
Errors in case of failure, otherwise returns nothing.
......@@ -71,6 +71,12 @@ function test_fsutils()
assert_table_equal({["x"] = "r"}, ls(ldir))
end
-- Test setting the environment
function test_env()
setenv("TEST_ENV", "42")
assert_equal("42", os.getenv("TEST_ENV"))
end
function teardown()
utils.cleanup_dirs(tmp_dirs)
tmp_dirs = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment