home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / dosmap.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  11KB  |  288 lines

  1. /***
  2. *dosmap.c - Maps OS errors to errno values
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       _dosmaperr: Maps OS errors to errno values
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13. #include <cruntime.h>
  14. #include <errno.h>
  15. #include <oscalls.h>
  16. #include <stdlib.h>
  17. #include <internal.h>
  18. #include <mtdll.h>
  19.  
  20. /* This is the error table that defines the mapping between OS error
  21.    codes and errno values */
  22.  
  23. struct errentry {
  24.         unsigned long oscode;           /* OS return value */
  25.         int errnocode;  /* System V error code */
  26. };
  27.  
  28. static struct errentry errtable[] = {
  29.         {  ERROR_INVALID_FUNCTION,       EINVAL    },  /* 1 */
  30.         {  ERROR_FILE_NOT_FOUND,         ENOENT    },  /* 2 */
  31.         {  ERROR_PATH_NOT_FOUND,         ENOENT    },  /* 3 */
  32.         {  ERROR_TOO_MANY_OPEN_FILES,    EMFILE    },  /* 4 */
  33.         {  ERROR_ACCESS_DENIED,          EACCES    },  /* 5 */
  34.         {  ERROR_INVALID_HANDLE,         EBADF     },  /* 6 */
  35.         {  ERROR_ARENA_TRASHED,          ENOMEM    },  /* 7 */
  36.         {  ERROR_NOT_ENOUGH_MEMORY,      ENOMEM    },  /* 8 */
  37.         {  ERROR_INVALID_BLOCK,          ENOMEM    },  /* 9 */
  38.         {  ERROR_BAD_ENVIRONMENT,        E2BIG     },  /* 10 */
  39.         {  ERROR_BAD_FORMAT,             ENOEXEC   },  /* 11 */
  40.         {  ERROR_INVALID_ACCESS,         EINVAL    },  /* 12 */
  41.         {  ERROR_INVALID_DATA,           EINVAL    },  /* 13 */
  42.         {  ERROR_INVALID_DRIVE,          ENOENT    },  /* 15 */
  43.         {  ERROR_CURRENT_DIRECTORY,      EACCES    },  /* 16 */
  44.         {  ERROR_NOT_SAME_DEVICE,        EXDEV     },  /* 17 */
  45.         {  ERROR_NO_MORE_FILES,          ENOENT    },  /* 18 */
  46.         {  ERROR_LOCK_VIOLATION,         EACCES    },  /* 33 */
  47.         {  ERROR_BAD_NETPATH,            ENOENT    },  /* 53 */
  48.         {  ERROR_NETWORK_ACCESS_DENIED,  EACCES    },  /* 65 */
  49.         {  ERROR_BAD_NET_NAME,           ENOENT    },  /* 67 */
  50.         {  ERROR_FILE_EXISTS,            EEXIST    },  /* 80 */
  51.         {  ERROR_CANNOT_MAKE,            EACCES    },  /* 82 */
  52.         {  ERROR_FAIL_I24,               EACCES    },  /* 83 */
  53.         {  ERROR_INVALID_PARAMETER,      EINVAL    },  /* 87 */
  54.         {  ERROR_NO_PROC_SLOTS,          EAGAIN    },  /* 89 */
  55.         {  ERROR_DRIVE_LOCKED,           EACCES    },  /* 108 */
  56.         {  ERROR_BROKEN_PIPE,            EPIPE     },  /* 109 */
  57.         {  ERROR_DISK_FULL,              ENOSPC    },  /* 112 */
  58.         {  ERROR_INVALID_TARGET_HANDLE,  EBADF     },  /* 114 */
  59.         {  ERROR_INVALID_HANDLE,         EINVAL    },  /* 124 */
  60.         {  ERROR_WAIT_NO_CHILDREN,       ECHILD    },  /* 128 */
  61.         {  ERROR_CHILD_NOT_COMPLETE,     ECHILD    },  /* 129 */
  62.         {  ERROR_DIRECT_ACCESS_HANDLE,   EBADF     },  /* 130 */
  63.         {  ERROR_NEGATIVE_SEEK,          EINVAL    },  /* 131 */
  64.         {  ERROR_SEEK_ON_DEVICE,         EACCES    },  /* 132 */
  65.         {  ERROR_DIR_NOT_EMPTY,          ENOTEMPTY },  /* 145 */
  66.         {  ERROR_NOT_LOCKED,             EACCES    },  /* 158 */
  67.         {  ERROR_BAD_PATHNAME,           ENOENT    },  /* 161 */
  68.         {  ERROR_MAX_THRDS_REACHED,      EAGAIN    },  /* 164 */
  69.         {  ERROR_LOCK_FAILED,            EACCES    },  /* 167 */
  70.         {  ERROR_ALREADY_EXISTS,         EEXIST    },  /* 183 */
  71.         {  ERROR_FILENAME_EXCED_RANGE,   ENOENT    },  /* 206 */
  72.         {  ERROR_NESTING_NOT_ALLOWED,    EAGAIN    },  /* 215 */
  73.         {  ERROR_NOT_ENOUGH_QUOTA,       ENOMEM    }    /* 1816 */
  74. };
  75.  
  76. /* size of the table */
  77. #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
  78.  
  79. /* The following two constants must be the minimum and maximum
  80.    values in the (contiguous) range of Exec Failure errors. */
  81. #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
  82. #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
  83.  
  84. /* These are the low and high value in the range of errors that are
  85.    access violations */
  86. #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
  87. #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
  88.  
  89.  
  90. /***
  91. *void _dosmaperr(oserrno) - Map function number
  92. *
  93. *Purpose:
  94. *       This function takes an OS error number, and maps it to the
  95. *       corresponding errno value (based on UNIX System V values). The
  96. *       OS error number is stored in _doserrno (and the mapped value is
  97. *       stored in errno)
  98. *
  99. *Entry:
  100. *       ULONG oserrno = OS error value
  101. *
  102. *Exit:
  103. *       sets _doserrno and errno.
  104. *
  105. *Exceptions:
  106. *
  107. *******************************************************************************/
  108.  
  109. void __cdecl _dosmaperr (
  110.         unsigned long oserrno
  111.         )
  112. {
  113.         int i;
  114.  
  115.         _doserrno = oserrno;        /* set _doserrno */
  116.  
  117.         /* check the table for the OS error code */
  118.         for (i = 0; i < ERRTABLESIZE; ++i) {
  119.                 if (oserrno == errtable[i].oscode) {
  120.                         errno = errtable[i].errnocode;
  121.                         return;
  122.                 }
  123.         }
  124.  
  125.         /* The error code wasn't in the table.  We check for a range of */
  126.         /* EACCES errors or exec failure errors (ENOEXEC).  Otherwise   */
  127.         /* EINVAL is returned.                                          */
  128.  
  129.         if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
  130.                 errno = EACCES;
  131.         else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
  132.                 errno = ENOEXEC;
  133.         else
  134.                 errno = EINVAL;
  135. }
  136.  
  137. #ifdef _MT
  138.  
  139. /***
  140. *int * _errno()                 - return pointer to thread's errno
  141. *unsigned long * __doserrno()   - return pointer to thread's _doserrno
  142. *
  143. *Purpose:
  144. *       _errno() returns a pointer to the _terrno field in the current
  145. *       thread's _tiddata structure.
  146. *       __doserrno returns a pointer to the _tdoserrno field in the current
  147. *       thread's _tiddata structure
  148. *
  149. *Entry:
  150. *       None.
  151. *
  152. *Exit:
  153. *       See above.
  154. *
  155. *Exceptions:
  156. *
  157. *******************************************************************************/
  158.  
  159. int * __cdecl _errno(
  160.         void
  161.         )
  162. {
  163.         return ( &(_getptd()->_terrno) );
  164. }
  165.  
  166. unsigned long * __cdecl __doserrno(
  167.         void
  168.         )
  169. {
  170.         return ( &(_getptd()->_tdoserrno) );
  171. }
  172.  
  173. #endif  /* _MT */
  174.  
  175. #else  /* _MAC */
  176.  
  177.  
  178. #include <cruntime.h>
  179. #include <errno.h>
  180. #include <internal.h>
  181. #include <stdlib.h>
  182. #include <macos\osutils.h>
  183. #include <macos\files.h>
  184. #include <macos\errors.h>
  185.  
  186. /* This is the error table that defines the mapping between Mac and
  187.    System V error values */
  188.  
  189. struct errentry {
  190.         short maccode;                  /* MAC return value */
  191.         unsigned short errnocode;       /* System V error code */
  192. };
  193.  
  194.  
  195. static struct errentry errtable[] = {
  196.         {  ioErr,        EIO     },     /*I/O error (bummers)*/
  197.         {  paramErr,     ENOENT  },     /*error in user parameter list */
  198.         {  dirFulErr,    ENOSPC  },     /*Directory full*/
  199.         {  dskFulErr,    ENOSPC  },     /*disk full*/
  200.         {  nsvErr,       ENOENT  },     /*no such volume*/
  201.         {  bdNamErr,     EINVAL  },     /*there may be no bad names in the final system!*/
  202.         {  fnOpnErr,     EACCES  },     /*File not open*/
  203.         {  eofErr,       EINVAL  },     /*End of file*/
  204.         {  posErr,       EINVAL  },     /*tried to position to before start of file (r/w)*/
  205.         {  mFulErr,      ENOMEM  },     /*memory full (open) or file won't fit (load)*/
  206.         {  tmfoErr,      EMFILE  },     /*too many files open*/
  207.         {  fnfErr,       ENOENT  },     /*File not found*/
  208.         {  wPrErr,       EACCES  },     /*diskette is write protected.*/
  209.         {  fLckdErr,     EACCES  },     /*file is locked*/
  210.         {  vLckdErr,     EACCES  },     /*volume is locked*/
  211.         {  fBsyErr,      EACCES  },     /*File is busy (delete)*/
  212.         {  dupFNErr,     EEXIST  },     /*duplicate filename (rename)*/
  213.         {  opWrErr,      EACCES  },     /*file already open with with write permission*/
  214.         {  rfNumErr,     EINVAL  },     /*refnum error*/
  215.         {  gfpErr,       EINVAL  },     /*get file position error*/
  216.         {  volOffLinErr, EINVAL  },     /*volume not on line error (was Ejected)*/
  217.         {  permErr,      EACCES  },     /*permissions error (on file open)*/
  218.         {  volOnLinErr,  EINVAL  },     /*drive volume already on-line at MountVol*/
  219.         {  nsDrvErr,     EINVAL  },     /*no such drive (tried to mount a bad drive num)*/
  220.         {  noMacDskErr,  EINVAL  },     /*not a mac diskette (sig bytes are wrong)*/
  221.         {  extFSErr,     EINVAL  },     /*volume in question belongs to an external fs*/
  222.         {  fsRnErr,      EINVAL  },     /*file system internal error:during rename the old entry was deleted but could not be restored.*/
  223.         {  badMDBErr,    EINVAL  },     /*bad master directory block*/
  224.         {  wrPermErr,    EACCES  },     /*write permissions error*/
  225.         {  dirNFErr,     ENOENT  },     /*Directory not found*/
  226.         {  tmwdoErr,     EMFILE  },     /*No free WDCB available*/
  227.         {  badMovErr,    EINVAL  },     /*Move into offspring error*/
  228.         {  wrgVolTypErr, EINVAL  },     /*Wrong volume type error [operation not supported for MFS]*/
  229.         {  volGoneErr,   EINVAL  },     /*Server volume has been disconnected.*/
  230.         {  fidNotFound,  EBADF   },     /*no file thread exists.*/
  231.         {  fidExists,    EEXIST  },     /*file id already exists*/
  232.         {  notAFileErr,  EINVAL  },     /*directory specified*/
  233.         {  diffVolErr,   EINVAL  },     /*files on different volumes*/
  234.         {  catChangedErr, EINVAL  },    /*the catalog has been modified*/
  235.         {  desktopDamagedErr, EINVAL }, /*desktop database files are corrupted*/
  236.         {  sameFileErr,  EINVAL  },     /*can't exchange a file with itself*/
  237.         {  badFidErr,    EBADF   },     /*file id is dangling or doesn't match with the file number*/
  238.         {  afpRangeOverlap, EACCES  },  /*locking error*/
  239.         {  afpRangeNotLocked, EACCES }, /*unlocking error*/
  240.         {  afpObjectTypeErr, EACCES },  /*file is a directory*/
  241.         {  afpAccessDenied, EACCES },   /*user does not have correct acces to the file*/
  242. };
  243.  
  244. /* size of the table */
  245. #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
  246.  
  247.  
  248. /***
  249. *void _dosmaperr(short macerrno) - Map function number
  250. *
  251. *Purpose:
  252. *       This function takes a Mac error number, and maps it to
  253. *       an System V error number.  The short error number is
  254. *       stored in _doserrno, while the System V error number
  255. *       is stored in errno.
  256. *
  257. *Entry:
  258. *       short macerrno = Mac error value
  259. *
  260. *Exit:
  261. *       sets _macerrno and errno.
  262. *
  263. *Exceptions:
  264. *
  265. *******************************************************************************/
  266.  
  267. void __cdecl _dosmaperr (
  268.         short macerrno
  269.         )
  270. {
  271.         int i;
  272.  
  273.         _macerrno = macerrno;       /* set _macerrno */
  274.  
  275.         /* check the table for the mac error code */
  276.         for (i = 0; i < ERRTABLESIZE; ++i) {
  277.                 if (macerrno == errtable[i].maccode) {
  278.                         errno = errtable[i].errnocode;
  279.                         return;
  280.                 }
  281.         }
  282.  
  283.         errno = EINVAL;
  284. }
  285.  
  286. #endif  /* _MAC */
  287.  
  288.