syscall.go 3.3 KB

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