Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.   Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.
  3.  
  4.   See the accompanying file LICENSE, version 2000-Apr-09 or later
  5.   (the contents of which are also included in unzip.h) for terms of use.
  6.   If, for some reason, all these files are missing, the Info-ZIP license
  7.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  8. */
  9. /*---------------------------------------------------------------------------
  10.  
  11.   tanunz.c
  12.  
  13.   Tandem/NSK routines for use with Info-ZIP's UnZip 5.3 and later.
  14.  
  15.   Contains:  do_wild()           <-- generic enough to put in fileio.c?
  16.              ef_scan_for_tandem()
  17.              open_outfile()
  18.              mapattr()
  19.              mapname()
  20.              checkdir()
  21.              mkdir()
  22.              close_outfile()
  23.              version()
  24.  
  25.   ---------------------------------------------------------------------------*/
  26.  
  27.  
  28. #define UNZIP_INTERNAL
  29. #include "unzip.h"
  30.  
  31. #include <tal.h>
  32. #include "$system.zsysdefs.zsysc" nolist
  33. #include <cextdecs> nolist
  34. #include "tannsk.h"
  35.  
  36.  
  37. char *in2ex OF((__GPRO__ char *));
  38.  
  39. static nsk_file_attrs *ef_scan_for_tandem (
  40.     uch *ef_buf,
  41.     unsigned ef_len
  42.   );
  43.  
  44.  
  45. static int created_dir;        /* used in mapname(), checkdir() */
  46. static int renamed_fullpath;   /* ditto */
  47.  
  48.  
  49. /****************************/
  50. /* Strings used in tanunz.c */
  51. /****************************/
  52.  
  53. static ZCONST char Far CannotDeleteOldFile[] =
  54.   "error:  cannot delete old %s\n";
  55. static ZCONST char Far CannotCreateFile[] = "error:  cannot create %s\n";
  56.  
  57.  
  58. #ifndef SFX
  59. /**********************/
  60. /* Function do_wild() */  /* for porting: dir separator; match(ignore_case) */
  61. /**********************/
  62.  
  63. char *do_wild(__G__ wildspec)
  64.     __GDEF
  65.     ZCONST char *wildspec;  /* only used first time on a given dir */
  66. {
  67.     static DIR *wild_dir = (DIR *)NULL;
  68.     static ZCONST char *wildname;
  69.     static char *dirname, matchname[FILNAMSIZ];
  70.     static int notfirstcall=FALSE, have_dirname, dirnamelen;
  71.     struct dirent *file;
  72.     static char *intname;
  73.     int isdir = 0;
  74.     int pdosflag = 0;
  75.  
  76.     /* Even when we're just returning wildspec, we *always* do so in
  77.      * matchname[]--calling routine is allowed to append four characters
  78.      * to the returned string, and wildspec may be a pointer to argv[].
  79.      */
  80.     if (!notfirstcall) {    /* first call:  must initialize everything */
  81.         notfirstcall = TRUE;
  82.  
  83.         if (!iswild(wildspec)) {
  84.             strncpy(matchname, wildspec, FILNAMSIZ);
  85.             matchname[FILNAMSIZ-1] = '\0';
  86.             have_dirname = FALSE;
  87.             wild_dir = NULL;
  88.             return matchname;
  89.         }
  90.  
  91.         dirnamelen = strlen(wildspec);
  92.  
  93.         if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  94.             Info(slide, 0x201, ((char *)slide,
  95.               "warning:  cannot allocate wildcard buffers\n"));
  96.              strncpy(matchname, wildspec, FILNAMSIZ);
  97.              matchname[FILNAMSIZ-1] = '\0';
  98.              return matchname;   /* but maybe filespec was not a wildcard */
  99.         }
  100.         strcpy(dirname, wildspec);
  101.         wildname = wildspec;
  102.         have_dirname = FALSE;
  103.  
  104.         if ((wild_dir = opendir(dirname)) != (DIR *)NULL) {
  105.             while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  106.                 Trace((stderr, "do_wild: readdir returns %s\n",
  107.                   FnFilter1(file->d_name)));
  108.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  109.                     continue;  /* Unix: '*' and '?' do not match leading dot */
  110.                 if (match(file->d_name, wildname, 0 WISEP) && /* 0=case sens.*/
  111.                     /* skip "." and ".." directory entries */
  112.                     strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  113.                     Trace((stderr, "do_wild: match() succeeds\n"));
  114.                     if (have_dirname) {
  115.                         strcpy(matchname, dirname);
  116.                         strcpy(matchname+dirnamelen, file->d_name);
  117.                     } else
  118.                         strcpy(matchname, file->d_name);
  119.                     return matchname;
  120.                 }
  121.             }
  122.             /* if we get to here directory is exhausted, so close it */
  123.             closedir(wild_dir);
  124.             wild_dir = (DIR *)NULL;
  125.         }
  126.  
  127.         /* return the raw wildspec in case that works (e.g., directory not
  128.          * searchable, but filespec was not wild and file is readable) */
  129.         strncpy(matchname, wildspec, FILNAMSIZ);
  130.         matchname[FILNAMSIZ-1] = '\0';
  131.         return matchname;
  132.     }
  133.  
  134.     /* last time through, might have failed opendir but returned raw wildspec */
  135.     if (wild_dir == (DIR *)NULL) {
  136.         notfirstcall = FALSE; /* nothing left to try--reset for new wildspec */
  137.         if (have_dirname)
  138.             free(dirname);
  139.         return (char *)NULL;
  140.     }
  141.  
  142.     /* If we've gotten this far, we've read and matched at least one entry
  143.      * successfully (in a previous call), so dirname has been copied into
  144.      * matchname already.
  145.      */
  146.     while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  147.         Trace((stderr, "do_wild:  readdir returns %s\n",
  148.           FnFilter1(file->d_name)));
  149.         if (file->d_name[0] == '.' && wildname[0] != '.')
  150.             continue;   /* Unix:  '*' and '?' do not match leading dot */
  151.         if (match(file->d_name, wildname, 0 WISEP)) {   /* 0 == case sens. */
  152.             if (have_dirname) {
  153.                 /* strcpy(matchname, dirname); */
  154.                 strcpy(matchname+dirnamelen, file->d_name);
  155.             } else
  156.                 strcpy(matchname, file->d_name);
  157.             return matchname;
  158.         }
  159.     }
  160.  
  161.     closedir(wild_dir);     /* have read at least one entry; nothing left */
  162.     wild_dir = (DIR *)NULL;
  163.     notfirstcall = FALSE;   /* reset for new wildspec */
  164.     if (have_dirname)
  165.         free(dirname);
  166.     return (char *)NULL;
  167.  
  168. } /* end function do_wild() */
  169.  
  170. #endif /* !SFX */
  171.  
  172.  
  173.  
  174. /*********************************/
  175. /* Function ef_scan_for_tandem() */
  176. /*********************************/
  177.  
  178. static nsk_file_attrs *ef_scan_for_tandem(ef_buf, ef_len)
  179.     uch *ef_buf;                /* buffer containing extra field */
  180.     unsigned ef_len;            /* total length of extra field */
  181. {
  182.     unsigned eb_id;
  183.     unsigned eb_len;
  184.  
  185.   /*---------------------------------------------------------------------------
  186.     This function scans the extra field for EF_TANDEM
  187.     -------------------------------------------------------------------------*/
  188.  
  189.     if (ef_buf == NULL)
  190.       return NULL;
  191.  
  192.     while (ef_len >= EB_HEADSIZE) {
  193.       eb_id = makeword(EB_ID + ef_buf);
  194.       eb_len = makeword(EB_LEN + ef_buf);
  195.  
  196.       if (eb_len > (ef_len - EB_HEADSIZE)) {
  197.           /* discovered some extra field inconsistency! */
  198.           TTrace((stderr,
  199.             "ef_scan_for_tandem: block length %u > rest ef_size %u\n", eb_len,
  200.             ef_len - EB_HEADSIZE));
  201.           break;
  202.       }
  203.  
  204.       switch (eb_id) {
  205.         case EF_TANDEM:
  206.           return (nsk_file_attrs *)(char *)(ef_buf + EB_HEADSIZE);
  207.           break;
  208.  
  209.         default:
  210.           break;
  211.       }
  212.  
  213.       /* Skip this extra field block */
  214.       ef_buf += (eb_len + EB_HEADSIZE);
  215.       ef_len -= (eb_len + EB_HEADSIZE);
  216.   }
  217.  
  218.   return NULL;
  219. }
  220.  
  221.  
  222. /***************************/
  223. /* Function open_outfile() */
  224. /***************************/
  225.  
  226. int open_outfile(__G)           /* return 1 if fail */
  227.     __GDEF
  228. {
  229.     int fdesc;
  230.     short fnum, err, len;
  231.     int priext, secext;
  232.     short maxext, filecode, blocksize;
  233.  
  234.     #define alist_items 1
  235.     #define vlist_bytes 2
  236.     short alist[alist_items]={42};
  237.     unsigned short vlist[alist_items];
  238.     short extra, *err_item=&extra;
  239.     nsk_file_attrs *znsk_attr;
  240.     ulg eof, pages;
  241.     char nsk_work[FILENAME_MAX + 1], *nsk_fname=&nsk_work[0];
  242.  
  243. #ifdef DLL
  244.     if (G.redirect_data)
  245.         return (redirect_outfile(__G) == FALSE);
  246. #endif
  247.     if (SSTAT(G.filename, &G.statbuf) == 0) {
  248.         Trace((stderr, "open_outfile:  stat(%s) returns 0:  file exists\n",
  249.           FnFilter1(G.filename)));
  250.         if (unlink(G.filename) != 0) {
  251.             Trace((stderr, "open_outfile:  existing file %s is read-only\n",
  252.               FnFilter1(G.filename)));
  253.             chmod(G.filename, S_IRUSR | S_IWUSR);
  254.             Trace((stderr, "open_outfile:  %s now writable\n",
  255.               FnFilter1(G.filename)));
  256.             if (unlink(G.filename) != 0) {
  257.                 Info(slide, 0x401, ((char *)slide,
  258.                   LoadFarString(CannotDeleteOldFile), FnFilter1(G.filename)));
  259.                 return 1;
  260.             }
  261.         }
  262.         Trace((stderr, "open_outfile:  %s now deleted\n",
  263.           FnFilter1(G.filename)));
  264.     }
  265.  
  266.     /* Set up Tandem specific file information if present */
  267.     znsk_attr = ef_scan_for_tandem(G.extra_field, G.lrec.extra_field_length);
  268.     if (znsk_attr != NULL) {
  269.         /* Set extent sizes */
  270.         priext = znsk_attr->priext;
  271.         secext  = znsk_attr->secext;
  272.         maxext  = (int) znsk_attr->maxext;
  273.         /* If original file was Enscribe and text then recreate as Edit */
  274.         filecode = (znsk_attr->filetype != NSK_UNSTRUCTURED ?
  275.                     (G.pInfo->textmode ? NSK_EDITFILECODE : NSK_UNSTRUCTURED) :
  276.                     znsk_attr->filecode);
  277.         blocksize = znsk_attr->block;
  278.     } else {
  279.         /* Try to work out some decent sizes based on how big the file is */
  280.         eof = G.lrec.ucsize;
  281.         pages = (eof/2048) + 2;
  282.         if (pages <= 500) {
  283.             priext = pages; /* fits into one extent */
  284.             maxext = 16;
  285.         } else {
  286.             priext = 500; /* Try and fit into 500 page chunks */
  287.             maxext = pages/(priext - 50); /* Allow for Enscribe overhead */
  288.             if (maxext > 978) {
  289.                 priext = eof >> 10;         /* 512 equal extents */
  290.                 maxext = 978;                /* 2048 * 512 == 2^10 */
  291.             }
  292.         }
  293.         secext = priext;
  294.         filecode = (G.pInfo->textmode ? NSK_EDITFILECODE : NSK_UNSTRUCTURED);
  295.         blocksize = TANDEM_BLOCKSIZE;
  296.     }
  297.  
  298.     if ((fdesc = creat(G.filename,,priext,secext)) != -1){
  299.         fnum = fdtogfn ((short)fdesc);
  300.         err = (SETMODE (fnum, SET_FILE_BUFFERSIZE, blocksize) != CCE);
  301.         err = (SETMODE (fnum, SET_FILE_BUFFERED, 0, 0) != CCE);
  302.         err = (SETMODE (fnum, SET_FILE_BUFFERED, 0, 1) != CCE);
  303.         err = (SETMODE (fnum, SET_FILE_MAXEXTENTS, maxext) != CCE);
  304.         err = close(fdesc);
  305.  
  306.         vlist[0] = filecode;
  307.  
  308.         /* Note that FILE_ALTERLIST_ expects uppercase names */
  309.         /* Need to call strlen and upshift                   */
  310.         len = strlen(G.filename);
  311.         err = STRING_UPSHIFT_(G.filename,
  312.                               len,
  313.                               nsk_fname,
  314.                               len);
  315.  
  316.         err = FILE_ALTERLIST_(nsk_fname,
  317.                               len,
  318.                               alist,
  319.                               alist_items,
  320.                               vlist,
  321.                               vlist_bytes,
  322.                               ,
  323.                               err_item);
  324.     };
  325.  
  326.     G.outfile = fopen(G.filename, (G.pInfo->textmode ? FOPWT : FOPW));
  327.  
  328.     if (G.outfile == (FILE *)NULL) {
  329.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  330.           FnFilter1(G.filename)));
  331.         return 1;
  332.     }
  333.  
  334. #ifdef USE_FWRITE
  335. #ifdef _IOFBF  /* make output fully buffered (works just about like write()) */
  336.     setvbuf(G.outfile, (char *)slide, _IOFBF, WSIZE);
  337. #else
  338.     setbuf(G.outfile, (char *)slide);
  339. #endif
  340. #endif /* USE_FWRITE */
  341.     return 0;
  342.  
  343. } /* end function open_outfile() */
  344.  
  345.  
  346.  
  347. /**********************/
  348. /* Function mapattr() */
  349. /**********************/
  350.  
  351. int mapattr(__G)
  352.     __GDEF
  353. {
  354.     ulg tmp = G.crec.external_file_attributes;
  355.  
  356.     switch (G.pInfo->hostnum) {
  357.         case AMIGA_:
  358.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  359.             G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  360.             break;
  361.         case THEOS_:
  362.             tmp &= 0xF1FFFFFFL;
  363.             if ((tmp & 0xF0000000L) != 0x40000000L)
  364.                 tmp &= 0x01FFFFFFL;     /* not a dir, mask all ftype bits */
  365.             else
  366.                 tmp &= 0x41FFFFFFL;     /* leave directory bit as set */
  367.             /* fall through! */
  368.         case TANDEM_:
  369.         case UNIX_:
  370.         case VMS_:
  371.         case ACORN_:
  372.         case ATARI_:
  373.         case ATHEOS_:
  374.         case BEOS_:
  375.         case QDOS_:
  376.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  377.             if (G.pInfo->file_attr != 0 || !G.extra_field) {
  378.                 return 0;
  379.             } else {
  380.                 /* Some (non-Info-ZIP) implementations of Zip for Unix and
  381.                    VMS (and probably others ??) leave 0 in the upper 16-bit
  382.                    part of the external_file_attributes field. Instead, they
  383.                    store file permission attributes in some extra field.
  384.                    As a work-around, we search for the presence of one of
  385.                    these extra fields and fall back to the MSDOS compatible
  386.                    part of external_file_attributes if one of the known
  387.                    e.f. types has been detected.
  388.                    Later, we might implement extraction of the permission
  389.                    bits from the VMS extra field. But for now, the work-around
  390.                    should be sufficient to provide "readable" extracted files.
  391.                    (For ASI Unix e.f., an experimental remap of the e.f.
  392.                    mode value IS already provided!)
  393.                  */
  394.                 ush ebID;
  395.                 unsigned ebLen;
  396.                 uch *ef = G.extra_field;
  397.                 unsigned ef_len = G.crec.extra_field_length;
  398.                 int r = FALSE;
  399.  
  400.                 while (!r && ef_len >= EB_HEADSIZE) {
  401.                     ebID = makeword(ef);
  402.                     ebLen = (unsigned)makeword(ef+EB_LEN);
  403.                     if (ebLen > (ef_len - EB_HEADSIZE))
  404.                         /* discoverd some e.f. inconsistency! */
  405.                         break;
  406.                     switch (ebID) {
  407.                       case EF_ASIUNIX:
  408.                         if (ebLen >= (EB_ASI_MODE+2)) {
  409.                             G.pInfo->file_attr =
  410.                               (unsigned)makeword(ef+(EB_HEADSIZE+EB_ASI_MODE));
  411.                             /* force stop of loop: */
  412.                             ef_len = (ebLen + EB_HEADSIZE);
  413.                             break;
  414.                         }
  415.                         /* else: fall through! */
  416.                       case EF_PKVMS:
  417.                         /* "found nondecypherable e.f. with perm. attr" */
  418.                         r = TRUE;
  419.                       default:
  420.                         break;
  421.                     }
  422.                     ef_len -= (ebLen + EB_HEADSIZE);
  423.                     ef += (ebLen + EB_HEADSIZE);
  424.                 }
  425.                 if (!r)
  426.                     return 0;
  427.             }
  428.             /* fall through! */
  429.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  430.         case FS_FAT_:
  431.             /* PKWARE's PKZip for Unix marks entries as FS_FAT_, but stores the
  432.              * Unix attributes in the upper 16 bits of the external attributes
  433.              * field, just like Info-ZIP's Zip for Unix.  We try to use that
  434.              * value, after a check for consistency with the MSDOS attribute
  435.              * bits (see below).
  436.              */
  437.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  438.             /* fall through! */
  439.         case FS_HPFS_:
  440.         case FS_NTFS_:
  441.         case MAC_:
  442.         case TOPS20_:
  443.         default:
  444.             /* Ensure that DOS subdir bit is set when the entry's name ends
  445.              * in a '/'.  Some third-party Zip programs fail to set the subdir
  446.              * bit for directory entries.
  447.              */
  448.             if ((tmp & 0x10) == 0) {
  449.                 extent fnlen = strlen(G.filename);
  450.                 if (fnlen > 0 && G.filename[fnlen-1] == '/')
  451.                     tmp |= 0x10;
  452.             }
  453.             /* read-only bit --> write perms; subdir bit --> dir exec bit */
  454.             tmp = !(tmp & 1) << 1  |  (tmp & 0x10) >> 4;
  455.             if ((G.pInfo->file_attr & 0700) == (unsigned)(0400 | tmp<<6))
  456.                 /* keep previous G.pInfo->file_attr setting, when its "owner"
  457.                  * part appears to be consistent with DOS attribute flags!
  458.                  */
  459.                 return 0;
  460.             G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  461.             break;
  462.     } /* end switch (host-OS-created-by) */
  463.  
  464.     /* for originating systems without concept of "group," "other," "system" */
  465.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  466.     G.pInfo->file_attr &= ~tmp;
  467.  
  468.     return 0;
  469.  
  470. } /* end function mapattr() */
  471.  
  472.  
  473.  
  474.  
  475.  
  476. /************************/
  477. /*  Function mapname()  */
  478. /************************/
  479.  
  480. int mapname(__G__ renamed)
  481.     __GDEF
  482.     int renamed;
  483. /*
  484.  * returns:
  485.  *  MPN_OK          - no problem detected
  486.  *  MPN_INF_TRUNC   - caution (truncated filename)
  487.  *  MPN_INF_SKIP    - info "skip entry" (dir doesn't exist)
  488.  *  MPN_ERR_SKIP    - error -> skip entry
  489.  *  MPN_ERR_TOOLONG - error -> path is too long
  490.  *  MPN_NOMEM       - error (memory allocation failed) -> skip entry
  491.  *  [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  492.  */
  493. {
  494.     char pathcomp[FILNAMSIZ];      /* path-component buffer */
  495.     char *pp, *cp;                 /* character pointers */
  496.     char *lastsemi=(char *)NULL;   /* pointer to last semi-colon in pathcomp */
  497.     int error = MPN_OK;
  498.     register unsigned workch;      /* hold the character being tested */
  499.  
  500.  
  501. /*---------------------------------------------------------------------------
  502.     Initialize various pointers and counters and stuff.
  503.   ---------------------------------------------------------------------------*/
  504.  
  505.     if (G.pInfo->vollabel)
  506.         return MPN_VOL_LABEL;   /* can't set disk volume labels on Tandem */
  507.  
  508.     /* can create path as long as not just freshening, or if user told us */
  509.     G.create_dirs = (!uO.fflag || renamed);
  510.  
  511.     created_dir = FALSE;        /* not yet */
  512.  
  513.     /* user gave full pathname:  don't prepend rootpath */
  514.     renamed_fullpath = (renamed && (*G.filename == '/'));
  515.  
  516.     if (checkdir(__G__ (char *)NULL, INIT) == MPN_NOMEM)
  517.         return MPN_NOMEM;       /* initialize path buffer, unless no memory */
  518.  
  519.     /* TANDEM - call in2ex */
  520.     pp = in2ex(__G__ G.filename);
  521.     if (pp == (char *)NULL)
  522.         return MPN_NOMEM;
  523.     strcpy(G.filename, pp);
  524.     free(pp);
  525.  
  526.     *pathcomp = '\0';           /* initialize translation buffer */
  527.     pp = pathcomp;              /* point to translation buffer */
  528.     /* directories have already been junked in in2ex() */
  529.     cp = G.filename;            /* point to internal zipfile-member pathname */
  530.  
  531. /*---------------------------------------------------------------------------
  532.     Begin main loop through characters in filename.
  533.   ---------------------------------------------------------------------------*/
  534.  
  535.     while ((workch = (uch)*cp++) != 0) {
  536.  
  537.         switch (workch) { /* includes space char, let checkdir handle it */
  538.             case TANDEM_DELIMITER: /* can assume -j flag not given */
  539.                 *pp = '\0';
  540.                 if (((error = checkdir(__G__ pathcomp, APPEND_DIR))
  541.                      & MPN_MASK) > MPN_INF_TRUNC)
  542.                     return error;
  543.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  544.                 lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  545.                 break;
  546.  
  547.             case ';':             /* VMS version (or DEC-20 attrib?) */
  548.                 lastsemi = pp;
  549.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  550.                 break;            /*  later, if requested */
  551.  
  552.             default:
  553.                 /* allow European characters in filenames: */
  554.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  555.                     *pp++ = (char)workch;
  556.         } /* end switch */
  557.  
  558.     } /* end while loop */
  559.  
  560. /*---------------------------------------------------------------------------
  561.     Report if directory was created (and no file to create:  filename ended
  562.     in '/'), check name to be sure it exists, and combine path and name be-
  563.     fore exiting.
  564.   ---------------------------------------------------------------------------*/
  565.  
  566.     if (G.filename[strlen(G.filename) - 1] == TANDEM_DELIMITER) {
  567.         checkdir(__G__ G.filename, GETPATH);
  568.         if (created_dir) {
  569.             if (QCOND2) {
  570.                 Info(slide, 0, ((char *)slide, "   creating: %s\n",
  571.                   FnFilter1(G.filename)));
  572.             }
  573.             /* set dir time (note trailing '/') */
  574.             return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  575.         }
  576.         /* dir existed already; don't look for data to extract */
  577.         return (error & ~MPN_MASK) | MPN_INF_SKIP;
  578.     }
  579.  
  580.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  581.  
  582.     /* if not saving them, remove VMS version numbers (appended ";###") */
  583.     if (!uO.V_flag && lastsemi) {
  584.         pp = lastsemi + 1;
  585.         while (isdigit((uch)(*pp)))
  586.             ++pp;
  587.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  588.             *lastsemi = '\0';
  589.     }
  590.  
  591.     if (*pathcomp == '\0') {
  592.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  593.           FnFilter1(G.filename)));
  594.         return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  595.     }
  596.  
  597.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  598.     checkdir(__G__ G.filename, GETPATH);
  599.  
  600.     return error;
  601.  
  602. } /* end function mapname() */
  603.  
  604.  
  605. /***********************/
  606. /* Function checkdir() */
  607. /***********************/
  608.  
  609. int checkdir(__G__ pathcomp, flag)
  610.     __GDEF
  611.     char *pathcomp;
  612.     int flag;
  613. /*
  614.  * returns:
  615.  *  MPN_OK          - no problem detected
  616.  *  MPN_INF_TRUNC   - (on APPEND_NAME) truncated filename
  617.  *  MPN_INF_SKIP    - path doesn't exist, not allowed to create
  618.  *  MPN_ERR_SKIP    - path doesn't exist, tried to create and failed; or path
  619.  *                    exists and is not a directory, but is supposed to be
  620.  *  MPN_ERR_TOOLONG - path is too long
  621.  *  MPN_NOMEM       - can't allocate memory for filename buffers
  622.  */
  623. /* By using in2ex() to pre-process the filename we do not need to worry
  624.    about the lengths of filename parts.  We just need to cope with mapping the
  625.    pseudo file extensions properly of the actual filename (last part of name).
  626.    e.g.  "tandem c" -> tandemc
  627.          "revsion.h" -> revisioh
  628.  */
  629. {
  630.     static int rootlen = 0;   /* length of rootpath */
  631.     static char *rootpath;    /* user's "extract-to" directory */
  632.     static char *buildpath;   /* full path (so far) to extracted file */
  633.     static char *end;         /* pointer to end of buildpath ('\0') */
  634.  
  635. #   define FN_MASK   7
  636. #   define FUNCTION  (flag & FN_MASK)
  637.  
  638.     char fname[FILENAME_MAX + 1];
  639.     short fnamelen, extlen, trunclen, i;
  640.     char ext[EXTENSION_MAX + 1];
  641.     char *ptr;
  642.  
  643.  
  644. /*---------------------------------------------------------------------------
  645.     APPEND_DIR:  append the path component to the path being built and check
  646.     for its existence.  If doesn't exist and we are creating directories, do
  647.     so for this one; else signal success or error as appropriate.
  648.   ---------------------------------------------------------------------------*/
  649.  
  650.     if (FUNCTION == APPEND_DIR) {
  651.         int too_long = FALSE;
  652.  
  653.         Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  654.         while ((*end = *pathcomp++) != '\0')
  655.             ++end;
  656.  
  657.         if (stat(buildpath, &G.statbuf)) {  /* path doesn't exist */
  658.             if (!G.create_dirs) { /* told not to create (freshening) */
  659.                 free(buildpath);
  660.                 return MPN_INF_SKIP;    /* path doesn't exist: nothing to do */
  661.             }
  662.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  663.                 Info(slide, 1, ((char *)slide,
  664.                   "checkdir error:  cannot create %s\n\
  665.                 unable to process %s.\n",
  666.                   FnFilter2(buildpath), FnFilter1(G.filename)));
  667.                 free(buildpath);
  668.                 /* path didn't exist, tried to create, failed */
  669.                 return MPN_ERR_SKIP;
  670.             }
  671.             created_dir = TRUE;
  672.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  673.             Info(slide, 1, ((char *)slide,
  674.               "checkdir error:  %s exists but is not directory\n\
  675.                 unable to process %s.\n",
  676.                   FnFilter2(buildpath), FnFilter1(G.filename)));
  677.             free(buildpath);
  678.             /* path existed but wasn't dir */
  679.             return MPN_ERR_SKIP;
  680.         }
  681.         *end++ = TANDEM_DELIMITER;
  682.         *end = '\0';
  683.         Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  684.         return MPN_OK;
  685.  
  686.     } /* end if (FUNCTION == APPEND_DIR) */
  687.  
  688. /*---------------------------------------------------------------------------
  689.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  690.     buildpath.
  691.   ---------------------------------------------------------------------------*/
  692.  
  693.     if (FUNCTION == GETPATH) {
  694.         strcpy(pathcomp, buildpath);
  695.         Trace((stderr, "getting and freeing path [%s]\n",
  696.           FnFilter1(pathcomp)));
  697.         free(buildpath);
  698.         buildpath = end = (char *)NULL;
  699.         return MPN_OK;
  700.     }
  701.  
  702. /*---------------------------------------------------------------------------
  703.     APPEND_NAME:  assume the path component is the filename; append it and
  704.     return without checking for existence.
  705.   ---------------------------------------------------------------------------*/
  706.  
  707.     if (FUNCTION == APPEND_NAME) {
  708.         Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  709.  
  710.         if (!uO.rflag  /* Do not add extension if asked */
  711.             && parsename(pathcomp, fname, ext))
  712.         {
  713.             fnamelen = strlen(fname);
  714.             extlen = strlen(ext);
  715.             if (fnamelen+extlen > MAXFILEPARTLEN) {
  716.                 /* Doesn't fit.  Best approx is to use up to three characters
  717.                    from extension and place these on the end of as much of the
  718.                    start of the filename part as possible.
  719.                  */
  720.                 if (extlen > EXTENSION_MAX)
  721.                     extlen = EXTENSION_MAX;
  722.                 trunclen = MAXFILEPARTLEN - extlen;
  723.                 ptr = fname;
  724.                 for (i=0; i < trunclen; i++)
  725.                     *end++ = *ptr++;
  726.                 ptr = ext;
  727.                 for (i=0; i < extlen; i++)
  728.                     *end++ = *ptr++;
  729.                 *end = '\0';     /* mark end of string */
  730.             }
  731.             else {
  732.                 /* Just join parts end to end */
  733.                 ptr = fname;
  734.                 while ((*end = *ptr++) != '\0')
  735.                     ++end;
  736.                 ptr = ext;
  737.                 while ((*end = *ptr++) != '\0')
  738.                   ++end;
  739.             }
  740.         }
  741.         else
  742.             while ((*end = *pathcomp++) != '\0')
  743.               ++end;
  744.  
  745.         Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  746.         /* could check for existence here, prompt for new name... */
  747.         return MPN_OK;
  748.     }
  749.  
  750. /*---------------------------------------------------------------------------
  751.     INIT:  allocate and initialize buffer space for the file currently being
  752.     extracted.  If file was renamed with an absolute path, don't prepend the
  753.     extract-to path.
  754.   ---------------------------------------------------------------------------*/
  755.  
  756. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  757.  
  758.     if (FUNCTION == INIT) {
  759.         Trace((stderr, "initializing buildpath to "));
  760.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1))
  761.             == (char *)NULL)
  762.             return MPN_NOMEM;
  763.         if ((rootlen > 0) && !renamed_fullpath) {
  764.             strcpy(buildpath, rootpath);
  765.             end = buildpath + rootlen;
  766.         } else {
  767.             *buildpath = '\0';
  768.             end = buildpath;
  769.         }
  770.         Trace((stderr, "[%s]\n", FnFilter1(buildpath)));
  771.         return MPN_OK;
  772.     }
  773.  
  774. /*---------------------------------------------------------------------------
  775.     ROOT:  if appropriate, store the path in rootpath and create it if
  776.     necessary; else assume it's a zipfile member and return.  This path
  777.     segment gets used in extracting all members from every zipfile specified
  778.     on the command line.
  779.   ---------------------------------------------------------------------------*/
  780.  
  781. #if (!defined(SFX) || defined(SFX_EXDIR))
  782.     if (FUNCTION == ROOT) {
  783.         Trace((stderr, "initializing root path to [%s]\n",
  784.           FnFilter1(pathcomp)));
  785.         if (pathcomp == (char *)NULL) {
  786.             rootlen = 0;
  787.             return MPN_OK;
  788.         }
  789.         if (rootlen > 0)        /* rootpath was already set, nothing to do */
  790.             return MPN_OK;
  791.         if ((rootlen = strlen(pathcomp)) > 0) {
  792.             char *tmproot;
  793.  
  794.             if ((tmproot = (char *)malloc(rootlen+2)) == (char *)NULL) {
  795.                 rootlen = 0;
  796.                 return MPN_NOMEM;
  797.             }
  798.             strcpy(tmproot, pathcomp);
  799.             if (tmproot[rootlen-1] == TANDEM_DELIMITER) {
  800.                 tmproot[--rootlen] = '\0';
  801.             }
  802.             if (rootlen > 0 && (stat(tmproot, &G.statbuf) ||
  803.                                 !S_ISDIR(G.statbuf.st_mode)))
  804.             {   /* path does not exist */
  805.                 if (!G.create_dirs /* || iswild(tmproot) */ ) {
  806.                     free(tmproot);
  807.                     rootlen = 0;
  808.                     /* skip (or treat as stored file) */
  809.                     return MPN_INF_SKIP;
  810.                 }
  811.                 /* create the directory (could add loop here scanning tmproot
  812.                  * to create more than one level, but why really necessary?) */
  813.                 if (mkdir(tmproot, 0777) == -1) {
  814.                     Info(slide, 1, ((char *)slide,
  815.                       "checkdir:  cannot create extraction directory: %s\n",
  816.                       FnFilter1(tmproot)));
  817.                     free(tmproot);
  818.                     rootlen = 0;
  819.                     /* path didn't exist, tried to create, and failed: */
  820.                     /* file exists, or 2+ subdir levels required */
  821.                     return MPN_ERR_SKIP;
  822.                 }
  823.             }
  824.             tmproot[rootlen++] = TANDEM_DELIMITER;
  825.             tmproot[rootlen] = '\0';
  826.             if ((rootpath = (char *)realloc(tmproot, rootlen+1)) == NULL) {
  827.                 free(tmproot);
  828.                 rootlen = 0;
  829.                 return MPN_NOMEM;
  830.             }
  831.             Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath)));
  832.         }
  833.         return MPN_OK;
  834.     }
  835. #endif /* !SFX || SFX_EXDIR */
  836.  
  837. /*---------------------------------------------------------------------------
  838.     END:  free rootpath, immediately prior to program exit.
  839.   ---------------------------------------------------------------------------*/
  840.  
  841.     if (FUNCTION == END) {
  842.         Trace((stderr, "freeing rootpath\n"));
  843.         if (rootlen > 0) {
  844.             free(rootpath);
  845.             rootlen = 0;
  846.         }
  847.         return MPN_OK;
  848.     }
  849.  
  850.     return MPN_INVALID; /* should never reach */
  851.  
  852. } /* end function checkdir() */
  853.  
  854.  
  855.  
  856. /********************/
  857. /* Function mkdir() */
  858. /********************/
  859.  
  860. int mkdir(path, mode)
  861. const char *path;  /* both    */
  862. mode_t mode;       /* ignored */
  863. /*
  864.  * returns:   0 - successful
  865.  *           -1 - failed (errno not set, however)
  866.  */
  867. {
  868.     return 0;
  869. }
  870.  
  871.  
  872. /****************************/
  873. /* Function close_outfile() */
  874. /****************************/
  875.  
  876. void close_outfile(__G)    /* GRR: change to return PK-style warning level */
  877.     __GDEF
  878. {
  879.     union {
  880.         iztimes t3;             /* mtime, atime, ctime */
  881.         ztimbuf t2;             /* modtime, actime */
  882.     } zt;
  883.     ulg z_uidgid[2];
  884.     unsigned eb_izux_flg;
  885.     nsk_file_attrs *znsk_attr;
  886.     short err;
  887.     unsigned short len;
  888.  
  889.     #define alist_items 1
  890.     #define vlist_bytes 2
  891.     short alist[alist_items]={42     };
  892.     unsigned short vlist[alist_items];
  893.     short extra, *err_item=&extra;
  894.  
  895.     char nsk_work[FILENAME_MAX + 1], *nsk_fname=&nsk_work[0];
  896.  
  897.     fclose(G.outfile);
  898.  
  899.     /* Set up Tandem specific file information if present */
  900.     znsk_attr = ef_scan_for_tandem(G.extra_field, G.lrec.extra_field_length);
  901.  
  902.     /* Currently we do not create 'proper' Tandem files in unzip - we only  */
  903.     /* create unstructured (180) or edit (101) files.  This code allows     */
  904.     /* some limited support for restoration of Tandem file attributes       */
  905.  
  906.     if (znsk_attr != NULL) {
  907.       /* Reset File Code */
  908.       if (!uO.bflag && !G.pInfo->textmode
  909.           && znsk_attr->filetype == NSK_UNSTRUCTURED) {
  910.         /* leave type 101 files alone */
  911.         vlist[0] = znsk_attr->filecode;
  912.  
  913.         /* Note that FILE_ALTERLIST_ expect uppercase names and that        */
  914.         /* G.lrec.filename_length is the length of original zipped filename */
  915.         /* not any 'rename' name.  Need to re call strlen and upshift       */
  916.         len = strlen(G.filename);
  917.         err = STRING_UPSHIFT_(G.filename,
  918.                               len,
  919.                               nsk_fname,
  920.                               len);
  921.  
  922.         err = FILE_ALTERLIST_(nsk_fname,
  923.                               len,
  924.                               alist,
  925.                               alist_items,
  926.                               vlist,
  927.                               vlist_bytes,
  928.                               ,
  929.                               err_item);
  930.       }
  931.     }
  932.  
  933.  
  934. /*---------------------------------------------------------------------------
  935.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  936.     time:  adjust base year from 1980 to 1970, do usual conversions from
  937.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  938.     light savings time differences.  If we have a Unix extra field, however,
  939.     we're laughing:  both mtime and atime are ours.  On the other hand, we
  940.     then have to check for restoration of UID/GID.
  941.   ---------------------------------------------------------------------------*/
  942.  
  943.     eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
  944.                    G.lrec.extra_field_length, 0, G.lrec.last_mod_dos_datetime,
  945. #ifdef IZ_CHECK_TZ
  946.                    (G.tz_is_valid ? &(zt.t3) : NULL),
  947. #else
  948.                    &(zt.t3),
  949. #endif
  950.                    z_uidgid) : 0);
  951.     if (eb_izux_flg & EB_UT_FL_MTIME) {
  952.         TTrace((stderr, "\nclose_outfile:  Unix e.f. modif. time = %ld\n",
  953.           zt.t3.mtime));
  954.     } else {
  955.         zt.t3.mtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  956.     }
  957.     if (eb_izux_flg & EB_UT_FL_ATIME) {
  958.         TTrace((stderr, "close_outfile:  Unix e.f. access time = %ld\n",
  959.           zt.t3.atime));
  960.     } else {
  961.         zt.t3.atime = zt.t3.mtime;
  962.         TTrace((stderr, "\nclose_outfile:  modification/access times = %ld\n",
  963.           zt.t3.mtime));
  964.     }
  965.  
  966. /*---------------------------------------------------------------------------
  967.     Change the file's last modified time to that stored in the zipfile.
  968.     Not sure how (yet) or whether it's a good idea to set the last open time
  969.   ---------------------------------------------------------------------------*/
  970.  
  971.     /* skip restoring time stamps on user's request */
  972.     if (uO.D_flag <= 1) {
  973.         /* set the file's access and modification times */
  974.         if (utime(G.filename, &(zt.t2)))
  975.             if (uO.qflag)
  976.                 Info(slide, 0x201, ((char *)slide,
  977.                   "warning:  cannot set times for %s\n",
  978.                   FnFilter1(G.filename)));
  979.             else
  980.                 Info(slide, 0x201, ((char *)slide,
  981.                   " (warning) cannot set times"));
  982.     }
  983.  
  984. /*---------------------------------------------------------------------------
  985.     Change the file permissions from default ones to those stored in the
  986.     zipfile.
  987.   ---------------------------------------------------------------------------*/
  988.  
  989. #ifndef NO_CHMOD
  990.     if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  991.         perror("chmod (file attributes) error");
  992. #endif
  993.  
  994. /*---------------------------------------------------------------------------
  995.        if -X option was specified and we have UID/GID info, restore it
  996.        this must come after the file security and modtimes changes - since once
  997.        we have secured the file to somebody else we cannot access it again.
  998.   ---------------------------------------------------------------------------*/
  999.  
  1000.     if (uO.X_flag && eb_izux_flg & EB_UX2_VALID) {
  1001.         TTrace((stderr, "close_outfile:  restoring Unix UID/GID info\n"));
  1002.         if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
  1003.         {
  1004.             if (uO.qflag)
  1005.                 Info(slide, 0x201, ((char *)slide,
  1006.                   "warning:  cannot set UID %lu and/or GID %lu for %s\n",
  1007.                   z_uidgid[0], z_uidgid[1], FnFilter1(G.filename)));
  1008.             else
  1009.                 Info(slide, 0x201, ((char *)slide,
  1010.                   " (warning) cannot set UID %lu and/or GID %lu",
  1011.                   z_uidgid[0], z_uidgid[1]));
  1012.         }
  1013.     }
  1014.  
  1015. } /* end function close_outfile() */
  1016.  
  1017.  
  1018. #ifndef SFX
  1019.  
  1020. /************************/
  1021. /*  Function version()  */
  1022. /************************/
  1023.  
  1024. void version(__G)
  1025.     __GDEF
  1026. {
  1027.  
  1028.     /* Pyramid, NeXT have problems with huge macro expansion, too: no Info() */
  1029.     sprintf((char *)slide, LoadFarString(CompiledWith),
  1030.  
  1031.     "C ",
  1032.     "T9255D44 - (16OCT98)",
  1033.     "NonStop ",
  1034.     "(Tandem/NSK)",
  1035.  
  1036. #ifdef __DATE__
  1037.       " on ", __DATE__
  1038. #else
  1039.       "", ""
  1040. #endif
  1041.     );
  1042.  
  1043.     (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  1044.  
  1045. } /* end function version() */
  1046.  
  1047. #endif /* !SFX */
  1048.