github.com/gotranspile/cxgo@v0.3.7/runtime/libc/errno.go (about) 1 package libc 2 3 import ( 4 "errors" 5 "os" 6 "syscall" 7 ) 8 9 var ( 10 Errno int 11 errno int // used to check if C code have changed Errno after SetErr was called 12 goerr error 13 ) 14 15 func strError(e int) error { 16 if e == 0 { 17 return nil 18 } 19 if goerr != nil && errno == e { 20 return goerr 21 } 22 return syscall.Errno(e) 23 } 24 25 // StrError returns a C string for the current error, if any. 26 func StrError(e int) *byte { 27 return CString(strError(e).Error()) 28 } 29 30 // SetErr sets the Errno value to a specified Go error equivalent. 31 func SetErr(err error) { 32 code := ErrCode(err) 33 Errno = code 34 // preserve original Go errors as well 35 errno = code 36 goerr = err 37 } 38 39 // ErrCode returns an error code corresponding to a Go error. 40 func ErrCode(err error) int { 41 if err == nil { 42 return 0 43 } 44 if os.IsPermission(err) { 45 return 13 46 } else if os.IsNotExist(err) { 47 return 2 48 } else if os.IsExist(err) { 49 return 17 50 } 51 var errno syscall.Errno 52 if errors.As(err, &errno) { 53 return int(errno) 54 } 55 return 1 56 } 57 58 // Error returns a Go error value that corresponds to the current Errno. 59 func Error() error { 60 if Errno == 0 { 61 return nil 62 } 63 return strError(Errno) 64 }