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