Path: blob/main/vendor/golang.org/x/sys/windows/syscall.go
2880 views
// Copyright 2009 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34//go:build windows56// Package windows contains an interface to the low-level operating system7// primitives. OS details vary depending on the underlying system, and8// by default, godoc will display the OS-specific documentation for the current9// system. If you want godoc to display syscall documentation for another10// system, set $GOOS and $GOARCH to the desired system. For example, if11// you want to view documentation for freebsd/arm on linux/amd64, set $GOOS12// to freebsd and $GOARCH to arm.13//14// The primary use of this package is inside other packages that provide a more15// portable interface to the system, such as "os", "time" and "net". Use16// those packages rather than this one if you can.17//18// For details of the functions and data types in this package consult19// the manuals for the appropriate operating system.20//21// These calls return err == nil to indicate success; otherwise22// err represents an operating system error describing the failure and23// holds a value of type syscall.Errno.24package windows // import "golang.org/x/sys/windows"2526import (27"bytes"28"strings"29"syscall"30"unsafe"31)3233// ByteSliceFromString returns a NUL-terminated slice of bytes34// containing the text of s. If s contains a NUL byte at any35// location, it returns (nil, syscall.EINVAL).36func ByteSliceFromString(s string) ([]byte, error) {37if strings.IndexByte(s, 0) != -1 {38return nil, syscall.EINVAL39}40a := make([]byte, len(s)+1)41copy(a, s)42return a, nil43}4445// BytePtrFromString returns a pointer to a NUL-terminated array of46// bytes containing the text of s. If s contains a NUL byte at any47// location, it returns (nil, syscall.EINVAL).48func BytePtrFromString(s string) (*byte, error) {49a, err := ByteSliceFromString(s)50if err != nil {51return nil, err52}53return &a[0], nil54}5556// ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any57// bytes after the NUL removed.58func ByteSliceToString(s []byte) string {59if i := bytes.IndexByte(s, 0); i != -1 {60s = s[:i]61}62return string(s)63}6465// BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.66// If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated67// at a zero byte; if the zero byte is not present, the program may crash.68func BytePtrToString(p *byte) string {69if p == nil {70return ""71}72if *p == 0 {73return ""74}7576// Find NUL terminator.77n := 078for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {79ptr = unsafe.Pointer(uintptr(ptr) + 1)80}8182return string(unsafe.Slice(p, n))83}8485// Single-word zero for use when we need a valid pointer to 0 bytes.86// See mksyscall.pl.87var _zero uintptr8889func (ts *Timespec) Unix() (sec int64, nsec int64) {90return int64(ts.Sec), int64(ts.Nsec)91}9293func (tv *Timeval) Unix() (sec int64, nsec int64) {94return int64(tv.Sec), int64(tv.Usec) * 100095}9697func (ts *Timespec) Nano() int64 {98return int64(ts.Sec)*1e9 + int64(ts.Nsec)99}100101func (tv *Timeval) Nano() int64 {102return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000103}104105106