Skip to content

Commit

Permalink
std::os: add support for WIN32 in set_var and clear_var
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Curto <[email protected]>
  • Loading branch information
pierrec committed Sep 25, 2023
1 parent bfe358e commit 8e37118
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/std/os/env.c3
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,24 @@ macro usz get_env_win32(name, buffer) @local
**/
fn void set_var(String name, String value, bool overwrite = true)
{
$if env::LIBC && !env::WIN32:
@pool()
{
if (libc::setenv(name.zstr_tcopy(), value.zstr_copy(), (int)overwrite))
ZString zname = name.zstr_tcopy();
ZString zvalue = value.zstr_tcopy();
$switch
$case env::LIBC && !env::WIN32:
if (libc::setenv(zname, zvalue, (int)overwrite))
{
unreachable();
}
$case env::WIN32:
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setenvironmentvariable
if (!win32::setEnvironmentVariable((Win32_LPCSTR)zname.ptr, (Win32_LPCSTR)zvalue.ptr))
{
unreachable();
}
$endswitch
};
$endif
}

/**
Expand Down Expand Up @@ -124,21 +133,25 @@ fn String! get_config_dir(Allocator* using = mem::heap())
**/
fn void clear_var(String name)
{
$if env::LIBC && !env::WIN32:
@pool()
{
if (libc::unsetenv(name.zstr_tcopy()))
ZString zname = name.zstr_tcopy();
$switch
$case env::LIBC && !env::WIN32:
if (libc::unsetenv(zname))
{
unreachable();
}
$case env::WIN32:
win32::getEnvironmentVariable((Win32_LPCSTR)zname.ptr, 0);
$endswitch
};
$endif
}

fn String! executable_path(Allocator *using = mem::heap())
{
$if env::DARWIN:
return darwin::executable_path();
return darwin::executable_path(using);
$else
return "<Unsupported>";
$endif
Expand Down
4 changes: 4 additions & 0 deletions lib/std/os/win32/process.c3
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ extern fn Win32_DWORD getEnvironmentVariable(
Win32_LPSTR lpBuffer,
Win32_DWORD nSize
) @extern("GetEnvironmentVariableA");
extern fn Win32_BOOL setEnvironmentVariable(
Win32_LPCSTR lpName,
Win32_LPCSTR lpBuffer
) @extern("SetEnvironmentVariableA");

struct SystemInfo
{
Expand Down
17 changes: 17 additions & 0 deletions test/unit/stdlib/os/env.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module std::os::env @test;

fn void! set_get_unset()
{
const NAME = "C3_TEST_ENVVAR";
const VALUE = "foobar";

env::set_var(NAME, VALUE);
String v = env::get_var(NAME)!;
assert(v == VALUE, "got %s; want %s", v, VALUE);

env::clear_var(NAME);
if (try env::get_var(NAME))
{
assert(false, "environment variable should no longer exist");
}
}

0 comments on commit 8e37118

Please sign in to comment.