github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/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  }
    65  
    66  // Argument list too long.
    67  const E2BIG = 2
    68  
    69  // Permission denied.
    70  const EACCES = 3
    71  
    72  // Address in use.
    73  const EADDRINUSE = 4
    74  
    75  // Address not available.
    76  const EADDRNOTAVAIL = 5
    77  
    78  // Address family not supported.
    79  const EAFNOSUPPORT = 6
    80  
    81  // Resource unavailable, try again (may be the same value as EWOULDBLOCK).
    82  const EAGAIN = 7
    83  
    84  // Connection already in progress.
    85  const EALREADY = 8
    86  
    87  // Bad file descriptor.
    88  const EBADF = 9
    89  
    90  // Bad message.
    91  const EBADMSG = 10
    92  
    93  // Device or resource busy.
    94  const EBUSY = 11
    95  
    96  // Operation canceled.
    97  const ECANCELED = 12
    98  
    99  // No child processes.
   100  const ECHILD = 13
   101  
   102  // Connection aborted.
   103  const ECONNABORTED = 14
   104  
   105  // Connection refused.
   106  const ECONNREFUSED = 15
   107  
   108  // Connection reset.
   109  const ECONNRESET = 16
   110  
   111  // Resource deadlock would occur.
   112  const EDEADLK = 17
   113  
   114  // Destination address required.
   115  const EDESTADDRREQ = 18
   116  
   117  // Mathematics argument out of domain of function.
   118  const EDOM = 19
   119  
   120  // Reserved.
   121  const EDQUOT = 20
   122  
   123  // File exists.
   124  const EEXIST = 21
   125  
   126  // Bad address.
   127  const EFAULT = 22
   128  
   129  // File too large.
   130  const EFBIG = 23
   131  
   132  // Host is unreachable.
   133  const EHOSTUNREACH = 24
   134  
   135  // Identifier removed.
   136  const EIDRM = 25
   137  
   138  // Illegal byte sequence.
   139  const EILSEQ = 26
   140  
   141  // Operation in progress.
   142  const EINPROGRESS = 27
   143  
   144  // Interrupted function.
   145  const EINTR = 28
   146  
   147  // Invalid argument.
   148  const EINVAL = 29
   149  
   150  // I/O error.
   151  const EIO = 30
   152  
   153  // Socket is connected.
   154  const EISCONN = 31
   155  
   156  // Is a directory.
   157  const EISDIR = 32
   158  
   159  // Too many levels of symbolic links.
   160  const ELOOP = 33
   161  
   162  // File descriptor value too large.
   163  const EMFILE = 34
   164  
   165  // Too many links.
   166  const EMLINK = 35
   167  
   168  // Message too large.
   169  const EMSGSIZE = 36
   170  
   171  // Reserved.
   172  const EMULTIHOP = 37
   173  
   174  // Filename too long.
   175  const ENAMETOOLONG = 38
   176  
   177  // Network is down.
   178  const ENETDOWN = 39
   179  
   180  // Connection aborted by network.
   181  const ENETRESET = 40
   182  
   183  // Network unreachable.
   184  const ENETUNREACH = 41
   185  
   186  // Too many files open in system.
   187  const ENFILE = 42
   188  
   189  // No buffer space available.
   190  const ENOBUFS = 43
   191  
   192  // No message is available on the STREAM head read queue.
   193  const ENODATA = 44
   194  
   195  // No such device.
   196  const ENODEV = 45
   197  
   198  // No such file or directory.
   199  const ENOENT = 46
   200  
   201  // Executable file format error.
   202  const ENOEXEC = 47
   203  
   204  // No locks available.
   205  const ENOLCK = 48
   206  
   207  // Reserved.
   208  const ENOLINK = 49
   209  
   210  // Not enough space.
   211  const ENOMEM = 50
   212  
   213  // No message of the desired type.
   214  const ENOMSG = 51
   215  
   216  // Protocol not available.
   217  const ENOPROTOOPT = 52
   218  
   219  // No space left on device.
   220  const ENOSPC = 53
   221  
   222  // No STREAM resources.
   223  const ENOSR = 54
   224  
   225  // Not a STREAM.
   226  const ENOSTR = 55
   227  
   228  // Functionality not supported.
   229  const ENOSYS = 56
   230  
   231  // The socket is not connected.
   232  const ENOTCONN = 57
   233  
   234  // Not a directory or a symbolic link to a directory.
   235  const ENOTDIR = 58
   236  
   237  // Directory not empty.
   238  const ENOTEMPTY = 59
   239  
   240  // Env not recoverable.
   241  const ENOTRECOVERABLE = 60
   242  
   243  // Not a socket.
   244  const ENOTSOCK = 61
   245  
   246  // Not supported (may be the same value as EOPNOTSUPP).
   247  const ENOTSUP = 62
   248  
   249  // Inappropriate I/O control operation.
   250  const ENOTTY = 63
   251  
   252  // No such device or address.
   253  const ENXIO = 64
   254  
   255  // Operation not supported on socket (may be the same value as ENOTSUP).
   256  const EOPNOTSUPP = 65
   257  
   258  // Value too large to be stored in data type.
   259  const EOVERFLOW = 66
   260  
   261  // Previous owner died.
   262  const EOWNERDEAD = 67
   263  
   264  // Operation not permitted.
   265  const EPERM = 68
   266  
   267  // Broken pipe.
   268  const EPIPE = 69
   269  
   270  // Protocol error.
   271  const EPROTO = 79
   272  
   273  // Protocol not supported.
   274  const EPROTONOSUPPORT = 80
   275  
   276  // Protocol wrong type for socket.
   277  const EPROTOTYPE = 81
   278  
   279  // Result too large.
   280  const ERANGE = 82
   281  
   282  // Read-only file system.
   283  const EROFS = 83
   284  
   285  // Invalid seek.
   286  const ESPIPE = 84
   287  
   288  // No such process.
   289  const ESRCH = 85
   290  
   291  // Reserved.
   292  const ESTALE = 86
   293  
   294  // Stream ioctl() timeout.
   295  const ETIME = 87
   296  
   297  // Connection timed out.
   298  const ETIMEDOUT = 88
   299  
   300  // Text file busy.
   301  const ETXTBSY = 89
   302  
   303  // Operation would block (may be the same value as EAGAIN).
   304  const EWOULDBLOCK = 90
   305  
   306  // Cross-device link.
   307  const EXDEV = 91