2020-03-10 14:25:16 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module os
|
|
|
|
|
2020-07-14 00:16:31 +02:00
|
|
|
fn C.getenv(charptr) &char
|
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
|
|
|
|
// `getenv` returns the value of the environment variable named by the key.
|
|
|
|
pub fn getenv(key string) string {
|
|
|
|
$if windows {
|
|
|
|
s := C._wgetenv(key.to_wide())
|
|
|
|
if s == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string_from_wide(s)
|
|
|
|
} $else {
|
2020-07-14 00:16:31 +02:00
|
|
|
s := C.getenv(charptr(key.str))
|
2020-05-27 05:42:48 +02:00
|
|
|
if s == voidptr(0) {
|
2020-03-10 14:25:16 +01:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
// NB: C.getenv *requires* that the result be copied.
|
|
|
|
return cstring_to_vstring(byteptr(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return C._putenv(format.str)
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
} $else {
|
2020-07-14 00:16:31 +02:00
|
|
|
return C.setenv(charptr(name.str), charptr(value.str), overwrite)
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// os.unsetenv clears an environment variable with `name`.
|
|
|
|
pub fn unsetenv(name string) int {
|
|
|
|
$if windows {
|
|
|
|
format := '${name}='
|
|
|
|
return C._putenv(format.str)
|
|
|
|
} $else {
|
2020-07-14 00:16:31 +02:00
|
|
|
return C.unsetenv(charptr(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
|
|
|
|
pub fn environ() map[string]string {
|
|
|
|
mut res := map[string]string
|
|
|
|
$if windows {
|
|
|
|
mut estrings := C.GetEnvironmentStringsW()
|
|
|
|
mut eline := ''
|
2020-07-03 18:10:10 +02:00
|
|
|
for c := estrings; *c != 0; {
|
2020-03-10 14:25:16 +01:00
|
|
|
eline = string_from_wide(c)
|
|
|
|
eq_index := eline.index_byte(`=`)
|
|
|
|
if eq_index > 0 {
|
|
|
|
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
c = c + eline.len + 1
|
|
|
|
}
|
2020-03-10 14:25:16 +01:00
|
|
|
}
|
|
|
|
C.FreeEnvironmentStringsW(estrings)
|
|
|
|
} $else {
|
2020-04-08 17:01:14 +02:00
|
|
|
e := &charptr(C.environ)
|
2020-03-10 14:25:16 +01:00
|
|
|
for i := 0; !isnil(e[i]); i++ {
|
2020-07-14 00:16:31 +02:00
|
|
|
eline := cstring_to_vstring(byteptr(e[i]))
|
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
|
|
|
|
}
|