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