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

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