OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Linux-specific | 5 // Linux-specific |
6 | 6 |
7 package os | 7 package os |
8 | 8 |
9 | 9 |
10 // Hostname returns the host name reported by the kernel. | 10 // Hostname returns the host name reported by the kernel. |
11 func Hostname() (name string, err Error) { | 11 func Hostname() (name string, err Error) { |
12 » f, err := Open("/proc/sys/kernel/hostname", O_RDONLY, 0); | 12 » f, err := Open("/proc/sys/kernel/hostname", O_RDONLY, 0) |
13 if err != nil { | 13 if err != nil { |
14 return "", err | 14 return "", err |
15 } | 15 } |
16 » defer f.Close(); | 16 » defer f.Close() |
17 | 17 |
18 » var buf [512]byte;» // Enough for a DNS name. | 18 » var buf [512]byte // Enough for a DNS name. |
19 » n, err := f.Read(&buf); | 19 » n, err := f.Read(&buf) |
20 if err != nil { | 20 if err != nil { |
21 return "", err | 21 return "", err |
22 } | 22 } |
23 | 23 |
24 if n > 0 && buf[n-1] == '\n' { | 24 if n > 0 && buf[n-1] == '\n' { |
25 n-- | 25 n-- |
26 } | 26 } |
27 » return string(buf[0:n]), nil; | 27 » return string(buf[0:n]), nil |
28 } | 28 } |
OLD | NEW |