syscall.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. //
  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. // ByteSliceFromString returns a NUL-terminated slice of bytes
  25. // containing the text of s. If s contains a NUL byte at any
  26. // location, it returns (nil, EINVAL).
  27. func ByteSliceFromString(s string) ([]byte, error) {
  28. for i := 0; i < len(s); i++ {
  29. if s[i] == 0 {
  30. return nil, EINVAL
  31. }
  32. }
  33. a := make([]byte, len(s)+1)
  34. copy(a, s)
  35. return a, nil
  36. }
  37. // BytePtrFromString returns a pointer to a NUL-terminated array of
  38. // bytes containing the text of s. If s contains a NUL byte at any
  39. // location, it returns (nil, EINVAL).
  40. func BytePtrFromString(s string) (*byte, error) {
  41. a, err := ByteSliceFromString(s)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &a[0], nil
  46. }
  47. // Single-word zero for use when we need a valid pointer to 0 bytes.
  48. // See mkunix.pl.
  49. var _zero uintptr