Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add system.setenv #1706

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
system.setenv: report failure, including of utfconv
  • Loading branch information
CosmicToast committed Jan 22, 2024
commit 7a513337abc84af8e880704d582923bf6b4bc73b
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);
CosmicToast marked this conversation as resolved.
Show resolved Hide resolved
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
Loading