home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / unzip.tar.gz / unzip.tar / unzip / MAC.arc / macstat.c < prev    next >
C/C++ Source or Header  |  1991-03-10  |  6KB  |  208 lines

  1. #ifdef THINK_C
  2. #define MACOS
  3. #include    <FileMgr.h>
  4. #include    <HFS.h>
  5. #include    <pascal.h>
  6. #endif
  7. #ifdef MPW
  8. #define MACOS
  9. #include    <Files.h>
  10. #include    <Errors.h>
  11. #define FSFCBLen    (*(short *)0x3F6)
  12. #define hFileInfo   hfileInfo
  13. #define CtoPstr c2pstr
  14. #define PtoCstr p2cstr
  15. #endif
  16.  
  17. #ifdef MACOS
  18. #include    <string.h>
  19. #include    "macstat.h"
  20. int stat(char *path, struct stat *buf);
  21.  
  22. /* assume that the path will contain a Mac-type pathname, i.e. ':'s, etc. */
  23. int stat(path, buf)
  24. char *path;
  25. struct stat *buf;
  26. {
  27.     char    temp[256];
  28.     short   curVolume;
  29.     long    curDir;
  30.     short   fIsHFS = false;
  31.     OSErr   err;
  32.  
  33.     if (buf == (struct stat *)0L || path == (char *)0L) {
  34.         SysBeep(1);
  35.         return -1;
  36.     }
  37.     
  38.     if (path[0] == '\0' || strlen(path)>255) {
  39.         return -1;
  40.     }
  41.  
  42.     if (GetVol((StringPtr)&temp[0], &curVolume) != noErr) {
  43.         SysBeep(1);
  44.         return -1;
  45.     }
  46.     
  47.     /* get info about the specified volume */
  48.     if (FSFCBLen > 0)   /* HFS Disk? */
  49.     {
  50.         WDPBRec wdpb;
  51.         HParamBlockRec    hpbr;
  52.         Str255 st;
  53.  
  54.         wdpb.ioCompletion = 0;
  55.         wdpb.ioNamePtr = temp;
  56.         err = PBHGetVol(&wdpb, 0);
  57.         if (err == noErr)
  58.         {
  59.             hpbr.volumeParam.ioCompletion = 0;
  60.             hpbr.volumeParam.ioNamePtr = st;
  61.             hpbr.volumeParam.ioVRefNum = wdpb.ioVRefNum;
  62.             hpbr.volumeParam.ioVolIndex = 0;
  63.             err = PBHGetVInfo(&hpbr, 0);
  64.  
  65.             if (err == noErr && hpbr.volumeParam.ioVFSID == 0
  66.                 && hpbr.volumeParam.ioVSigWord == 0x4244) {
  67.                     fIsHFS = true;
  68.             }
  69.         }
  70.     }
  71.  
  72.  
  73.     /* number of links, at least in System 6.0x, 0 */
  74.     buf->st_nlink = 0;
  75.     /* user id */
  76.     buf->st_uid = 0;
  77.     /* group id */
  78.     buf->st_gid = 0;
  79.  
  80.     if (fIsHFS == true)   /* HFS? */
  81.     {
  82.         CInfoPBRec  cPB;
  83.         HParamBlockRec  wPB;
  84.         
  85.         wPB.wdParam.ioCompletion = (ProcPtr)0L;
  86.         wPB.wdParam.ioNamePtr = (StringPtr)temp;
  87.         err = PBHGetVol((WDPBPtr)&wPB, false);
  88.         if (err != noErr) {
  89.             SysBeep(1);
  90.             return -1;
  91.         }
  92.  
  93.         curVolume = wPB.wdParam.ioWDVRefNum;
  94.         buf->st_dev = curDir = wPB.wdParam.ioWDDirID;
  95.         
  96.         /* get information about file */
  97.         cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
  98.         CtoPstr(path);
  99.         strncpy(temp,path, path[0]+1);
  100.         PtoCstr(path);
  101.         cPB.hFileInfo.ioNamePtr = (StringPtr)temp;
  102.         cPB.hFileInfo.ioVRefNum = 0;
  103.         cPB.hFileInfo.ioDirID = 0;
  104.         cPB.hFileInfo.ioFDirIndex = 0;
  105.         
  106.         err = PBGetCatInfo(&cPB, false); 
  107.         
  108.         if (err != noErr) {
  109.             if (err != fnfErr) {
  110.                 SysBeep(1);
  111.             }
  112.             return -1;
  113.         }
  114.         
  115.         /* Type of file: directory or regular file */
  116.         buf->st_mode = (cPB.hFileInfo.ioFlAttrib & ioDirMask) ? S_IFDIR : S_IFREG;
  117.         
  118.         /* last access time, modification time and creation time(?) */
  119.         buf->st_atime = buf->st_mtime = cPB.hFileInfo.ioFlMdDat;
  120.         buf->st_ctime = cPB.hFileInfo.ioFlCrDat;
  121.         /* inode number */
  122.         buf->st_ino = cPB.hFileInfo.ioDirID + ((long)curVolume)<<16;
  123.         /* size of file - use only the data fork */
  124.         buf->st_size = cPB.hFileInfo.ioFlLgLen;
  125.         /* size of disk block */
  126.         if (cPB.hFileInfo.ioFlClpSiz == 0) {
  127.             HParamBlockRec hPB;
  128.             
  129.             hPB.volumeParam.ioCompletion = (ProcPtr)0L;
  130.             hPB.volumeParam.ioNamePtr = (StringPtr)temp;
  131.             hPB.volumeParam.ioVRefNum = 0;
  132.             hPB.volumeParam.ioVolIndex = 0;
  133.             
  134.             err = PBHGetVInfo(&hPB, false);
  135.             
  136.             if (err != noErr) {
  137.                 SysBeep(1);
  138.                 return -1;
  139.             }
  140.             
  141.             buf->st_blksize = hPB.volumeParam.ioVClpSiz;
  142.         }
  143.         else
  144.             buf->st_blksize = cPB.hFileInfo.ioFlClpSiz;
  145.     
  146.         buf->st_blocks = cPB.hFileInfo.ioFlPyLen/ buf->st_blksize;
  147.     }
  148.     else    /* MFS? */
  149.     {
  150.         ParamBlockRec   pPB;
  151.         
  152.         buf->st_dev = 0;
  153.         
  154.         CtoPstr(path);
  155.         strncpy(temp, path, path[0]+1);
  156.         PtoCstr(path);
  157.         pPB.fileParam.ioCompletion = (ProcPtr)0;
  158.         pPB.fileParam.ioNamePtr = (StringPtr)temp;
  159.         pPB.fileParam.ioVRefNum = curVolume;
  160.         pPB.fileParam.ioFVersNum = 0;
  161.         pPB.fileParam.ioFDirIndex = 0;
  162.         
  163.         err = PBGetFInfo(&pPB, false);   
  164.         
  165.         if (err != noErr) {
  166.             SysBeep(1);
  167.             return -1;
  168.         }
  169.         
  170.         /* Type of file: either directory or regular file */
  171.         buf->st_mode = (pPB.fileParam.ioFlAttrib & ioDirMask) ? S_IFDIR : S_IFREG;
  172.         
  173.         /* last access time, modification time and creation time(?) */
  174.         buf->st_atime = buf->st_mtime = pPB.fileParam.ioFlMdDat;
  175.         buf->st_ctime = pPB.fileParam.ioFlCrDat;
  176.         /* inode number */
  177.         buf->st_ino = pPB.fileParam.ioFlNum + ((long)curVolume)<<16;
  178.         /* size of file - use only the data fork */
  179.         buf->st_size = pPB.fileParam.ioFlLgLen;
  180.         /* size of disk block */
  181.         {
  182.             ParamBlockRec hPB;
  183.             
  184.             hPB.volumeParam.ioCompletion = (ProcPtr)0;
  185.             hPB.volumeParam.ioNamePtr = (StringPtr)temp;
  186.             hPB.volumeParam.ioVRefNum = curVolume;
  187.             hPB.volumeParam.ioVolIndex = 0;
  188.             
  189.             err = PBGetVInfo(&hPB, false);
  190.             
  191.             if (err != noErr) {
  192.                 SysBeep(1);
  193.                 return -1;
  194.             }
  195.             
  196.             buf->st_blksize = hPB.volumeParam.ioVClpSiz;
  197.         }
  198.     
  199.         /* number of disk blocks used by file - includes resource fork */
  200.         buf->st_blocks = pPB.fileParam.ioFlPyLen/ buf->st_blksize +
  201.                             pPB.fileParam.ioFlRPyLen/buf->st_blksize;
  202.     }
  203.  
  204.     return 0;
  205. }
  206. #else
  207. #error 1
  208. #endif