home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  3.6 KB  |  125 lines  |  [TEXT/KAHL]

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/param.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include "crtlocal.h"
  7.  
  8. int macstat(StringPtr path, struct stat *buf, short nVRefNum, long lDirID)
  9.         {
  10.         CInfoPBRec  cPB;
  11.            memset(buf, 0, sizeof(struct stat));
  12.         cPB.hFileInfo.ioNamePtr = path;
  13.         cPB.hFileInfo.ioVRefNum = nVRefNum;
  14.         cPB.hFileInfo.ioDirID = lDirID;
  15.         cPB.hFileInfo.ioFDirIndex = 0;
  16.  
  17.         if (PBGetCatInfoSync(&cPB))
  18.             {
  19.             errtran(cPB.hFileInfo.ioResult);
  20.             return -1;
  21.             }
  22.  
  23.         /* Type of file: directory or regular file + access */
  24.         
  25.         if (cPB.hFileInfo.ioFlAttrib & ioDirMask)
  26.             {
  27.              buf->st_ino = cPB.dirInfo.ioDrDirID;
  28.             buf->st_mode = S_IFDIR|0777;
  29.               }
  30.         else
  31.             {
  32.             int siz = cPB.hFileInfo.ioFlClpSiz;
  33.             if (!siz) siz = 8192;
  34.             buf->st_blksize = siz;
  35.             buf->st_blocks = (cPB.hFileInfo.ioFlPyLen+siz-1) / siz;
  36.             buf->st_uid = cPB.hFileInfo.ioFlFndrInfo.fdCreator >> 16;
  37.             buf->st_gid = cPB.hFileInfo.ioFlFndrInfo.fdCreator & 0xFFFF;
  38.              buf->st_ino = cPB.hFileInfo.ioDirID;
  39.             buf->st_size = cPB.hFileInfo.ioFlLgLen;
  40.             buf->st_mode = S_IFREG;
  41.             if (cPB.hFileInfo.ioFlFndrInfo.fdFlags & 0x8000)
  42.                 {
  43.                 buf->st_size = MAXPATHLEN;
  44.                    buf->st_mode = S_IFLNK;
  45.                    }
  46.                if (cPB.hFileInfo.ioFlFndrInfo.fdType == 
  47.                        (cPB.hFileInfo.ioFlFndrInfo.fdType&0xFFFF)*0x10001)
  48.                    {
  49.                    buf->st_mode = cPB.hFileInfo.ioFlFndrInfo.fdType;
  50.                    }
  51.                else 
  52.                    buf->st_mode |= 0666;
  53.                if (cPB.hFileInfo.ioFlAttrib & 0x01)
  54.                    {
  55.                    buf->st_mode &= ~0222;
  56.                    }
  57.                if (cPB.hFileInfo.ioFlFndrInfo.fdFlags & fInvisible)
  58.                    {
  59.                    buf->st_mode &= ~0444;
  60.                    }
  61.                buf->st_rdev = cPB.hFileInfo.ioFlFndrInfo.fdFldr;
  62.                }
  63.         /* last access time, modification time and creation time(?) */
  64.         buf->st_atime = buf->st_mtime = unixTime(cPB.hFileInfo.ioFlMdDat);
  65.         buf->st_ctime = unixTime(cPB.hFileInfo.ioFlCrDat);
  66.         buf->st_dev = (long)cPB.hFileInfo.ioVRefNum;
  67.         buf->st_nlink = 1;
  68.         return 0;
  69.         }
  70.     
  71. int fstat(int fd, struct stat *buf)
  72.     {
  73.     FCBPBRec pb;
  74.     Str255 name;
  75.     int refnum = crt_fd_tab[fd].fd;
  76.     if (!refnum || (crt_fd_tab[fd].flags & O_PIPE))
  77.         {
  78.            memset(buf, 0, sizeof(struct stat));
  79.         buf->st_mode = S_IFIFO;
  80.         buf->st_blksize = 8192;
  81.         return 0;
  82.         }
  83.     if (crt_fd_tab[fd].flags & O_CATALOG)
  84.         {
  85.         FSSpec canon = getparent(crt_fd_tab[fd].fd);
  86.         return macstat(canon.name, buf, canon.vRefNum, canon.parID);
  87.         }
  88.     pb.ioRefNum = refnum;
  89.     pb.ioFCBIndx = 0;
  90.     pb.ioCompletion = 0;
  91.     pb.ioVRefNum = crt_ioVRefNum;
  92.     pb.ioNamePtr = (StringPtr) name;
  93.     PBGetFCBInfoSync(&pb);
  94.     return macstat(name, buf, pb.ioFCBVRefNum, pb.ioFCBParID);
  95.     }
  96.  
  97. int stat(const char *name, struct stat *buf)
  98.     {
  99.     FSSpec canon = hfs_canon(crt_parID, name, 1);
  100.     if (!*canon.name) return -1;
  101.     return macstat(canon.name, buf, canon.vRefNum, canon.parID);
  102.     }
  103.  
  104. int lstat(const char *name, struct stat *buf)
  105.     {
  106.     FSSpec canon = hfs_canon(crt_parID, name, 0);
  107.     if (!*canon.name) return -1;
  108.     return macstat(canon.name, buf, canon.vRefNum, canon.parID);
  109.     }
  110.  
  111. #include <sys/unistd.h>
  112.  
  113. int access(const char *name, int cmd)
  114.     {
  115.     int err;
  116.     struct stat statbuf;
  117.     FSSpec canon = hfs_canon(crt_parID, name, 1);
  118.     if (!*canon.name) return -1;
  119.     err = macstat(canon.name, &statbuf, canon.vRefNum, canon.parID);
  120.     if ((cmd&R_OK) && (S_IREAD&~statbuf.st_mode)) err = -1;
  121.     if ((cmd&W_OK) && (S_IWRITE&~statbuf.st_mode)) err = -1;
  122.     if ((cmd&X_OK) && (S_IEXEC&~statbuf.st_mode)) err = -1;
  123.     return err;
  124.     }
  125.