mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: make hostname and loginname functions return Result (#17414)
This commit is contained in:
parent
9c511e03f6
commit
15cb18cbd2
@ -235,7 +235,7 @@ pub fn uname() Uname {
|
|||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hostname() string {
|
pub fn hostname() !string {
|
||||||
mut hstnme := ''
|
mut hstnme := ''
|
||||||
size := 256
|
size := 256
|
||||||
mut buf := unsafe { &char(malloc_noscan(size)) }
|
mut buf := unsafe { &char(malloc_noscan(size)) }
|
||||||
@ -244,15 +244,15 @@ pub fn hostname() string {
|
|||||||
unsafe { free(buf) }
|
unsafe { free(buf) }
|
||||||
return hstnme
|
return hstnme
|
||||||
}
|
}
|
||||||
return ''
|
return error(posix_get_error_msg(C.errno))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn loginname() string {
|
pub fn loginname() !string {
|
||||||
x := C.getlogin()
|
x := C.getlogin()
|
||||||
if !isnil(x) {
|
if !isnil(x) {
|
||||||
return unsafe { cstring_to_vstring(x) }
|
return unsafe { cstring_to_vstring(x) }
|
||||||
}
|
}
|
||||||
return ''
|
return error(posix_get_error_msg(C.errno))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_os_args(argc int, argv &&u8) []string {
|
fn init_os_args(argc int, argv &&u8) []string {
|
||||||
|
@ -169,6 +169,7 @@ fn test_write_and_read_string_to_file() {
|
|||||||
|
|
||||||
// test_write_and_read_bytes checks for regressions made in the functions
|
// test_write_and_read_bytes checks for regressions made in the functions
|
||||||
// read_bytes, read_bytes_at and write_bytes.
|
// read_bytes, read_bytes_at and write_bytes.
|
||||||
|
|
||||||
fn test_write_and_read_bytes() {
|
fn test_write_and_read_bytes() {
|
||||||
file_name := './byte_reader_writer.tst'
|
file_name := './byte_reader_writer.tst'
|
||||||
payload := [u8(`I`), `D`, `D`, `Q`, `D`]
|
payload := [u8(`I`), `D`, `D`, `Q`, `D`]
|
||||||
@ -457,6 +458,7 @@ fn test_realpath_absolutizes_existing_relative_paths() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: think much more about whether this is desirable:
|
// TODO: think much more about whether this is desirable:
|
||||||
|
|
||||||
fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
|
fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
|
||||||
relative_path := os.join_path('one', 'nonexisting_folder', '..', 'something')
|
relative_path := os.join_path('one', 'nonexisting_folder', '..', 'something')
|
||||||
$if !windows {
|
$if !windows {
|
||||||
@ -554,6 +556,7 @@ fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() {
|
|||||||
// println(cpid)
|
// println(cpid)
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn test_symlink() {
|
fn test_symlink() {
|
||||||
os.mkdir('symlink') or { panic(err) }
|
os.mkdir('symlink') or { panic(err) }
|
||||||
os.symlink('symlink', 'symlink2') or { panic(err) }
|
os.symlink('symlink', 'symlink2') or { panic(err) }
|
||||||
@ -805,9 +808,15 @@ fn test_truncate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_hostname() {
|
fn test_hostname() {
|
||||||
assert os.hostname().len > 2
|
hostname := os.hostname() or { '' }
|
||||||
|
assert hostname.len > 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fn test_loginname() {
|
||||||
|
// loginname := os.loginname() or { '' }
|
||||||
|
// assert loginname.len > 2
|
||||||
|
//}
|
||||||
|
|
||||||
fn test_glob() {
|
fn test_glob() {
|
||||||
os.mkdir('test_dir') or { panic(err) }
|
os.mkdir('test_dir') or { panic(err) }
|
||||||
for i in 0 .. 4 {
|
for i in 0 .. 4 {
|
||||||
|
@ -460,7 +460,7 @@ pub fn add_vectored_exception_handler(first bool, handler VectoredExceptionHandl
|
|||||||
// See: [NT Version Info](https://en.wikipedia.org/wiki/Windows_NT) @@ <https://archive.is/GnnvF>
|
// See: [NT Version Info](https://en.wikipedia.org/wiki/Windows_NT) @@ <https://archive.is/GnnvF>
|
||||||
// and: [NT Version Info (detailed)](https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions#NT_Kernel-based_2)
|
// and: [NT Version Info (detailed)](https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions#NT_Kernel-based_2)
|
||||||
pub fn uname() Uname {
|
pub fn uname() Uname {
|
||||||
nodename := hostname()
|
nodename := hostname() or { '' }
|
||||||
// ToDO: environment variables have low reliability; check for another quick way
|
// ToDO: environment variables have low reliability; check for another quick way
|
||||||
machine := getenv('PROCESSOR_ARCHITECTURE') // * note: 'AMD64' == 'x86_64' (not standardized, but 'x86_64' use is more common; but, python == 'AMD64')
|
machine := getenv('PROCESSOR_ARCHITECTURE') // * note: 'AMD64' == 'x86_64' (not standardized, but 'x86_64' use is more common; but, python == 'AMD64')
|
||||||
version_info := execute('cmd /d/c ver').output
|
version_info := execute('cmd /d/c ver').output
|
||||||
@ -474,22 +474,22 @@ pub fn uname() Uname {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hostname() string {
|
pub fn hostname() !string {
|
||||||
hostname := [255]u16{}
|
hostname := [255]u16{}
|
||||||
size := u32(255)
|
size := u32(255)
|
||||||
res := C.GetComputerNameW(&hostname[0], &size)
|
res := C.GetComputerNameW(&hostname[0], &size)
|
||||||
if !res {
|
if !res {
|
||||||
return get_error_msg(int(C.GetLastError()))
|
return error(get_error_msg(int(C.GetLastError())))
|
||||||
}
|
}
|
||||||
return unsafe { string_from_wide(&hostname[0]) }
|
return unsafe { string_from_wide(&hostname[0]) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn loginname() string {
|
pub fn loginname() !string {
|
||||||
loginname := [255]u16{}
|
loginname := [255]u16{}
|
||||||
size := u32(255)
|
size := u32(255)
|
||||||
res := C.GetUserNameW(&loginname[0], &size)
|
res := C.GetUserNameW(&loginname[0], &size)
|
||||||
if !res {
|
if !res {
|
||||||
return get_error_msg(int(C.GetLastError()))
|
return error(get_error_msg(int(C.GetLastError())))
|
||||||
}
|
}
|
||||||
return unsafe { string_from_wide(&loginname[0]) }
|
return unsafe { string_from_wide(&loginname[0]) }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user