Fixed buf on hostname and added optional type.

Before submitting this PR, submit https://github.com/vlang/v/pull/1188
pull/1196/head
0x9ef 2019-07-16 21:29:17 +03:00 committed by Alexander Medvednikov
parent 91a712fdf0
commit 8ef3313d63
1 changed files with 9 additions and 5 deletions

View File

@ -484,10 +484,14 @@ pub fn user_os() string {
}
// hostname returns hostname
pub fn hostname() string {
mut hname := [1024]byte
hname[1023] = `\0`
C.gethostname(&hname, 1023)
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)
}