syscall.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 unix 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 OS-specific documentation for the current
  7. // system. If you want godoc to display OS 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 is an operating system error describing the failure.
  18. // On Unix systems, that error has type unix.Errno.
  19. package unix
  20. // StringByteSlice is deprecated. Use ByteSliceFromString instead.
  21. // If s contains a NUL byte this function panics instead of
  22. // returning an error.
  23. func StringByteSlice(s string) []byte {
  24. a, err := ByteSliceFromString(s)
  25. if err != nil {
  26. panic("unix: string with NUL passed to StringByteSlice")
  27. }
  28. return a
  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. for i := 0; i < len(s); i++ {
  35. if s[i] == 0 {
  36. return nil, EINVAL
  37. }
  38. }
  39. a := make([]byte, len(s)+1)
  40. copy(a, s)
  41. return a, nil
  42. }
  43. // StringBytePtr is deprecated. Use BytePtrFromString instead.
  44. // If s contains a NUL byte this function panics instead of
  45. // returning an error.
  46. func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] }
  47. // BytePtrFromString returns a pointer to a NUL-terminated array of
  48. // bytes containing the text of s. If s contains a NUL byte at any
  49. // location, it returns (nil, EINVAL).
  50. func BytePtrFromString(s string) (*byte, error) {
  51. a, err := ByteSliceFromString(s)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &a[0], nil
  56. }
  57. // Single-word zero for use when we need a valid pointer to 0 bytes.
  58. // See mkunix.pl.
  59. var _zero uintptr
  60. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  61. return int64(ts.Sec), int64(ts.Nsec)
  62. }
  63. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  64. return int64(tv.Sec), int64(tv.Usec) * 1000
  65. }
  66. func (ts *Timespec) Nano() int64 {
  67. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  68. }
  69. func (tv *Timeval) Nano() int64 {
  70. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  71. }