Skip to content

Commit

Permalink
os: make hostname and loginname functions return Result (#17414)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-elesin committed Feb 27, 2023
1 parent 9c511e0 commit 15cb18c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions vlib/os/os_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn uname() Uname {
return u
}

pub fn hostname() string {
pub fn hostname() !string {
mut hstnme := ''
size := 256
mut buf := unsafe { &char(malloc_noscan(size)) }
Expand All @@ -244,15 +244,15 @@ pub fn hostname() string {
unsafe { free(buf) }
return hstnme
}
return ''
return error(posix_get_error_msg(C.errno))
}

pub fn loginname() string {
pub fn loginname() !string {
x := C.getlogin()
if !isnil(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 {
Expand Down
11 changes: 10 additions & 1 deletion vlib/os/os_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn test_write_and_read_string_to_file() {

// test_write_and_read_bytes checks for regressions made in the functions
// read_bytes, read_bytes_at and write_bytes.

fn test_write_and_read_bytes() {
file_name := './byte_reader_writer.tst'
payload := [u8(`I`), `D`, `D`, `Q`, `D`]
Expand Down Expand Up @@ -457,6 +458,7 @@ fn test_realpath_absolutizes_existing_relative_paths() {
}

// TODO: think much more about whether this is desirable:

fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
relative_path := os.join_path('one', 'nonexisting_folder', '..', 'something')
$if !windows {
Expand Down Expand Up @@ -554,6 +556,7 @@ fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() {
// println(cpid)
// }
// }

fn test_symlink() {
os.mkdir('symlink') or { panic(err) }
os.symlink('symlink', 'symlink2') or { panic(err) }
Expand Down Expand Up @@ -805,9 +808,15 @@ fn test_truncate() {
}

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() {
os.mkdir('test_dir') or { panic(err) }
for i in 0 .. 4 {
Expand Down
10 changes: 5 additions & 5 deletions vlib/os/os_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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>
// and: [NT Version Info (detailed)](https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions#NT_Kernel-based_2)
pub fn uname() Uname {
nodename := hostname()
nodename := hostname() or { '' }
// 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')
version_info := execute('cmd /d/c ver').output
Expand All @@ -474,22 +474,22 @@ pub fn uname() Uname {
}
}

pub fn hostname() string {
pub fn hostname() !string {
hostname := [255]u16{}
size := u32(255)
res := C.GetComputerNameW(&hostname[0], &size)
if !res {
return get_error_msg(int(C.GetLastError()))
return error(get_error_msg(int(C.GetLastError())))
}
return unsafe { string_from_wide(&hostname[0]) }
}

pub fn loginname() string {
pub fn loginname() !string {
loginname := [255]u16{}
size := u32(255)
res := C.GetUserNameW(&loginname[0], &size)
if !res {
return get_error_msg(int(C.GetLastError()))
return error(get_error_msg(int(C.GetLastError())))
}
return unsafe { string_from_wide(&loginname[0]) }
}
Expand Down

0 comments on commit 15cb18c

Please sign in to comment.