~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/os/sys_linux.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
 
 
5
// Linux-specific
 
6
 
 
7
package os
 
8
 
 
9
 
 
10
// Hostname returns the host name reported by the kernel.
 
11
func Hostname() (name string, err Error) {
 
12
        f, err := Open("/proc/sys/kernel/hostname", O_RDONLY, 0)
 
13
        if err != nil {
 
14
                return "", err
 
15
        }
 
16
        defer f.Close()
 
17
 
 
18
        var buf [512]byte // Enough for a DNS name.
 
19
        n, err := f.Read(buf[0:])
 
20
        if err != nil {
 
21
                return "", err
 
22
        }
 
23
 
 
24
        if n > 0 && buf[n-1] == '\n' {
 
25
                n--
 
26
        }
 
27
        return string(buf[0:n]), nil
 
28
}