errstr_glibc.go 917 B

123456789101112131415161718192021222324252627282930313233
  1. // errstr_glibc.go -- GNU/Linux and GNU/Hurd specific error strings.
  2. // Copyright 2010 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. // We use this rather than errstr.go because on GNU/Linux sterror_r
  6. // returns a pointer to the error message, and may not use buf at all.
  7. // +build hurd linux
  8. package syscall
  9. import "unsafe"
  10. //sysnb strerror_r(errnum int, b []byte) (errstr *byte)
  11. //strerror_r(errnum _C_int, b *byte, len Size_t) *byte
  12. func Errstr(errnum int) string {
  13. a := make([]byte, 128)
  14. p := strerror_r(errnum, a)
  15. b := (*[1000]byte)(unsafe.Pointer(p))
  16. i := 0
  17. for b[i] != 0 {
  18. i++
  19. }
  20. // Lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  21. if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' {
  22. c := b[0] + 'a' - 'A'
  23. return string(c) + string(b[1:i])
  24. }
  25. return string(b[:i])
  26. }