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.

95 lines
3.0 KiB

  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 aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
  6. // Package unix contains an interface to the low-level operating system
  7. // primitives. OS details vary depending on the underlying system, and
  8. // by default, godoc will display OS-specific documentation for the current
  9. // system. If you want godoc to display OS documentation for another
  10. // system, set $GOOS and $GOARCH to the desired system. For example, if
  11. // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS
  12. // to freebsd and $GOARCH to arm.
  13. //
  14. // The primary use of this package is inside other packages that provide a more
  15. // portable interface to the system, such as "os", "time" and "net". Use
  16. // those packages rather than this one if you can.
  17. //
  18. // For details of the functions and data types in this package consult
  19. // the manuals for the appropriate operating system.
  20. //
  21. // These calls return err == nil to indicate success; otherwise
  22. // err represents an operating system error describing the failure and
  23. // holds a value of type syscall.Errno.
  24. package unix // import "golang.org/x/sys/unix"
  25. import (
  26. "bytes"
  27. "strings"
  28. "unsafe"
  29. "golang.org/x/sys/internal/unsafeheader"
  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. if strings.IndexByte(s, 0) != -1 {
  36. return nil, EINVAL
  37. }
  38. a := make([]byte, len(s)+1)
  39. copy(a, s)
  40. return a, nil
  41. }
  42. // BytePtrFromString returns a pointer to a NUL-terminated array of
  43. // bytes containing the text of s. If s contains a NUL byte at any
  44. // location, it returns (nil, EINVAL).
  45. func BytePtrFromString(s string) (*byte, error) {
  46. a, err := ByteSliceFromString(s)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &a[0], nil
  51. }
  52. // ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any
  53. // bytes after the NUL removed.
  54. func ByteSliceToString(s []byte) string {
  55. if i := bytes.IndexByte(s, 0); i != -1 {
  56. s = s[:i]
  57. }
  58. return string(s)
  59. }
  60. // BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.
  61. // If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated
  62. // at a zero byte; if the zero byte is not present, the program may crash.
  63. func BytePtrToString(p *byte) string {
  64. if p == nil {
  65. return ""
  66. }
  67. if *p == 0 {
  68. return ""
  69. }
  70. // Find NUL terminator.
  71. n := 0
  72. for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {
  73. ptr = unsafe.Pointer(uintptr(ptr) + 1)
  74. }
  75. var s []byte
  76. h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
  77. h.Data = unsafe.Pointer(p)
  78. h.Len = n
  79. h.Cap = n
  80. return string(s)
  81. }
  82. // Single-word zero for use when we need a valid pointer to 0 bytes.
  83. var _zero uintptr