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.

51 lines
2.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. // +build 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. // 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 represents an operating system error describing the failure and
  19. // holds a value of type syscall.Errno.
  20. package unix // import "golang.org/x/sys/unix"
  21. // ByteSliceFromString returns a NUL-terminated slice of bytes
  22. // containing the text of s. If s contains a NUL byte at any
  23. // location, it returns (nil, EINVAL).
  24. func ByteSliceFromString(s string) ([]byte, error) {
  25. for i := 0; i < len(s); i++ {
  26. if s[i] == 0 {
  27. return nil, EINVAL
  28. }
  29. }
  30. a := make([]byte, len(s)+1)
  31. copy(a, s)
  32. return a, nil
  33. }
  34. // BytePtrFromString returns a pointer to a NUL-terminated array of
  35. // bytes containing the text of s. If s contains a NUL byte at any
  36. // location, it returns (nil, EINVAL).
  37. func BytePtrFromString(s string) (*byte, error) {
  38. a, err := ByteSliceFromString(s)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &a[0], nil
  43. }
  44. // Single-word zero for use when we need a valid pointer to 0 bytes.
  45. // See mkunix.pl.
  46. var _zero uintptr