diff --git a/core/os/os2/env.odin b/core/os/os2/env.odin index f25290a59..f1a3e40c7 100644 --- a/core/os/os2/env.odin +++ b/core/os/os2/env.odin @@ -1,20 +1,11 @@ package os2 -// get_env retrieves the value of the environment variable named by the key -// It returns the value, which will be empty if the variable is not present -// To distinguish between an empty value and an unset value, use lookup_env -// NOTE: the value will be allocated with the supplied allocator -get_env :: proc(key: string, allocator := context.allocator) -> string { - value, _ := lookup_env(key, allocator) - return value -} - -// lookup_env gets the value of the environment variable named by the key +// get_env gets the value of the environment variable named by the key // If the variable is found in the environment the value (which can be empty) is returned and the boolean is true // Otherwise the returned value will be empty and the boolean will be false // NOTE: the value will be allocated with the supplied allocator -lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { - return _lookup_env(key, allocator) +get_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { + return _get_env(key, allocator) } // set_env sets the value of the environment variable named by the key diff --git a/core/os/os2/env_windows.odin b/core/os/os2/env_windows.odin index af04db858..a3b97375b 100644 --- a/core/os/os2/env_windows.odin +++ b/core/os/os2/env_windows.odin @@ -1,50 +1,56 @@ //+private package os2 +import "core:runtime" import "core:mem" import win32 "core:sys/windows" -_lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { +_get_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { if key == "" { return } wkey := win32.utf8_to_wstring(key) - b := make([dynamic]u16, 100, context.temp_allocator) - for { - n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b))) - if n == 0 { - err := win32.GetLastError() - if err == win32.ERROR_ENVVAR_NOT_FOUND { - return "", false - } - } - if n <= u32(len(b)) { - value = win32.utf16_to_utf8(b[:n], allocator) - found = true - return - } - - resize(&b, len(b)*2) + // https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablew + buf_len := win32.GetEnvironmentVariableW(wkey, nil, 0) + if buf_len == 0 { + return } + buf := make([dynamic]u16, buf_len, context.temp_allocator) + n := win32.GetEnvironmentVariableW(wkey, raw_data(buf), buf_len) + if n == 0 { + if win32.GetLastError() == win32.ERROR_ENVVAR_NOT_FOUND { + return "", false + } + value = "" + found = true + return + } + + value = win32.utf16_to_utf8(buf[:n], allocator) + found = true + return } _set_env :: proc(key, value: string) -> bool { k := win32.utf8_to_wstring(key) v := win32.utf8_to_wstring(value) + // https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew return bool(win32.SetEnvironmentVariableW(k, v)) } _unset_env :: proc(key: string) -> bool { k := win32.utf8_to_wstring(key) + + // https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew return bool(win32.SetEnvironmentVariableW(k, nil)) } _clear_env :: proc() { envs := environ(context.temp_allocator) for env in envs { - for j in 1..