|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #define _ALLOW_OLD_VALIDATE_MACROS
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
| #include <errno.h>
|
|
|
|
|
|
|
|
|
|
|
| namespace
|
| {
|
| struct errentry
|
| {
|
| unsigned long oscode;
|
| int errnocode;
|
| };
|
| }
|
|
|
|
|
|
|
| static errentry const errtable[]
|
| {
|
| { ERROR_INVALID_FUNCTION, EINVAL },
|
| { ERROR_FILE_NOT_FOUND, ENOENT },
|
| { ERROR_PATH_NOT_FOUND, ENOENT },
|
| { ERROR_TOO_MANY_OPEN_FILES, EMFILE },
|
| { ERROR_ACCESS_DENIED, EACCES },
|
| { ERROR_INVALID_HANDLE, EBADF },
|
| { ERROR_ARENA_TRASHED, ENOMEM },
|
| { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
|
| { ERROR_INVALID_BLOCK, ENOMEM },
|
| { ERROR_BAD_ENVIRONMENT, E2BIG },
|
| { ERROR_BAD_FORMAT, ENOEXEC },
|
| { ERROR_INVALID_ACCESS, EINVAL },
|
| { ERROR_INVALID_DATA, EINVAL },
|
| { ERROR_INVALID_DRIVE, ENOENT },
|
| { ERROR_CURRENT_DIRECTORY, EACCES },
|
| { ERROR_NOT_SAME_DEVICE, EXDEV },
|
| { ERROR_NO_MORE_FILES, ENOENT },
|
| { ERROR_LOCK_VIOLATION, EACCES },
|
| { ERROR_BAD_NETPATH, ENOENT },
|
| { ERROR_NETWORK_ACCESS_DENIED, EACCES },
|
| { ERROR_BAD_NET_NAME, ENOENT },
|
| { ERROR_FILE_EXISTS, EEXIST },
|
| { ERROR_CANNOT_MAKE, EACCES },
|
| { ERROR_FAIL_I24, EACCES },
|
| { ERROR_INVALID_PARAMETER, EINVAL },
|
| { ERROR_NO_PROC_SLOTS, EAGAIN },
|
| { ERROR_DRIVE_LOCKED, EACCES },
|
| { ERROR_BROKEN_PIPE, EPIPE },
|
| { ERROR_DISK_FULL, ENOSPC },
|
| { ERROR_INVALID_TARGET_HANDLE, EBADF },
|
| { ERROR_WAIT_NO_CHILDREN, ECHILD },
|
| { ERROR_CHILD_NOT_COMPLETE, ECHILD },
|
| { ERROR_DIRECT_ACCESS_HANDLE, EBADF },
|
| { ERROR_NEGATIVE_SEEK, EINVAL },
|
| { ERROR_SEEK_ON_DEVICE, EACCES },
|
| { ERROR_DIR_NOT_EMPTY, ENOTEMPTY },
|
| { ERROR_NOT_LOCKED, EACCES },
|
| { ERROR_BAD_PATHNAME, ENOENT },
|
| { ERROR_MAX_THRDS_REACHED, EAGAIN },
|
| { ERROR_LOCK_FAILED, EACCES },
|
| { ERROR_ALREADY_EXISTS, EEXIST },
|
| { ERROR_FILENAME_EXCED_RANGE, ENOENT },
|
| { ERROR_NESTING_NOT_ALLOWED, EAGAIN },
|
| { ERROR_NO_UNICODE_TRANSLATION, EILSEQ },
|
| { ERROR_NOT_ENOUGH_QUOTA, ENOMEM }
|
| };
|
|
|
|
|
| #define ERRTABLECOUNT (sizeof(errtable) / sizeof(errtable[0]))
|
|
|
|
|
|
|
| #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
|
| #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
|
|
|
|
|
|
|
| #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
|
| #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
|
|
|
|
|
| extern "C" void __cdecl __acrt_errno_map_os_error(unsigned long const oserrno)
|
| {
|
| _doserrno = oserrno;
|
| errno = __acrt_errno_from_os_error(oserrno);
|
| }
|
|
|
| extern "C" void __cdecl __acrt_errno_map_os_error_ptd(unsigned long const oserrno, __crt_cached_ptd_host& ptd)
|
| {
|
| ptd.get_doserrno().set(oserrno);
|
| ptd.get_errno().set(__acrt_errno_from_os_error(oserrno));
|
| }
|
|
|
| inline const errentry* bsearch_errentry(
|
| unsigned long const oserrno,
|
| errentry const* const errtable,
|
| size_t num
|
| )
|
| {
|
| size_t lo = 0;
|
| size_t hi = num - 1;
|
|
|
| while (lo <= hi)
|
| {
|
| size_t const half = num / 2;
|
| if (half != 0)
|
| {
|
| size_t mid = lo + (num & 1 ? half : (half - 1));
|
|
|
| if (oserrno == errtable[mid].oscode)
|
| {
|
| return &errtable[mid];
|
| }
|
| else if (oserrno < errtable[mid].oscode)
|
| {
|
| hi = mid - 1;
|
| num = num & 1 ? half : half - 1;
|
| }
|
| else
|
| {
|
| lo = mid + 1;
|
| num = half;
|
| }
|
| }
|
| else if (num != 0)
|
| {
|
| return (oserrno != errtable[lo].oscode) ? nullptr : &errtable[lo];
|
| }
|
| else
|
| {
|
| break;
|
| }
|
| }
|
|
|
| return nullptr;
|
| }
|
|
|
| extern "C" int __cdecl __acrt_errno_from_os_error(unsigned long const oserrno)
|
| {
|
|
|
| if (oserrno >= errtable[0].oscode)
|
| {
|
| if (oserrno <= ERROR_INVALID_DATA)
|
| {
|
| return errtable[oserrno-1].errnocode;
|
| }
|
| else if (oserrno <= errtable[ERRTABLECOUNT - 1].oscode)
|
| {
|
| const errentry* result = bsearch_errentry(oserrno, errtable, ERRTABLECOUNT);
|
| if (result != nullptr)
|
| {
|
| return result->errnocode;
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
|
| {
|
| return EACCES;
|
| }
|
| else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
|
| {
|
| return ENOEXEC;
|
| }
|
| else
|
| {
|
| return EINVAL;
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| extern "C" errno_t _set_errno(int const value)
|
| {
|
| __acrt_ptd* const ptd{__acrt_getptd_noexit()};
|
| if (!ptd)
|
| return ENOMEM;
|
|
|
| errno = value;
|
| return 0;
|
| }
|
|
|
| extern "C" errno_t _get_errno(int* const result)
|
| {
|
| _VALIDATE_RETURN_NOERRNO(result != nullptr, EINVAL);
|
|
|
|
|
| *result = errno;
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
| extern "C" errno_t _set_doserrno(unsigned long const value)
|
| {
|
| __acrt_ptd* const ptd{__acrt_getptd_noexit()};
|
| if (!ptd)
|
| return ENOMEM;
|
|
|
| _doserrno = value;
|
| return 0;
|
| }
|
|
|
| extern "C" errno_t _get_doserrno(unsigned long* const result)
|
| {
|
| _VALIDATE_RETURN_NOERRNO(result != nullptr, EINVAL);
|
|
|
|
|
| *result = _doserrno;
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| static int errno_no_memory {ENOMEM};
|
| static unsigned long doserrno_no_memory{ERROR_NOT_ENOUGH_MEMORY};
|
|
|
| extern "C" int* __cdecl _errno()
|
| {
|
| __acrt_ptd* const ptd{__acrt_getptd_noexit()};
|
| if (!ptd)
|
| {
|
| return &errno_no_memory;
|
| }
|
|
|
| return &ptd->_terrno;
|
| }
|
|
|
| extern "C" unsigned long* __cdecl __doserrno()
|
| {
|
| __acrt_ptd* const ptd{__acrt_getptd_noexit()};
|
| if (!ptd)
|
| {
|
| return &doserrno_no_memory;
|
| }
|
|
|
| return &ptd->_tdoserrno;
|
| }
|
|
|