home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / vrac / unzip512.zip / mac / mac.c < prev    next >
C/C++ Source or Header  |  1994-08-16  |  32KB  |  1,068 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.     if (FUNCTION == INIT) {
  433.         Trace((stderr, "initializing buildpath to "));
  434.         if ((buildpath = (char *)malloc(strlen(filename)+rootlen+2)) == NULL)
  435.             return 10;
  436.         if ((rootlen > 0) && !renamed_fullpath) {
  437.             strcpy(buildpath, rootpath);
  438.             end = buildpath + rootlen;
  439.         } else {
  440.             *buildpath = '\0';
  441.             end = buildpath;
  442.         }
  443.         Trace((stderr, "[%s]\n", buildpath));
  444.         return 0;
  445.     }
  446.  
  447. /*---------------------------------------------------------------------------
  448.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  449.     sary; else assume it's a zipfile member and return.  This path segment
  450.     gets used in extracting all members from every zipfile specified on the
  451.     command line.
  452.   ---------------------------------------------------------------------------*/
  453.  
  454. #if (!defined(SFX) || defined(SFX_EXDIR))
  455.     if (FUNCTION == ROOT) {
  456.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  457.         if (pathcomp == NULL) {
  458.             rootlen = 0;
  459.             return 0;
  460.         }
  461.         if ((rootlen = strlen(pathcomp)) > 0) {
  462.             int had_trailing_pathsep=FALSE;
  463.  
  464.             if (pathcomp[rootlen-1] == ':') {
  465.                 pathcomp[--rootlen] = '\0';
  466.                 had_trailing_pathsep = TRUE;
  467.             }
  468.             if (rootlen > 0 && (stat(pathcomp, &statbuf) ||
  469.                 !S_ISDIR(statbuf.st_mode)))          /* path does not exist */
  470.             {
  471.                 if (!create_dirs                     /* || iswild(pathcomp) */
  472. #ifdef OLD_EXDIR
  473.                                  || !had_trailing_pathsep
  474. #endif
  475.                                                          ) {
  476.                     rootlen = 0;
  477.                     return 2;   /* treat as stored file */
  478.                 }
  479.                 /* create the directory (could add loop here to scan pathcomp
  480.                  * and create more than one level, but why really necessary?) */
  481.                 if (MKDIR(pathcomp) == -1) {
  482.                     fprintf(stderr,
  483.                       "checkdir:  can't create extraction directory: %s\n",
  484.                       pathcomp);
  485.                     fflush(stderr);
  486.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  487.                     return 3;  /* failed:  file exists, or 2+ levels required */
  488.                 }
  489.             }
  490.             if ((rootpath = (char *)malloc(rootlen+2)) == NULL) {
  491.                 rootlen = 0;
  492.                 return 10;
  493.             }
  494.             strcpy(rootpath, pathcomp);
  495.             rootpath[rootlen++] = ':';
  496.             rootpath[rootlen] = '\0';
  497.         }
  498.         Trace((stderr, "rootpath now = [%s]\n", rootpath));
  499.         return 0;
  500.     }
  501. #endif /* !SFX || SFX_EXDIR */
  502.  
  503. /*---------------------------------------------------------------------------
  504.     END:  free rootpath, immediately prior to program exit.
  505.   ---------------------------------------------------------------------------*/
  506.  
  507.     if (FUNCTION == END) {
  508.         Trace((stderr, "freeing rootpath\n"));
  509.         if (rootlen > 0)
  510.             free(rootpath);
  511.         return 0;
  512.     }
  513.  
  514.     return 99;  /* should never reach */
  515.  
  516. } /* end function checkdir() */
  517.  
  518.  
  519.  
  520.  
  521.  
  522. /****************************/
  523. /* Function close_outfile() */
  524. /****************************/
  525.  
  526. void close_outfile()
  527. {
  528.     long m_time;
  529.     DateTimeRec dtr;
  530.     ParamBlockRec pbr;
  531.     HParamBlockRec hpbr;
  532.     OSErr err;
  533.  
  534.  
  535.     if (fileno(outfile) == 1)   /* don't attempt to close or set time on stdout */
  536.         return;
  537.  
  538.     fclose(outfile);
  539.  
  540.     /*
  541.      * Macintosh bases all file modification times on the number of seconds
  542.      * elapsed since Jan 1, 1904, 00:00:00.  Therefore, to maintain
  543.      * compatibility with MS-DOS archives, which date from Jan 1, 1980,
  544.      * with NO relation to GMT, the following conversions must be made:
  545.      *      the Year (yr) must be incremented by 1980;
  546.      *      and converted to seconds using the Mac routine Date2Secs(),
  547.      *      almost similar in complexity to the Unix version :-)
  548.      *                                     J. Lee
  549.      */
  550.  
  551.     dtr.year = (((lrec.last_mod_file_date >> 9) & 0x7f) + 1980);
  552.     dtr.month = ((lrec.last_mod_file_date >> 5) & 0x0f);
  553.     dtr.day = (lrec.last_mod_file_date & 0x1f);
  554.  
  555.     dtr.hour = ((lrec.last_mod_file_time >> 11) & 0x1f);
  556.     dtr.minute = ((lrec.last_mod_file_time >> 5) & 0x3f);
  557.     dtr.second = ((lrec.last_mod_file_time & 0x1f) * 2);
  558.  
  559.     Date2Secs(&dtr, (unsigned long *)&m_time);
  560.     c2pstr(filename);
  561.     if (HFSFlag) {
  562.         hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  563.         hpbr.fileParam.ioVRefNum = gnVRefNum;
  564.         hpbr.fileParam.ioDirID = glDirID;
  565.         hpbr.fileParam.ioFDirIndex = 0;
  566.         err = PBHGetFInfo(&hpbr, 0L);
  567.         hpbr.fileParam.ioFlMdDat = m_time;
  568.         if ( !fMacZipped )
  569.             hpbr.fileParam.ioFlCrDat = m_time;
  570.         hpbr.fileParam.ioDirID = glDirID;
  571.         if (err == noErr)
  572.             err = PBHSetFInfo(&hpbr, 0L);
  573.         if (err != noErr)
  574.             printf("error:  can't set the time for %s\n", filename);
  575.     } else {
  576.         pbr.fileParam.ioNamePtr = (StringPtr)filename;
  577.         pbr.fileParam.ioVRefNum = pbr.fileParam.ioFVersNum =
  578.           pbr.fileParam.ioFDirIndex = 0;
  579.         err = PBGetFInfo(&pbr, 0L);
  580.         pbr.fileParam.ioFlMdDat = pbr.fileParam.ioFlCrDat = m_time;
  581.         if (err == noErr)
  582.             err = PBSetFInfo(&pbr, 0L);
  583.         if (err != noErr)
  584.             printf("error:  can't set the time for %s\n", filename);
  585.     }
  586.  
  587.     /* set read-only perms if needed */
  588.     if ((err == noErr) && pInfo->read_only) {
  589.         if (HFSFlag) {
  590.             hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  591.             hpbr.fileParam.ioVRefNum = gnVRefNum;
  592.             hpbr.fileParam.ioDirID = glDirID;
  593.             err = PBHSetFLock(&hpbr, 0);
  594.         } else
  595.             err = SetFLock((ConstStr255Param)filename, 0);
  596.     }
  597.     p2cstr(filename);
  598.  
  599. } /* end function close_outfile() */
  600.  
  601.  
  602.  
  603.  
  604.  
  605. #ifndef SFX
  606.  
  607. /************************/
  608. /*  Function version()  */
  609. /************************/
  610.  
  611. void version()
  612. {
  613.     extern char Far  CompiledWith[];
  614. #if 0
  615.     char buf[40];
  616. #endif
  617.  
  618.     printf(LoadFarString(CompiledWith),
  619.  
  620. #ifdef __GNUC__
  621.       "gcc ", __VERSION__,
  622. #else
  623. #  if 0
  624.       "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
  625. #  else
  626. #  ifdef THINK_C
  627.       "Think C", "",
  628. #  else
  629. #  ifdef MPW
  630.       "MPW C", "",
  631. #  else
  632.       "unknown compiler", "",
  633. #  endif
  634. #  endif
  635. #  endif
  636. #endif
  637.  
  638.       "MacOS",
  639.  
  640. #if defined(foobar) || defined(FOOBAR)
  641.       " (Foo BAR)",    /* hardware or OS version */
  642. #else
  643.       "",
  644. #endif /* Foo BAR */
  645.  
  646. #ifdef __DATE__
  647.       " on ", __DATE__
  648. #else
  649.       "", ""
  650. #endif
  651.       );
  652.  
  653. } /* end function version() */
  654.  
  655. #endif /* !SFX */
  656.  
  657.  
  658.  
  659.  
  660.  
  661. /************************/
  662. /* Function IsHFSDisk() */
  663. /************************/
  664.  
  665. static int IsHFSDisk(short wRefNum)
  666. {
  667.     /* get info about the specified volume */
  668.     if (HFSFlag == true) {
  669.         HParamBlockRec    hpbr;
  670.         Str255 temp;
  671.         short wErr;
  672.  
  673.         hpbr.volumeParam.ioCompletion = 0;
  674.         hpbr.volumeParam.ioNamePtr = temp;
  675.         hpbr.volumeParam.ioVRefNum = wRefNum;
  676.         hpbr.volumeParam.ioVolIndex = 0;
  677.         wErr = PBHGetVInfo(&hpbr, 0);
  678.  
  679.         if (wErr == noErr && hpbr.volumeParam.ioVFSID == 0
  680.             && hpbr.volumeParam.ioVSigWord == 0x4244) {
  681.                 return true;
  682.         }
  683.     }
  684.  
  685.     return false;
  686. } /* IsHFSDisk */
  687.  
  688.  
  689.  
  690.  
  691.  
  692. /************************/
  693. /* Function MacFSTest() */
  694. /************************/
  695.  
  696. void MacFSTest(int vRefNum)
  697. {
  698.     Str255 st;
  699.  
  700.     /* is this machine running HFS file system? */
  701.     if (FSFCBLen <= 0) {
  702.         HFSFlag = false;
  703.     }
  704.     else
  705.     {
  706.         HFSFlag = true;
  707.     }
  708.  
  709.     /* get the file's volume reference number and directory ID */
  710.     if (HFSFlag == true) {
  711.         WDPBRec    wdpb;
  712.         OSErr err = noErr;
  713.  
  714.         if (vRefNum != 0) {
  715.             wdpb.ioCompletion = false;
  716.             wdpb.ioNamePtr = st;
  717.             wdpb.ioWDIndex = 0;
  718.             wdpb.ioVRefNum = vRefNum;
  719.             err = PBHGetVol(&wdpb, false);
  720.  
  721.             if (err == noErr) {
  722.                 wAppVRefNum = wdpb.ioWDVRefNum;
  723.                 lAppDirID = wdpb.ioWDDirID;
  724.             }
  725.         }
  726.  
  727.         /* is the disk we're using formatted for HFS? */
  728.         HFSFlag = IsHFSDisk(wAppVRefNum);
  729.     }
  730.  
  731.     return;
  732. } /* mactest */
  733.  
  734.  
  735.  
  736.  
  737.  
  738. /***********************/
  739. /* Function macmkdir() */
  740. /***********************/
  741.  
  742. int macmkdir(char *path, short nVRefNum, long lDirID)
  743. {
  744.     OSErr    err = -1;
  745.  
  746.     if (path != 0 && strlen(path)<256 && HFSFlag == true) {
  747.         HParamBlockRec    hpbr;
  748.         Str255    st;
  749.  
  750.         c2pstr(path);
  751.         if ((nVRefNum == 0) && (lDirID == 0))
  752.         {
  753.             hpbr.fileParam.ioNamePtr = st;
  754.             hpbr.fileParam.ioCompletion = NULL;
  755.             err = PBHGetVol((WDPBPtr)&hpbr, false);
  756.             nVRefNum = hpbr.wdParam.ioWDVRefNum;
  757.             lDirID = hpbr.wdParam.ioWDDirID;
  758.         }
  759.         else
  760.         {
  761.             err = noErr;
  762.         }
  763.         if (err == noErr) {
  764.             hpbr.fileParam.ioCompletion = NULL;
  765.             hpbr.fileParam.ioVRefNum = nVRefNum;
  766.             hpbr.fileParam.ioDirID = lDirID;
  767.             hpbr.fileParam.ioNamePtr = (StringPtr)path;
  768.             err = PBDirCreate(&hpbr, false);
  769.         }
  770.         p2cstr(path);
  771.     }
  772.  
  773.     return (int)err;
  774. } /* macmkdir */
  775.  
  776.  
  777.  
  778.  
  779.  
  780. /****************************/
  781. /* Function ResolveMacVol() */
  782. /****************************/
  783.  
  784. void ResolveMacVol(short nVRefNum, short *pnVRefNum, long *plDirID, StringPtr pst)
  785. {
  786.     if (HFSFlag)
  787.     {
  788.         WDPBRec  wdpbr;
  789.         Str255   st;
  790.         OSErr    err;
  791.  
  792.         wdpbr.ioCompletion = (ProcPtr)NULL;
  793.         wdpbr.ioNamePtr = st;
  794.         wdpbr.ioVRefNum = nVRefNum;
  795.         wdpbr.ioWDIndex = 0;
  796.         wdpbr.ioWDProcID = 0;
  797.         wdpbr.ioWDVRefNum = 0;
  798.         err = PBGetWDInfo( &wdpbr, false );
  799.         if ( err == noErr )
  800.         {
  801.             if (pnVRefNum)
  802.                 *pnVRefNum = wdpbr.ioWDVRefNum;
  803.             if (plDirID)
  804.                 *plDirID = wdpbr.ioWDDirID;
  805.             if (pst)
  806.                 BlockMove( st, pst, st[0]+1 );
  807.         }
  808.     }
  809.     else
  810.     {
  811.         if (pnVRefNum)
  812.             *pnVRefNum = nVRefNum;
  813.         if (plDirID)
  814.             *plDirID = 0;
  815.         if (pst)
  816.             *pst = 0;
  817.     }
  818. }
  819.  
  820.  
  821.  
  822.  
  823.  
  824. /**********************/
  825. /* Function macopen() */
  826. /**********************/
  827.  
  828. short macopen(char *sz, short nFlags, short nVRefNum, long lDirID)
  829. {
  830.     OSErr   err;
  831.     Str255  st;
  832.     char    chPerms = (!nFlags) ? fsRdPerm : fsRdWrPerm;
  833.     short   nFRefNum;
  834.  
  835.     c2pstr( sz );
  836.     BlockMove( sz, st, sz[0]+1 );
  837.     p2cstr( sz );
  838.     if (HFSFlag)
  839.     {
  840.         if (nFlags > 1)
  841.             err = HOpenRF( nVRefNum, lDirID, st, chPerms, &nFRefNum);
  842.         else
  843.             err = HOpen( nVRefNum, lDirID, st, chPerms, &nFRefNum);
  844.     }
  845.     else
  846.     {
  847.         /*
  848.          * Have to use PBxxx style calls since the high level
  849.          * versions don't support specifying permissions
  850.          */
  851.         ParamBlockRec    pbr;
  852.  
  853.         pbr.ioParam.ioNamePtr = st;
  854.         pbr.ioParam.ioVRefNum = gnVRefNum;
  855.         pbr.ioParam.ioVersNum = 0;
  856.         pbr.ioParam.ioPermssn = chPerms;
  857.         pbr.ioParam.ioMisc = 0;
  858.         if (nFlags >1)
  859.             err = PBOpenRF( &pbr, false );
  860.         else
  861.             err = PBOpen( &pbr, false );
  862.         nFRefNum = pbr.ioParam.ioRefNum;
  863.     }
  864.     if ( err || (nFRefNum == 1) )
  865.         return -1;
  866.     else {
  867.         if ( nFlags )
  868.             SetEOF( nFRefNum, 0 );
  869.         return nFRefNum;
  870.     }
  871. }
  872.  
  873.  
  874.  
  875.  
  876.  
  877. /***********************/
  878. /* Function macfopen() */
  879. /***********************/
  880.  
  881. FILE *macfopen(char *filename, char *mode, short nVRefNum, long lDirID)
  882.     {
  883.         short outfd, fDataFork=TRUE;
  884.         MACINFO mi;
  885.         OSErr err;
  886.  
  887.         fMacZipped = FALSE;
  888.         c2pstr(filename);
  889.         if (extra_field &&
  890.             (lrec.extra_field_length > sizeof(MACINFOMIN)) &&
  891.             (lrec.extra_field_length <= sizeof(MACINFO))) {
  892.             BlockMove(extra_field, &mi, lrec.extra_field_length);
  893.             if ((makeword((uch *)&mi.header) == 1992) &&
  894.                 (makeword((uch *)&mi.data) ==
  895.                     lrec.extra_field_length-sizeof(ZIP_EXTRA_HEADER)) &&
  896.                 (mi.signature == 'JLEE')) {
  897.                 gostCreator = mi.finfo.fdCreator;
  898.                 gostType = mi.finfo.fdType;
  899.                 fDataFork = (mi.flags & 1) ? TRUE : FALSE;
  900.                 fMacZipped = true;
  901.                 /* If it was Zipped w/Mac version, the filename has either */
  902.                 /* a 'd' or 'r' appended.  Remove the d/r when unzipping */
  903.                 filename[0]-=1;
  904.             }
  905.         }
  906.         if (!fMacZipped) {
  907.             if (!aflag)
  908.                 gostType = gostCreator = '\?\?\?\?';
  909.             else {
  910.                 gostCreator = CREATOR;
  911.                 gostType = 'TEXT';
  912.             }
  913.         }
  914.         p2cstr(filename);
  915.  
  916.         if ((outfd = creat(filename, 0)) != -1) {
  917.             if (fMacZipped) {
  918.                 c2pstr(filename);
  919.                 if (HFSFlag) {
  920.                     HParamBlockRec   hpbr;
  921.  
  922.                     hpbr.fileParam.ioNamePtr = (StringPtr)filename;
  923.                     hpbr.fileParam.ioVRefNum = gnVRefNum;
  924.                     hpbr.fileParam.ioDirID = glDirID;
  925.                     hpbr.fileParam.ioFlFndrInfo = mi.finfo;
  926.                     hpbr.fileParam.ioFlCrDat = mi.lCrDat;
  927.                     hpbr.fileParam.ioFlMdDat = mi.lMdDat;
  928.                     err = PBHSetFInfo(&hpbr, 0);
  929.                 } else {
  930.                     err = SetFInfo((StringPtr)filename , 0, &mi.finfo);
  931.                 }
  932.                 p2cstr(filename);
  933.             }
  934.             outfd = open(filename, (fDataFork) ? 1 : 2);
  935.         }
  936.  
  937.         if (outfd == -1)
  938.             return NULL;
  939.         else
  940.             return (FILE *)outfd;
  941.     }
  942.  
  943.  
  944.  
  945.  
  946.  
  947. /***********************/
  948. /* Function maccreat() */
  949. /***********************/
  950.  
  951. short maccreat(char *sz, short nVRefNum, long lDirID, OSType ostCreator, OSType ostType)
  952. {
  953.     OSErr   err;
  954.     Str255  st;
  955.     FInfo   fi;
  956.  
  957.     c2pstr( sz );
  958.     BlockMove( sz, st, sz[0]+1 );
  959.     p2cstr( sz );
  960.     if (HFSFlag)
  961.     {
  962.         err = HGetFInfo( nVRefNum, lDirID, st, &fi );
  963.         if (err == fnfErr)
  964.             err = HCreate( nVRefNum, lDirID, st, ostCreator, ostType );
  965.         else if (err == noErr)
  966.         {
  967.             fi.fdCreator = ostCreator;
  968.             fi.fdType = ostType;
  969.             err = HSetFInfo( nVRefNum, lDirID, st, &fi );
  970.         }
  971.     }
  972.     else
  973.     {
  974.         err = GetFInfo( st, nVRefNum, &fi );
  975.         if (err == fnfErr)
  976.             err = Create( st, nVRefNum, ostCreator, ostType );
  977.         else if (err == noErr)
  978.         {
  979.             fi.fdCreator = ostCreator;
  980.             fi.fdType = ostType;
  981.             err = SetFInfo( st, nVRefNum, &fi );
  982.         }
  983.     }
  984.     if (err == noErr)
  985.         return noErr;
  986.     else
  987.         return -1;
  988. }
  989.  
  990.  
  991.  
  992.  
  993.  
  994. /**********************/
  995. /* Function macread() */
  996. /**********************/
  997.  
  998. short macread(short nFRefNum, char *pb, unsigned cb)
  999. {
  1000.     long    lcb = cb;
  1001.  
  1002.     (void)FSRead( nFRefNum, &lcb, pb );
  1003.  
  1004.     return (short)lcb;
  1005. }
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011. /***********************/
  1012. /* Function macwrite() */
  1013. /***********************/
  1014.  
  1015. long macwrite(short nFRefNum, char *pb, unsigned cb)
  1016. {
  1017.     long    lcb = cb;
  1018.  
  1019. #ifdef THINK_C
  1020.     if ( (nFRefNum == 1) )
  1021.         screenDump( pb, lcb );
  1022.     else
  1023. #endif
  1024.         (void)FSWrite( nFRefNum, &lcb, pb );
  1025.  
  1026.     return (long)lcb;
  1027. }
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033. /***********************/
  1034. /* Function macclose() */
  1035. /***********************/
  1036.  
  1037. short macclose(short nFRefNum)
  1038. {
  1039.     return FSClose( nFRefNum );
  1040. }
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046. /***********************/
  1047. /* Function maclseek() */
  1048. /***********************/
  1049.  
  1050. long maclseek(short nFRefNum, long lib, short nMode)
  1051. {
  1052.     ParamBlockRec   pbr;
  1053.  
  1054.     if (nMode == SEEK_SET)
  1055.         nMode = fsFromStart;
  1056.     else if (nMode == SEEK_CUR)
  1057.         nMode = fsFromMark;
  1058.     else if (nMode == SEEK_END)
  1059.         nMode = fsFromLEOF;
  1060.     pbr.ioParam.ioRefNum = nFRefNum;
  1061.     pbr.ioParam.ioPosMode = nMode;
  1062.     pbr.ioParam.ioPosOffset = lib;
  1063.     (void)PBSetFPos(&pbr, 0);
  1064.     return pbr.ioParam.ioPosOffset;
  1065. }
  1066.  
  1067. #endif /* MACOS */
  1068.