2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-03-10 14:25:16 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module os
|
|
|
|
|
2021-04-04 16:43:32 +02:00
|
|
|
fn C.getenv(&char) &char
|
2020-12-16 10:02:36 +01:00
|
|
|
|
2020-03-10 14:25:16 +01:00
|
|
|
// C.GetEnvironmentStringsW & C.FreeEnvironmentStringsW are defined only on windows
|
|
|
|
fn C.GetEnvironmentStringsW() &u16
|
|
|
|
|
|
|
|
fn C.FreeEnvironmentStringsW(&u16) int
|
2020-12-16 10:02:36 +01:00
|
|
|
|
2020-03-10 14:25:16 +01:00
|
|
|
// `getenv` returns the value of the environment variable named by the key.
|
|
|
|
pub fn getenv(key string) string {
|
2021-02-14 19:31:42 +01:00
|
|
|
unsafe {
|
|
|
|
$if windows {
|
|
|
|
s := C._wgetenv(key.to_wide())
|
|
|
|
if s == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string_from_wide(s)
|
|
|
|
} $else {
|
2021-04-04 16:43:32 +02:00
|
|
|
s := C.getenv(&char(key.str))
|
2021-02-14 19:31:42 +01:00
|
|
|
if s == voidptr(0) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
// NB: C.getenv *requires* that the result be copied.
|
2021-04-04 16:43:32 +02:00
|
|
|
return cstring_to_vstring(&byte(s))
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// os.setenv sets the value of an environment variable with `name` to `value`.
|
|
|
|
pub fn setenv(name string, value string, overwrite bool) int {
|
|
|
|
$if windows {
|
|
|
|
format := '$name=$value'
|
|
|
|
if overwrite {
|
2020-07-20 19:06:41 +02:00
|
|
|
unsafe {
|
2021-04-04 16:43:32 +02:00
|
|
|
return C._putenv(&char(format.str))
|
2020-07-20 19:06:41 +02:00
|
|
|
}
|
2020-10-04 19:43:28 +02:00
|
|
|
} else {
|
|
|
|
if getenv(name).len == 0 {
|
|
|
|
unsafe {
|
2021-04-04 16:43:32 +02:00
|
|
|
return C._putenv(&char(format.str))
|
2020-10-04 19:43:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
} $else {
|
2020-07-20 19:06:41 +02:00
|
|
|
unsafe {
|
2021-04-04 16:43:32 +02:00
|
|
|
return C.setenv(&char(name.str), &char(value.str), overwrite)
|
2020-07-20 19:06:41 +02:00
|
|
|
}
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// os.unsetenv clears an environment variable with `name`.
|
|
|
|
pub fn unsetenv(name string) int {
|
|
|
|
$if windows {
|
2020-12-16 10:02:36 +01:00
|
|
|
format := '$name='
|
2021-04-04 16:43:32 +02:00
|
|
|
return C._putenv(&char(format.str))
|
2020-03-10 14:25:16 +01:00
|
|
|
} $else {
|
2021-04-04 16:43:32 +02:00
|
|
|
return C.unsetenv(&char(name.str))
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-04-04 20:11:17 +02:00
|
|
|
type PChar = &char
|
|
|
|
type PPChar = &&char
|
|
|
|
|
|
|
|
fn deref_ppchar(x &PChar) &char {
|
|
|
|
return &char(*x)
|
|
|
|
}
|
|
|
|
|
2020-03-10 14:25:16 +01:00
|
|
|
pub fn environ() map[string]string {
|
2020-12-16 10:02:36 +01:00
|
|
|
mut res := map[string]string{}
|
2020-03-10 14:25:16 +01:00
|
|
|
$if windows {
|
|
|
|
mut estrings := C.GetEnvironmentStringsW()
|
|
|
|
mut eline := ''
|
2020-07-03 18:10:10 +02:00
|
|
|
for c := estrings; *c != 0; {
|
2021-02-15 16:15:52 +01:00
|
|
|
eline = unsafe { string_from_wide(c) }
|
2020-03-10 14:25:16 +01:00
|
|
|
eq_index := eline.index_byte(`=`)
|
|
|
|
if eq_index > 0 {
|
|
|
|
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
|
|
|
}
|
2020-12-16 10:02:36 +01:00
|
|
|
unsafe {
|
2020-07-03 18:10:10 +02:00
|
|
|
c = c + eline.len + 1
|
|
|
|
}
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
C.FreeEnvironmentStringsW(estrings)
|
|
|
|
} $else {
|
2021-04-04 20:11:17 +02:00
|
|
|
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) }
|
2020-03-10 14:25:16 +01:00
|
|
|
eq_index := eline.index_byte(`=`)
|
|
|
|
if eq_index > 0 {
|
|
|
|
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|