Skip to content

Commit

Permalink
system.setenv: report failure, including of utfconv
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicToast committed Jan 22, 2024
1 parent 07e5ab7 commit 7a51333
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/api/system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ function system.path_compare(path1, type1, path2, type2) end
---
---@param key string
---@param val string
---@return boolean ok True if call succeeded
function system.setenv(key, val) end

return system
15 changes: 8 additions & 7 deletions src/api/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,21 +1151,22 @@ static int f_text_input(lua_State* L) {
}

static int f_setenv(lua_State* L) {
size_t keylen, vallen;
const char *key = luaL_checklstring(L, 1, &keylen);
const char *val = luaL_checklstring(L, 2, &vallen);
const char *key = luaL_checkstring(L, 1);
const char *val = luaL_checkstring(L, 2);

int ok; // unused: this could be used later to report success
int ok;
#ifdef _WIN32
LPWSTR wkey = utfconv_utf8towc(key);
LPWSTR wval = utfconv_utf8towc(val);
ok = SetEnvironmentVariableW(wkey, wval);
ok = (wkey && wval) ? SetEnvironmentVariableW(wkey, wval)
/* utfconv error */ : 0;
#else
// right now we overwrite unconditionally
// this could be expanded later as an optional 3rd boolean argument
ok = setenv(key, val, 1);
ok = !setenv(key, val, 1);
#endif
return 0;
lua_pushboolean(L, ok);
return 1;
}


Expand Down

0 comments on commit 7a51333

Please sign in to comment.