syscall.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // +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 "unsafe"
  25. // ByteSliceFromString returns a NUL-terminated slice of bytes
  26. // containing the text of s. If s contains a NUL byte at any
  27. // location, it returns (nil, EINVAL).
  28. func ByteSliceFromString(s string) ([]byte, error) {
  29. for i := 0; i < len(s); i++ {
  30. if s[i] == 0 {
  31. return nil, EINVAL
  32. }
  33. }
  34. a := make([]byte, len(s)+1)
  35. copy(a, s)
  36. return a, nil
  37. }
  38. // BytePtrFromString returns a pointer to a NUL-terminated array of
  39. // bytes containing the text of s. If s contains a NUL byte at any
  40. // location, it returns (nil, EINVAL).
  41. func BytePtrFromString(s string) (*byte, error) {
  42. a, err := ByteSliceFromString(s)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &a[0], nil
  47. }
  48. // Single-word zero for use when we need a valid pointer to 0 bytes.
  49. // See mksyscall.pl.
  50. var _zero uintptr
  51. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  52. return int64(ts.Sec), int64(ts.Nsec)
  53. }
  54. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  55. return int64(tv.Sec), int64(tv.Usec) * 1000
  56. }
  57. func (ts *Timespec) Nano() int64 {
  58. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  59. }
  60. func (tv *Timeval) Nano() int64 {
  61. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  62. }
  63. // use is a no-op, but the compiler cannot see that it is.
  64. // Calling use(p) ensures that p is kept live until that point.
  65. //go:noescape
  66. func use(p unsafe.Pointer)