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