github.com/bir3/gocompiler@v0.9.2202/src/xvendor/golang.org/x/sys/plan9/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 //go:build plan9 6 7 // Package plan9 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 the OS-specific documentation for the current 10 // system. If you want godoc to display 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 // 15 // The primary use of this package is inside other packages that provide a more 16 // portable interface to the system, such as "os", "time" and "net". Use 17 // those packages rather than this one if you can. 18 // 19 // For details of the functions and data types in this package consult 20 // the manuals for the appropriate operating system. 21 // 22 // These calls return err == nil to indicate success; otherwise 23 // err represents an operating system error describing the failure and 24 // holds a value of type syscall.ErrorString. 25 package plan9 // import "golang.org/x/sys/plan9" 26 27 import ( 28 "bytes" 29 "strings" 30 "unsafe" 31 ) 32 33 // ByteSliceFromString returns a NUL-terminated slice of bytes 34 // containing the text of s. If s contains a NUL byte at any 35 // location, it returns (nil, EINVAL). 36 func ByteSliceFromString(s string) ([]byte, error) { 37 if strings.IndexByte(s, 0) != -1 { 38 return nil, EINVAL 39 } 40 a := make([]byte, len(s)+1) 41 copy(a, s) 42 return a, nil 43 } 44 45 // BytePtrFromString returns a pointer to a NUL-terminated array of 46 // bytes containing the text of s. If s contains a NUL byte at any 47 // location, it returns (nil, EINVAL). 48 func BytePtrFromString(s string) (*byte, error) { 49 a, err := ByteSliceFromString(s) 50 if err != nil { 51 return nil, err 52 } 53 return &a[0], nil 54 } 55 56 // ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any 57 // bytes after the NUL removed. 58 func ByteSliceToString(s []byte) string { 59 if i := bytes.IndexByte(s, 0); i != -1 { 60 s = s[:i] 61 } 62 return string(s) 63 } 64 65 // BytePtrToString takes a pointer to a sequence of text and returns the corresponding string. 66 // If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated 67 // at a zero byte; if the zero byte is not present, the program may crash. 68 func BytePtrToString(p *byte) string { 69 if p == nil { 70 return "" 71 } 72 if *p == 0 { 73 return "" 74 } 75 76 // Find NUL terminator. 77 n := 0 78 for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ { 79 ptr = unsafe.Pointer(uintptr(ptr) + 1) 80 } 81 82 return string(unsafe.Slice(p, n)) 83 } 84 85 // Single-word zero for use when we need a valid pointer to 0 bytes. 86 // See mksyscall.pl. 87 var _zero uintptr 88 89 func (ts *Timespec) Unix() (sec int64, nsec int64) { 90 return int64(ts.Sec), int64(ts.Nsec) 91 } 92 93 func (tv *Timeval) Unix() (sec int64, nsec int64) { 94 return int64(tv.Sec), int64(tv.Usec) * 1000 95 } 96 97 func (ts *Timespec) Nano() int64 { 98 return int64(ts.Sec)*1e9 + int64(ts.Nsec) 99 } 100 101 func (tv *Timeval) Nano() int64 { 102 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 103 } 104 105 // use is a no-op, but the compiler cannot see that it is. 106 // Calling use(p) ensures that p is kept live until that point. 107 // 108 //go:noescape 109 func use(p unsafe.Pointer)