blob: fd4ee8ebeb7072d32b042e2d6f1ba7b8bc055122 [file] [log] [blame]
Rob Pike20acc5c2014-08-11 14:48:46 -07001// 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
Tony Reixd99a5782018-08-17 10:25:03 +02005// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
Rob Pike279b3782014-08-12 22:59:00 -07006
Rob Pike8442dd22014-08-11 15:58:26 -07007// Package unix contains an interface to the low-level operating system
Tobias Klauser9e4fff12017-10-25 22:17:19 +02008// primitives. OS details vary depending on the underlying system, and
Rob Pike8442dd22014-08-11 15:58:26 -07009// by default, godoc will display OS-specific documentation for the current
Tobias Klauser9e4fff12017-10-25 22:17:19 +020010// system. If you want godoc to display OS documentation for another
11// system, set $GOOS and $GOARCH to the desired system. For example, if
Rob Pike20acc5c2014-08-11 14:48:46 -070012// you want to view documentation for freebsd/arm on linux/amd64, set $GOOS
13// to freebsd and $GOARCH to arm.
Kevin Burke01acb382018-03-18 11:58:34 -070014//
Rob Pike8442dd22014-08-11 15:58:26 -070015// The primary use of this package is inside other packages that provide a more
Rob Pike20acc5c2014-08-11 14:48:46 -070016// portable interface to the system, such as "os", "time" and "net". Use
17// those packages rather than this one if you can.
Kevin Burke01acb382018-03-18 11:58:34 -070018//
Rob Pike20acc5c2014-08-11 14:48:46 -070019// For details of the functions and data types in this package consult
20// the manuals for the appropriate operating system.
Kevin Burke01acb382018-03-18 11:58:34 -070021//
Rob Pike20acc5c2014-08-11 14:48:46 -070022// These calls return err == nil to indicate success; otherwise
Rob Pikedc3c21c2014-08-15 09:57:24 -070023// err represents an operating system error describing the failure and
24// holds a value of type syscall.Errno.
David Symonds1da8e6e2014-12-09 14:19:05 +110025package unix // import "golang.org/x/sys/unix"
Rob Pike20acc5c2014-08-11 14:48:46 -070026
Tobias Klauser91ee8cd2018-03-22 15:10:41 +010027import "strings"
28
Rob Pike20acc5c2014-08-11 14:48:46 -070029// ByteSliceFromString returns a NUL-terminated slice of bytes
30// containing the text of s. If s contains a NUL byte at any
31// location, it returns (nil, EINVAL).
32func ByteSliceFromString(s string) ([]byte, error) {
Tobias Klauser91ee8cd2018-03-22 15:10:41 +010033 if strings.IndexByte(s, 0) != -1 {
34 return nil, EINVAL
Rob Pike20acc5c2014-08-11 14:48:46 -070035 }
36 a := make([]byte, len(s)+1)
37 copy(a, s)
38 return a, nil
39}
40
Rob Pike20acc5c2014-08-11 14:48:46 -070041// 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).
44func 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
52// Single-word zero for use when we need a valid pointer to 0 bytes.
Rob Pike20acc5c2014-08-11 14:48:46 -070053var _zero uintptr