|
| 1 | +// Adapted from dosmap.c in Visual Studio 12.0 CRT sources. |
| 2 | +// |
| 3 | +// The _dosmaperr function is required by the MySQL lib we use, |
| 4 | +// but no longer exists in the VS 14.0+ crt. |
| 5 | + |
| 6 | +#define WIN32_LEAN_AND_MEAN |
| 7 | +#include <windows.h> |
| 8 | + |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +static struct errentry |
| 12 | +{ |
| 13 | + DWORD oscode; // OS return value |
| 14 | + int errnocode; // System V error code |
| 15 | + |
| 16 | +} errtable[] = |
| 17 | +{ |
| 18 | + { ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */ |
| 19 | + { ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */ |
| 20 | + { ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */ |
| 21 | + { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */ |
| 22 | + { ERROR_ACCESS_DENIED, EACCES }, /* 5 */ |
| 23 | + { ERROR_INVALID_HANDLE, EBADF }, /* 6 */ |
| 24 | + { ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */ |
| 25 | + { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */ |
| 26 | + { ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */ |
| 27 | + { ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */ |
| 28 | + { ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */ |
| 29 | + { ERROR_INVALID_ACCESS, EINVAL }, /* 12 */ |
| 30 | + { ERROR_INVALID_DATA, EINVAL }, /* 13 */ |
| 31 | + { ERROR_INVALID_DRIVE, ENOENT }, /* 15 */ |
| 32 | + { ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */ |
| 33 | + { ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */ |
| 34 | + { ERROR_NO_MORE_FILES, ENOENT }, /* 18 */ |
| 35 | + { ERROR_LOCK_VIOLATION, EACCES }, /* 33 */ |
| 36 | + { ERROR_BAD_NETPATH, ENOENT }, /* 53 */ |
| 37 | + { ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */ |
| 38 | + { ERROR_BAD_NET_NAME, ENOENT }, /* 67 */ |
| 39 | + { ERROR_FILE_EXISTS, EEXIST }, /* 80 */ |
| 40 | + { ERROR_CANNOT_MAKE, EACCES }, /* 82 */ |
| 41 | + { ERROR_FAIL_I24, EACCES }, /* 83 */ |
| 42 | + { ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */ |
| 43 | + { ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */ |
| 44 | + { ERROR_DRIVE_LOCKED, EACCES }, /* 108 */ |
| 45 | + { ERROR_BROKEN_PIPE, EPIPE }, /* 109 */ |
| 46 | + { ERROR_DISK_FULL, ENOSPC }, /* 112 */ |
| 47 | + { ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */ |
| 48 | + { ERROR_INVALID_HANDLE, EINVAL }, /* 124 */ |
| 49 | + { ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */ |
| 50 | + { ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */ |
| 51 | + { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */ |
| 52 | + { ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */ |
| 53 | + { ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */ |
| 54 | + { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */ |
| 55 | + { ERROR_NOT_LOCKED, EACCES }, /* 158 */ |
| 56 | + { ERROR_BAD_PATHNAME, ENOENT }, /* 161 */ |
| 57 | + { ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */ |
| 58 | + { ERROR_LOCK_FAILED, EACCES }, /* 167 */ |
| 59 | + { ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */ |
| 60 | + { ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */ |
| 61 | + { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */ |
| 62 | + { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */ |
| 63 | +}; |
| 64 | + |
| 65 | +// The following two constants must be the minimum and maximum |
| 66 | +// values in the (contiguous) range of Exec Failure errors. |
| 67 | +#define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG |
| 68 | +#define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN |
| 69 | + |
| 70 | +// These are the low and high value in the range of errors that are |
| 71 | +// access violations |
| 72 | +#define MIN_EACCES_RANGE ERROR_WRITE_PROTECT |
| 73 | +#define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED |
| 74 | + |
| 75 | +void _dosmaperr(DWORD oserrno) |
| 76 | +{ |
| 77 | + _doserrno = oserrno; |
| 78 | + |
| 79 | + // Check the table for the OS error code |
| 80 | + for (size_t i = 0; i < _countof(errtable); ++i) |
| 81 | + { |
| 82 | + if (oserrno == errtable[i].oscode) |
| 83 | + { |
| 84 | + errno = errtable[i].errnocode; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // The error code wasn't in the table. We check for a range of |
| 89 | + // EACCES errors or exec failure errors (ENOEXEC). Otherwise |
| 90 | + // EINVAL is returned. |
| 91 | + if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE) |
| 92 | + errno = EACCES; |
| 93 | + else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR) |
| 94 | + errno = ENOEXEC; |
| 95 | + else |
| 96 | + errno = EINVAL; |
| 97 | +} |
0 commit comments