From 8ef3313d63edf13a5e608d58b9f62849d7fbe606 Mon Sep 17 00:00:00 2001 From: 0x9ef <43169346+0x9ef@users.noreply.github.com> Date: Tue, 16 Jul 2019 21:29:17 +0300 Subject: [PATCH] Fixed buf on hostname and added optional type. Before submitting this PR, submit https://github.com/vlang/v/pull/1188 --- vlib/os/os.v | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index 49f69ee874..a17b12b53e 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -484,11 +484,15 @@ pub fn user_os() string { } // hostname returns hostname -pub fn hostname() string { - mut hname := [1024]byte - hname[1023] = `\0` - C.gethostname(&hname, 1023) - return tos_clone(hname) +pub fn hostname() ?string { + mut hname := [256]byte + // https://www.ietf.org/rfc/rfc1035.txt + // The host name is returned as a null-terminated string. + res := C.gethostname(&hname, 256) + if res != 0 { + return error('os: hostname cannot get host name of PC.') + } + return tos_clone(hname) } // home_dir returns path to user's home directory.