Map from a Windows error code to a `errno` value Most code in this function is taken from CPython's `PC/errmap.h`. Unlike CPython however, we return 0 for unknown / unsupported values.
| 265 | // Most code in this function is taken from CPython's `PC/errmap.h`. |
| 266 | // Unlike CPython however, we return 0 for unknown / unsupported values. |
| 267 | int WinErrorToErrno(int winerror) { |
| 268 | // Unwrap FACILITY_WIN32 HRESULT errors. |
| 269 | if ((winerror & 0xFFFF0000) == 0x80070000) { |
| 270 | winerror &= 0x0000FFFF; |
| 271 | } |
| 272 | |
| 273 | // Winsock error codes (10000-11999) are errno values. |
| 274 | if (winerror >= 10000 && winerror < 12000) { |
| 275 | switch (winerror) { |
| 276 | case WSAEINTR: |
| 277 | case WSAEBADF: |
| 278 | case WSAEACCES: |
| 279 | case WSAEFAULT: |
| 280 | case WSAEINVAL: |
| 281 | case WSAEMFILE: |
| 282 | // Winsock definitions of errno values. See WinSock2.h |
| 283 | return winerror - 10000; |
| 284 | default: |
| 285 | return winerror; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | switch (winerror) { |
| 290 | case ERROR_FILE_NOT_FOUND: // 2 |
| 291 | case ERROR_PATH_NOT_FOUND: // 3 |
| 292 | case ERROR_INVALID_DRIVE: // 15 |
| 293 | case ERROR_NO_MORE_FILES: // 18 |
| 294 | case ERROR_BAD_NETPATH: // 53 |
| 295 | case ERROR_BAD_NET_NAME: // 67 |
| 296 | case ERROR_BAD_PATHNAME: // 161 |
| 297 | case ERROR_FILENAME_EXCED_RANGE: // 206 |
| 298 | return ENOENT; |
| 299 | |
| 300 | case ERROR_BAD_ENVIRONMENT: // 10 |
| 301 | return E2BIG; |
| 302 | |
| 303 | case ERROR_BAD_FORMAT: // 11 |
| 304 | case ERROR_INVALID_STARTING_CODESEG: // 188 |
| 305 | case ERROR_INVALID_STACKSEG: // 189 |
| 306 | case ERROR_INVALID_MODULETYPE: // 190 |
| 307 | case ERROR_INVALID_EXE_SIGNATURE: // 191 |
| 308 | case ERROR_EXE_MARKED_INVALID: // 192 |
| 309 | case ERROR_BAD_EXE_FORMAT: // 193 |
| 310 | case ERROR_ITERATED_DATA_EXCEEDS_64k: // 194 |
| 311 | case ERROR_INVALID_MINALLOCSIZE: // 195 |
| 312 | case ERROR_DYNLINK_FROM_INVALID_RING: // 196 |
| 313 | case ERROR_IOPL_NOT_ENABLED: // 197 |
| 314 | case ERROR_INVALID_SEGDPL: // 198 |
| 315 | case ERROR_AUTODATASEG_EXCEEDS_64k: // 199 |
| 316 | case ERROR_RING2SEG_MUST_BE_MOVABLE: // 200 |
| 317 | case ERROR_RELOC_CHAIN_XEEDS_SEGLIM: // 201 |
| 318 | case ERROR_INFLOOP_IN_RELOC_CHAIN: // 202 |
| 319 | return ENOEXEC; |
| 320 | |
| 321 | case ERROR_INVALID_HANDLE: // 6 |
| 322 | case ERROR_INVALID_TARGET_HANDLE: // 114 |
| 323 | case ERROR_DIRECT_ACCESS_HANDLE: // 130 |
| 324 | return EBADF; |