Source file src/cmd/vendor/golang.org/x/sys/plan9/syscall.go

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

View as plain text