home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / unistd / access.c next >
Encoding:
C/C++ Source or Header  |  1996-01-25  |  1.4 KB  |  64 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <sys/stat.h>
  7. #include <io.h>
  8. #include <dir.h>
  9. #include <errno.h>
  10.  
  11. int access(const char *fn, int flags)
  12. {
  13.   unsigned attr = _chmod(fn, 0);
  14.  
  15.   if (attr == -1) {
  16.     struct ffblk ff;
  17.     char fixed_path[FILENAME_MAX];
  18.  
  19.     /* Root directories on some non-local drives (e.g. CD-ROM)
  20.        might fail `_chmod'.  `findfirst' to the rescue.  */
  21.     _fixpath(fn, fixed_path);
  22.     if (fixed_path[1] == ':' && fixed_path[2] == '/' && fixed_path[3] == 0)
  23.       {
  24.         char *fp = fixed_path + 3;
  25.  
  26.         *fp++ = '*';
  27.         *fp++ = '.';
  28.         *fp++ = '*';
  29.         *fp++ = '\0';
  30.         /* Under LFN, we lose a handle here.  Solutions, anyone?  */
  31.         if (findfirst(fixed_path, &ff, FA_DIREC) == 0)
  32.           return 0;
  33.       }
  34.  
  35.     errno = ENOENT;
  36.     return -1;
  37.   }
  38.  
  39.   if (attr & 0x10)        /* directory? */
  40.       return 0;            /* directories always OK */
  41.   if (flags & D_OK)
  42.   {
  43.     errno = EACCES;
  44.     return -1;            /* not a directory */
  45.   }
  46.  
  47.   if ((flags & W_OK) && (attr & 1))
  48.   {
  49.     errno = EACCES;
  50.     return -1;            /* not writable */
  51.   }
  52.  
  53.   if (flags & X_OK)
  54.   {
  55.     if (!_is_executable(fn, 0, 0))
  56.     {
  57.       errno = EACCES;
  58.       return -1;
  59.     }
  60.   }
  61.  
  62.   return 0;            /* everything else is OK */
  63. }
  64.