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

  1. /***
  2. *access.c - access function
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file has the _access() function which checks on file accessability.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13. #include <cruntime.h>
  14. #include <io.h>
  15. #include <oscalls.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <msdos.h>
  19. #include <internal.h>
  20. #include <tchar.h>
  21.  
  22. /***
  23. *int _access(path, amode) - check whether file can be accessed under mode
  24. *
  25. *Purpose:
  26. *       Checks to see if the specified file exists and can be accessed
  27. *       in the given mode.
  28. *
  29. *Entry:
  30. *       _TSCHAR *path - pathname
  31. *       int amode -     access mode
  32. *                       (0 = exist only, 2 = write, 4 = read, 6 = read/write)
  33. *
  34. *Exit:
  35. *       returns 0 if file has given mode
  36. *       returns -1 and sets errno if file does not have given mode or
  37. *       does not exist
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43. int __cdecl _taccess (
  44.         const _TSCHAR *path,
  45.         int amode
  46.         )
  47. {
  48.         DWORD attr;
  49.  
  50.         attr = GetFileAttributes((LPTSTR)path);
  51.         if (attr  == 0xffffffff) {
  52.                 /* error occured -- map error code and return */
  53.                 _dosmaperr(GetLastError());
  54.                 return -1;
  55.         }
  56.  
  57.         /* no error; see if returned premission settings OK */
  58.         if ( (attr & FILE_ATTRIBUTE_READONLY) && (amode & 2) ) {
  59.                 /* no write permission on file, return error */
  60.                 errno = EACCES;
  61.                 _doserrno = E_access;
  62.                 return -1;
  63.         }
  64.         else
  65.                 /* file exists and has requested permission setting */
  66.                 return 0;
  67.  
  68. }
  69.  
  70. #else  /* _MAC */
  71.  
  72. #include <cruntime.h>
  73. #include <io.h>
  74. #include <stdlib.h>
  75. #include <errno.h>
  76. #include <internal.h>
  77. #include <string.h>
  78. #include <macos\osutils.h>
  79. #include <macos\files.h>
  80. #include <macos\errors.h>
  81.  
  82. /***
  83. *int _access(path, amode) - check whether file can be accessed under mode
  84. *
  85. *Purpose:
  86. *       Checks to see if the specified file exists and can be accessed
  87. *       in the given mode.
  88. *
  89. *Entry:
  90. *       char *path -    pathname
  91. *       int amode -     access mode
  92. *                       (0 = exist only, 2 = write, 4 = read, 6 = read/write)
  93. *
  94. *Exit:
  95. *       returns 0 if file has given mode
  96. *       returns -1 and sets errno if file does not have given mode or
  97. *       does not exist
  98. *
  99. *Exceptions:
  100. *
  101. *******************************************************************************/
  102.  
  103. int __cdecl _access (
  104.         const char *path,
  105.         int amode
  106.         )
  107. {
  108.         CInfoPBRec cinfoPB;
  109.         char szPath[256];
  110.         OSErr osErr;
  111.  
  112.         if (!*path)
  113.         {
  114.                 errno = ENOENT;
  115.                 return -1;
  116.         }
  117.  
  118.         strcpy(szPath, path);
  119.         cinfoPB.dirInfo.ioVRefNum = 0;
  120.         cinfoPB.dirInfo.ioDrDirID = 0;
  121.         cinfoPB.dirInfo.ioFDirIndex = 0;
  122.         cinfoPB.dirInfo.ioNamePtr = _c2pstr(szPath);
  123.         osErr = PBGetCatInfoSync(&cinfoPB);
  124.         if (osErr)
  125.         {
  126.                 _dosmaperr(osErr);
  127.                 return -1;
  128.         }
  129.  
  130.         //file locked or read only permission
  131.         if ( (cinfoPB.dirInfo.ioFlAttrib & 0x1 ||
  132.                 cinfoPB.dirInfo.ioDrUsrWds.frFlags == 1) && (amode & 2) )
  133.         {
  134.                 /* no write permission on file, return error */
  135.                 errno = EACCES;
  136.                 _macerrno = permErr;
  137.                 return -1;
  138.         }
  139.         else
  140.                 /* file exists and has requested permission setting */
  141.                 return 0;
  142.  
  143. }
  144.  
  145. #endif  /* _MAC */
  146.