blob: 8732cdb957f39a51e686765ffec2bdc4c970792e [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -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
Akash Reddy Kankanalac0014632025-05-21 17:12:20 +05305//go:build windows
Zack Williamse940c7a2019-08-21 14:25:39 -07006// +build windows
7
8// Package windows 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 the OS-specific documentation for the current
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
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.
26package windows // import "golang.org/x/sys/windows"
27
28import (
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000029 "bytes"
30 "strings"
Zack Williamse940c7a2019-08-21 14:25:39 -070031 "syscall"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000032 "unsafe"
Zack Williamse940c7a2019-08-21 14:25:39 -070033)
34
35// ByteSliceFromString returns a NUL-terminated slice of bytes
36// containing the text of s. If s contains a NUL byte at any
37// location, it returns (nil, syscall.EINVAL).
38func ByteSliceFromString(s string) ([]byte, error) {
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000039 if strings.IndexByte(s, 0) != -1 {
40 return nil, syscall.EINVAL
Zack Williamse940c7a2019-08-21 14:25:39 -070041 }
42 a := make([]byte, len(s)+1)
43 copy(a, s)
44 return a, nil
45}
46
47// BytePtrFromString returns a pointer to a NUL-terminated array of
48// bytes containing the text of s. If s contains a NUL byte at any
49// location, it returns (nil, syscall.EINVAL).
50func BytePtrFromString(s string) (*byte, error) {
51 a, err := ByteSliceFromString(s)
52 if err != nil {
53 return nil, err
54 }
55 return &a[0], nil
56}
57
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000058// ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any
59// bytes after the NUL removed.
60func ByteSliceToString(s []byte) string {
61 if i := bytes.IndexByte(s, 0); i != -1 {
62 s = s[:i]
63 }
64 return string(s)
65}
66
67// BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.
68// If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated
69// at a zero byte; if the zero byte is not present, the program may crash.
70func BytePtrToString(p *byte) string {
71 if p == nil {
72 return ""
73 }
74 if *p == 0 {
75 return ""
76 }
77
78 // Find NUL terminator.
79 n := 0
80 for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {
81 ptr = unsafe.Pointer(uintptr(ptr) + 1)
82 }
83
Akash Reddy Kankanalac0014632025-05-21 17:12:20 +053084 return string(unsafe.Slice(p, n))
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000085}
86
Zack Williamse940c7a2019-08-21 14:25:39 -070087// Single-word zero for use when we need a valid pointer to 0 bytes.
88// See mksyscall.pl.
89var _zero uintptr
90
91func (ts *Timespec) Unix() (sec int64, nsec int64) {
92 return int64(ts.Sec), int64(ts.Nsec)
93}
94
95func (tv *Timeval) Unix() (sec int64, nsec int64) {
96 return int64(tv.Sec), int64(tv.Usec) * 1000
97}
98
99func (ts *Timespec) Nano() int64 {
100 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
101}
102
103func (tv *Timeval) Nano() int64 {
104 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
105}