Leaked source code of windows server 2003
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.

221 lines
8.7 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. * 12-11-01 BWT Replace _getptd with _getptd_noexit - failure to
  43. * allocate a ptd structure shouldn't terminate the app
  44. *
  45. *******************************************************************************/
  46. #include <cruntime.h>
  47. #include <errno.h>
  48. #include <oscalls.h>
  49. #include <stdlib.h>
  50. #include <internal.h>
  51. #include <mtdll.h>
  52. /* This is the error table that defines the mapping between OS error
  53. codes and errno values */
  54. struct errentry {
  55. unsigned long oscode; /* OS return value */
  56. int errnocode; /* System V error code */
  57. };
  58. static struct errentry errtable[] = {
  59. { ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */
  60. { ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */
  61. { ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */
  62. { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */
  63. { ERROR_ACCESS_DENIED, EACCES }, /* 5 */
  64. { ERROR_INVALID_HANDLE, EBADF }, /* 6 */
  65. { ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */
  66. { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */
  67. { ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */
  68. { ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */
  69. { ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */
  70. { ERROR_INVALID_ACCESS, EINVAL }, /* 12 */
  71. { ERROR_INVALID_DATA, EINVAL }, /* 13 */
  72. { ERROR_INVALID_DRIVE, ENOENT }, /* 15 */
  73. { ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */
  74. { ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */
  75. { ERROR_NO_MORE_FILES, ENOENT }, /* 18 */
  76. { ERROR_LOCK_VIOLATION, EACCES }, /* 33 */
  77. { ERROR_BAD_NETPATH, ENOENT }, /* 53 */
  78. { ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */
  79. { ERROR_BAD_NET_NAME, ENOENT }, /* 67 */
  80. { ERROR_FILE_EXISTS, EEXIST }, /* 80 */
  81. { ERROR_CANNOT_MAKE, EACCES }, /* 82 */
  82. { ERROR_FAIL_I24, EACCES }, /* 83 */
  83. { ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */
  84. { ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */
  85. { ERROR_DRIVE_LOCKED, EACCES }, /* 108 */
  86. { ERROR_BROKEN_PIPE, EPIPE }, /* 109 */
  87. { ERROR_DISK_FULL, ENOSPC }, /* 112 */
  88. { ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */
  89. { ERROR_INVALID_HANDLE, EINVAL }, /* 124 */
  90. { ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */
  91. { ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */
  92. { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */
  93. { ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */
  94. { ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */
  95. { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */
  96. { ERROR_NOT_LOCKED, EACCES }, /* 158 */
  97. { ERROR_BAD_PATHNAME, ENOENT }, /* 161 */
  98. { ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */
  99. { ERROR_LOCK_FAILED, EACCES }, /* 167 */
  100. { ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */
  101. { ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */
  102. { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */
  103. { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */
  104. };
  105. /* size of the table */
  106. #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
  107. /* The following two constants must be the minimum and maximum
  108. values in the (contiguous) range of Exec Failure errors. */
  109. #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
  110. #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
  111. /* These are the low and high value in the range of errors that are
  112. access violations */
  113. #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
  114. #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
  115. /***
  116. *void _dosmaperr(oserrno) - Map function number
  117. *
  118. *Purpose:
  119. * This function takes an OS error number, and maps it to the
  120. * corresponding errno value (based on UNIX System V values). The
  121. * OS error number is stored in _doserrno (and the mapped value is
  122. * stored in errno)
  123. *
  124. *Entry:
  125. * ULONG oserrno = OS error value
  126. *
  127. *Exit:
  128. * sets _doserrno and errno.
  129. *
  130. *Exceptions:
  131. *
  132. *******************************************************************************/
  133. void __cdecl _dosmaperr (
  134. unsigned long oserrno
  135. )
  136. {
  137. int i;
  138. _doserrno = oserrno; /* set _doserrno */
  139. /* check the table for the OS error code */
  140. for (i = 0; i < ERRTABLESIZE; ++i) {
  141. if (oserrno == errtable[i].oscode) {
  142. errno = errtable[i].errnocode;
  143. return;
  144. }
  145. }
  146. /* The error code wasn't in the table. We check for a range of */
  147. /* EACCES errors or exec failure errors (ENOEXEC). Otherwise */
  148. /* EINVAL is returned. */
  149. if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
  150. errno = EACCES;
  151. else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
  152. errno = ENOEXEC;
  153. else
  154. errno = EINVAL;
  155. }
  156. #ifdef _MT
  157. /***
  158. *int * _errno() - return pointer to thread's errno
  159. *unsigned long * __doserrno() - return pointer to thread's _doserrno
  160. *
  161. *Purpose:
  162. * _errno() returns a pointer to the _terrno field in the current
  163. * thread's _tiddata structure.
  164. * __doserrno returns a pointer to the _tdoserrno field in the current
  165. * thread's _tiddata structure
  166. *
  167. *Entry:
  168. * None.
  169. *
  170. *Exit:
  171. * See above.
  172. *
  173. *Exceptions:
  174. *
  175. *******************************************************************************/
  176. static int ErrnoNoMem = ENOMEM;
  177. static unsigned long DoserrorNoMem = ERROR_NOT_ENOUGH_MEMORY;
  178. int * __cdecl _errno(
  179. void
  180. )
  181. {
  182. _ptiddata ptd = _getptd_noexit();
  183. if (!ptd) {
  184. return &ErrnoNoMem;
  185. } else {
  186. return ( &ptd->_terrno );
  187. }
  188. }
  189. unsigned long * __cdecl __doserrno(
  190. void
  191. )
  192. {
  193. _ptiddata ptd = _getptd_noexit();
  194. if (!ptd) {
  195. return &DoserrorNoMem;
  196. } else {
  197. return ( &ptd->_tdoserrno );
  198. }
  199. }
  200. #endif /* _MT */