Skip to content

Commit 15cb18c

Browse files
authored
os: make hostname and loginname functions return Result (#17414)
1 parent 9c511e0 commit 15cb18c

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

vlib/os/os_nix.c.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn uname() Uname {
235235
return u
236236
}
237237

238-
pub fn hostname() string {
238+
pub fn hostname() !string {
239239
mut hstnme := ''
240240
size := 256
241241
mut buf := unsafe { &char(malloc_noscan(size)) }
@@ -244,15 +244,15 @@ pub fn hostname() string {
244244
unsafe { free(buf) }
245245
return hstnme
246246
}
247-
return ''
247+
return error(posix_get_error_msg(C.errno))
248248
}
249249

250-
pub fn loginname() string {
250+
pub fn loginname() !string {
251251
x := C.getlogin()
252252
if !isnil(x) {
253253
return unsafe { cstring_to_vstring(x) }
254254
}
255-
return ''
255+
return error(posix_get_error_msg(C.errno))
256256
}
257257

258258
fn init_os_args(argc int, argv &&u8) []string {

vlib/os/os_test.v

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ fn test_write_and_read_string_to_file() {
169169

170170
// test_write_and_read_bytes checks for regressions made in the functions
171171
// read_bytes, read_bytes_at and write_bytes.
172+
172173
fn test_write_and_read_bytes() {
173174
file_name := './byte_reader_writer.tst'
174175
payload := [u8(`I`), `D`, `D`, `Q`, `D`]
@@ -457,6 +458,7 @@ fn test_realpath_absolutizes_existing_relative_paths() {
457458
}
458459

459460
// TODO: think much more about whether this is desirable:
461+
460462
fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
461463
relative_path := os.join_path('one', 'nonexisting_folder', '..', 'something')
462464
$if !windows {
@@ -554,6 +556,7 @@ fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() {
554556
// println(cpid)
555557
// }
556558
// }
559+
557560
fn test_symlink() {
558561
os.mkdir('symlink') or { panic(err) }
559562
os.symlink('symlink', 'symlink2') or { panic(err) }
@@ -805,9 +808,15 @@ fn test_truncate() {
805808
}
806809

807810
fn test_hostname() {
808-
assert os.hostname().len > 2
811+
hostname := os.hostname() or { '' }
812+
assert hostname.len > 2
809813
}
810814

815+
// fn test_loginname() {
816+
// loginname := os.loginname() or { '' }
817+
// assert loginname.len > 2
818+
//}
819+
811820
fn test_glob() {
812821
os.mkdir('test_dir') or { panic(err) }
813822
for i in 0 .. 4 {

vlib/os/os_windows.c.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub fn add_vectored_exception_handler(first bool, handler VectoredExceptionHandl
460460
// See: [NT Version Info](https://en.wikipedia.org/wiki/Windows_NT) @@ <https://archive.is/GnnvF>
461461
// and: [NT Version Info (detailed)](https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions#NT_Kernel-based_2)
462462
pub fn uname() Uname {
463-
nodename := hostname()
463+
nodename := hostname() or { '' }
464464
// ToDO: environment variables have low reliability; check for another quick way
465465
machine := getenv('PROCESSOR_ARCHITECTURE') // * note: 'AMD64' == 'x86_64' (not standardized, but 'x86_64' use is more common; but, python == 'AMD64')
466466
version_info := execute('cmd /d/c ver').output
@@ -474,22 +474,22 @@ pub fn uname() Uname {
474474
}
475475
}
476476

477-
pub fn hostname() string {
477+
pub fn hostname() !string {
478478
hostname := [255]u16{}
479479
size := u32(255)
480480
res := C.GetComputerNameW(&hostname[0], &size)
481481
if !res {
482-
return get_error_msg(int(C.GetLastError()))
482+
return error(get_error_msg(int(C.GetLastError())))
483483
}
484484
return unsafe { string_from_wide(&hostname[0]) }
485485
}
486486

487-
pub fn loginname() string {
487+
pub fn loginname() !string {
488488
loginname := [255]u16{}
489489
size := u32(255)
490490
res := C.GetUserNameW(&loginname[0], &size)
491491
if !res {
492-
return get_error_msg(int(C.GetLastError()))
492+
return error(get_error_msg(int(C.GetLastError())))
493493
}
494494
return unsafe { string_from_wide(&loginname[0]) }
495495
}

0 commit comments

Comments
 (0)