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.
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.
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.
19 // For details of the functions and data types in this package consult
20 // the manuals for the appropriate operating system.
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"
32 "golang.org/x/sys/internal/unsafeheader"
35 // ByteSliceFromString returns a NUL-terminated slice of bytes
36 // containing the text of s. If s contains a NUL byte at any
37 // location, it returns (nil, EINVAL).
38 func ByteSliceFromString(s string) ([]byte, error) {
39 if strings.IndexByte(s, 0) != -1 {
42 a := make([]byte, len(s)+1)
47 // BytePtrFromString returns a pointer to a NUL-terminated array of
48 // bytes containing the text of s. If s contains a NUL byte at any
49 // location, it returns (nil, EINVAL).
50 func BytePtrFromString(s string) (*byte, error) {
51 a, err := ByteSliceFromString(s)
58 // ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any
59 // bytes after the NUL removed.
60 func ByteSliceToString(s []byte) string {
61 if i := bytes.IndexByte(s, 0); i != -1 {
67 // BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.
68 // If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated
69 // at a zero byte; if the zero byte is not present, the program may crash.
70 func BytePtrToString(p *byte) string {
78 // Find NUL terminator.
80 for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {
81 ptr = unsafe.Pointer(uintptr(ptr) + 1)
85 h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
86 h.Data = unsafe.Pointer(p)
93 // Single-word zero for use when we need a valid pointer to 0 bytes.
97 func (ts *Timespec) Unix() (sec int64, nsec int64) {
98 return int64(ts.Sec), int64(ts.Nsec)
101 func (tv *Timeval) Unix() (sec int64, nsec int64) {
102 return int64(tv.Sec), int64(tv.Usec) * 1000
105 func (ts *Timespec) Nano() int64 {
106 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
109 func (tv *Timeval) Nano() int64 {
110 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
113 // use is a no-op, but the compiler cannot see that it is.
114 // Calling use(p) ensures that p is kept live until that point.
116 func use(p unsafe.Pointer)