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