You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

syscall.go 3.3KB

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