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