Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
8.1 KiB

  1. /***
  2. *dosmap.c - Maps OS errors to errno values
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * _dosmaperr: Maps OS errors to errno values
  8. *
  9. *Revision History:
  10. * 06-06-89 PHG Module created, based on asm version
  11. * 06-16-89 PHG Changed name to _dosmaperr
  12. * 08-22-89 JCR ERROR_INVALID_DRIVE (15) now maps to ENOENT not EXDEV
  13. * 03-07-90 GJF Made calling type _CALLTYPE1, added #include
  14. * <cruntime.h> and fixed copyright. Also, cleaned up the
  15. * formatting a bit.
  16. * 09-27-90 GJF New-style function declarator.
  17. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  18. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  19. * 04-26-91 SRW Added ERROR_LOCK_VIOLATION translation to EACCES
  20. * 08-15-91 GJF Multi-thread support for Win32.
  21. * 03-31-92 GJF Added more error codes (Win32 only) and removed OS/2
  22. * specific nomenclature.
  23. * 07-29-92 GJF Added ERROR_FILE_EXISTS to table for Win32. It gets
  24. * mapped it to EEXIST.
  25. * 09-14-92 SRW Added ERROR_BAD_PATHNAME table for Win32. It gets
  26. * mapped it to ENOENT.
  27. * 10-02-92 GJF Map ERROR_INVALID_PARAMETER to EINVAL (rather than
  28. * EACCES). Added ERROR_NOT_LOCKED and mapped it to
  29. * EACCES. Added ERROR_DIR_NOT_EMPTY and mapped it to
  30. * ENOTEMPTY.
  31. * 02-16-93 GJF Changed for new _getptd().
  32. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  33. * 01-06-94 GJF Dumped obsolete Cruiser support, revised errentry
  34. * struct definition and added mapping for infamous
  35. * ERROR_NOT_ENOUGH_QUOTA (non-swappable memory pages)
  36. * which might result from a CreateThread call.
  37. * 02-08-95 JWM Spliced _WIN32 & Mac versions.
  38. * 05-24-95 CFW Map dupFNErr to EEXIST rather than EACCESS.
  39. * 07-01-96 GJF Replaced defined(_WIN32) with !defined(_MAC). Also,
  40. * detab-ed and cleaned up the format a bit.
  41. * 05-17-99 PML Remove all Macintosh support.
  42. *
  43. *******************************************************************************/
  44. #include <cruntime.h>
  45. #include <errno.h>
  46. #include <oscalls.h>
  47. #include <stdlib.h>
  48. #include <internal.h>
  49. #include <mtdll.h>
  50. /* This is the error table that defines the mapping between OS error
  51. codes and errno values */
  52. struct errentry {
  53. unsigned long oscode; /* OS return value */
  54. int errnocode; /* System V error code */
  55. };
  56. static struct errentry errtable[] = {
  57. { ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */
  58. { ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */
  59. { ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */
  60. { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */
  61. { ERROR_ACCESS_DENIED, EACCES }, /* 5 */
  62. { ERROR_INVALID_HANDLE, EBADF }, /* 6 */
  63. { ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */
  64. { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */
  65. { ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */
  66. { ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */
  67. { ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */
  68. { ERROR_INVALID_ACCESS, EINVAL }, /* 12 */
  69. { ERROR_INVALID_DATA, EINVAL }, /* 13 */
  70. { ERROR_INVALID_DRIVE, ENOENT }, /* 15 */
  71. { ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */
  72. { ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */
  73. { ERROR_NO_MORE_FILES, ENOENT }, /* 18 */
  74. { ERROR_LOCK_VIOLATION, EACCES }, /* 33 */
  75. { ERROR_BAD_NETPATH, ENOENT }, /* 53 */
  76. { ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */
  77. { ERROR_BAD_NET_NAME, ENOENT }, /* 67 */
  78. { ERROR_FILE_EXISTS, EEXIST }, /* 80 */
  79. { ERROR_CANNOT_MAKE, EACCES }, /* 82 */
  80. { ERROR_FAIL_I24, EACCES }, /* 83 */
  81. { ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */
  82. { ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */
  83. { ERROR_DRIVE_LOCKED, EACCES }, /* 108 */
  84. { ERROR_BROKEN_PIPE, EPIPE }, /* 109 */
  85. { ERROR_DISK_FULL, ENOSPC }, /* 112 */
  86. { ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */
  87. { ERROR_INVALID_HANDLE, EINVAL }, /* 124 */
  88. { ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */
  89. { ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */
  90. { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */
  91. { ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */
  92. { ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */
  93. { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */
  94. { ERROR_NOT_LOCKED, EACCES }, /* 158 */
  95. { ERROR_BAD_PATHNAME, ENOENT }, /* 161 */
  96. { ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */
  97. { ERROR_LOCK_FAILED, EACCES }, /* 167 */
  98. { ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */
  99. { ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */
  100. { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */
  101. { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */
  102. };
  103. /* size of the table */
  104. #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
  105. /* The following two constants must be the minimum and maximum
  106. values in the (contiguous) range of Exec Failure errors. */
  107. #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
  108. #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
  109. /* These are the low and high value in the range of errors that are
  110. access violations */
  111. #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
  112. #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
  113. /***
  114. *void _dosmaperr(oserrno) - Map function number
  115. *
  116. *Purpose:
  117. * This function takes an OS error number, and maps it to the
  118. * corresponding errno value (based on UNIX System V values). The
  119. * OS error number is stored in _doserrno (and the mapped value is
  120. * stored in errno)
  121. *
  122. *Entry:
  123. * ULONG oserrno = OS error value
  124. *
  125. *Exit:
  126. * sets _doserrno and errno.
  127. *
  128. *Exceptions:
  129. *
  130. *******************************************************************************/
  131. void __cdecl _dosmaperr (
  132. unsigned long oserrno
  133. )
  134. {
  135. int i;
  136. _doserrno = oserrno; /* set _doserrno */
  137. /* check the table for the OS error code */
  138. for (i = 0; i < ERRTABLESIZE; ++i) {
  139. if (oserrno == errtable[i].oscode) {
  140. errno = errtable[i].errnocode;
  141. return;
  142. }
  143. }
  144. /* The error code wasn't in the table. We check for a range of */
  145. /* EACCES errors or exec failure errors (ENOEXEC). Otherwise */
  146. /* EINVAL is returned. */
  147. if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
  148. errno = EACCES;
  149. else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
  150. errno = ENOEXEC;
  151. else
  152. errno = EINVAL;
  153. }
  154. #ifdef _MT
  155. /***
  156. *int * _errno() - return pointer to thread's errno
  157. *unsigned long * __doserrno() - return pointer to thread's _doserrno
  158. *
  159. *Purpose:
  160. * _errno() returns a pointer to the _terrno field in the current
  161. * thread's _tiddata structure.
  162. * __doserrno returns a pointer to the _tdoserrno field in the current
  163. * thread's _tiddata structure
  164. *
  165. *Entry:
  166. * None.
  167. *
  168. *Exit:
  169. * See above.
  170. *
  171. *Exceptions:
  172. *
  173. *******************************************************************************/
  174. int * __cdecl _errno(
  175. void
  176. )
  177. {
  178. return ( &(_getptd()->_terrno) );
  179. }
  180. unsigned long * __cdecl __doserrno(
  181. void
  182. )
  183. {
  184. return ( &(_getptd()->_tdoserrno) );
  185. }
  186. #endif /* _MT */