errstr_nor.go 794 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // errstr.go -- Error strings when there is no strerror_r.
  2. // Copyright 2011 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package syscall
  6. import (
  7. "sync"
  8. "unsafe"
  9. )
  10. //sysnb strerror(errnum int) (buf *byte)
  11. //strerror(errnum _C_int) *byte
  12. var errstr_lock sync.Mutex
  13. func Errstr(errno int) string {
  14. errstr_lock.Lock()
  15. bp := strerror(errno)
  16. b := (*[1000]byte)(unsafe.Pointer(bp))
  17. i := 0
  18. for b[i] != 0 {
  19. i++
  20. }
  21. // Lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  22. var s string
  23. if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' {
  24. c := b[0] + 'a' - 'A'
  25. s = string(c) + string(b[1:i])
  26. } else {
  27. s = string(b[:i])
  28. }
  29. errstr_lock.Unlock()
  30. return s
  31. }