1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/os/environment.c.v

121 lines
2.8 KiB
V
Raw Normal View History

2022-01-04 12:21:08 +03:00
// Copyright (c) 2019-2022 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
fn C.getenv(&char) &char
// 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.
2021-11-19 18:32:01 +03:00
// If there is not one found, it returns an empty string ''.
pub fn getenv(key string) string {
2021-11-19 18:32:01 +03:00
return getenv_opt(key) or { '' }
}
// `getenv_opt` returns the value of the environment variable named by the key
// If there is not one found, it returns `none`.
[manualfree]
pub fn getenv_opt(key string) ?string {
unsafe {
$if windows {
2021-11-19 18:32:01 +03:00
kw := key.to_wide()
defer {
free(voidptr(kw))
}
s := C._wgetenv(kw)
if s == 0 {
2021-11-19 18:32:01 +03:00
return none
}
return string_from_wide(s)
} $else {
s := C.getenv(&char(key.str))
2022-07-21 21:01:30 +03:00
if s == nil {
2021-11-19 18:32:01 +03:00
return none
}
// Note: C.getenv *requires* that the result be copied.
return cstring_to_vstring(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 {
unsafe {
return C._putenv(&char(format.str))
}
} else {
if getenv(name).len == 0 {
unsafe {
return C._putenv(&char(format.str))
}
}
}
return -1
} $else {
unsafe {
return C.setenv(&char(name.str), &char(value.str), overwrite)
}
}
}
// os.unsetenv clears an environment variable with `name`.
pub fn unsetenv(name string) int {
$if windows {
format := '$name='
return C._putenv(&char(format.str))
} $else {
return C.unsetenv(&char(name.str))
}
}
// 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 21:11:17 +03:00
2021-04-06 18:00:42 +03:00
fn unix_environ() &&char {
// TODO: remove this helper function, when `&&char(C.environ)` works properly
return voidptr(C.environ)
2021-04-04 21:11:17 +03:00
}
pub fn environ() map[string]string {
mut res := map[string]string{}
$if windows {
mut estrings := C.GetEnvironmentStringsW()
mut eline := ''
for c := estrings; *c != 0; {
eline = unsafe { string_from_wide(c) }
2022-04-15 14:58:56 +03:00
eq_index := eline.index_u8(`=`)
if eq_index > 0 {
res[eline[0..eq_index]] = eline[eq_index + 1..]
}
unsafe {
c = c + eline.len + 1
}
}
C.FreeEnvironmentStringsW(estrings)
} $else {
2021-04-06 18:00:42 +03:00
start := unix_environ()
mut i := 0
for {
x := unsafe { start[i] }
if x == 0 {
2021-04-04 21:11:17 +03:00
break
}
2021-04-06 18:00:42 +03:00
eline := unsafe { cstring_to_vstring(x) }
2022-04-15 14:58:56 +03:00
eq_index := eline.index_u8(`=`)
if eq_index > 0 {
res[eline[0..eq_index]] = eline[eq_index + 1..]
}
2021-04-06 18:00:42 +03:00
i++
}
}
return res
}