David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 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 | // +build darwin dragonfly freebsd linux netbsd openbsd solaris |
| 6 | |
| 7 | // Package unix contains an interface to the low-level operating system |
| 8 | // primitives. OS details vary depending on the underlying system, and |
| 9 | // by default, godoc will display OS-specific documentation for the current |
| 10 | // system. If you want godoc to display OS documentation for another |
| 11 | // system, set $GOOS and $GOARCH to the desired system. For example, if |
| 12 | // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS |
| 13 | // to freebsd and $GOARCH to arm. |
| 14 | // The primary use of this package is inside other packages that provide a more |
| 15 | // portable interface to the system, such as "os", "time" and "net". Use |
| 16 | // those packages rather than this one if you can. |
| 17 | // For details of the functions and data types in this package consult |
| 18 | // the manuals for the appropriate operating system. |
| 19 | // These calls return err == nil to indicate success; otherwise |
| 20 | // err represents an operating system error describing the failure and |
| 21 | // holds a value of type syscall.Errno. |
| 22 | package unix // import "golang.org/x/sys/unix" |
| 23 | |
| 24 | // ByteSliceFromString returns a NUL-terminated slice of bytes |
| 25 | // containing the text of s. If s contains a NUL byte at any |
| 26 | // location, it returns (nil, EINVAL). |
| 27 | func ByteSliceFromString(s string) ([]byte, error) { |
| 28 | for i := 0; i < len(s); i++ { |
| 29 | if s[i] == 0 { |
| 30 | return nil, EINVAL |
| 31 | } |
| 32 | } |
| 33 | a := make([]byte, len(s)+1) |
| 34 | copy(a, s) |
| 35 | return a, nil |
| 36 | } |
| 37 | |
| 38 | // BytePtrFromString returns a pointer to a NUL-terminated array of |
| 39 | // bytes containing the text of s. If s contains a NUL byte at any |
| 40 | // location, it returns (nil, EINVAL). |
| 41 | func BytePtrFromString(s string) (*byte, error) { |
| 42 | a, err := ByteSliceFromString(s) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | return &a[0], nil |
| 47 | } |
| 48 | |
| 49 | // Single-word zero for use when we need a valid pointer to 0 bytes. |
| 50 | // See mkunix.pl. |
| 51 | var _zero uintptr |
| 52 | |
| 53 | func (ts *Timespec) Unix() (sec int64, nsec int64) { |
| 54 | return int64(ts.Sec), int64(ts.Nsec) |
| 55 | } |
| 56 | |
| 57 | func (tv *Timeval) Unix() (sec int64, nsec int64) { |
| 58 | return int64(tv.Sec), int64(tv.Usec) * 1000 |
| 59 | } |
| 60 | |
| 61 | func (ts *Timespec) Nano() int64 { |
| 62 | return int64(ts.Sec)*1e9 + int64(ts.Nsec) |
| 63 | } |
| 64 | |
| 65 | func (tv *Timeval) Nano() int64 { |
| 66 | return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 |
| 67 | } |
| 68 | |
| 69 | func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 } |