From 8a362588aa06d595fded3677f210509c82c1459c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 4 Apr 2021 21:11:17 +0300 Subject: [PATCH] ci: fix os.environ :-| --- vlib/os/environment.c.v | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/vlib/os/environment.c.v b/vlib/os/environment.c.v index a4d81685d3..fcd2c65ca3 100644 --- a/vlib/os/environment.c.v +++ b/vlib/os/environment.c.v @@ -66,6 +66,13 @@ pub fn unsetenv(name string) int { // See: https://linux.die.net/man/5/environ for unix platforms. // See: https://docs.microsoft.com/bg-bg/windows/win32/api/processenv/nf-processenv-getenvironmentstrings // os.environ returns a map of all the current environment variables +type PChar = &char +type PPChar = &&char + +fn deref_ppchar(x &PChar) &char { + return &char(*x) +} + pub fn environ() map[string]string { mut res := map[string]string{} $if windows { @@ -83,18 +90,21 @@ pub fn environ() map[string]string { } C.FreeEnvironmentStringsW(estrings) } $else { - /* - // e := unsafe { &&char(C.environ) } - e := unsafe { &charptr(C.environ) } - for i := 0; !isnil(unsafe { e[i] }); i++ { - x := &byte(e[i]) - eline := unsafe { cstring_to_vstring(x) } + e := unsafe { PPChar(voidptr(C.environ)) } + size_of_pchar := int(sizeof(PChar)) + for i := 0; true; i++ { + z := voidptr(unsafe { &byte(voidptr(e)) + size_of_pchar * i }) + current_ptr := &PChar(z) + derefed := deref_ppchar(current_ptr) + if isnil(derefed) { + break + } + eline := unsafe { cstring_to_vstring(derefed) } eq_index := eline.index_byte(`=`) if eq_index > 0 { res[eline[0..eq_index]] = eline[eq_index + 1..] } } - */ } return res }