home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip201.zip / mac / macstat.c < prev    next >
C/C++ Source or Header  |  1992-08-20  |  6KB  |  207 lines

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