c++: decltype of (by-value captured reference) [PR79620]
[official-gcc.git] / libgo / go / os / sys_linux.go
blob36a8a244555aa71e41978c2778190735c4bfca84
1 // Copyright 2009 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.
5 package os
7 import (
8 "runtime"
9 "syscall"
12 func hostname() (name string, err error) {
13 // Try uname first, as it's only one system call and reading
14 // from /proc is not allowed on Android.
15 var un syscall.Utsname
16 err = syscall.Uname(&un)
18 var buf [512]byte // Enough for a DNS name.
19 for i, b := range un.Nodename[:] {
20 buf[i] = uint8(b)
21 if b == 0 {
22 name = string(buf[:i])
23 break
26 // If we got a name and it's not potentially truncated
27 // (Nodename is 65 bytes), return it.
28 if err == nil && len(name) > 0 && len(name) < 64 {
29 return name, nil
31 if runtime.GOOS == "android" {
32 if name != "" {
33 return name, nil
35 return "localhost", nil
38 f, err := Open("/proc/sys/kernel/hostname")
39 if err != nil {
40 return "", err
42 defer f.Close()
44 n, err := f.Read(buf[:])
45 if err != nil {
46 return "", err
49 if n > 0 && buf[n-1] == '\n' {
50 n--
52 return string(buf[:n]), nil