syscall.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 darwin dragonfly freebsd linux netbsd openbsd
  5. // Package unix 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 OS-specific documentation for the current
  8. // system. If you want godoc to display OS 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. // The primary use of this package is inside other packages that provide a more
  13. // portable interface to the system, such as "os", "time" and "net". Use
  14. // those packages rather than this one if you can.
  15. // For details of the functions and data types in this package consult
  16. // the manuals for the appropriate operating system.
  17. // These calls return err == nil to indicate success; otherwise
  18. // err is an operating system error describing the failure.
  19. // On Unix systems, that error has type unix.Errno.
  20. package unix
  21. // StringByteSlice is deprecated. Use ByteSliceFromString instead.
  22. // If s contains a NUL byte this function panics instead of
  23. // returning an error.
  24. func StringByteSlice(s string) []byte {
  25. a, err := ByteSliceFromString(s)
  26. if err != nil {
  27. panic("unix: string with NUL passed to StringByteSlice")
  28. }
  29. return a
  30. }
  31. // ByteSliceFromString returns a NUL-terminated slice of bytes
  32. // containing the text of s. If s contains a NUL byte at any
  33. // location, it returns (nil, EINVAL).
  34. func ByteSliceFromString(s string) ([]byte, error) {
  35. for i := 0; i < len(s); i++ {
  36. if s[i] == 0 {
  37. return nil, EINVAL
  38. }
  39. }
  40. a := make([]byte, len(s)+1)
  41. copy(a, s)
  42. return a, nil
  43. }
  44. // StringBytePtr is deprecated. Use BytePtrFromString instead.
  45. // If s contains a NUL byte this function panics instead of
  46. // returning an error.
  47. func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] }
  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. // Single-word zero for use when we need a valid pointer to 0 bytes.
  59. // See mkunix.pl.
  60. var _zero uintptr
  61. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  62. return int64(ts.Sec), int64(ts.Nsec)
  63. }
  64. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  65. return int64(tv.Sec), int64(tv.Usec) * 1000
  66. }
  67. func (ts *Timespec) Nano() int64 {
  68. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  69. }
  70. func (tv *Timeval) Nano() int64 {
  71. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  72. }