Add Error as part of the return values of os2.environ

This commit is contained in:
gingerBill
2025-02-28 13:43:50 +00:00
parent 94152ca701
commit b3bbb00f1a
6 changed files with 46 additions and 41 deletions
+14 -6
View File
@@ -52,7 +52,7 @@ _unset_env :: proc(key: string) -> bool {
_clear_env :: proc() {
TEMP_ALLOCATOR_GUARD()
envs := environ(temp_allocator())
envs, _ := environ(temp_allocator())
for env in envs {
for j in 1..<len(env) {
if env[j] == '=' {
@@ -63,10 +63,10 @@ _clear_env :: proc() {
}
}
_environ :: proc(allocator: runtime.Allocator) -> []string {
_environ :: proc(allocator: runtime.Allocator) -> (environ: []string, err: Error) {
envs := win32.GetEnvironmentStringsW()
if envs == nil {
return nil
return
}
defer win32.FreeEnvironmentStringsW(envs)
@@ -82,7 +82,13 @@ _environ :: proc(allocator: runtime.Allocator) -> []string {
}
}
r := make([dynamic]string, 0, n, allocator)
r := make([dynamic]string, 0, n, allocator) or_return
defer if err != nil {
for e in r {
delete(e, allocator)
}
delete(r)
}
for from, i, p := 0, 0, envs; true; i += 1 {
c := ([^]u16)(p)[i]
if c == 0 {
@@ -90,12 +96,14 @@ _environ :: proc(allocator: runtime.Allocator) -> []string {
break
}
w := ([^]u16)(p)[from:i]
append(&r, win32_utf16_to_utf8(w, allocator) or_else "")
s := win32_utf16_to_utf8(w, allocator) or_return
append(&r, s)
from = i + 1
}
}
return r[:]
environ = r[:]
return
}