mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: cleanup os.environ implementation
This commit is contained in:
parent
1e2a92945c
commit
db84d5e221
@ -52,6 +52,10 @@ pub fn ptr_str(ptr voidptr) string {
|
|||||||
return buf1
|
return buf1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (cptr &char) str() string {
|
||||||
|
return u64(cptr).hex()
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
|
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
|
||||||
)
|
)
|
||||||
|
@ -66,11 +66,10 @@ pub fn unsetenv(name string) int {
|
|||||||
// See: https://linux.die.net/man/5/environ for unix platforms.
|
// 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
|
// 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
|
// os.environ returns a map of all the current environment variables
|
||||||
type PChar = &char
|
|
||||||
type PPChar = &&char
|
|
||||||
|
|
||||||
fn deref_ppchar(x &PChar) &char {
|
fn unix_environ() &&char {
|
||||||
return &char(*x)
|
// TODO: remove this helper function, when `&&char(C.environ)` works properly
|
||||||
|
return voidptr(C.environ)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn environ() map[string]string {
|
pub fn environ() map[string]string {
|
||||||
@ -90,20 +89,19 @@ pub fn environ() map[string]string {
|
|||||||
}
|
}
|
||||||
C.FreeEnvironmentStringsW(estrings)
|
C.FreeEnvironmentStringsW(estrings)
|
||||||
} $else {
|
} $else {
|
||||||
e := unsafe { PPChar(voidptr(C.environ)) }
|
start := unix_environ()
|
||||||
size_of_pchar := int(sizeof(PChar))
|
mut i := 0
|
||||||
for i := 0; true; i++ {
|
for {
|
||||||
z := voidptr(unsafe { &byte(voidptr(e)) + size_of_pchar * i })
|
x := unsafe { start[i] }
|
||||||
current_ptr := &PChar(z)
|
if x == 0 {
|
||||||
derefed := deref_ppchar(current_ptr)
|
|
||||||
if isnil(derefed) {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
eline := unsafe { cstring_to_vstring(derefed) }
|
eline := unsafe { cstring_to_vstring(x) }
|
||||||
eq_index := eline.index_byte(`=`)
|
eq_index := eline.index_byte(`=`)
|
||||||
if eq_index > 0 {
|
if eq_index > 0 {
|
||||||
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
||||||
}
|
}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
Loading…
Reference in New Issue
Block a user