mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
os: use _wputenv instead of _putenv to stay in sync with _wgetenv (fix changing env variables with non ASCII content on windows) (#22920)
This commit is contained in:
parent
b995e64be9
commit
ca6727303d
3 changed files with 28 additions and 5 deletions
|
@ -327,6 +327,7 @@ fn C._wsystem(command &u16) int
|
||||||
fn C._wgetenv(varname &u16) voidptr
|
fn C._wgetenv(varname &u16) voidptr
|
||||||
|
|
||||||
fn C._putenv(envstring &char) int
|
fn C._putenv(envstring &char) int
|
||||||
|
fn C._wputenv(envstring &u16) int
|
||||||
|
|
||||||
fn C._waccess(path &u16, mode int) int
|
fn C._waccess(path &u16, mode int) int
|
||||||
|
|
||||||
|
|
|
@ -45,15 +45,18 @@ pub fn getenv_opt(key string) ?string {
|
||||||
// os.setenv sets the value of an environment variable with `name` to `value`.
|
// os.setenv sets the value of an environment variable with `name` to `value`.
|
||||||
pub fn setenv(name string, value string, overwrite bool) int {
|
pub fn setenv(name string, value string, overwrite bool) int {
|
||||||
$if windows {
|
$if windows {
|
||||||
format := '${name}=${value}'
|
format := '${name}=${value}'.to_wide()
|
||||||
|
defer {
|
||||||
|
unsafe { free(voidptr(format)) }
|
||||||
|
}
|
||||||
if overwrite {
|
if overwrite {
|
||||||
unsafe {
|
unsafe {
|
||||||
return C._putenv(&char(format.str))
|
return C._wputenv(format)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if getenv(name).len == 0 {
|
if getenv(name).len == 0 {
|
||||||
unsafe {
|
unsafe {
|
||||||
return C._putenv(&char(format.str))
|
return C._wputenv(format)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,8 +71,11 @@ pub fn setenv(name string, value string, overwrite bool) int {
|
||||||
// os.unsetenv clears an environment variable with `name`.
|
// os.unsetenv clears an environment variable with `name`.
|
||||||
pub fn unsetenv(name string) int {
|
pub fn unsetenv(name string) int {
|
||||||
$if windows {
|
$if windows {
|
||||||
format := '${name}='
|
format := '${name}='.to_wide()
|
||||||
return C._putenv(&char(format.str))
|
defer {
|
||||||
|
unsafe { free(voidptr(format)) }
|
||||||
|
}
|
||||||
|
return C._wputenv(format)
|
||||||
} $else {
|
} $else {
|
||||||
return C.unsetenv(&char(name.str))
|
return C.unsetenv(&char(name.str))
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,3 +52,19 @@ fn test_getenv_empty_var() {
|
||||||
os.setenv('empty${key}', '""', false)
|
os.setenv('empty${key}', '""', false)
|
||||||
assert os.getenv('empty${key}') == '""'
|
assert os.getenv('empty${key}') == '""'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_environ_non_ascii() {
|
||||||
|
os.setenv('Büro', 'gebäude', false)
|
||||||
|
assert os.getenv('Büro') == 'gebäude'
|
||||||
|
os.setenv('Büro', 'gebäudehaus', true)
|
||||||
|
assert os.getenv('Büro') == 'gebäudehaus'
|
||||||
|
os.setenv('Büro', 'gebäudehaus in der Straße', true)
|
||||||
|
assert os.getenv('Büro') == 'gebäudehaus in der Straße'
|
||||||
|
os.unsetenv('Büro')
|
||||||
|
assert os.getenv('Büro') == ''
|
||||||
|
|
||||||
|
os.setenv('한국어', '초보자를 위한', false)
|
||||||
|
assert os.getenv('한국어') == '초보자를 위한'
|
||||||
|
os.unsetenv('한국어')
|
||||||
|
assert os.getenv('한국어') == ''
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue