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.

syscall.go 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 aix darwin dragonfly freebsd linux netbsd openbsd solaris
  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. //
  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.Errno.
  23. package unix // import "golang.org/x/sys/unix"
  24. import (
  25. "bytes"
  26. "strings"
  27. "unsafe"
  28. "golang.org/x/sys/internal/unsafeheader"
  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. if strings.IndexByte(s, 0) != -1 {
  35. return nil, EINVAL
  36. }
  37. a := make([]byte, len(s)+1)
  38. copy(a, s)
  39. return a, nil
  40. }
  41. // BytePtrFromString returns a pointer to a NUL-terminated array of
  42. // bytes containing the text of s. If s contains a NUL byte at any
  43. // location, it returns (nil, EINVAL).
  44. func BytePtrFromString(s string) (*byte, error) {
  45. a, err := ByteSliceFromString(s)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &a[0], nil
  50. }
  51. // ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any
  52. // bytes after the NUL removed.
  53. func ByteSliceToString(s []byte) string {
  54. if i := bytes.IndexByte(s, 0); i != -1 {
  55. s = s[:i]
  56. }
  57. return string(s)
  58. }
  59. // BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.
  60. // If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated
  61. // at a zero byte; if the zero byte is not present, the program may crash.
  62. func BytePtrToString(p *byte) string {
  63. if p == nil {
  64. return ""
  65. }
  66. if *p == 0 {
  67. return ""
  68. }
  69. // Find NUL terminator.
  70. n := 0
  71. for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {
  72. ptr = unsafe.Pointer(uintptr(ptr) + 1)
  73. }
  74. var s []byte
  75. h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
  76. h.Data = unsafe.Pointer(p)
  77. h.Len = n
  78. h.Cap = n
  79. return string(s)
  80. }
  81. // Single-word zero for use when we need a valid pointer to 0 bytes.
  82. var _zero uintptr