authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-09-17 02:13:14-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-17 11:05:06-07:00
logbc626e8b896bee26f565a271da7e60b2564ee341
tree6ff887213006e4780180a018eab249d11cddd3b1
parentc1e94b28a370bdfe40e8e767b82535e248318aa7

Make `std.os.getenv` always a compile error on Windows

The _environ variable that is populated when linking libc on Windows does not support Unicode keys/values (or, at least, the encoding is not necessarily UTF-8). So, for Unicode support, _wenviron would need to be used instead. However, this means that the keys/values would be encoded as UTF-16, so they would need to be converted to UTF-8 before being returned by `os.getenv`. This would require allocation which is not part of the `os.getenv` API, so `os.getenv` is not implementable on Windows even when linking libc. Closes https://github.com/ziglang/zig/issues/8456

1 files changed, 4 insertions(+), 3 deletions(-)

lib/std/os.zig+4-3
......@@ -1893,6 +1893,9 @@ pub fn execvpeZ(
18931893/// Get an environment variable.
18941894/// See also `getenvZ`.
18951895pub fn getenv(key: []const u8) ?[:0]const u8 {
1896 if (builtin.os.tag == .windows) {
1897 @compileError("std.os.getenv is unavailable for Windows because environment strings are in WTF-16 format. See std.process.getEnvVarOwned for a cross-platform API or std.os.getenvW for a Windows-specific API.");
1898 }
18961899 if (builtin.link_libc) {
18971900 var ptr = std.c.environ;
18981901 while (ptr[0]) |line| : (ptr += 1) {
......@@ -1906,9 +1909,7 @@ pub fn getenv(key: []const u8) ?[:0]const u8 {
19061909 }
19071910 return null;
19081911 }
1909 if (builtin.os.tag == .windows) {
1910 @compileError("std.os.getenv is unavailable for Windows because environment string is in WTF-16 format. See std.process.getEnvVarOwned for cross-platform API or std.os.getenvW for Windows-specific API.");
1911 } else if (builtin.os.tag == .wasi) {
1912 if (builtin.os.tag == .wasi) {
19121913 @compileError("std.os.getenv is unavailable for WASI. See std.process.getEnvMap or std.process.getEnvVarOwned for a cross-platform API.");
19131914 }
19141915 // The simplified start logic doesn't populate environ.