forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/windows/syscall.go

175 lines
3.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
//go:build windows
// Package windows contains an interface to the low-level operating system
2024-02-18 10:42:21 +00:00
// primitives. OS details vary depending on the underlying system, and
2024-02-18 10:42:21 +00:00
// by default, godoc will display the OS-specific documentation for the current
2024-02-18 10:42:21 +00:00
// system. If you want godoc to display syscall documentation for another
2024-02-18 10:42:21 +00:00
// system, set $GOOS and $GOARCH to the desired system. For example, if
2024-02-18 10:42:21 +00:00
// you want to view documentation for freebsd/arm on linux/amd64, set $GOOS
2024-02-18 10:42:21 +00:00
// to freebsd and $GOARCH to arm.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The primary use of this package is inside other packages that provide a more
2024-02-18 10:42:21 +00:00
// portable interface to the system, such as "os", "time" and "net". Use
2024-02-18 10:42:21 +00:00
// those packages rather than this one if you can.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For details of the functions and data types in this package consult
2024-02-18 10:42:21 +00:00
// the manuals for the appropriate operating system.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// These calls return err == nil to indicate success; otherwise
2024-02-18 10:42:21 +00:00
// err represents an operating system error describing the failure and
2024-02-18 10:42:21 +00:00
// holds a value of type syscall.Errno.
2024-02-18 10:42:21 +00:00
package windows // import "golang.org/x/sys/windows"
import (
"bytes"
"strings"
"syscall"
"unsafe"
)
// ByteSliceFromString returns a NUL-terminated slice of bytes
2024-02-18 10:42:21 +00:00
// containing the text of s. If s contains a NUL byte at any
2024-02-18 10:42:21 +00:00
// location, it returns (nil, syscall.EINVAL).
2024-02-18 10:42:21 +00:00
func ByteSliceFromString(s string) ([]byte, error) {
2024-02-18 10:42:21 +00:00
if strings.IndexByte(s, 0) != -1 {
2024-02-18 10:42:21 +00:00
return nil, syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
a := make([]byte, len(s)+1)
2024-02-18 10:42:21 +00:00
copy(a, s)
2024-02-18 10:42:21 +00:00
return a, nil
2024-02-18 10:42:21 +00:00
}
// BytePtrFromString returns a pointer to a NUL-terminated array of
2024-02-18 10:42:21 +00:00
// bytes containing the text of s. If s contains a NUL byte at any
2024-02-18 10:42:21 +00:00
// location, it returns (nil, syscall.EINVAL).
2024-02-18 10:42:21 +00:00
func BytePtrFromString(s string) (*byte, error) {
2024-02-18 10:42:21 +00:00
a, err := ByteSliceFromString(s)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &a[0], nil
2024-02-18 10:42:21 +00:00
}
// ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any
2024-02-18 10:42:21 +00:00
// bytes after the NUL removed.
2024-02-18 10:42:21 +00:00
func ByteSliceToString(s []byte) string {
2024-02-18 10:42:21 +00:00
if i := bytes.IndexByte(s, 0); i != -1 {
2024-02-18 10:42:21 +00:00
s = s[:i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(s)
2024-02-18 10:42:21 +00:00
}
// BytePtrToString takes a pointer to a sequence of text and returns the corresponding string.
2024-02-18 10:42:21 +00:00
// If the pointer is nil, it returns the empty string. It assumes that the text sequence is terminated
2024-02-18 10:42:21 +00:00
// at a zero byte; if the zero byte is not present, the program may crash.
2024-02-18 10:42:21 +00:00
func BytePtrToString(p *byte) string {
2024-02-18 10:42:21 +00:00
if p == nil {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if *p == 0 {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// Find NUL terminator.
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ {
2024-02-18 10:42:21 +00:00
ptr = unsafe.Pointer(uintptr(ptr) + 1)
2024-02-18 10:42:21 +00:00
}
return string(unsafe.Slice(p, n))
2024-02-18 10:42:21 +00:00
}
// Single-word zero for use when we need a valid pointer to 0 bytes.
2024-02-18 10:42:21 +00:00
// See mksyscall.pl.
2024-02-18 10:42:21 +00:00
var _zero uintptr
func (ts *Timespec) Unix() (sec int64, nsec int64) {
2024-02-18 10:42:21 +00:00
return int64(ts.Sec), int64(ts.Nsec)
2024-02-18 10:42:21 +00:00
}
func (tv *Timeval) Unix() (sec int64, nsec int64) {
2024-02-18 10:42:21 +00:00
return int64(tv.Sec), int64(tv.Usec) * 1000
2024-02-18 10:42:21 +00:00
}
func (ts *Timespec) Nano() int64 {
2024-02-18 10:42:21 +00:00
return int64(ts.Sec)*1e9 + int64(ts.Nsec)
2024-02-18 10:42:21 +00:00
}
func (tv *Timeval) Nano() int64 {
2024-02-18 10:42:21 +00:00
return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
2024-02-18 10:42:21 +00:00
}