github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/syscall/syscall.go (about) 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 // Package syscall contains an interface to the low-level operating system 6 // primitives. The details vary depending on the underlying system, and 7 // by default, godoc will display the syscall documentation for the current 8 // system. If you want godoc to display syscall documentation for another 9 // system, set $GOOS and $GOARCH to the desired system. For example, if 10 // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS 11 // to freebsd and $GOARCH to arm. 12 // The primary use of syscall is inside other packages that provide a more 13 // portable interface to the system, such as "os", "time" and "net". Use 14 // those packages rather than this one if you can. 15 // For details of the functions and data types in this package consult 16 // the manuals for the appropriate operating system. 17 // These calls return err == nil to indicate success; otherwise 18 // err is an operating system error describing the failure. 19 // On most systems, that error has type syscall.Errno. 20 // 21 // NOTE: This package is locked down. Code outside the standard 22 // Go repository should be migrated to use the corresponding 23 // package in the go.sys subrepository. That is also where updates 24 // required by new systems or versions should be applied. 25 // See https://golang.org/s/go1.4-syscall for more information. 26 // 27 package syscall 28 29 import "unsafe" 30 31 // StringByteSlice is deprecated. Use ByteSliceFromString instead. 32 // If s contains a NUL byte this function panics instead of 33 // returning an error. 34 func StringByteSlice(s string) []byte { 35 a, err := ByteSliceFromString(s) 36 if err != nil { 37 panic("syscall: string with NUL passed to StringByteSlice") 38 } 39 return a 40 } 41 42 // ByteSliceFromString returns a NUL-terminated slice of bytes 43 // containing the text of s. If s contains a NUL byte at any 44 // location, it returns (nil, EINVAL). 45 func ByteSliceFromString(s string) ([]byte, error) { 46 for i := 0; i < len(s); i++ { 47 if s[i] == 0 { 48 return nil, EINVAL 49 } 50 } 51 a := make([]byte, len(s)+1) 52 copy(a, s) 53 return a, nil 54 } 55 56 // StringBytePtr is deprecated. Use BytePtrFromString instead. 57 // If s contains a NUL byte this function panics instead of 58 // returning an error. 59 func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] } 60 61 // BytePtrFromString returns a pointer to a NUL-terminated array of 62 // bytes containing the text of s. If s contains a NUL byte at any 63 // location, it returns (nil, EINVAL). 64 func BytePtrFromString(s string) (*byte, error) { 65 a, err := ByteSliceFromString(s) 66 if err != nil { 67 return nil, err 68 } 69 return &a[0], nil 70 } 71 72 // Single-word zero for use when we need a valid pointer to 0 bytes. 73 // See mksyscall.pl. 74 var _zero uintptr 75 76 func (ts *Timespec) Unix() (sec int64, nsec int64) { 77 return int64(ts.Sec), int64(ts.Nsec) 78 } 79 80 func (tv *Timeval) Unix() (sec int64, nsec int64) { 81 return int64(tv.Sec), int64(tv.Usec) * 1000 82 } 83 84 func (ts *Timespec) Nano() int64 { 85 return int64(ts.Sec)*1e9 + int64(ts.Nsec) 86 } 87 88 func (tv *Timeval) Nano() int64 { 89 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 90 } 91 92 // use is a no-op, but the compiler cannot see that it is. 93 // Calling use(p) ensures that p is kept live until that point. 94 //go:noescape 95 func use(p unsafe.Pointer)