home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip51.zip / mac / mac.c < prev    next >
C/C++ Source or Header  |  1993-12-22  |  31KB  |  996 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   mac.c
  4.  
  5.   Macintosh-specific routines for use with Info-ZIP's UnZip 5.1 and later.
  6.  
  7.   This source file incorporates the contents of what was formerly macfile.c,
  8.   which supported commands (such as mkdir()) not available directly on the
  9.   Mac, and which also determined whether HFS (Hierarchical File System) or
  10.   MFS (Macintosh File System) was in use.
  11.  
  12.   ---------------------------------------------------------------------------*/
  13.  
  14.  
  15.  
  16. #include "unzip.h"
  17.  
  18. #ifdef MACOS
  19. #ifndef FSFCBLen
  20. #  define FSFCBLen  (*(short *)0x3F6)
  21. #endif
  22.  
  23. #define read_only   file_attr   /* for readability only */
  24.  
  25. static short wAppVRefNum;
  26. static long lAppDirID;
  27. int HFSFlag;            /* set if disk has hierarchical file system */
  28.  
  29. static int created_dir;        /* used in mapname(), checkdir() */
  30. static int renamed_fullpath;   /* ditto */
  31.  
  32. #define MKDIR(path)     macmkdir(path, gnVRefNum, glDirID)
  33.  
  34.  
  35.  
  36.  
  37.  
  38. /**********************/
  39. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  40. /**********************/
  41.  
  42. char *do_wild(wildspec)
  43.     char *wildspec;         /* only used first time on a given dir */
  44. {
  45.     static DIR *dir = NULL;
  46.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  47.     static int firstcall=TRUE, have_dirname, dirnamelen;
  48.     struct direct *file;
  49.  
  50.  
  51.     /* Even when we're just returning wildspec, we *always* do so in
  52.      * matchname[]--calling routine is allowed to append four characters
  53.      * to the returned string, and wildspec may be a pointer to argv[].
  54.      */
  55.     if (firstcall) {        /* first call:  must initialize everything */
  56.         firstcall = FALSE;
  57.  
  58.         /* break the wildspec into a directory part and a wildcard filename */
  59.         if ((wildname = strrchr(wildspec, ':')) == NULL) {
  60.             dirname = ":";
  61.             dirnamelen = 1;
  62.             have_dirname = FALSE;
  63.             wildname = wildspec;
  64.         } else {
  65.             ++wildname;     /* point at character after ':' */
  66.             dirnamelen = wildname - wildspec;
  67.             if ((dirname = (char *)malloc(dirnamelen+1)) == NULL) {
  68.                 fprintf(stderr, "warning:  can't allocate wildcard buffers\n");
  69.                 strcpy(matchname, wildspec);
  70.                 return matchname;   /* but maybe filespec was not a wildcard */
  71.             }
  72.             strncpy(dirname, wildspec, dirnamelen);
  73.             dirname[dirnamelen] = '\0';
  74.             have_dirname = TRUE;
  75.         }
  76.  
  77.         if ((dir = opendir(dirname)) != NULL) {
  78.             while ((file = readdir(dir)) != NULL) {
  79.                 if (match(file->d_name, wildname, 0)) {  /* 0 == case sens. */
  80.                     if (have_dirname) {
  81.                         strcpy(matchname, dirname);
  82.                         strcpy(matchname+dirnamelen, file->d_name);
  83.                     } else
  84.                         strcpy(matchname, file->d_name);
  85.                     return matchname;
  86.                 }
  87.             }
  88.             /* if we get to here directory is exhausted, so close it */
  89.             closedir(dir);
  90.             dir = NULL;
  91.         }
  92.  
  93.         /* return the raw wildspec in case that works (e.g., directory not
  94.          * searchable, but filespec was not wild and file is readable) */
  95.         strcpy(matchname, wildspec);
  96.         return matchname;
  97.     }
  98.  
  99.     /* last time through, might have failed opendir but returned raw wildspec */
  100.     if (dir == NULL) {
  101.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  102.         if (have_dirname)
  103.             free(dirname);
  104.         return (char *)NULL;
  105.     }
  106.  
  107. #ifndef THINK_C            /* Think C only matches one at most (for now) */
  108.     /* If we've gotten this far, we've read and matched at least one entry
  109.      * successfully (in a previous call), so dirname has been copied into
  110.      * matchname already.
  111.      */
  112.     while ((file = readdir(dir)) != NULL)
  113.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  114.             if (have_dirname) {
  115.                 /* strcpy(matchname, dirname); */
  116.                 strcpy(matchname+dirnamelen, file->d_name);
  117.             } else
  118.                 strcpy(matchname, file->d_name);
  119.             return matchname;
  120.         }
  121. #endif
  122.  
  123.     closedir(dir);     /* have read at least one dir entry; nothing left */
  124.     dir = NULL;
  125.     firstcall = TRUE;  /* reset for new wildspec */
  126.     if (have_dirname)
  127.         free(dirname);
  128.     return (char *)NULL;
  129.  
  130. } /* end function do_wild() */
  131.  
  132.  
  133.  
  134.  
  135.  
  136. /**********************/
  137. /* Function mapattr() */
  138. /**********************/
  139.  
  140. int mapattr()
  141. {
  142.     /* only care about read-only bit, so just look at MS-DOS side of attrs */
  143.     pInfo->read_only = (unsigned)(crec.external_file_attributes & 1);
  144.     return 0;
  145.  
  146. } /* end function mapattr() */
  147.  
  148.  
  149.  
  150.  
  151.  
  152. /************************/
  153. /*  Function mapname()  */
  154. /************************/
  155.  
  156. int mapname(renamed)  /* return 0 if no error, 1 if caution (filename trunc), */
  157.     int renamed;      /* 2 if warning (skip file because dir doesn't exist), */
  158. {                     /* 3 if error (skip file), 10 if no memory (skip file) */
  159.     char pathcomp[FILNAMSIZ];   /* path-component buffer */
  160.     char *pp, *cp=NULL;         /* character pointers */
  161.     char *lastsemi = NULL;      /* pointer to last semi-colon in pathcomp */
  162.     int quote = FALSE;          /* flags */
  163.     int error = 0;
  164.     register unsigned workch;   /* hold the character being tested */
  165.  
  166.  
  167. /*---------------------------------------------------------------------------
  168.     Initialize various pointers and counters and stuff.
  169.   ---------------------------------------------------------------------------*/
  170.  
  171.     /* can create path as long as not just freshening, or if user told us */
  172.     create_dirs = (!fflag || renamed);
  173.  
  174.     created_dir = FALSE;        /* not yet */
  175.  
  176.     /* user gave full pathname:  don't prepend rootpath */
  177.     renamed_fullpath = (renamed && (*filename == '/'));
  178.  
  179.     if (checkdir((char *)NULL, INIT) == 10)
  180.         return 10;              /* initialize path buffer, unless no memory */
  181.  
  182.     pp = pathcomp;              /* point to translation buffer */
  183.     if (!(renamed_fullpath || jflag))
  184.         *pp++ = ':';
  185.     *pp = '\0';
  186.  
  187.     if (jflag)                  /* junking directories */
  188.         cp = (char *)strrchr(filename, '/');
  189.     if (cp == NULL)             /* no '/' or not junking dirs */
  190.         cp = filename;          /* point to internal zipfile-member pathname */
  191.     else
  192.         ++cp;                   /* point to start of last component of path */
  193.  
  194. /*---------------------------------------------------------------------------
  195.     Begin main loop through characters in filename.
  196.   ---------------------------------------------------------------------------*/
  197.  
  198.     while ((workch = (uch)*cp++) != 0) {
  199.  
  200.         if (quote) {                 /* if character quoted, */
  201.             *pp++ = (char)workch;    /*  include it literally */
  202.             quote = FALSE;
  203.         } else
  204.             switch (workch) {
  205.             case '/':             /* can assume -j flag not given */
  206.                 *pp = '\0';
  207.                 if ((error = checkdir(pathcomp, APPEND_DIR)) > 1)
  208.                     return error;
  209.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  210.                 lastsemi = NULL;  /* leave directory semi-colons alone */
  211.                 break;
  212.  
  213.             case ';':             /* VMS version (or DEC-20 attrib?) */
  214.                 lastsemi = pp;         /* keep for now; remove VMS ";##" */
  215.                 *pp++ = (char)workch;  /*  later, if requested */
  216.                 break;
  217.  
  218.             case '\026':          /* control-V quote for special chars */
  219.                 quote = TRUE;     /* set flag for next character */
  220.                 break;
  221.  
  222.             default:
  223.                 /* allow European characters in filenames: */
  224.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  225.                     *pp++ = (char)workch;
  226.             } /* end switch */
  227.  
  228.     } /* end while loop */
  229.  
  230.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  231.  
  232.     /* if not saving them, remove with VMS version numbers (appended ";###") */
  233.     if (!V_flag && lastsemi) {
  234.         pp = lastsemi + 1;
  235.         while (isdigit((uch)(*pp)))
  236.             ++pp;
  237.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  238.             *lastsemi = '\0';
  239.     }
  240.  
  241. /*---------------------------------------------------------------------------
  242.     Report if directory was created (and no file to create:  filename ended
  243.     in '/'), check name to be sure it exists, and combine path and name be-
  244.     fore exiting.
  245.   ---------------------------------------------------------------------------*/
  246.  
  247.     if (filename[strlen(filename) - 1] == '/') {
  248.         checkdir(filename, GETPATH);
  249.         if (created_dir && QCOND2) {
  250.             fprintf(stdout, "   creating: %s\n", filename);
  251.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  252.         }
  253.         return 2;   /* dir existed already; don't look for data to extract */
  254.     }
  255.  
  256.     if (*pathcomp == '\0') {
  257.         fprintf(stderr, "mapname:  conversion of %s failed\n", filename);
  258.         return 3;
  259.     }
  260.  
  261.     checkdir(pathcomp, APPEND_NAME);   /* returns 1 if truncated:  care? */
  262.     checkdir(filename, GETPATH);
  263.  
  264.     return error;
  265.  
  266. } /* end function mapname() */
  267.  
  268.  
  269.  
  270.  
  271.  
  272. /***********************/
  273. /* Function checkdir() */
  274. /***********************/
  275.  
  276. int checkdir(pathcomp, flag)
  277.     char *pathcomp;
  278.     int flag;
  279. /*
  280.  * returns:  1 - (on APPEND_NAME) truncated filename
  281.  *           2 - path doesn't exist, not allowed to create
  282.  *           3 - path doesn't exist, tried to create and failed; or
  283.  *               path exists and is not a directory, but is supposed to be
  284.  *           4 - path is too long
  285.  *          10 - can't allocate memory for filename buffers
  286.  */
  287. {
  288.     static int rootlen = 0;   /* length of rootpath */
  289.     static char *rootpath;    /* user's "extract-to" directory */
  290.     static char *buildpath;   /* full path (so far) to extracted file */
  291.     static char *end;         /* pointer to end of buildpath ('\0') */
  292.  
  293. #   define FN_MASK   7
  294. #   define FUNCTION  (flag & FN_MASK)
  295.  
  296.  
  297.  
  298. /*---------------------------------------------------------------------------
  299.     APPEND_DIR:  append the path component to the path being built and check
  300.     for its existence.  If doesn't exist and we are creating directories, do
  301.     so for this one; else signal success or error as appropriate.
  302.   ---------------------------------------------------------------------------*/
  303.  
  304.     if (FUNCTION == APPEND_DIR) {
  305.         int too_long = FALSE;
  306. #ifdef SHORT_NAMES
  307.         char *old_end = end;
  308. #endif
  309.  
  310.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  311.         while ((*end = *pathcomp++) != '\0')
  312.             ++end;
  313. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  314.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  315.             *(end = old_end + FILENAME_MAX) = '\0';
  316. #endif
  317.  
  318.         /* GRR:  could do better check, see if overrunning buffer as we go:
  319.          * check end-buildpath after each append, set warning variable if
  320.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  321.          * appending.  Clear variable when begin new path. */
  322.  
  323.         if ((end-buildpath) > FILNAMSIZ-3)  /* need ':', one-char name, '\0' */
  324.             too_long = TRUE;                /* check if extracting directory? */
  325.         if (stat(buildpath, &statbuf)) {    /* path doesn't exist */
  326.             if (!create_dirs) {   /* told not to create (freshening) */
  327.                 free(buildpath);
  328.                 return 2;         /* path doesn't exist:  nothing to do */
  329.             }
  330.             if (too_long) {
  331.                 fprintf(stderr, "checkdir error:  path too long: %s\n",
  332.                   buildpath);
  333.                 fflush(stderr);
  334.                 free(buildpath);
  335.                 return 4;         /* no room for filenames:  fatal */
  336.             }
  337.             if (MKDIR(buildpath) == -1) {   /* create the directory */
  338.                 fprintf(stderr, "checkdir error:  can't create %s\n\
  339.                  unable to process %s.\n", buildpath, filename);
  340.                 fflush(stderr);
  341.                 free(buildpath);
  342.                 return 3;      /* path didn't exist, tried to create, failed */
  343.             }
  344.             created_dir = TRUE;
  345.         } else if (!S_ISDIR(statbuf.st_mode)) {
  346.             fprintf(stderr, "checkdir error:  %s exists but is not directory\n\
  347.                  unable to process %s.\n", buildpath, filename);
  348.             fflush(stderr);
  349.             free(buildpath);
  350.             return 3;          /* path existed but wasn't dir */
  351.         }
  352.         if (too_long) {
  353.             fprintf(stderr, "checkdir error:  path too long: %s\n", buildpath);
  354.             fflush(stderr);
  355.             free(buildpath);
  356.             return 4;         /* no room for filenames:  fatal */
  357.         }
  358.         *end++ = ':';
  359.         *end = '\0';
  360.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  361.         return 0;
  362.  
  363.     } /* end if (FUNCTION == APPEND_DIR) */
  364.  
  365. /*---------------------------------------------------------------------------
  366.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  367.     buildpath.
  368.   ---------------------------------------------------------------------------*/
  369.  
  370.     if (FUNCTION == GETPATH) {
  371.         strcpy(pathcomp, buildpath);
  372.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  373.         free(buildpath);
  374.         buildpath = end = NULL;
  375.         return 0;
  376.     }
  377.  
  378. /*---------------------------------------------------------------------------
  379.     APPEND_NAME:  assume the path component is the filename; append it and
  380.     return without checking for existence.
  381.   ---------------------------------------------------------------------------*/
  382.  
  383.     if (FUNCTION == APPEND_NAME) {
  384. #ifdef SHORT_NAMES
  385.         char *old_end = end;
  386. #endif
  387.  
  388.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  389.         while ((*end = *pathcomp++) != '\0') {
  390.             ++end;
  391. #ifdef SHORT_NAMES  /* truncate name at 14 characters, typically */
  392.             if ((end-old_end) > FILENAME_MAX)      /* GRR:  proper constant? */
  393.                 *(end = old_end + FILENAME_MAX) = '\0';
  394. #endif
  395.             if ((end-buildpath) >= FILNAMSIZ) {
  396.                 *--end = '\0';
  397.                 fprintf(stderr, "checkdir warning:  path too long; truncating\n\
  398. checkdir warning:  path too long; truncating\n\
  399.                    %s\n                -> %s\n", filename, buildpath);
  400.                 fflush(stderr);
  401.                 return 1;   /* filename truncated */
  402.             }
  403.         }
  404.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  405.         return 0;  /* could check for existence here, prompt for new name... */
  406.     }
  407.  
  408. /*---------------------------------------------------------------------------
  409.     INIT:  allocate and initialize buffer space for the file currently being
  410.     extracted.  If file was renamed with an absolute path, don't prepend the
  411.     extract-to path.
  412.   ---------------------------------------------------------------------------*/
  413.  
  414. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  415.  
  416.     if (FUNCTION == INIT) {
  417.         Trace((stderr, "initializing buildpath to "));
  418.         if ((buildpath = (char *)malloc(strlen(filename)+rootlen+2)) == NULL)
  419.             return 10;
  420.         if ((rootlen > 0) && !renamed_fullpath) {
  421.             strcpy(buildpath, rootpath);
  422.             end = buildpath + rootlen;
  423.         } else {
  424.             *buildpath = '\0';
  425.             end = buildpath;
  426.         }
  427.         Trace((stderr, "[%s]\n", buildpath));
  428.         return 0;
  429.     }
  430.  
  431. /*---------------------------------------------------------------------------
  432.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  433.     sary; else assume it's a zipfile member and return.  This path segment
  434.     gets used in extracting all members from every zipfile specified on the
  435.     command line.
  436.   ---------------------------------------------------------------------------*/
  437.  
  438. /* GRR:  for VMS and TOPS-20, allow either y]z.dir or y.z] forms; fix as
  439.  * appropriate before stat call */
  440.  
  441. /* GRR:  for MS-DOS and OS/2, may need to append '.' to path of form "x:" */
  442.  
  443.     if (FUNCTION == ROOT) {
  444.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  445.         if (pathcomp == NULL) {
  446.             rootlen = 0;
  447.             return 0;
  448.         }
  449.         if ((rootlen = strlen(pathcomp)) > 0) {
  450.             int had_trailing_pathsep=FALSE;
  451.  
  452.             if (pathcomp[rootlen-1] == ':') {
  453.                 pathcomp[--rootlen] = '\0';
  454.                 had_trailing_pathsep = TRUE;
  455.             }
  456.             if (stat(pathcomp, &statbuf) || !S_ISDIR(statbuf.st_mode)) {
  457.                 /* path does not exist */
  458.                 if (!create_dirs || !had_trailing_pathsep) {
  459.                     rootlen = 0;
  460.                     return 2;   /* treat as stored file */
  461.                 }
  462. /* GRR:  scan for wildcard characters?  OS-dependent...  if find any, return 2:
  463.  * treat as stored file(s) */
  464.                 /* create the directory (could add loop here to scan pathcomp
  465.                  * and create more than one level, but why really necessary?) */
  466.                 if (MKDIR(pathcomp) == -1) {
  467.                     fprintf(stderr,
  468.                       "checkdir:  can't create extraction directory: %s\n",
  469.                       pathcomp);
  470.                     fflush(stderr);
  471.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  472.                     return 3;  /* failed:  file exists, or 2+ levels required */
  473.                 }
  474.             }
  475.             if ((rootpath = (char *)malloc(rootlen+2)) == NULL) {
  476.                 rootlen = 0;
  477.                 return 10;
  478.             }
  479.             strcpy(rootpath, pathcomp);
  480.             rootpath[rootlen++] = ':';
  481.             rootpath[rootlen] = '\0';
  482.         }
  483.         Trace((stderr, "rootpath now = [%s]\n", rootpath));
  484.         return 0;
  485.     }
  486.  
  487. /*---------------------------------------------------------------------------
  488.     END:  free rootpath, immediately prior to program exit.
  489.   ---------------------------------------------------------------------------*/
  490.  
  491.     if (FUNCTION == END) {
  492.         Trace((stderr, "freeing rootpath\n"));
  493.         if (rootlen > 0)
  494.             free(rootpath);
  495.         return 0;
  496.     }
  497.  
  498.     return 99;  /* should never reach */
  499.  
  500. } /* end function checkdir() */
  501.  
  502.  
  503.  
  504.  
  505.  
  506. /****************************/
  507. /* Function close_outfile() */
  508. /****************************/
  509.  
  510. void close_outfile()
  511. {
  512.     long m_time;
  513.     DateTimeRec dtr;
  514.     ParamBlockRec pbr;
  515.     HParamBlockRec hpbr;
  516.     OSErr err;
  517.  
  518.  
  519.     if (fileno(outfile) == 1)   /* don't attempt to close or set time on stdout */
  520.         return;
  521.  
  522.     fclose(outfile);
  523.  
  524.     /*
  525.      * Macintosh bases all file modification times on the number of seconds
  526.      * elapsed since Jan 1, 1904, 00:00:00.  Therefore, to maintain
  527.      * compatibility with MS-DOS archives, which date from Jan 1, 1980,
  528.      * with NO relation to GMT, the following conversions must be made:
  529.      *      the Year (yr) must be incremented by 1980;
  530.      *      and converted to seconds using the Mac routine Date2Secs(),
  531.      *      almost similar in complexity to the Unix version :-)
  532.      *                                     J. Lee
  533.      */
  534.  
  535.     dtr.year = (((lrec.last_mod_file_date >> 9) & 0x7f) + 1980);
  536.     dtr.month = ((lrec.last_mod_file_date >> 5) & 0x0f);
  537.     dtr.day = (lrec.last_mod_file_date & 0x1f);
  538.  
  539.     dtr.hour = ((lrec.last_mod_file_time >> 11) & 0x1f);
  540.     dtr.minute = ((lrec.last_mod_file_time >> 5) & 0x3f);
  541.     dtr.second = ((lrec.last_mod_file_time & 0x1f) * 2);
  542.  
  543.     Date2Secs(&dtr, (unsigned long *)&m_time);
  544.     c2pstr(filename);
  545.     if (HFSFlag) {
  546.         hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  547.         hpbr.fileParam.ioVRefNum = gnVRefNum;
  548.         hpbr.fileParam.ioDirID = glDirID;
  549.         hpbr.fileParam.ioFDirIndex = 0;
  550.         err = PBHGetFInfo(&hpbr, 0L);
  551.         hpbr.fileParam.ioFlMdDat = m_time;
  552.         if ( !fMacZipped )
  553.             hpbr.fileParam.ioFlCrDat = m_time;
  554.         hpbr.fileParam.ioDirID = glDirID;
  555.         if (err == noErr)
  556.             err = PBHSetFInfo(&hpbr, 0L);
  557.         if (err != noErr)
  558.             printf("error:  can't set the time for %s\n", filename);
  559.     } else {
  560.         pbr.fileParam.ioNamePtr = (StringPtr)filename;
  561.         pbr.fileParam.ioVRefNum = pbr.fileParam.ioFVersNum =
  562.           pbr.fileParam.ioFDirIndex = 0;
  563.         err = PBGetFInfo(&pbr, 0L);
  564.         pbr.fileParam.ioFlMdDat = pbr.fileParam.ioFlCrDat = m_time;
  565.         if (err == noErr)
  566.             err = PBSetFInfo(&pbr, 0L);
  567.         if (err != noErr)
  568.             printf("error:  can't set the time for %s\n", filename);
  569.     }
  570.  
  571.     /* set read-only perms if needed */
  572.     if ((err == noErr) && pInfo->read_only) {
  573.         if (HFSFlag) {
  574.             hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  575.             hpbr.fileParam.ioVRefNum = gnVRefNum;
  576.             hpbr.fileParam.ioDirID = glDirID;
  577.             err = PBHSetFLock(&hpbr, 0);
  578.         } else
  579.             err = SetFLock((ConstStr255Param)filename, 0);
  580.     }
  581.     p2cstr(filename);
  582.  
  583. } /* end function close_outfile() */
  584.  
  585.  
  586.  
  587.  
  588.  
  589. /************************/
  590. /* Function IsHFSDisk() */
  591. /************************/
  592.  
  593. static int IsHFSDisk(short wRefNum)
  594. {
  595.     /* get info about the specified volume */
  596.     if (HFSFlag == true) {
  597.         HParamBlockRec    hpbr;
  598.         Str255 temp;
  599.         short wErr;
  600.  
  601.         hpbr.volumeParam.ioCompletion = 0;
  602.         hpbr.volumeParam.ioNamePtr = temp;
  603.         hpbr.volumeParam.ioVRefNum = wRefNum;
  604.         hpbr.volumeParam.ioVolIndex = 0;
  605.         wErr = PBHGetVInfo(&hpbr, 0);
  606.  
  607.         if (wErr == noErr && hpbr.volumeParam.ioVFSID == 0
  608.             && hpbr.volumeParam.ioVSigWord == 0x4244) {
  609.                 return true;
  610.         }
  611.     }
  612.  
  613.     return false;
  614. } /* IsHFSDisk */
  615.  
  616.  
  617.  
  618.  
  619.  
  620. /************************/
  621. /* Function MacFSTest() */
  622. /************************/
  623.  
  624. void MacFSTest(int vRefNum)
  625. {
  626.     Str255 st;
  627.  
  628.     /* is this machine running HFS file system? */
  629.     if (FSFCBLen <= 0) {
  630.         HFSFlag = false;
  631.     }
  632.     else
  633.     {
  634.         HFSFlag = true;
  635.     }
  636.  
  637.     /* get the file's volume reference number and directory ID */
  638.     if (HFSFlag == true) {
  639.         WDPBRec    wdpb;
  640.         OSErr err = noErr;
  641.  
  642.         if (vRefNum != 0) {
  643.             wdpb.ioCompletion = false;
  644.             wdpb.ioNamePtr = st;
  645.             wdpb.ioWDIndex = 0;
  646.             wdpb.ioVRefNum = vRefNum;
  647.             err = PBHGetVol(&wdpb, false);
  648.  
  649.             if (err == noErr) {
  650.                 wAppVRefNum = wdpb.ioWDVRefNum;
  651.                 lAppDirID = wdpb.ioWDDirID;
  652.             }
  653.         }
  654.  
  655.         /* is the disk we're using formatted for HFS? */
  656.         HFSFlag = IsHFSDisk(wAppVRefNum);
  657.     }
  658.  
  659.     return;
  660. } /* mactest */
  661.  
  662.  
  663.  
  664.  
  665.  
  666. /***********************/
  667. /* Function macmkdir() */
  668. /***********************/
  669.  
  670. int macmkdir(char *path, short nVRefNum, long lDirID)
  671. {
  672.     OSErr    err = -1;
  673.  
  674.     if (path != 0 && strlen(path)<256 && HFSFlag == true) {
  675.         HParamBlockRec    hpbr;
  676.         Str255    st;
  677.  
  678.         c2pstr(path);
  679.         if ((nVRefNum == 0) && (lDirID == 0))
  680.         {
  681.             hpbr.fileParam.ioNamePtr = st;
  682.             hpbr.fileParam.ioCompletion = NULL;
  683.             err = PBHGetVol((WDPBPtr)&hpbr, false);
  684.             nVRefNum = hpbr.wdParam.ioWDVRefNum;
  685.             lDirID = hpbr.wdParam.ioWDDirID;
  686.         }
  687.         else
  688.         {
  689.             err = noErr;
  690.         }
  691.         if (err == noErr) {
  692.             hpbr.fileParam.ioCompletion = NULL;
  693.             hpbr.fileParam.ioVRefNum = nVRefNum;
  694.             hpbr.fileParam.ioDirID = lDirID;
  695.             hpbr.fileParam.ioNamePtr = (StringPtr)path;
  696.             err = PBDirCreate(&hpbr, false);
  697.         }
  698.         p2cstr(path);
  699.     }
  700.  
  701.     return (int)err;
  702. } /* macmkdir */
  703.  
  704.  
  705.  
  706.  
  707.  
  708. /****************************/
  709. /* Function ResolveMacVol() */
  710. /****************************/
  711.  
  712. void ResolveMacVol(short nVRefNum, short *pnVRefNum, long *plDirID, StringPtr pst)
  713. {
  714.     if (HFSFlag)
  715.     {
  716.         WDPBRec  wdpbr;
  717.         Str255   st;
  718.         OSErr    err;
  719.  
  720.         wdpbr.ioCompletion = (ProcPtr)NULL;
  721.         wdpbr.ioNamePtr = st;
  722.         wdpbr.ioVRefNum = nVRefNum;
  723.         wdpbr.ioWDIndex = 0;
  724.         wdpbr.ioWDProcID = 0;
  725.         wdpbr.ioWDVRefNum = 0;
  726.         err = PBGetWDInfo( &wdpbr, false );
  727.         if ( err == noErr )
  728.         {
  729.             if (pnVRefNum)
  730.                 *pnVRefNum = wdpbr.ioWDVRefNum;
  731.             if (plDirID)
  732.                 *plDirID = wdpbr.ioWDDirID;
  733.             if (pst)
  734.                 BlockMove( st, pst, st[0]+1 );
  735.         }
  736.     }
  737.     else
  738.     {
  739.         if (pnVRefNum)
  740.             *pnVRefNum = nVRefNum;
  741.         if (plDirID)
  742.             *plDirID = 0;
  743.         if (pst)
  744.             *pst = 0;
  745.     }
  746. }
  747.  
  748.  
  749.  
  750.  
  751.  
  752. /**********************/
  753. /* Function macopen() */
  754. /**********************/
  755.  
  756. short macopen(char *sz, short nFlags, short nVRefNum, long lDirID)
  757. {
  758.     OSErr   err;
  759.     Str255  st;
  760.     char    chPerms = (!nFlags) ? fsRdPerm : fsRdWrPerm;
  761.     short   nFRefNum;
  762.  
  763.     c2pstr( sz );
  764.     BlockMove( sz, st, sz[0]+1 );
  765.     p2cstr( sz );
  766.     if (HFSFlag)
  767.     {
  768.         if (nFlags > 1)
  769.             err = HOpenRF( nVRefNum, lDirID, st, chPerms, &nFRefNum);
  770.         else
  771.             err = HOpen( nVRefNum, lDirID, st, chPerms, &nFRefNum);
  772.     }
  773.     else
  774.     {
  775.         /*
  776.          * Have to use PBxxx style calls since the high level
  777.          * versions don't support specifying permissions
  778.          */
  779.         ParamBlockRec    pbr;
  780.  
  781.         pbr.ioParam.ioNamePtr = st;
  782.         pbr.ioParam.ioVRefNum = gnVRefNum;
  783.         pbr.ioParam.ioVersNum = 0;
  784.         pbr.ioParam.ioPermssn = chPerms;
  785.         pbr.ioParam.ioMisc = 0;
  786.         if (nFlags >1)
  787.             err = PBOpenRF( &pbr, false );
  788.         else
  789.             err = PBOpen( &pbr, false );
  790.         nFRefNum = pbr.ioParam.ioRefNum;
  791.     }
  792.     if ( err || (nFRefNum == 1) )
  793.         return -1;
  794.     else {
  795.         if ( nFlags )
  796.             SetEOF( nFRefNum, 0 );
  797.         return nFRefNum;
  798.     }
  799. }
  800.  
  801.  
  802.  
  803.  
  804.  
  805. /***********************/
  806. /* Function macfopen() */
  807. /***********************/
  808.  
  809. FILE *macfopen(char *filename, char *mode, short nVRefNum, long lDirID)
  810.     {
  811.         short outfd, fDataFork=TRUE;
  812.         MACINFO mi;
  813.         OSErr err;
  814.  
  815.         fMacZipped = FALSE;
  816.         c2pstr(filename);
  817.         if (extra_field &&
  818.             (lrec.extra_field_length > sizeof(MACINFOMIN)) &&
  819.             (lrec.extra_field_length <= sizeof(MACINFO))) {
  820.             BlockMove(extra_field, &mi, lrec.extra_field_length);
  821.             if ((makeword((uch *)&mi.header) == 1992) &&
  822.                 (makeword((uch *)&mi.data) ==
  823.                     lrec.extra_field_length-sizeof(ZIP_EXTRA_HEADER)) &&
  824.                 (mi.signature == 'JLEE')) {
  825.                 gostCreator = mi.finfo.fdCreator;
  826.                 gostType = mi.finfo.fdType;
  827.                 fDataFork = (mi.flags & 1) ? TRUE : FALSE;
  828.                 fMacZipped = true;
  829.                 /* If it was Zipped w/Mac version, the filename has either */
  830.                 /* a 'd' or 'r' appended.  Remove the d/r when unzipping */
  831.                 filename[0]-=1;
  832.             }
  833.         }
  834.         if (!fMacZipped) {
  835.             if (!aflag)
  836.                 gostType = gostCreator = '\?\?\?\?';
  837.             else {
  838.                 gostCreator = CREATOR;
  839.                 gostType = 'TEXT';
  840.             }
  841.         }
  842.         p2cstr(filename);
  843.  
  844.         if ((outfd = creat(filename, 0)) != -1) {
  845.             if (fMacZipped) {
  846.                 c2pstr(filename);
  847.                 if (HFSFlag) {
  848.                     HParamBlockRec   hpbr;
  849.  
  850.                     hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  851.                     hpbr.fileParam.ioVRefNum = gnVRefNum;
  852.                     hpbr.fileParam.ioDirID = glDirID;
  853.                     hpbr.fileParam.ioFlFndrInfo = mi.finfo;
  854.                     hpbr.fileParam.ioFlCrDat = mi.lCrDat;
  855.                     hpbr.fileParam.ioFlMdDat = mi.lMdDat;
  856.                     err = PBHSetFInfo(&hpbr, 0);
  857.                 } else {
  858.                     err = SetFInfo((StringPtr)filename , 0, &mi.finfo);
  859.                 }
  860.                 p2cstr(filename);
  861.             }
  862.             outfd = open(filename, (fDataFork) ? 1 : 2);
  863.         }
  864.  
  865.         if (outfd == -1)
  866.             return NULL;
  867.         else
  868.             return (FILE *)outfd;
  869.     }
  870.  
  871.  
  872.  
  873.  
  874.  
  875. /***********************/
  876. /* Function maccreat() */
  877. /***********************/
  878.  
  879. short maccreat(char *sz, short nVRefNum, long lDirID, OSType ostCreator, OSType ostType)
  880. {
  881.     OSErr   err;
  882.     Str255  st;
  883.     FInfo   fi;
  884.  
  885.     c2pstr( sz );
  886.     BlockMove( sz, st, sz[0]+1 );
  887.     p2cstr( sz );
  888.     if (HFSFlag)
  889.     {
  890.         err = HGetFInfo( nVRefNum, lDirID, st, &fi );
  891.         if (err == fnfErr)
  892.             err = HCreate( nVRefNum, lDirID, st, ostCreator, ostType );
  893.         else if (err == noErr)
  894.         {
  895.             fi.fdCreator = ostCreator;
  896.             fi.fdType = ostType;
  897.             err = HSetFInfo( nVRefNum, lDirID, st, &fi );
  898.         }
  899.     }
  900.     else
  901.     {
  902.         err = GetFInfo( st, nVRefNum, &fi );
  903.         if (err == fnfErr)
  904.             err = Create( st, nVRefNum, ostCreator, ostType );
  905.         else if (err == noErr)
  906.         {
  907.             fi.fdCreator = ostCreator;
  908.             fi.fdType = ostType;
  909.             err = SetFInfo( st, nVRefNum, &fi );
  910.         }
  911.     }
  912.     if (err == noErr)
  913.         return noErr;
  914.     else
  915.         return -1;
  916. }
  917.  
  918.  
  919.  
  920.  
  921.  
  922. /**********************/
  923. /* Function macread() */
  924. /**********************/
  925.  
  926. short macread(short nFRefNum, char *pb, unsigned cb)
  927. {
  928.     long    lcb = cb;
  929.  
  930.     (void)FSRead( nFRefNum, &lcb, pb );
  931.  
  932.     return (short)lcb;
  933. }
  934.  
  935.  
  936.  
  937.  
  938.  
  939. /***********************/
  940. /* Function macwrite() */
  941. /***********************/
  942.  
  943. long macwrite(short nFRefNum, char *pb, unsigned cb)
  944. {
  945.     long    lcb = cb;
  946.  
  947. #ifdef THINK_C
  948.     if ( (nFRefNum == 1) )
  949.         screenDump( pb, lcb );
  950.     else
  951. #endif
  952.         (void)FSWrite( nFRefNum, &lcb, pb );
  953.  
  954.     return (long)lcb;
  955. }
  956.  
  957.  
  958.  
  959.  
  960.  
  961. /***********************/
  962. /* Function macclose() */
  963. /***********************/
  964.  
  965. short macclose(short nFRefNum)
  966. {
  967.     return FSClose( nFRefNum );
  968. }
  969.  
  970.  
  971.  
  972.  
  973.  
  974. /***********************/
  975. /* Function maclseek() */
  976. /***********************/
  977.  
  978. long maclseek(short nFRefNum, long lib, short nMode)
  979. {
  980.     ParamBlockRec   pbr;
  981.  
  982.     if (nMode == SEEK_SET)
  983.         nMode = fsFromStart;
  984.     else if (nMode == SEEK_CUR)
  985.         nMode = fsFromMark;
  986.     else if (nMode == SEEK_END)
  987.         nMode = fsFromLEOF;
  988.     pbr.ioParam.ioRefNum = nFRefNum;
  989.     pbr.ioParam.ioPosMode = nMode;
  990.     pbr.ioParam.ioPosOffset = lib;
  991.     (void)PBSetFPos(&pbr, 0);
  992.     return pbr.ioParam.ioPosOffset;
  993. }
  994.  
  995. #endif /* MACOS */
  996.