另客网go项目公用的代码库

syscall.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // +build windows
  5. // Package windows contains an interface to the low-level operating system
  6. // primitives. OS details vary depending on the underlying system, and
  7. // by default, godoc will display the OS-specific documentation for the current
  8. // system. If you want godoc to display syscall documentation for another
  9. // system, set $GOOS and $GOARCH to the desired system. For example, if
  10. // you want to view documentation for freebsd/arm on linux/amd64, set $GOOS
  11. // to freebsd and $GOARCH to arm.
  12. //
  13. // The primary use of this package is inside other packages that provide a more
  14. // portable interface to the system, such as "os", "time" and "net". Use
  15. // those packages rather than this one if you can.
  16. //
  17. // For details of the functions and data types in this package consult
  18. // the manuals for the appropriate operating system.
  19. //
  20. // These calls return err == nil to indicate success; otherwise
  21. // err represents an operating system error describing the failure and
  22. // holds a value of type syscall.Errno.
  23. package windows // import "golang.org/x/sys/windows"
  24. import (
  25. "syscall"
  26. )
  27. // ByteSliceFromString returns a NUL-terminated slice of bytes
  28. // containing the text of s. If s contains a NUL byte at any
  29. // location, it returns (nil, syscall.EINVAL).
  30. func ByteSliceFromString(s string) ([]byte, error) {
  31. for i := 0; i < len(s); i++ {
  32. if s[i] == 0 {
  33. return nil, syscall.EINVAL
  34. }
  35. }
  36. a := make([]byte, len(s)+1)
  37. copy(a, s)
  38. return a, nil
  39. }
  40. // BytePtrFromString returns a pointer to a NUL-terminated array of
  41. // bytes containing the text of s. If s contains a NUL byte at any
  42. // location, it returns (nil, syscall.EINVAL).
  43. func BytePtrFromString(s string) (*byte, error) {
  44. a, err := ByteSliceFromString(s)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &a[0], nil
  49. }
  50. // Single-word zero for use when we need a valid pointer to 0 bytes.
  51. // See mksyscall.pl.
  52. var _zero uintptr
  53. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  54. return int64(ts.Sec), int64(ts.Nsec)
  55. }
  56. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  57. return int64(tv.Sec), int64(tv.Usec) * 1000
  58. }
  59. func (ts *Timespec) Nano() int64 {
  60. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  61. }
  62. func (tv *Timeval) Nano() int64 {
  63. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  64. }