1 int winerror_to_errno(int winerror)2winerror_to_errno(int winerror) 3 { 4 // Unwrap FACILITY_WIN32 HRESULT errors. 5 if ((winerror & 0xFFFF0000) == 0x80070000) { 6 winerror &= 0x0000FFFF; 7 } 8 9 // Winsock error codes (10000-11999) are errno values. 10 if (winerror >= 10000 && winerror < 12000) { 11 switch (winerror) { 12 case WSAEINTR: 13 case WSAEBADF: 14 case WSAEACCES: 15 case WSAEFAULT: 16 case WSAEINVAL: 17 case WSAEMFILE: 18 // Winsock definitions of errno values. See WinSock2.h 19 return winerror - 10000; 20 default: 21 return winerror; 22 } 23 } 24 25 switch (winerror) { 26 case ERROR_FILE_NOT_FOUND: // 2 27 case ERROR_PATH_NOT_FOUND: // 3 28 case ERROR_INVALID_DRIVE: // 15 29 case ERROR_NO_MORE_FILES: // 18 30 case ERROR_BAD_NETPATH: // 53 31 case ERROR_BAD_NET_NAME: // 67 32 case ERROR_BAD_PATHNAME: // 161 33 case ERROR_FILENAME_EXCED_RANGE: // 206 34 return ENOENT; 35 36 case ERROR_BAD_ENVIRONMENT: // 10 37 return E2BIG; 38 39 case ERROR_BAD_FORMAT: // 11 40 case ERROR_INVALID_STARTING_CODESEG: // 188 41 case ERROR_INVALID_STACKSEG: // 189 42 case ERROR_INVALID_MODULETYPE: // 190 43 case ERROR_INVALID_EXE_SIGNATURE: // 191 44 case ERROR_EXE_MARKED_INVALID: // 192 45 case ERROR_BAD_EXE_FORMAT: // 193 46 case ERROR_ITERATED_DATA_EXCEEDS_64k: // 194 47 case ERROR_INVALID_MINALLOCSIZE: // 195 48 case ERROR_DYNLINK_FROM_INVALID_RING: // 196 49 case ERROR_IOPL_NOT_ENABLED: // 197 50 case ERROR_INVALID_SEGDPL: // 198 51 case ERROR_AUTODATASEG_EXCEEDS_64k: // 199 52 case ERROR_RING2SEG_MUST_BE_MOVABLE: // 200 53 case ERROR_RELOC_CHAIN_XEEDS_SEGLIM: // 201 54 case ERROR_INFLOOP_IN_RELOC_CHAIN: // 202 55 return ENOEXEC; 56 57 case ERROR_INVALID_HANDLE: // 6 58 case ERROR_INVALID_TARGET_HANDLE: // 114 59 case ERROR_DIRECT_ACCESS_HANDLE: // 130 60 return EBADF; 61 62 case ERROR_WAIT_NO_CHILDREN: // 128 63 case ERROR_CHILD_NOT_COMPLETE: // 129 64 return ECHILD; 65 66 case ERROR_NO_PROC_SLOTS: // 89 67 case ERROR_MAX_THRDS_REACHED: // 164 68 case ERROR_NESTING_NOT_ALLOWED: // 215 69 return EAGAIN; 70 71 case ERROR_ARENA_TRASHED: // 7 72 case ERROR_NOT_ENOUGH_MEMORY: // 8 73 case ERROR_INVALID_BLOCK: // 9 74 case ERROR_NOT_ENOUGH_QUOTA: // 1816 75 return ENOMEM; 76 77 case ERROR_ACCESS_DENIED: // 5 78 case ERROR_CURRENT_DIRECTORY: // 16 79 case ERROR_WRITE_PROTECT: // 19 80 case ERROR_BAD_UNIT: // 20 81 case ERROR_NOT_READY: // 21 82 case ERROR_BAD_COMMAND: // 22 83 case ERROR_CRC: // 23 84 case ERROR_BAD_LENGTH: // 24 85 case ERROR_SEEK: // 25 86 case ERROR_NOT_DOS_DISK: // 26 87 case ERROR_SECTOR_NOT_FOUND: // 27 88 case ERROR_OUT_OF_PAPER: // 28 89 case ERROR_WRITE_FAULT: // 29 90 case ERROR_READ_FAULT: // 30 91 case ERROR_GEN_FAILURE: // 31 92 case ERROR_SHARING_VIOLATION: // 32 93 case ERROR_LOCK_VIOLATION: // 33 94 case ERROR_WRONG_DISK: // 34 95 case ERROR_SHARING_BUFFER_EXCEEDED: // 36 96 case ERROR_NETWORK_ACCESS_DENIED: // 65 97 case ERROR_CANNOT_MAKE: // 82 98 case ERROR_FAIL_I24: // 83 99 case ERROR_DRIVE_LOCKED: // 108 100 case ERROR_SEEK_ON_DEVICE: // 132 101 case ERROR_NOT_LOCKED: // 158 102 case ERROR_LOCK_FAILED: // 167 103 case 35: // 35 (undefined) 104 return EACCES; 105 106 case ERROR_FILE_EXISTS: // 80 107 case ERROR_ALREADY_EXISTS: // 183 108 return EEXIST; 109 110 case ERROR_NOT_SAME_DEVICE: // 17 111 return EXDEV; 112 113 case ERROR_DIRECTORY: // 267 (bpo-12802) 114 return ENOTDIR; 115 116 case ERROR_TOO_MANY_OPEN_FILES: // 4 117 return EMFILE; 118 119 case ERROR_DISK_FULL: // 112 120 return ENOSPC; 121 122 case ERROR_BROKEN_PIPE: // 109 123 case ERROR_NO_DATA: // 232 (bpo-13063) 124 return EPIPE; 125 126 case ERROR_DIR_NOT_EMPTY: // 145 127 return ENOTEMPTY; 128 129 case ERROR_NO_UNICODE_TRANSLATION: // 1113 130 return EILSEQ; 131 132 case ERROR_INVALID_FUNCTION: // 1 133 case ERROR_INVALID_ACCESS: // 12 134 case ERROR_INVALID_DATA: // 13 135 case ERROR_INVALID_PARAMETER: // 87 136 case ERROR_NEGATIVE_SEEK: // 131 137 default: 138 return EINVAL; 139 } 140 } 141