home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / qdos / qdos.c < prev    next >
C/C++ Source or Header  |  1997-10-21  |  30KB  |  949 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   qdos.c
  4.  
  5.   QDOS-specific routines for use with Info-ZIP's UnZip 5.3 and later.
  6.  
  7.   Contains:  Qstrfix()
  8.              QFilename()
  9.              QMatch()
  10.              chowner()
  11.              Qgetch()
  12.              QReturn()
  13.              LastDir()
  14.              do_wild()           <-- generic enough to put in file_io.c?
  15.              mapattr()
  16.              mapname()
  17.              checkdir()
  18.              qfix()
  19.              close_outfile()
  20.              stamp_file()
  21.              getp()
  22.              version()
  23.  
  24.   ---------------------------------------------------------------------------*/
  25.  
  26. #define UNZIP_INTERNAL
  27.  
  28. #include "unzip.h"
  29. #include "crypt.h"
  30. #include "ttyio.h"
  31. #include <dirent.h>
  32. #include "izqdos.h"
  33. #include "version.h"
  34.  
  35. char _prog_name[] = "UnZip";
  36. /* sorrid hack at request of GRR follows; hope the compiler stays kind to us */
  37. char _version[] = {UZ_MAJORVER+'0','.',UZ_MINORVER+'0',PATCHLEVEL+'0'};
  38. char _extra[] = " " BETALEVEL;
  39. char _copyright[] = "(c) Info-ZIP Group";
  40. char *  _endmsg = NULL;
  41. long _stack = 8*1024;         /* huge stack (for qdos) */
  42.  
  43. extern void consetup_title(chanid_t,struct WINDOWDEF *);
  44. void (*_consetup)(chanid_t,struct WINDOWDEF *) = consetup_title;
  45.  
  46. struct WINDOWDEF _condetails =
  47. {
  48.     2,
  49.     1,
  50.     0,
  51.     7,
  52.     500,
  53.     220,
  54.     2,
  55.     30
  56. };
  57.  
  58.  
  59. static jobid_t chowner(chanid_t chan)
  60. {
  61.     extern char *_sys_var;
  62.     char *scht;
  63.     long *cdb;
  64.     long jid;
  65.  
  66.     scht = *((char **)(_sys_var + 0x78));
  67.     cdb = *(long **)((long *)scht  + (chan & 0xffff));
  68.     jid = *(cdb + 2);
  69.     return jid;
  70. }
  71.  
  72. int QReturn(int err)
  73. {
  74.     jobid_t me,you;
  75.  
  76.     me = getpid();
  77.     you = chowner(getchid(0));
  78.  
  79.     if((me == you) && ((qlflag & 4) == 0))
  80.     {
  81.         if(isatty(0) && isatty(2) && qlwait)
  82.         {
  83.             char c = 0;
  84.             fputs("Press a key to exit", stderr);
  85.             if((io_fbyte(getchid(0), qlwait, &c) == 0) && c == 27)
  86.             {
  87.                 io_fbyte(getchid(0), -1, &c);
  88.             }
  89.         }
  90.     }
  91.     exit(err);
  92. }
  93.  
  94. #ifndef FUNZIP
  95.  
  96. static int created_dir;        /* used in mapname(), checkdir() */
  97. static int renamed_fullpath;   /* ditto */
  98.  
  99. char *Qstrfix (char *p)
  100. {
  101.     char *q;
  102.     for (q = p; (q = strstr(q, ".zip"));)
  103.     {
  104.         memcpy(q, "_zip", 4);
  105.         q += 4;
  106.     }
  107.     return p;
  108. }
  109.  
  110. void QFilename(char *f)
  111. {
  112.     char *o,*p,*q = strdup(f);
  113.     p = q;
  114.  
  115.     if(*q == '.' && *(q+1) == '/') q += 2;
  116.     o = q;
  117.  
  118.     for(;*q;q++)
  119.     {
  120.         if(*q == '/') *q = '_';
  121.         if((qlflag & 1) == 0)
  122.         {
  123.             if(*q == '.') *q = '_';
  124.         }
  125.     }
  126.     strcpy(f,o);
  127.     free(p);
  128. }
  129.  
  130. int QMatch(uch c1, uch c2)
  131. {
  132.     int m =0;
  133.  
  134.     if(c1 != c2)
  135.     {
  136.         if(c1 == '_' && (c2 == '.' || c2 == '/'))
  137.         {
  138.             m = 1;
  139.         }
  140.     }
  141.     else
  142.     {
  143.         m = 1;
  144.     }
  145.     return m;
  146. }
  147.  
  148.  
  149. int Qgetch(void)
  150. {
  151.     char ch;
  152.  
  153.     if(io_fbyte(getchid(0), -1, &ch) < 0)
  154.     {
  155.         return EOF;
  156.     }
  157.     else
  158.     {
  159.         return (int) ch;
  160.     }
  161. }
  162.  
  163. #ifndef SFX
  164. char *LastDir(char *ws)
  165. {
  166.     char *p;
  167.     char *q = ws;
  168.     struct stat s;
  169.  
  170.     for(p = ws; *p; p++)
  171.     {
  172.         if(*p == '_')
  173.         {
  174.             char c;
  175.  
  176.             p++;
  177.             c = *p;
  178.             *p = 0;
  179.             if(stat(ws, &s) == 0 && S_ISDIR(s.st_mode))
  180.             {
  181.                 q = p;
  182.             }
  183.             *p = c;
  184.         }
  185.     }
  186.     return q;
  187. }
  188.  
  189.  
  190. /**********************/
  191. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  192. /**********************/
  193.  
  194. char *do_wild(__G__ wildspec)
  195.     __GDEF
  196.     char *wildspec;         /* only used first time on a given dir */
  197. {
  198.     static DIR *dir = (DIR *)NULL;
  199.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  200.     static int firstcall=TRUE, have_dirname, dirnamelen;
  201.     struct dirent *file;
  202.     char basedir[40];
  203.  
  204.     /* Even when we're just returning wildspec, we *always* do so in
  205.      * matchname[]--calling routine is allowed to append four characters
  206.      * to the returned string, and wildspec may be a pointer to argv[].
  207.      */
  208.     if (firstcall) {        /* first call:  must initialize everything */
  209.         char *ws = NULL, *us = NULL;
  210.  
  211.         firstcall = FALSE;
  212.  
  213.         /* break the wildspec into a directory part and a wildcard filename */
  214.  
  215.         ws = (char *) iswild(wildspec);
  216.         us = LastDir(wildspec);
  217.  
  218.         if(us == wildspec || us > ws)
  219.         {
  220.             dirname = basedir;
  221.             getcwd(basedir, sizeof(basedir)-1);
  222.             dirnamelen = strlen(basedir);
  223.             have_dirname = FALSE;
  224.             wildname = wildspec;
  225.         } else {
  226.             wildname = us;     /* point at character after '/' */
  227.             dirnamelen = wildname - wildspec;
  228.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  229.                 Info(slide, 0x201, ((char *)slide,
  230.                   "warning:  cannot allocate wildcard buffers\n"));
  231.                 strcpy(matchname, wildspec);
  232.                 return matchname;   /* but maybe filespec was not a wildcard */
  233.             }
  234.             strncpy(dirname, wildspec, dirnamelen);
  235.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  236.             have_dirname = TRUE;
  237.         }
  238.  
  239.         if ((dir = opendir(dirname)) != (DIR *)NULL) {
  240.             while ((file = readdir(dir)) != (struct dirent *)NULL) {
  241.                 if (match(file->d_name, wildname, 2)) {  /* 0 == case sens. */
  242.                     if (have_dirname) {
  243.                         strcpy(matchname, dirname);
  244.                         strcpy(matchname+dirnamelen, file->d_name);
  245.                     } else
  246.                         strcpy(matchname, file->d_name);
  247.                     return matchname;
  248.                 }
  249.             }
  250.             /* if we get to here directory is exhausted, so close it */
  251.             closedir(dir);
  252.             dir = (DIR *)NULL;
  253.         }
  254.  
  255.         /* return the raw wildspec in case that works (e.g., directory not
  256.          * searchable, but filespec was not wild and file is readable) */
  257.         strcpy(matchname, wildspec);
  258.         return matchname;
  259.     }
  260.  
  261.     /* last time through, might have failed opendir but returned raw wildspec */
  262.     if (dir == (DIR *)NULL) {
  263.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  264.         if (have_dirname)
  265.             free(dirname);
  266.         return (char *)NULL;
  267.     }
  268.  
  269.     /* If we've gotten this far, we've read and matched at least one entry
  270.      * successfully (in a previous call), so dirname has been copied into
  271.      * matchname already.
  272.      */
  273.     while ((file = readdir(dir)) != (struct dirent *)NULL)
  274.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  275.             if (have_dirname) {
  276.                 /* strcpy(matchname, dirname); */
  277.                 strcpy(matchname+dirnamelen, file->d_name);
  278.             } else
  279.                 strcpy(matchname, file->d_name);
  280.             return matchname;
  281.         }
  282.  
  283.     closedir(dir);     /* have read at least one dir entry; nothing left */
  284.     dir = (DIR *)NULL;
  285.     firstcall = TRUE;  /* reset for new wildspec */
  286.     if (have_dirname)
  287.         free(dirname);
  288.     return (char *)NULL;
  289.  
  290. } /* end function do_wild() */
  291.  
  292. #endif /* !SFX */
  293.  
  294.  
  295.  
  296.  
  297.  
  298. /**********************/
  299. /* Function mapattr() */
  300. /**********************/
  301. int mapattr(__G)
  302.     __GDEF
  303. {
  304.     ulg tmp = G.crec.external_file_attributes;
  305.  
  306.     switch (G.pInfo->hostnum) {
  307.         case QDOS_:
  308.         case UNIX_:
  309.         case VMS_:
  310.         case ACORN_:
  311.         case ATARI_:
  312.         case BEOS_:
  313.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  314.             return 0;
  315.         case AMIGA_:
  316.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  317.             G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  318.             break;
  319.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  320.         case FS_FAT_:
  321.         case FS_HPFS_:
  322.         case FS_NTFS_:
  323.         case MAC_:
  324.         case TOPS20_:
  325.         default:
  326.             tmp = !(tmp & 1) << 1;   /* read-only bit --> write perms bits */
  327.             G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  328.             break;
  329.     } /* end switch (host-OS-created-by) */
  330.  
  331.     /* for originating systems with no concept of "group," "other," "system": */
  332.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  333.     G.pInfo->file_attr &= ~tmp;
  334.  
  335.     return 0;
  336.  
  337. } /* end function mapattr() */
  338.  
  339.  
  340.  
  341. /************************/
  342. /*  Function mapname()  */
  343. /************************/
  344.                              /* return 0 if no error, 1 if caution (filename */
  345. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  346.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  347.     int renamed;             /*  or 10 if out of memory (skip file) */
  348. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  349.     char pathcomp[FILNAMSIZ];    /* path-component buffer */
  350.     char *pp, *cp=(char *)NULL;  /* character pointers */
  351.     char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  352.     int quote = FALSE;           /* flags */
  353.     int error = 0;
  354.     register unsigned workch;    /* hold the character being tested */
  355.  
  356.  
  357. /*---------------------------------------------------------------------------
  358.     Initialize various pointers and counters and stuff.
  359.   ---------------------------------------------------------------------------*/
  360.  
  361.     if (G.pInfo->vollabel)
  362.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  363.  
  364.     /* can create path as long as not just freshening, or if user told us */
  365.     G.create_dirs = (!G.fflag || renamed);
  366.  
  367.     created_dir = FALSE;        /* not yet */
  368.  
  369.     /* user gave full pathname:  don't prepend rootpath */
  370.     renamed_fullpath = (renamed && (*G.filename == '/'));
  371.  
  372.     if (checkdir(__G__ (char *)NULL, INIT) == 10)
  373.         return 10;              /* initialize path buffer, unless no memory */
  374.  
  375.     *pathcomp = '\0';           /* initialize translation buffer */
  376.     pp = pathcomp;              /* point to translation buffer */
  377.     if (G.jflag)                /* junking directories */
  378.         cp = (char *)strrchr(G.filename, '/');
  379.     if (cp == (char *)NULL)     /* no '/' or not junking dirs */
  380.         cp = G.filename;        /* point to internal zipfile-member pathname */
  381.     else
  382.         ++cp;                   /* point to start of last component of path */
  383.  
  384. #ifdef QDOS
  385.     if(*cp == '.' && *(cp+1) == '/')
  386.     {
  387.         cp += 2;
  388.     }
  389. #endif
  390. /*---------------------------------------------------------------------------
  391.     Begin main loop through characters in filename.
  392.   ---------------------------------------------------------------------------*/
  393.  
  394.     while ((workch = (uch)*cp++) != 0) {
  395.  
  396.         if (quote) {                 /* if character quoted, */
  397.             *pp++ = (char)workch;    /*  include it literally */
  398.             quote = FALSE;
  399.         } else
  400.             switch (workch) {
  401.             case '/':             /* can assume -j flag not given */
  402.                 *pp = '\0';
  403.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  404.                     return error;
  405.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  406.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  407.                 break;
  408.  
  409.             case ';':             /* VMS version (or DEC-20 attrib?) */
  410.                 lastsemi = pp;
  411.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  412.                 break;            /*  later, if requested */
  413.  
  414.             case '\026':          /* control-V quote for special chars */
  415.                 quote = TRUE;     /* set flag for next character */
  416.                 break;
  417.  
  418.             case '.':
  419.                 if((qlflag & 1) == 0)
  420.                 {
  421.                     *pp++ = '_';
  422.                 }
  423.                 else
  424.                 {
  425.                     *pp++ = '.';
  426.                 }
  427.                 break;
  428.  
  429.             default:
  430.                 /* allow European characters in filenames: */
  431.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  432.                     *pp++ = (char)workch;
  433.             } /* end switch */
  434.  
  435.     } /* end while loop */
  436.  
  437.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  438.  
  439.     /* if not saving them, remove VMS version numbers (appended ";###") */
  440.     if (!G.V_flag && lastsemi) {
  441.         pp = lastsemi + 1;
  442.         while (isdigit((uch)(*pp)))
  443.             ++pp;
  444.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  445.             *lastsemi = '\0';
  446.     }
  447.  
  448. /*---------------------------------------------------------------------------
  449.     Report if directory was created (and no file to create:  filename ended
  450.     in '/'), check name to be sure it exists, and combine path and name be-
  451.     fore exiting.
  452.   ---------------------------------------------------------------------------*/
  453.  
  454.     if (G.filename[strlen(G.filename) - 1] == '/') {
  455.         G.filename[strlen(G.filename) - 1] = '_';
  456.         checkdir(__G__ G.filename, GETPATH);
  457.         if (created_dir) {
  458.             if (QCOND2) {
  459.                 Info(slide, 0, ((char *)slide, "   creating: %s\n",
  460.                   G.filename));
  461.             }
  462.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  463.         }
  464.         return 2;   /* dir existed already; don't look for data to extract */
  465.     }
  466.  
  467.     if (*pathcomp == '\0') {
  468.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  469.           G.filename));
  470.         return 3;
  471.     }
  472.  
  473.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  474.     checkdir(__G__ G.filename, GETPATH);
  475.  
  476.     return error;
  477.  
  478. } /* end function mapname() */
  479.  
  480.  
  481.  
  482.  
  483. /***********************/
  484. /* Function checkdir() */
  485. /***********************/
  486.  
  487. int checkdir(__G__ pathcomp, flag)
  488.     __GDEF
  489.     char *pathcomp;
  490.     int flag;
  491. /*
  492.  * returns:  1 - (on APPEND_NAME) truncated filename
  493.  *           2 - path doesn't exist, not allowed to create
  494.  *           3 - path doesn't exist, tried to create and failed; or
  495.  *               path exists and is not a directory, but is supposed to be
  496.  *           4 - path is too long
  497.  *          10 - can't allocate memory for filename buffers
  498.  */
  499. {
  500.     static int rootlen = 0;   /* length of rootpath */
  501.     static char *rootpath;    /* user's "extract-to" directory */
  502.     static char *buildpath;   /* full path (so far) to extracted file */
  503.     static char *end;         /* pointer to end of buildpath ('\0') */
  504.  
  505. #   define FN_MASK   7
  506. #   define FUNCTION  (flag & FN_MASK)
  507.  
  508.  
  509. /*---------------------------------------------------------------------------
  510.     APPEND_DIR:  append the path component to the path being built and check
  511.     for its existence.  If doesn't exist and we are creating directories, do
  512.     so for this one; else signal success or error as appropriate.
  513.   ---------------------------------------------------------------------------*/
  514.  
  515.     if (FUNCTION == APPEND_DIR) {
  516.         int too_long = FALSE;
  517. #ifdef SHORT_NAMES
  518.         char *old_end = end;
  519. #endif
  520.  
  521.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  522.         while ((*end = *pathcomp++) != '\0')
  523.             ++end;
  524. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  525.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  526.             *(end = old_end + FILENAME_MAX) = '\0';
  527. #endif
  528.  
  529.         /* GRR:  could do better check, see if overrunning buffer as we go:
  530.          * check end-buildpath after each append, set warning variable if
  531.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  532.          * appending.  Clear variable when begin new path. */
  533.  
  534.         if ((end-buildpath) > FILNAMSIZ-2)  /* need '/', one-char name, '\0' */
  535.             too_long = TRUE;                /* check if extracting directory? */
  536.         if (stat(buildpath, &G.statbuf)) {    /* path doesn't exist */
  537.             if (!G.create_dirs) {   /* told not to create (freshening) */
  538.                 free(buildpath);
  539.                 return 2;         /* path doesn't exist:  nothing to do */
  540.             }
  541.             if (too_long) {
  542.                 Info(slide, 1, ((char *)slide,
  543.                   "checkdir error:  path too long: %s\n", buildpath));
  544.                 free(buildpath);
  545.                 return 4;         /* no room for filenames:  fatal */
  546.             }
  547.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  548.                 Info(slide, 1, ((char *)slide,
  549.                   "checkdir error:  cannot create %s\n\
  550.                  unable to process %s.\n", buildpath, G.filename));
  551.                 free(buildpath);
  552.                 return 3;      /* path didn't exist, tried to create, failed */
  553.             }
  554.             created_dir = TRUE;
  555.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  556.             Info(slide, 1, ((char *)slide,
  557.               "checkdir error:  %s exists but is not directory\n\
  558.                  unable to process %s.\n", buildpath, G.filename));
  559.             free(buildpath);
  560.             return 3;          /* path existed but wasn't dir */
  561.         }
  562.         if (too_long) {
  563.             Info(slide, 1, ((char *)slide,
  564.               "checkdir error:  path too long: %s\n", buildpath));
  565.             free(buildpath);
  566.             return 4;         /* no room for filenames:  fatal */
  567.         }
  568.         *end++ = '_';
  569.         *end = '\0';
  570.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  571.         return 0;
  572.  
  573.     } /* end if (FUNCTION == APPEND_DIR) */
  574.  
  575. /*---------------------------------------------------------------------------
  576.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  577.     buildpath.
  578.   ---------------------------------------------------------------------------*/
  579.  
  580.     if (FUNCTION == GETPATH) {
  581.         strcpy(pathcomp, buildpath);
  582.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  583.         free(buildpath);
  584.         buildpath = end = (char *)NULL;
  585.         return 0;
  586.     }
  587.  
  588. /*---------------------------------------------------------------------------
  589.     APPEND_NAME:  assume the path component is the filename; append it and
  590.     return without checking for existence.
  591.   ---------------------------------------------------------------------------*/
  592.  
  593.     if (FUNCTION == APPEND_NAME) {
  594. #ifdef SHORT_NAMES
  595.         char *old_end = end;
  596. #endif
  597.         short dlen;
  598.  
  599.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  600.         while ((*end = *pathcomp++) != '\0') {
  601.             ++end;
  602. #ifdef SHORT_NAMES  /* truncate name at 14 characters, typically */
  603.             if ((end-old_end) > FILENAME_MAX)      /* GRR:  proper constant? */
  604.                 *(end = old_end + FILENAME_MAX) = '\0';
  605. #endif
  606.             if(isdirdev(buildpath))
  607.             {
  608.                 dlen = 5;
  609.             }
  610.             else
  611.             {
  612.                 dlen = 0;
  613.             }
  614.  
  615.             if ((end-buildpath-dlen) >= FILNAMSIZ) {
  616.                 *--end = '\0';
  617.                 Info(slide, 0x201, ((char *)slide,
  618.                   "checkdir warning:  path too long; truncating\n\
  619.                    %s\n                -> %s\n", G.filename, buildpath));
  620.                 return 1;   /* filename truncated */
  621.             }
  622.         }
  623.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  624.         return 0;  /* could check for existence here, prompt for new name... */
  625.     }
  626.  
  627. /*---------------------------------------------------------------------------
  628.     INIT:  allocate and initialize buffer space for the file currently being
  629.     extracted.  If file was renamed with an absolute path, don't prepend the
  630.     extract-to path.
  631.   ---------------------------------------------------------------------------*/
  632.  
  633. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  634.  
  635.     if (FUNCTION == INIT) {
  636.         Trace((stderr, "initializing buildpath to "));
  637.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1)) ==
  638.             (char *)NULL)
  639.             return 10;
  640.         if ((rootlen > 0) && !renamed_fullpath) {
  641.             strcpy(buildpath, rootpath);
  642.             end = buildpath + rootlen;
  643.         } else {
  644.             *buildpath = '\0';
  645.             end = buildpath;
  646.         }
  647.         Trace((stderr, "[%s]\n", buildpath));
  648.         return 0;
  649.     }
  650.  
  651. /*---------------------------------------------------------------------------
  652.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  653.     sary; else assume it's a zipfile member and return.  This path segment
  654.     gets used in extracting all members from every zipfile specified on the
  655.     command line.
  656.   ---------------------------------------------------------------------------*/
  657.  
  658. #if (!defined(SFX) || defined(SFX_EXDIR))
  659.     if (FUNCTION == ROOT) {
  660.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  661.         if (pathcomp == (char *)NULL) {
  662.             rootlen = 0;
  663.             return 0;
  664.         }
  665.         if ((rootlen = strlen(pathcomp)) > 0) {
  666.  
  667.             if (rootlen > 0 && (stat(pathcomp, &G.statbuf) ||
  668.                 !S_ISDIR(G.statbuf.st_mode)))          /* path does not exist */
  669.             {
  670.                 if (!G.create_dirs                     /* || iswild(pathcomp) */
  671. #ifdef OLD_EXDIR
  672.                                  || !had_trailing_pathsep
  673. #endif
  674.                                                          ) {
  675.                     rootlen = 0;
  676.                     return 2;   /* skip (or treat as stored file) */
  677.                 }
  678.                 /* create the directory (could add loop here to scan pathcomp
  679.                  * and create more than one level, but why really necessary?) */
  680.  
  681.                 if (mkdir(pathcomp, 0777) == -1) {
  682.                     Info(slide, 1, ((char *)slide,
  683.                       "checkdir:  cannot create extraction directory: %s\n",
  684.                       pathcomp));
  685.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  686.                     return 3;  /* failed:  file exists, or 2+ levels required */
  687.                 }
  688.             }
  689.             if (pathcomp[rootlen-1] == '/' || pathcomp[rootlen-1] == '_') {
  690.                 pathcomp[--rootlen] = '\0';
  691.             }
  692.             if ((rootpath = (char *)malloc(rootlen+2)) == (char *)NULL) {
  693.                 rootlen = 0;
  694.                 return 10;
  695.             }
  696.             strcpy(rootpath, pathcomp);
  697.             rootpath[rootlen++] = '_';
  698.             rootpath[rootlen] = '\0';
  699.         }
  700.         Trace((stderr, "rootpath now = [%s]\n", rootpath));
  701.         return 0;
  702.     }
  703. #endif /* !SFX || SFX_EXDIR */
  704.  
  705. /*---------------------------------------------------------------------------
  706.     END:  free rootpath, immediately prior to program exit.
  707.   ---------------------------------------------------------------------------*/
  708.  
  709.     if (FUNCTION == END) {
  710.         Trace((stderr, "freeing rootpath\n"));
  711.         if (rootlen > 0) {
  712.             free(rootpath);
  713.             rootlen = 0;
  714.         }
  715.         return 0;
  716.     }
  717.  
  718.     return 99;  /* should never reach */
  719.  
  720. } /* end function checkdir() */
  721.  
  722.  
  723. static void qfix(__G__ ef_ptr, ef_len)
  724.     __GDEF
  725.     uch *ef_ptr;
  726.     unsigned ef_len;
  727. {
  728.  
  729.     while (ef_len >= EB_HEADSIZE)
  730.     {
  731.         qdosextra   *extra = (qdosextra *)ef_ptr;
  732.         jbextra     *jbp   = (jbextra   *)ef_ptr;
  733.         unsigned    eb_len = makeword(EB_LEN + ef_ptr);
  734.  
  735.         if (eb_len > (ef_len - EB_HEADSIZE)) {
  736.             /* discovered some extra field inconsistency! */
  737.             Trace((stderr,
  738.               "qfix: block length %u > rest ef_size %u\n", eb_len,
  739.               ef_len - EB_HEADSIZE));
  740.             break;
  741.         }
  742.  
  743.         switch (extra->shortid) {
  744.           case SHORTID:
  745.             if (!strncmp(extra->longid, LONGID, strlen(LONGID)))
  746.             {
  747.                 if (eb_len != EXTRALEN)
  748.                     fputs("warning: invalid length in Qdos field", stderr);
  749.                 if (extra->header.d_type)
  750.                 {
  751.                     fs_heads(fgetchid(G.outfile), (timeout_t)-1,
  752.                              &extra->header, 14);
  753.                     G.pInfo->file_attr |= S_IXUSR;
  754.                 }
  755.             }
  756.  
  757.             if (!strncmp(jbp->longid, JBLONGID, strlen(JBLONGID)))
  758.             {
  759.                 if (eb_len != JBEXTRALEN)
  760.                     fputs("warning: invalid length in QZ field", stderr);
  761.                 if (jbp->header.d_type)
  762.                 {
  763.                     fs_heads(fgetchid(G.outfile), (timeout_t)-1,
  764.                              &jbp->header, 14);
  765.                     G.pInfo->file_attr |= S_IXUSR;
  766.                 }
  767.             }
  768.             break;
  769.  
  770.           default:
  771.             Trace((stderr,"qfix: unknown extra field block, ID=%d\n",
  772.                extra->shortid));
  773.             break;
  774.         }
  775.  
  776.         /* Skip this extra field block */
  777.         ef_ptr += (eb_len + EB_HEADSIZE);
  778.         ef_len -= (eb_len + EB_HEADSIZE);
  779.     }
  780. }
  781.  
  782.  
  783. #ifdef QDOS
  784. #  include <utime.h>
  785.    long timezone = 0;
  786. #endif
  787.  
  788.  
  789. /****************************/
  790. /* Function close_outfile() */
  791. /****************************/
  792.  
  793. void close_outfile(__G)
  794.     __GDEF
  795. {
  796.     iztimes zt;
  797. #ifdef USE_EF_UT_TIME
  798.     unsigned eb_izux_flg;
  799. #endif
  800.  
  801.     if (G.extra_field) {
  802.         qfix(__G__ G.extra_field, G.lrec.extra_field_length);
  803.     }
  804.  
  805.     fclose(G.outfile);
  806.  
  807. /*---------------------------------------------------------------------------
  808.     Change the file permissions from default ones to those stored in the
  809.     zipfile.
  810.   ---------------------------------------------------------------------------*/
  811.  
  812. #ifndef NO_CHMOD
  813.     if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  814.         perror("chmod (file attributes) error");
  815. #endif
  816.  
  817. /*---------------------------------------------------------------------------
  818.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  819.     time:  adjust base year from 1980 to 1970, do usual conversions from
  820.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  821.     light savings time differences.  If we have a Unix extra field, however,
  822.     we're laughing:  both mtime and atime are ours.
  823.   ---------------------------------------------------------------------------*/
  824.  
  825. #ifdef USE_EF_UT_TIME
  826.     eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
  827.                    G.lrec.extra_field_length, 0, G.lrec.last_mod_file_date,
  828.                    &zt, z_uidgid) : 0);
  829.     if (eb_izux_flg & EB_UT_FL_MTIME) {
  830.         TTrace((stderr, "\nclose_outfile:  Unix e.f. modif. time = %ld\n",
  831.           zt.mtime));
  832.     } else {
  833.         zt.mtime = dos_to_unix_time(G.lrec.last_mod_file_date,
  834.                                     G.lrec.last_mod_file_time);
  835.     }
  836.     if (eb_izux_flg & EB_UT_FL_ATIME) {
  837.         TTrace((stderr, "close_outfile:  Unix e.f. access time = %ld\n",
  838.           zt.atime));
  839.     } else {
  840.         zt.atime = zt.mtime;
  841.  
  842.         TTrace((stderr, "\nclose_outfile:  modification/access times = %ld\n",
  843.           zt.mtime));
  844.     }
  845. #else
  846.     zt.atime = zt.mtime = dos_to_unix_time(G.lrec.last_mod_file_date,
  847.                                            G.lrec.last_mod_file_time);
  848. #endif
  849.  
  850.     /* set the file's access and modification times */
  851.     if (utime(G.filename, (struct utimbuf *)&zt)) {
  852.         Info(slide, 0x201, ((char *)slide,
  853.           "warning:  cannot set the time for %s\n", G.filename));
  854.     }
  855.  
  856. } /* end function close_outfile() */
  857.  
  858.  
  859.  
  860.  
  861. #ifdef TIMESTAMP
  862.  
  863. /***************************/
  864. /*  Function stamp_file()  */
  865. /***************************/
  866.  
  867. int stamp_file(fname, modtime)
  868.     ZCONST char *fname;
  869.     time_t modtime;
  870. {
  871.     struct utimbuf tp;
  872.  
  873.     tp.modtime = tp.actime = modtime;
  874.     return (utime(fname, &tp));
  875.  
  876. } /* end function stamp_file() */
  877.  
  878. #endif /* TIMESTAMP */
  879.  
  880.  
  881.  
  882.  
  883. #ifndef SFX
  884.  
  885. /************************/
  886. /*  Function version()  */
  887. /************************/
  888.  
  889. void version(__G)
  890.     __GDEF
  891. {
  892.  
  893.     sprintf((char *)slide, LoadFarString(CompiledWith),
  894.            "c68", " v4.2x", "SMS/QDOS",
  895.             " on ", __DATE__, "","");
  896.     (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  897.  
  898. } /* end function version() */
  899.  
  900. #endif /* !SFX */
  901. #endif /* !FUNZIP */
  902.  
  903. #if CRYPT
  904.  
  905. char *getp(__G__ m, p, n)
  906.     __GDEF
  907.     const char *m;              /* prompt for password */
  908.     char *p;                    /* return value: line input */
  909.     int n;                      /* bytes available in p[] */
  910. {
  911.     int c;                      /* one-byte buffer for read() to use */
  912.     int i;                      /* number of characters input */
  913.     char *w;                    /* warning on retry */
  914.  
  915.     /* get password */
  916.     w = "";
  917.     sd_cure(getchid(0), -1);    /* enable cursor */
  918.     do {
  919.         fputs(w, stderr);       /* warning if back again */
  920.         fputs(m, stderr);       /* display prompt and flush */
  921.         fflush(stderr);
  922.         i = 0;
  923.         do {
  924.             c = getch();
  925.             if (c == 0xc2) {
  926.                 if (i > 0) {
  927.                     i--; /* the `del' keys works */
  928.                     fputs("\b \b", stderr);
  929.                 }
  930.             }
  931.             else if (i < n) {
  932.                 p[i++] = c;     /* truncate past n */
  933.                 if(c != '\n') putc('*', stderr);
  934.             }
  935.         } while (c != '\n');
  936.  
  937.         putc('\n', stderr);  fflush(stderr);
  938.         w = "(line too long--try again)\n";
  939.     } while (p[i-1] != '\n');
  940.  
  941.     p[i-1] = 0;                 /* terminate at newline */
  942.     sd_curs(getchid(0), -1);    /* suppress cursor */
  943.     return p;                   /* return pointer to password */
  944.  
  945. } /* end function getp() */
  946.  
  947. #endif /* CRYPT */
  948.  
  949.