home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / UNZP50P1.ZIP / MAC / macfile.c < prev    next >
C/C++ Source or Header  |  1993-01-23  |  7KB  |  277 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   macfile.c
  4.  
  5.   This source file is used by the mac port to support commands not available
  6.   directly on the Mac, i.e. mkdir().
  7.   It also helps determine if we're running on a Mac with HFS and a disk
  8.   formatted for HFS (HFS - Hierarchical File System; compared to its predecessor,
  9.   MFS - Macintosh File System).
  10.   
  11.   ---------------------------------------------------------------------------*/
  12.  
  13. #include "unzip.h"
  14.  
  15. #ifdef MACOS
  16. #ifndef FSFCBLen
  17. #define FSFCBLen    (*(short *)0x3F6)
  18. #endif
  19.  
  20. static short wAppVRefNum;
  21. static long lAppDirID;
  22. int hfsflag;            /* set if disk has hierarchical file system */
  23.  
  24. static int IsHFSDisk(short wRefNum)
  25. {
  26.     /* get info about the specified volume */
  27.     if (hfsflag == true) {
  28.         HParamBlockRec    hpbr;
  29.         Str255 temp;
  30.         short wErr;
  31.         
  32.         hpbr.volumeParam.ioCompletion = 0;
  33.         hpbr.volumeParam.ioNamePtr = temp;
  34.         hpbr.volumeParam.ioVRefNum = wRefNum;
  35.         hpbr.volumeParam.ioVolIndex = 0;
  36.         wErr = PBHGetVInfo(&hpbr, 0);
  37.  
  38.         if (wErr == noErr && hpbr.volumeParam.ioVFSID == 0
  39.             && hpbr.volumeParam.ioVSigWord == 0x4244) {
  40.                 return true;
  41.         }
  42.     }
  43.  
  44.     return false;
  45. } /* IsHFSDisk */
  46.  
  47. void macfstest(int vrefnum)
  48. {
  49.     Str255 st;
  50.  
  51.     /* is this machine running HFS file system? */
  52.     if (FSFCBLen <= 0) {
  53.         hfsflag = false;
  54.     }
  55.     else
  56.     {
  57.         hfsflag = true;
  58.     }
  59.  
  60.     /* get the file's volume reference number and directory ID */
  61.     if (hfsflag == true) {
  62.         WDPBRec    wdpb;
  63.         OSErr err = noErr;
  64.  
  65.         if (vrefnum != 0) {
  66.             wdpb.ioCompletion = false;
  67.             wdpb.ioNamePtr = st;
  68.             wdpb.ioWDIndex = 0;
  69.             wdpb.ioVRefNum = vrefnum;
  70.             err = PBHGetVol(&wdpb, false);
  71.         
  72.             if (err == noErr) {
  73.                 wAppVRefNum = wdpb.ioWDVRefNum;
  74.                 lAppDirID = wdpb.ioWDDirID;
  75.             }
  76.         }
  77.  
  78.         /* is the disk we're using formatted for HFS? */
  79.         hfsflag = IsHFSDisk(wAppVRefNum);
  80.     }
  81.     
  82.     return;
  83. } /* mactest */
  84.  
  85. int macmkdir(char *path, short nVRefNum, long lDirID)
  86. {
  87.     OSErr    err = -1;
  88.  
  89.     if (path != 0 && strlen(path)<256 && hfsflag == true) {
  90.         HParamBlockRec    hpbr;
  91.         Str255    st;
  92.  
  93.         CtoPstr(path);
  94.         if ((nVRefNum == 0) && (lDirID == 0))
  95.         {
  96.             hpbr.fileParam.ioNamePtr = st;
  97.             hpbr.fileParam.ioCompletion = NULL;
  98.             err = PBHGetVol((WDPBPtr)&hpbr, false);
  99.             nVRefNum = hpbr.wdParam.ioWDVRefNum;
  100.             lDirID = hpbr.wdParam.ioWDDirID;
  101.         }
  102.         else
  103.         {
  104.             err = noErr;
  105.         }
  106.         if (err == noErr) {
  107.             hpbr.fileParam.ioCompletion = NULL;
  108.             hpbr.fileParam.ioVRefNum = nVRefNum;
  109.             hpbr.fileParam.ioDirID = lDirID;
  110.             hpbr.fileParam.ioNamePtr = (StringPtr)path;
  111.             err = PBDirCreate(&hpbr, false);
  112.         }    
  113.         PtoCstr(path);
  114.     }
  115.  
  116.     return (int)err;
  117. } /* mkdir */
  118.  
  119. void ResolveMacVol(short nVRefNum, short *pnVRefNum, long *plDirID, StringPtr pst)
  120. {
  121.     if (hfsflag)
  122.     {
  123.         WDPBRec  wdpbr;
  124.         Str255   st;
  125.         OSErr    err;
  126.  
  127.         wdpbr.ioCompletion = (ProcPtr)NULL;
  128.         wdpbr.ioNamePtr = st;
  129.         wdpbr.ioVRefNum = nVRefNum;
  130.         wdpbr.ioWDIndex = 0;
  131.         wdpbr.ioWDProcID = 0;
  132.         wdpbr.ioWDVRefNum = 0;
  133.         err = PBGetWDInfo( &wdpbr, false );
  134.         if ( err == noErr )
  135.         {
  136.             if (pnVRefNum)
  137.                 *pnVRefNum = wdpbr.ioWDVRefNum;
  138.             if (plDirID)
  139.                 *plDirID = wdpbr.ioWDDirID;
  140.             if (pst)
  141.                 BlockMove( st, pst, st[0]+1 );
  142.         }
  143.     }
  144.     else
  145.     {
  146.         if (pnVRefNum)
  147.             *pnVRefNum = nVRefNum;
  148.         if (plDirID)
  149.             *plDirID = 0;
  150.         if (pst)
  151.             *pst = 0;
  152.     }
  153. }
  154.  
  155. short macopen(char *sz, short nFlags, short nVRefNum, long lDirID)
  156. {
  157.     OSErr   err;
  158.     Str255  st;
  159.     char    chPerms = (!nFlags) ? fsRdPerm : fsRdWrPerm;
  160.     short   nFRefNum;
  161.  
  162.     CtoPstr( sz );
  163.     BlockMove( sz, st, sz[0]+1 );
  164.     PtoCstr( sz );
  165.     if (hfsflag)
  166.     {
  167.         if (nFlags > 1)
  168.             err = HOpenRF( nVRefNum, lDirID, st, chPerms, &nFRefNum);
  169.         else
  170.             err = HOpen( nVRefNum, lDirID, st, chPerms, &nFRefNum);
  171.     }
  172.     else
  173.     {
  174.         /*
  175.          * Have to use PBxxx style calls since the high level
  176.          * versions don't support specifying permissions
  177.          */
  178.         ParamBlockRec    pbr;
  179.  
  180.         pbr.ioParam.ioNamePtr = st;
  181.         pbr.ioParam.ioVRefNum = gnVRefNum;
  182.         pbr.ioParam.ioVersNum = 0;
  183.         pbr.ioParam.ioPermssn = chPerms;
  184.         pbr.ioParam.ioMisc = 0;
  185.         if (nFlags >1)
  186.             err = PBOpenRF( &pbr, false );
  187.         else
  188.             err = PBOpen( &pbr, false );
  189.         nFRefNum = pbr.ioParam.ioRefNum;
  190.     }
  191.     if ( err )
  192.         return -1;
  193.     else
  194.         return nFRefNum;
  195. }
  196.  
  197. short maccreat(char *sz, short nVRefNum, long lDirID, OSType ostCreator, OSType ostType)
  198. {
  199.     OSErr   err;
  200.     Str255  st;
  201.     FInfo   fi;
  202.  
  203.     CtoPstr( sz );
  204.     BlockMove( sz, st, sz[0]+1 );
  205.     PtoCstr( sz );
  206.     if (hfsflag)
  207.     {
  208.         err = HGetFInfo( nVRefNum, lDirID, st, &fi );
  209.         if (err == fnfErr)
  210.             err = HCreate( nVRefNum, lDirID, st, ostCreator, ostType );
  211.         else if (err == noErr)
  212.         {
  213.             fi.fdCreator = ostCreator;
  214.             fi.fdType = ostType;
  215.             err = HSetFInfo( nVRefNum, lDirID, st, &fi );
  216.         }
  217.     }
  218.     else
  219.     {
  220.         err = GetFInfo( st, nVRefNum, &fi );
  221.         if (err == fnfErr)
  222.             err = Create( st, nVRefNum, ostCreator, ostType );
  223.         else if (err == noErr)
  224.         {
  225.             fi.fdCreator = ostCreator;
  226.             fi.fdType = ostType;
  227.             err = SetFInfo( st, nVRefNum, &fi );
  228.         }
  229.     }
  230.     if (err == noErr)
  231.         return noErr;
  232.     else
  233.         return -1;
  234. }
  235.  
  236. short macread(short nFRefNum, char *pb, unsigned cb)
  237. {
  238.     long    lcb = cb;
  239.  
  240.     (void)FSRead( nFRefNum, &lcb, pb );
  241.  
  242.     return (short)lcb;
  243. }
  244.  
  245. short macwrite(short nFRefNum, char *pb, unsigned cb)
  246. {
  247.     long    lcb = cb;
  248.  
  249.     (void)FSWrite( nFRefNum, &lcb, pb );
  250.  
  251.     return (short)lcb;
  252. }
  253.  
  254. short macclose(short nFRefNum)
  255. {
  256.     return FSClose( nFRefNum );
  257. }
  258.  
  259. long maclseek(short nFRefNum, long lib, short nMode)
  260. {
  261.     ParamBlockRec   pbr;
  262.  
  263.     if (nMode == SEEK_SET)
  264.         nMode = fsFromStart;
  265.     else if (nMode == SEEK_CUR)
  266.         nMode = fsFromMark;
  267.     else if (nMode == SEEK_END)
  268.         nMode = fsFromLEOF;
  269.     pbr.ioParam.ioRefNum = nFRefNum;
  270.     pbr.ioParam.ioPosMode = nMode;
  271.     pbr.ioParam.ioPosOffset = lib;
  272.     (void)PBSetFPos(&pbr, 0);
  273.     return pbr.ioParam.ioPosOffset;
  274. }
  275.  
  276. #endif /* MACOS */
  277.