syscall.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // Package plan9 contains an interface to the low-level operating system
  5. // primitives. OS details vary depending on the underlying system, and
  6. // by default, godoc will display the OS-specific documentation for the current
  7. // system. If you want godoc to display documentation for another
  8. // system, set $GOOS and $GOARCH to the desired system. For example, if
  9. // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS
  10. // to freebsd and $GOARCH to arm.
  11. // The primary use of this package is inside other packages that provide a more
  12. // portable interface to the system, such as "os", "time" and "net". Use
  13. // those packages rather than this one if you can.
  14. // For details of the functions and data types in this package consult
  15. // the manuals for the appropriate operating system.
  16. // These calls return err == nil to indicate success; otherwise
  17. // err represents an operating system error describing the failure.
  18. package plan9
  19. // StringByteSlice is deprecated. Use ByteSliceFromString instead.
  20. // If s contains a NUL byte this function panics instead of
  21. // returning an error.
  22. func StringByteSlice(s string) []byte {
  23. a, err := ByteSliceFromString(s)
  24. if err != nil {
  25. panic("syscall: string with NUL passed to StringByteSlice")
  26. }
  27. return a
  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. for i := 0; i < len(s); i++ {
  34. if s[i] == 0 {
  35. return nil, EINVAL
  36. }
  37. }
  38. a := make([]byte, len(s)+1)
  39. copy(a, s)
  40. return a, nil
  41. }
  42. // StringBytePtr is deprecated. Use BytePtrFromString instead.
  43. // If s contains a NUL byte this function panics instead of
  44. // returning an error.
  45. func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] }
  46. // BytePtrFromString returns a pointer to a NUL-terminated array of
  47. // bytes containing the text of s. If s contains a NUL byte at any
  48. // location, it returns (nil, EINVAL).
  49. func BytePtrFromString(s string) (*byte, error) {
  50. a, err := ByteSliceFromString(s)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &a[0], nil
  55. }
  56. // Single-word zero for use when we need a valid pointer to 0 bytes.
  57. // See mksyscall.pl.
  58. var _zero uintptr
  59. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  60. return int64(ts.Sec), int64(ts.Nsec)
  61. }
  62. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  63. return int64(tv.Sec), int64(tv.Usec) * 1000
  64. }
  65. func (ts *Timespec) Nano() int64 {
  66. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  67. }
  68. func (tv *Timeval) Nano() int64 {
  69. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  70. }