aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-04-02 15:06:50 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-04-02 15:06:50 -0400
commit4aa797b6bb3716759738e057e90ac607e81ce6e5 (patch)
treea71b69111bbd63390e7b3a435a3f1932aaaf5fa5 /lib/std
parent503c4207972b1357ac11c6a6fb2e49958b68c2dc (diff)
parent8bf7cffe29b520cda094756309bca1d354af0d6b (diff)
downloadzig-4aa797b6bb3716759738e057e90ac607e81ce6e5.tar.gz
zig-4aa797b6bb3716759738e057e90ac607e81ce6e5.zip
Merge branch 'ilmaria-master'
closes #4608
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/os.zig16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 467badca95..bb206b289f 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -1208,12 +1208,15 @@ pub fn getenvZ(key: [*:0]const u8) ?[]const u8 {
/// Windows-only. Get an environment variable with a null-terminated, WTF-16 encoded name.
/// See also `getenv`.
+/// This function first attempts a case-sensitive lookup. If no match is found, and `key`
+/// is ASCII, then it attempts a second case-insensitive lookup.
pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {
if (builtin.os.tag != .windows) {
@compileError("std.os.getenvW is a Windows-only API");
}
const key_slice = mem.spanZ(key);
const ptr = windows.peb().ProcessParameters.Environment;
+ var ascii_match: ?[:0]const u16 = null;
var i: usize = 0;
while (ptr[i] != 0) {
const key_start = i;
@@ -1229,9 +1232,20 @@ pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {
if (mem.eql(u16, key_slice, this_key)) return this_value;
+ ascii_check: {
+ if (ascii_match != null) break :ascii_check;
+ if (key_slice.len != this_key.len) break :ascii_check;
+ for (key_slice) |a_c, key_index| {
+ const a = math.cast(u8, a_c) catch break :ascii_check;
+ const b = math.cast(u8, this_key[key_index]) catch break :ascii_check;
+ if (std.ascii.toLower(a) != std.ascii.toLower(b)) break :ascii_check;
+ }
+ ascii_match = this_value;
+ }
+
i += 1; // skip over null byte
}
- return null;
+ return ascii_match;
}
pub const GetCwdError = error{