blob: 53a40f26772fcc8d6242ffb81cc4151e4639b342 [file] [log] [blame]
Colin Cross1371fe42019-03-19 21:08:48 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package os
6
7import "syscall"
8
9// gethostname syscall cannot be used because it also returns the domain.
10// Therefore, hostname is retrieve with uname syscall and the Nodename field.
11
12func hostname() (name string, err error) {
13 var u syscall.Utsname
14 if errno := syscall.Uname(&u); errno != nil {
15 return "", NewSyscallError("uname", errno)
16 }
17 b := make([]byte, len(u.Nodename))
18 i := 0
19 for ; i < len(u.Nodename); i++ {
20 if u.Nodename[i] == 0 {
21 break
22 }
23 b[i] = byte(u.Nodename[i])
24 }
25 return string(b[:i]), nil
26}