Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 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 | |
Brad Fitzpatrick | fcb26fe | 2021-10-20 08:28:07 -0700 | [diff] [blame] | 5 | //go:build windows |
Rob Pike | 279b378 | 2014-08-12 22:59:00 -0700 | [diff] [blame] | 6 | // +build windows |
| 7 | |
Rob Pike | 8442dd2 | 2014-08-11 15:58:26 -0700 | [diff] [blame] | 8 | // Package windows contains an interface to the low-level operating system |
Tobias Klauser | 9e4fff1 | 2017-10-25 22:17:19 +0200 | [diff] [blame] | 9 | // primitives. OS details vary depending on the underlying system, and |
Rob Pike | 8442dd2 | 2014-08-11 15:58:26 -0700 | [diff] [blame] | 10 | // by default, godoc will display the OS-specific documentation for the current |
Tobias Klauser | 9e4fff1 | 2017-10-25 22:17:19 +0200 | [diff] [blame] | 11 | // system. If you want godoc to display syscall documentation for another |
| 12 | // system, set $GOOS and $GOARCH to the desired system. For example, if |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 13 | // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS |
| 14 | // to freebsd and $GOARCH to arm. |
Kevin Burke | 01acb38 | 2018-03-18 11:58:34 -0700 | [diff] [blame] | 15 | // |
Rob Pike | 8442dd2 | 2014-08-11 15:58:26 -0700 | [diff] [blame] | 16 | // The primary use of this package is inside other packages that provide a more |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 17 | // portable interface to the system, such as "os", "time" and "net". Use |
| 18 | // those packages rather than this one if you can. |
Kevin Burke | 01acb38 | 2018-03-18 11:58:34 -0700 | [diff] [blame] | 19 | // |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 20 | // For details of the functions and data types in this package consult |
| 21 | // the manuals for the appropriate operating system. |
Kevin Burke | 01acb38 | 2018-03-18 11:58:34 -0700 | [diff] [blame] | 22 | // |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 23 | // These calls return err == nil to indicate success; otherwise |
Rob Pike | 4faa336 | 2014-08-14 10:28:19 -0700 | [diff] [blame] | 24 | // err represents an operating system error describing the failure and |
| 25 | // holds a value of type syscall.Errno. |
David Symonds | 1da8e6e | 2014-12-09 14:19:05 +1100 | [diff] [blame] | 26 | package windows // import "golang.org/x/sys/windows" |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 27 | |
Rob Pike | c1f6f8b | 2014-09-16 13:36:53 -0700 | [diff] [blame] | 28 | import ( |
Jason A. Donenfeld | 6e5568b | 2020-10-20 00:58:12 +0200 | [diff] [blame] | 29 | "bytes" |
| 30 | "strings" |
Rob Pike | c1f6f8b | 2014-09-16 13:36:53 -0700 | [diff] [blame] | 31 | "syscall" |
Jason A. Donenfeld | 6e5568b | 2020-10-20 00:58:12 +0200 | [diff] [blame] | 32 | "unsafe" |
| 33 | |
| 34 | "golang.org/x/sys/internal/unsafeheader" |
Rob Pike | c1f6f8b | 2014-09-16 13:36:53 -0700 | [diff] [blame] | 35 | ) |
Alex Brainman | 70c4b52 | 2014-08-15 13:37:15 +1000 | [diff] [blame] | 36 | |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 37 | // ByteSliceFromString returns a NUL-terminated slice of bytes |
| 38 | // containing the text of s. If s contains a NUL byte at any |
Alex Brainman | 70c4b52 | 2014-08-15 13:37:15 +1000 | [diff] [blame] | 39 | // location, it returns (nil, syscall.EINVAL). |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 40 | func ByteSliceFromString(s string) ([]byte, error) { |
Jason A. Donenfeld | 6e5568b | 2020-10-20 00:58:12 +0200 | [diff] [blame] | 41 | if strings.IndexByte(s, 0) != -1 { |
| 42 | return nil, syscall.EINVAL |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 43 | } |
| 44 | a := make([]byte, len(s)+1) |
| 45 | copy(a, s) |
| 46 | return a, nil |
| 47 | } |
| 48 | |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 49 | // BytePtrFromString returns a pointer to a NUL-terminated array of |
| 50 | // bytes containing the text of s. If s contains a NUL byte at any |
Alex Brainman | 70c4b52 | 2014-08-15 13:37:15 +1000 | [diff] [blame] | 51 | // location, it returns (nil, syscall.EINVAL). |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 52 | func BytePtrFromString(s string) (*byte, error) { |
| 53 | a, err := ByteSliceFromString(s) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return &a[0], nil |
| 58 | } |
| 59 | |
Jason A. Donenfeld | 6e5568b | 2020-10-20 00:58:12 +0200 | [diff] [blame] | 60 | // ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any |
| 61 | // bytes after the NUL removed. |
| 62 | func ByteSliceToString(s []byte) string { |
| 63 | if i := bytes.IndexByte(s, 0); i != -1 { |
| 64 | s = s[:i] |
| 65 | } |
| 66 | return string(s) |
| 67 | } |
| 68 | |
| 69 | // BytePtrToString takes a pointer to a sequence of text and returns the corresponding string. |
| 70 | // If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated |
| 71 | // at a zero byte; if the zero byte is not present, the program may crash. |
| 72 | func BytePtrToString(p *byte) string { |
| 73 | if p == nil { |
| 74 | return "" |
| 75 | } |
| 76 | if *p == 0 { |
| 77 | return "" |
| 78 | } |
| 79 | |
| 80 | // Find NUL terminator. |
| 81 | n := 0 |
| 82 | for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ { |
| 83 | ptr = unsafe.Pointer(uintptr(ptr) + 1) |
| 84 | } |
| 85 | |
| 86 | var s []byte |
| 87 | h := (*unsafeheader.Slice)(unsafe.Pointer(&s)) |
| 88 | h.Data = unsafe.Pointer(p) |
| 89 | h.Len = n |
| 90 | h.Cap = n |
| 91 | |
| 92 | return string(s) |
| 93 | } |
| 94 | |
Rob Pike | 20acc5c | 2014-08-11 14:48:46 -0700 | [diff] [blame] | 95 | // Single-word zero for use when we need a valid pointer to 0 bytes. |
| 96 | // See mksyscall.pl. |
| 97 | var _zero uintptr |
| 98 | |
| 99 | func (ts *Timespec) Unix() (sec int64, nsec int64) { |
| 100 | return int64(ts.Sec), int64(ts.Nsec) |
| 101 | } |
| 102 | |
| 103 | func (tv *Timeval) Unix() (sec int64, nsec int64) { |
| 104 | return int64(tv.Sec), int64(tv.Usec) * 1000 |
| 105 | } |
| 106 | |
| 107 | func (ts *Timespec) Nano() int64 { |
| 108 | return int64(ts.Sec)*1e9 + int64(ts.Nsec) |
| 109 | } |
| 110 | |
| 111 | func (tv *Timeval) Nano() int64 { |
| 112 | return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 |
| 113 | } |