home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / mac / macstat.c < prev    next >
C/C++ Source or Header  |  1997-10-14  |  6KB  |  211 lines

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