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

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unix.c
  4.  
  5.   Unix-specific routines for use with Info-ZIP's UnZip 5.3 and later.
  6.  
  7.   Contains:  readdir()
  8.              do_wild()           <-- generic enough to put in fileio.c?
  9.              mapattr()
  10.              mapname()
  11.              checkdir()
  12.              mkdir()
  13.              close_outfile()
  14.              stamp_file()
  15.              version()
  16.  
  17.   ---------------------------------------------------------------------------*/
  18.  
  19.  
  20. #define UNZIP_INTERNAL
  21. #include "unzip.h"
  22.  
  23. #ifdef SCO_XENIX
  24. #  define SYSNDIR
  25. #else  /* SCO Unix, AIX, DNIX, TI SysV, Coherent 4.x, ... */
  26. #  if defined(__convexc__) || defined(SYSV) || defined(CRAY) || defined(BSD4_4)
  27. #    define DIRENT
  28. #  endif
  29. #endif
  30. #if defined(_AIX)
  31. #  define DIRENT
  32. #endif
  33. #ifdef COHERENT
  34. #  if defined(_I386) || (defined(__COHERENT__) && (__COHERENT__ >= 0x420))
  35. #    define DIRENT
  36. #  endif
  37. #endif
  38.  
  39. /* GRR:  may need to uncomment this: */
  40. #if 0
  41. #if defined(_POSIX_VERSION)
  42. #  define DIRENT
  43. #endif
  44. #endif
  45.  
  46. #ifdef DIRENT
  47. #  include <dirent.h>
  48. #else
  49. #  ifdef SYSV
  50. #    ifdef SYSNDIR
  51. #      include <sys/ndir.h>
  52. #    else
  53. #      include <ndir.h>
  54. #    endif
  55. #  else /* !SYSV */
  56. #    ifndef NO_SYSDIR
  57. #      include <sys/dir.h>
  58. #    endif
  59. #  endif /* ?SYSV */
  60. #  ifndef dirent
  61. #    define dirent direct
  62. #  endif
  63. #endif /* ?DIRENT */
  64.  
  65. static int created_dir;        /* used in mapname(), checkdir() */
  66. static int renamed_fullpath;   /* ditto */
  67.  
  68.  
  69. #ifndef SFX
  70. #ifdef NO_DIR                  /* for AT&T 3B1 */
  71.  
  72. #define opendir(path) fopen(path,"r")
  73. #define closedir(dir) fclose(dir)
  74. typedef FILE DIR;
  75.  
  76. /*
  77.  *  Apparently originally by Rich Salz.
  78.  *  Cleaned up and modified by James W. Birdsall.
  79.  */
  80. struct dirent *readdir(dirp)
  81.     DIR *dirp;
  82. {
  83.     static struct dirent entry;
  84.  
  85.     if (dirp == NULL)
  86.         return NULL;
  87.  
  88.     for (;;)
  89.         if (fread(&entry, sizeof (struct dirent), 1, dirp) == 0)
  90.             return (struct dirent *)NULL;
  91.         else if (entry.d_ino)
  92.             return &entry;
  93.  
  94. } /* end function readdir() */
  95.  
  96. #endif /* NO_DIR */
  97.  
  98.  
  99. /**********************/
  100. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  101. /**********************/
  102.  
  103. char *do_wild(__G__ wildspec)
  104.     __GDEF
  105.     char *wildspec;         /* only used first time on a given dir */
  106. {
  107.     static DIR *dir = (DIR *)NULL;
  108.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  109.     static int firstcall=TRUE, have_dirname, dirnamelen;
  110.     struct dirent *file;
  111.  
  112.  
  113.     /* Even when we're just returning wildspec, we *always* do so in
  114.      * matchname[]--calling routine is allowed to append four characters
  115.      * to the returned string, and wildspec may be a pointer to argv[].
  116.      */
  117.     if (firstcall) {        /* first call:  must initialize everything */
  118.         firstcall = FALSE;
  119.  
  120.         /* break the wildspec into a directory part and a wildcard filename */
  121.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL) {
  122.             dirname = ".";
  123.             dirnamelen = 1;
  124.             have_dirname = FALSE;
  125.             wildname = wildspec;
  126.         } else {
  127.             ++wildname;     /* point at character after '/' */
  128.             dirnamelen = wildname - wildspec;
  129.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  130.                 Info(slide, 0x201, ((char *)slide,
  131.                   "warning:  cannot allocate wildcard buffers\n"));
  132.                 strcpy(matchname, wildspec);
  133.                 return matchname;   /* but maybe filespec was not a wildcard */
  134.             }
  135.             strncpy(dirname, wildspec, dirnamelen);
  136.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  137.             have_dirname = TRUE;
  138.         }
  139.  
  140.         if ((dir = opendir(dirname)) != (DIR *)NULL) {
  141.             while ((file = readdir(dir)) != (struct dirent *)NULL) {
  142.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  143.                     continue;  /* Unix:  '*' and '?' do not match leading dot */
  144.                 if (match(file->d_name, wildname, 0)) {  /* 0 == case sens. */
  145.                     if (have_dirname) {
  146.                         strcpy(matchname, dirname);
  147.                         strcpy(matchname+dirnamelen, file->d_name);
  148.                     } else
  149.                         strcpy(matchname, file->d_name);
  150.                     return matchname;
  151.                 }
  152.             }
  153.             /* if we get to here directory is exhausted, so close it */
  154.             closedir(dir);
  155.             dir = (DIR *)NULL;
  156.         }
  157.  
  158.         /* return the raw wildspec in case that works (e.g., directory not
  159.          * searchable, but filespec was not wild and file is readable) */
  160.         strcpy(matchname, wildspec);
  161.         return matchname;
  162.     }
  163.  
  164.     /* last time through, might have failed opendir but returned raw wildspec */
  165.     if (dir == (DIR *)NULL) {
  166.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  167.         if (have_dirname)
  168.             free(dirname);
  169.         return (char *)NULL;
  170.     }
  171.  
  172.     /* If we've gotten this far, we've read and matched at least one entry
  173.      * successfully (in a previous call), so dirname has been copied into
  174.      * matchname already.
  175.      */
  176.     while ((file = readdir(dir)) != (struct dirent *)NULL)
  177.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  178.             if (have_dirname) {
  179.                 /* strcpy(matchname, dirname); */
  180.                 strcpy(matchname+dirnamelen, file->d_name);
  181.             } else
  182.                 strcpy(matchname, file->d_name);
  183.             return matchname;
  184.         }
  185.  
  186.     closedir(dir);     /* have read at least one dir entry; nothing left */
  187.     dir = (DIR *)NULL;
  188.     firstcall = TRUE;  /* reset for new wildspec */
  189.     if (have_dirname)
  190.         free(dirname);
  191.     return (char *)NULL;
  192.  
  193. } /* end function do_wild() */
  194.  
  195. #endif /* !SFX */
  196.  
  197.  
  198.  
  199.  
  200.  
  201. /**********************/
  202. /* Function mapattr() */
  203. /**********************/
  204.  
  205. int mapattr(__G)
  206.     __GDEF
  207. {
  208.     ulg tmp = G.crec.external_file_attributes;
  209.  
  210.     switch (G.pInfo->hostnum) {
  211.         case UNIX_:
  212.         case VMS_:
  213.         case ACORN_:
  214.         case ATARI_:
  215.         case BEOS_:
  216.         case QDOS_:
  217.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  218.             return 0;
  219.         case AMIGA_:
  220.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  221.             G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  222.             break;
  223.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  224.         case FS_FAT_:
  225.         case FS_HPFS_:
  226.         case FS_NTFS_:
  227.         case MAC_:
  228.         case TOPS20_:
  229.         default:
  230.             /* read-only bit --> write perms; subdir bit --> dir exec bit */
  231.             tmp = !(tmp & 1) << 1  |  (tmp & 0x10) >> 4;
  232.             G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  233.             break;
  234.     } /* end switch (host-OS-created-by) */
  235.  
  236.     /* for originating systems with no concept of "group," "other," "system": */
  237.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  238.     G.pInfo->file_attr &= ~tmp;
  239.  
  240.     return 0;
  241.  
  242. } /* end function mapattr() */
  243.  
  244.  
  245.  
  246.  
  247.  
  248. /************************/
  249. /*  Function mapname()  */
  250. /************************/
  251.                              /* return 0 if no error, 1 if caution (filename */
  252. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  253.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  254.     int renamed;             /*  or 10 if out of memory (skip file) */
  255. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  256.     char pathcomp[FILNAMSIZ];      /* path-component buffer */
  257.     char *pp, *cp=(char *)NULL;    /* character pointers */
  258.     char *lastsemi=(char *)NULL;   /* pointer to last semi-colon in pathcomp */
  259.     int quote = FALSE;             /* flags */
  260.     int error = 0;
  261.     register unsigned workch;      /* hold the character being tested */
  262.  
  263.  
  264. /*---------------------------------------------------------------------------
  265.     Initialize various pointers and counters and stuff.
  266.   ---------------------------------------------------------------------------*/
  267.  
  268.     if (G.pInfo->vollabel)
  269.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  270.  
  271.     /* can create path as long as not just freshening, or if user told us */
  272.     G.create_dirs = (!G.fflag || renamed);
  273.  
  274.     created_dir = FALSE;        /* not yet */
  275.  
  276.     /* user gave full pathname:  don't prepend rootpath */
  277.     renamed_fullpath = (renamed && (*G.filename == '/'));
  278.  
  279.     if (checkdir(__G__ (char *)NULL, INIT) == 10)
  280.         return 10;              /* initialize path buffer, unless no memory */
  281.  
  282.     *pathcomp = '\0';           /* initialize translation buffer */
  283.     pp = pathcomp;              /* point to translation buffer */
  284.     if (G.jflag)                /* junking directories */
  285.         cp = (char *)strrchr(G.filename, '/');
  286.     if (cp == (char *)NULL)     /* no '/' or not junking dirs */
  287.         cp = G.filename;        /* point to internal zipfile-member pathname */
  288.     else
  289.         ++cp;                   /* point to start of last component of path */
  290.  
  291. /*---------------------------------------------------------------------------
  292.     Begin main loop through characters in filename.
  293.   ---------------------------------------------------------------------------*/
  294.  
  295.     while ((workch = (uch)*cp++) != 0) {
  296.  
  297.         if (quote) {                 /* if character quoted, */
  298.             *pp++ = (char)workch;    /*  include it literally */
  299.             quote = FALSE;
  300.         } else
  301.             switch (workch) {
  302.             case '/':             /* can assume -j flag not given */
  303.                 *pp = '\0';
  304.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  305.                     return error;
  306.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  307.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  308.                 break;
  309.  
  310.             case ';':             /* VMS version (or DEC-20 attrib?) */
  311.                 lastsemi = pp;
  312.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  313.                 break;            /*  later, if requested */
  314.  
  315.             case '\026':          /* control-V quote for special chars */
  316.                 quote = TRUE;     /* set flag for next character */
  317.                 break;
  318.  
  319. #ifdef MTS
  320.             case ' ':             /* change spaces to underscore under */
  321.                 *pp++ = '_';      /*  MTS; leave as spaces under Unix */
  322.                 break;
  323. #endif
  324.  
  325.             default:
  326.                 /* allow European characters in filenames: */
  327.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  328.                     *pp++ = (char)workch;
  329.             } /* end switch */
  330.  
  331.     } /* end while loop */
  332.  
  333.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  334.  
  335.     /* if not saving them, remove VMS version numbers (appended ";###") */
  336.     if (!G.V_flag && lastsemi) {
  337.         pp = lastsemi + 1;
  338.         while (isdigit((uch)(*pp)))
  339.             ++pp;
  340.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  341.             *lastsemi = '\0';
  342.     }
  343.  
  344. /*---------------------------------------------------------------------------
  345.     Report if directory was created (and no file to create:  filename ended
  346.     in '/'), check name to be sure it exists, and combine path and name be-
  347.     fore exiting.
  348.   ---------------------------------------------------------------------------*/
  349.  
  350.     if (G.filename[strlen(G.filename) - 1] == '/') {
  351.         checkdir(__G__ G.filename, GETPATH);
  352.         if (created_dir) {
  353.             if (QCOND2) {
  354.                 Info(slide, 0, ((char *)slide, "   creating: %s\n",
  355.                   G.filename));
  356.             }
  357. #ifndef NO_CHMOD
  358.             /* set approx. dir perms (make sure can still read/write in dir) */
  359.             if (chmod(G.filename, (0xffff & G.pInfo->file_attr) | 0700))
  360.                 perror("chmod (directory attributes) error");
  361. #endif
  362.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  363.         }
  364.         return 2;   /* dir existed already; don't look for data to extract */
  365.     }
  366.  
  367.     if (*pathcomp == '\0') {
  368.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  369.           G.filename));
  370.         return 3;
  371.     }
  372.  
  373.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  374.     checkdir(__G__ G.filename, GETPATH);
  375.  
  376.     return error;
  377.  
  378. } /* end function mapname() */
  379.  
  380.  
  381.  
  382.  
  383. #if 0  /*========== NOTES ==========*/
  384.  
  385.   extract-to dir:      a:path/
  386.   buildpath:           path1/path2/ ...   (NULL-terminated)
  387.   pathcomp:                filename
  388.  
  389.   mapname():
  390.     loop over chars in zipfile member name
  391.       checkdir(path component, COMPONENT | CREATEDIR) --> map as required?
  392.         (d:/tmp/unzip/)                    (disk:[tmp.unzip.)
  393.         (d:/tmp/unzip/jj/)                 (disk:[tmp.unzip.jj.)
  394.         (d:/tmp/unzip/jj/temp/)            (disk:[tmp.unzip.jj.temp.)
  395.     finally add filename itself and check for existence? (could use with rename)
  396.         (d:/tmp/unzip/jj/temp/msg.outdir)  (disk:[tmp.unzip.jj.temp]msg.outdir)
  397.     checkdir(name, GETPATH)     -->  copy path to name and free space
  398.  
  399. #endif /* 0 */
  400.  
  401.  
  402.  
  403.  
  404. /***********************/
  405. /* Function checkdir() */
  406. /***********************/
  407.  
  408. int checkdir(__G__ pathcomp, flag)
  409.     __GDEF
  410.     char *pathcomp;
  411.     int flag;
  412. /*
  413.  * returns:  1 - (on APPEND_NAME) truncated filename
  414.  *           2 - path doesn't exist, not allowed to create
  415.  *           3 - path doesn't exist, tried to create and failed; or
  416.  *               path exists and is not a directory, but is supposed to be
  417.  *           4 - path is too long
  418.  *          10 - can't allocate memory for filename buffers
  419.  */
  420. {
  421.     static int rootlen = 0;   /* length of rootpath */
  422.     static char *rootpath;    /* user's "extract-to" directory */
  423.     static char *buildpath;   /* full path (so far) to extracted file */
  424.     static char *end;         /* pointer to end of buildpath ('\0') */
  425.  
  426. #   define FN_MASK   7
  427. #   define FUNCTION  (flag & FN_MASK)
  428.  
  429.  
  430.  
  431. /*---------------------------------------------------------------------------
  432.     APPEND_DIR:  append the path component to the path being built and check
  433.     for its existence.  If doesn't exist and we are creating directories, do
  434.     so for this one; else signal success or error as appropriate.
  435.   ---------------------------------------------------------------------------*/
  436.  
  437.     if (FUNCTION == APPEND_DIR) {
  438.         int too_long = FALSE;
  439. #ifdef SHORT_NAMES
  440.         char *old_end = end;
  441. #endif
  442.  
  443.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  444.         while ((*end = *pathcomp++) != '\0')
  445.             ++end;
  446. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  447.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  448.             *(end = old_end + FILENAME_MAX) = '\0';
  449. #endif
  450.  
  451.         /* GRR:  could do better check, see if overrunning buffer as we go:
  452.          * check end-buildpath after each append, set warning variable if
  453.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  454.          * appending.  Clear variable when begin new path. */
  455.  
  456.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  457.             too_long = TRUE;                /* check if extracting directory? */
  458.         if (stat(buildpath, &G.statbuf)) {  /* path doesn't exist */
  459.             if (!G.create_dirs) { /* told not to create (freshening) */
  460.                 free(buildpath);
  461.                 return 2;         /* path doesn't exist:  nothing to do */
  462.             }
  463.             if (too_long) {
  464.                 Info(slide, 1, ((char *)slide,
  465.                   "checkdir error:  path too long: %s\n", buildpath));
  466.                 free(buildpath);
  467.                 return 4;         /* no room for filenames:  fatal */
  468.             }
  469.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  470.                 Info(slide, 1, ((char *)slide,
  471.                   "checkdir error:  cannot create %s\n\
  472.                  unable to process %s.\n", buildpath, G.filename));
  473.                 free(buildpath);
  474.                 return 3;      /* path didn't exist, tried to create, failed */
  475.             }
  476.             created_dir = TRUE;
  477.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  478.             Info(slide, 1, ((char *)slide,
  479.               "checkdir error:  %s exists but is not directory\n\
  480.                  unable to process %s.\n", buildpath, G.filename));
  481.             free(buildpath);
  482.             return 3;          /* path existed but wasn't dir */
  483.         }
  484.         if (too_long) {
  485.             Info(slide, 1, ((char *)slide,
  486.               "checkdir error:  path too long: %s\n", buildpath));
  487.             free(buildpath);
  488.             return 4;         /* no room for filenames:  fatal */
  489.         }
  490.         *end++ = '/';
  491.         *end = '\0';
  492.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  493.         return 0;
  494.  
  495.     } /* end if (FUNCTION == APPEND_DIR) */
  496.  
  497. /*---------------------------------------------------------------------------
  498.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  499.     buildpath.
  500.   ---------------------------------------------------------------------------*/
  501.  
  502.     if (FUNCTION == GETPATH) {
  503.         strcpy(pathcomp, buildpath);
  504.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  505.         free(buildpath);
  506.         buildpath = end = (char *)NULL;
  507.         return 0;
  508.     }
  509.  
  510. /*---------------------------------------------------------------------------
  511.     APPEND_NAME:  assume the path component is the filename; append it and
  512.     return without checking for existence.
  513.   ---------------------------------------------------------------------------*/
  514.  
  515.     if (FUNCTION == APPEND_NAME) {
  516. #ifdef SHORT_NAMES
  517.         char *old_end = end;
  518. #endif
  519.  
  520.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  521.         while ((*end = *pathcomp++) != '\0') {
  522.             ++end;
  523. #ifdef SHORT_NAMES  /* truncate name at 14 characters, typically */
  524.             if ((end-old_end) > FILENAME_MAX)      /* GRR:  proper constant? */
  525.                 *(end = old_end + FILENAME_MAX) = '\0';
  526. #endif
  527.             if ((end-buildpath) >= FILNAMSIZ) {
  528.                 *--end = '\0';
  529.                 Info(slide, 0x201, ((char *)slide,
  530.                   "checkdir warning:  path too long; truncating\n\
  531.                    %s\n                -> %s\n", G.filename, buildpath));
  532.                 return 1;   /* filename truncated */
  533.             }
  534.         }
  535.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  536.         return 0;  /* could check for existence here, prompt for new name... */
  537.     }
  538.  
  539. /*---------------------------------------------------------------------------
  540.     INIT:  allocate and initialize buffer space for the file currently being
  541.     extracted.  If file was renamed with an absolute path, don't prepend the
  542.     extract-to path.
  543.   ---------------------------------------------------------------------------*/
  544.  
  545. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  546.  
  547.     if (FUNCTION == INIT) {
  548.         Trace((stderr, "initializing buildpath to "));
  549.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1)) ==
  550.             (char *)NULL)
  551.             return 10;
  552.         if ((rootlen > 0) && !renamed_fullpath) {
  553.             strcpy(buildpath, rootpath);
  554.             end = buildpath + rootlen;
  555.         } else {
  556.             *buildpath = '\0';
  557.             end = buildpath;
  558.         }
  559.         Trace((stderr, "[%s]\n", buildpath));
  560.         return 0;
  561.     }
  562.  
  563. /*---------------------------------------------------------------------------
  564.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  565.     sary; else assume it's a zipfile member and return.  This path segment
  566.     gets used in extracting all members from every zipfile specified on the
  567.     command line.
  568.   ---------------------------------------------------------------------------*/
  569.  
  570. #if (!defined(SFX) || defined(SFX_EXDIR))
  571.     if (FUNCTION == ROOT) {
  572.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  573.         if (pathcomp == (char *)NULL) {
  574.             rootlen = 0;
  575.             return 0;
  576.         }
  577.         if ((rootlen = strlen(pathcomp)) > 0) {
  578.             if (pathcomp[rootlen-1] == '/') {
  579.                 pathcomp[--rootlen] = '\0';
  580.             }
  581.             if (rootlen > 0 && (stat(pathcomp, &G.statbuf) ||
  582.                 !S_ISDIR(G.statbuf.st_mode)))        /* path does not exist */
  583.             {
  584.                 if (!G.create_dirs /* || iswild(pathcomp) */ ) {
  585.                     rootlen = 0;
  586.                     return 2;   /* skip (or treat as stored file) */
  587.                 }
  588.                 /* create the directory (could add loop here to scan pathcomp
  589.                  * and create more than one level, but why really necessary?) */
  590.                 if (mkdir(pathcomp, 0777) == -1) {
  591.                     Info(slide, 1, ((char *)slide,
  592.                       "checkdir:  cannot create extraction directory: %s\n",
  593.                       pathcomp));
  594.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  595.                     return 3;  /* failed:  file exists, or 2+ levels required */
  596.                 }
  597.             }
  598.             if ((rootpath = (char *)malloc(rootlen+2)) == (char *)NULL) {
  599.                 rootlen = 0;
  600.                 return 10;
  601.             }
  602.             strcpy(rootpath, pathcomp);
  603.             rootpath[rootlen++] = '/';
  604.             rootpath[rootlen] = '\0';
  605.             Trace((stderr, "rootpath now = [%s]\n", rootpath));
  606.         }
  607.         return 0;
  608.     }
  609. #endif /* !SFX || SFX_EXDIR */
  610.  
  611. /*---------------------------------------------------------------------------
  612.     END:  free rootpath, immediately prior to program exit.
  613.   ---------------------------------------------------------------------------*/
  614.  
  615.     if (FUNCTION == END) {
  616.         Trace((stderr, "freeing rootpath\n"));
  617.         if (rootlen > 0) {
  618.             free(rootpath);
  619.             rootlen = 0;
  620.         }
  621.         return 0;
  622.     }
  623.  
  624.     return 99;  /* should never reach */
  625.  
  626. } /* end function checkdir() */
  627.  
  628.  
  629.  
  630.  
  631.  
  632. #ifdef NO_MKDIR
  633.  
  634. /********************/
  635. /* Function mkdir() */
  636. /********************/
  637.  
  638. int mkdir(path, mode)
  639.     char *path;
  640.     int mode;   /* ignored */
  641. /*
  642.  * returns:   0 - successful
  643.  *           -1 - failed (errno not set, however)
  644.  */
  645. {
  646.     char command[FILNAMSIZ+40]; /* buffer for system() call */
  647.  
  648.     /* GRR 930416:  added single quotes around path to avoid bug with
  649.      * creating directories with ampersands in name; not yet tested */
  650.     sprintf(command, "IFS=\" \t\n\" /bin/mkdir '%s' 2>/dev/null", path);
  651.     if (system(command))
  652.         return -1;
  653.     return 0;
  654. }
  655.  
  656. #endif /* NO_MKDIR */
  657.  
  658.  
  659.  
  660.  
  661.  
  662. #if 0
  663. #ifdef MORE
  664.  
  665. /**************************/
  666. /* Function screenlines() */
  667. /**************************/
  668.  
  669. int screenlines()
  670. {
  671.     char *envptr, *getenv();
  672.     int n;
  673.  
  674.     /* GRR:  this is overly simplistic; should use winsize struct and
  675.      * appropriate TIOCGWINSZ ioctl(), assuming exists on enough systems
  676.      */
  677.     envptr = getenv("LINES");
  678.     if (envptr == (char *)NULL || (n = atoi(envptr)) < 5)
  679.         return 24;   /* VT-100 assumed to be minimal hardware */
  680.     else
  681.         return n;
  682. }
  683.  
  684. #endif /* MORE */
  685. #endif /* 0 */
  686.  
  687.  
  688.  
  689.  
  690.  
  691. #ifndef MTS
  692.  
  693. /****************************/
  694. /* Function close_outfile() */
  695. /****************************/
  696.  
  697. void close_outfile(__G)    /* GRR: change to return PK-style warning level */
  698.     __GDEF
  699. {
  700.     iztimes zt;
  701.     ush z_uidgid[2];
  702.     unsigned eb_izux_flg;
  703.  
  704. /*---------------------------------------------------------------------------
  705.     If symbolic links are supported, allocate a storage area, put the uncom-
  706.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  707.     link to start with, we shouldn't have to worry about overflowing unsigned
  708.     ints with unsigned longs.
  709.   ---------------------------------------------------------------------------*/
  710.  
  711. #ifdef SYMLINKS
  712.     if (G.symlnk) {
  713.         unsigned ucsize = (unsigned)G.lrec.ucsize;
  714.         char *linktarget = (char *)malloc((unsigned)G.lrec.ucsize+1);
  715.  
  716.         fclose(G.outfile);                      /* close "data" file... */
  717.         G.outfile = fopen(G.filename, FOPR);    /* ...and reopen for reading */
  718.         if (!linktarget || fread(linktarget, 1, ucsize, G.outfile) !=
  719.                            (int)ucsize)
  720.         {
  721.             Info(slide, 0x201, ((char *)slide,
  722.               "warning:  symbolic link (%s) failed\n", G.filename));
  723.             if (linktarget)
  724.                 free(linktarget);
  725.             fclose(G.outfile);
  726.             return;
  727.         }
  728.         fclose(G.outfile);                  /* close "data" file for good... */
  729.         unlink(G.filename);                 /* ...and delete it */
  730.         linktarget[ucsize] = '\0';
  731.         if (QCOND2)
  732.             Info(slide, 0, ((char *)slide, "-> %s ", linktarget));
  733.         if (symlink(linktarget, G.filename))  /* create the real link */
  734.             perror("symlink error");
  735.         free(linktarget);
  736.         return;                             /* can't set time on symlinks */
  737.     }
  738. #endif /* SYMLINKS */
  739.  
  740.     fclose(G.outfile);
  741. #ifdef QLZIP
  742.     if (G.extra_field) {
  743.         static void qlfix OF((__GPRO__ uch *ef_ptr, unsigned ef_len));
  744.  
  745.         qlfix(__G__ G.extra_field, G.lrec.extra_field_length);
  746.     }
  747. #endif
  748.  
  749. /*---------------------------------------------------------------------------
  750.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  751.     time:  adjust base year from 1980 to 1970, do usual conversions from
  752.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  753.     light savings time differences.  If we have a Unix extra field, however,
  754.     we're laughing:  both mtime and atime are ours.  On the other hand, we
  755.     then have to check for restoration of UID/GID.
  756.   ---------------------------------------------------------------------------*/
  757.  
  758.     eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
  759.                    G.lrec.extra_field_length, 0, G.lrec.last_mod_file_date,
  760.                    &zt, z_uidgid) : 0);
  761.     if (eb_izux_flg & EB_UT_FL_MTIME) {
  762.         TTrace((stderr, "\nclose_outfile:  Unix e.f. modif. time = %ld\n",
  763.           zt.mtime));
  764.     } else {
  765.         zt.mtime = dos_to_unix_time(G.lrec.last_mod_file_date,
  766.                                     G.lrec.last_mod_file_time);
  767.     }
  768.     if (eb_izux_flg & EB_UT_FL_ATIME) {
  769.         TTrace((stderr, "close_outfile:  Unix e.f. access time = %ld\n",
  770.           zt.atime));
  771.     } else {
  772.         zt.atime = zt.mtime;
  773.         TTrace((stderr, "\nclose_outfile:  modification/access times = %ld\n",
  774.           zt.mtime));
  775.     }
  776.  
  777.     /* if -X option was specified and we have UID/GID info, restore it */
  778.     if (G.X_flag && eb_izux_flg & EB_UX2_VALID) {
  779.         TTrace((stderr, "close_outfile:  restoring Unix UID/GID info\n"));
  780.         if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
  781.         {
  782.             if (G.qflag)
  783.                 Info(slide, 0x201, ((char *)slide,
  784.                   "warning:  cannot set UID %d and/or GID %d for %s\n",
  785.                   z_uidgid[0], z_uidgid[1], G.filename));
  786.             else
  787.                 Info(slide, 0x201, ((char *)slide,
  788.                   " (warning) cannot set UID %d and/or GID %d",
  789.                   z_uidgid[0], z_uidgid[1]));
  790. /* GRR: change return type to int and set up to return warning after utime() */
  791.         }
  792.     }
  793.  
  794.     /* set the file's access and modification times */
  795.     if (utime(G.filename, (ztimbuf *)&zt))
  796. #ifdef AOS_VS
  797.         if (G.qflag)
  798.             Info(slide, 0x201, ((char *)slide, "... cannot set time for %s\n",
  799.               G.filename));
  800.         else
  801.             Info(slide, 0x201, ((char *)slide, "... cannot set time"));
  802. #else
  803.         if (G.qflag)
  804.             Info(slide, 0x201, ((char *)slide,
  805.               "warning:  cannot set times for %s\n", G.filename));
  806.         else
  807.             Info(slide, 0x201, ((char *)slide,
  808.               " (warning) cannot set times"));
  809. #endif /* ?AOS_VS */
  810.  
  811. /*---------------------------------------------------------------------------
  812.     Change the file permissions from default ones to those stored in the
  813.     zipfile.
  814.   ---------------------------------------------------------------------------*/
  815.  
  816. #ifndef NO_CHMOD
  817.     if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  818.         perror("chmod (file attributes) error");
  819. #endif
  820.  
  821. } /* end function close_outfile() */
  822.  
  823. #endif /* !MTS */
  824.  
  825.  
  826.  
  827.  
  828. #ifdef TIMESTAMP
  829.  
  830. /***************************/
  831. /*  Function stamp_file()  */
  832. /***************************/
  833.  
  834. int stamp_file(fname, modtime)
  835.     ZCONST char *fname;
  836.     time_t modtime;
  837. {
  838.     ztimbuf tp;
  839.  
  840.     tp.modtime = tp.actime = modtime;
  841.     return (utime(fname, &tp));
  842.  
  843. } /* end function stamp_file() */
  844.  
  845. #endif /* TIMESTAMP */
  846.  
  847.  
  848.  
  849.  
  850. #ifndef SFX
  851.  
  852. /************************/
  853. /*  Function version()  */
  854. /************************/
  855.  
  856. void version(__G)
  857.     __GDEF
  858. {
  859. #if defined(CRAY) || defined(NX_CURRENT_COMPILER_RELEASE) || defined(NetBSD)
  860.     char buf1[40];
  861. #if defined(CRAY) || defined(NX_CURRENT_COMPILER_RELEASE)
  862.     char buf2[40];
  863. #endif
  864. #endif
  865.  
  866.     /* Pyramid, NeXT have problems with huge macro expansion, too:  no Info() */
  867.     sprintf((char *)slide, LoadFarString(CompiledWith),
  868.  
  869. #ifdef __GNUC__
  870. #  ifdef NX_CURRENT_COMPILER_RELEASE
  871.       (sprintf(buf1, "NeXT DevKit %d.%02d ", NX_CURRENT_COMPILER_RELEASE/100,
  872.         NX_CURRENT_COMPILER_RELEASE%100), buf1),
  873.       (strlen(__VERSION__) > 8)? "(gcc)" :
  874.         (sprintf(buf2, "(gcc %s)", __VERSION__), buf2),
  875. #  else
  876.       "gcc ", __VERSION__,
  877. #  endif
  878. #else
  879. #  if defined(CRAY) && defined(_RELEASE)
  880.       "cc ", (sprintf(buf1, "version %d", _RELEASE), buf1),
  881. #  else
  882. #  ifdef __VERSION__
  883.       "cc ", __VERSION__,
  884. #  else
  885.       "cc", "",
  886. #  endif
  887. #  endif
  888. #endif
  889.  
  890.       "Unix",
  891.  
  892. #if defined(sgi) || defined(__sgi)
  893.       " (Silicon Graphics IRIX)",
  894. #else
  895. #ifdef sun
  896. #  ifdef sparc
  897. #    ifdef __SVR4
  898.       " (Sun SPARC/Solaris)",
  899. #    else /* may or may not be SunOS */
  900.       " (Sun SPARC)",
  901. #    endif
  902. #  else
  903. #  if defined(sun386) || defined(i386)
  904.       " (Sun 386i)",
  905. #  else
  906. #  if defined(mc68020) || defined(__mc68020__)
  907.       " (Sun 3)",
  908. #  else /* mc68010 or mc68000:  Sun 2 or earlier */
  909.       " (Sun 2)",
  910. #  endif
  911. #  endif
  912. #  endif
  913. #else
  914. #ifdef __hpux
  915.       " (HP/UX)",
  916. #else
  917. #ifdef __osf__
  918.       " (DEC OSF/1)",
  919. #else
  920. #ifdef _AIX
  921.       " (IBM AIX)",
  922. #else
  923. #ifdef aiws
  924.       " (IBM RT/AIX)",
  925. #else
  926. #if defined(CRAY) || defined(cray)
  927. #  ifdef _UNICOS
  928.       (sprintf(buf2, " (Cray UNICOS release %d)", _UNICOS), buf2),
  929. #  else
  930.       " (Cray UNICOS)",
  931. #  endif
  932. #else
  933. #if defined(uts) || defined(UTS)
  934.       " (Amdahl UTS)",
  935. #else
  936. #ifdef NeXT
  937. #  ifdef mc68000
  938.       " (NeXTStep/black)",
  939. #  else
  940.       " (NeXTStep for Intel)",
  941. #  endif
  942. #else              /* the next dozen or so are somewhat order-dependent */
  943. #ifdef LINUX
  944. #  ifdef __ELF__
  945.       " (Linux ELF)",
  946. #  else
  947.       " (Linux a.out)",
  948. #  endif
  949. #else
  950. #ifdef MINIX
  951.       " (Minix)",
  952. #else
  953. #ifdef M_UNIX
  954.       " (SCO Unix)",
  955. #else
  956. #ifdef M_XENIX
  957.       " (SCO Xenix)",
  958. #else
  959. #ifdef __NetBSD__
  960. #  ifdef NetBSD0_8
  961.       (sprintf(buf1, " (NetBSD 0.8%c)", (char)(NetBSD0_8 - 1 + 'A')), buf1),
  962. #  else
  963. #  ifdef NetBSD0_9
  964.       (sprintf(buf1, " (NetBSD 0.9%c)", (char)(NetBSD0_9 - 1 + 'A')), buf1),
  965. #  else
  966. #  ifdef NetBSD1_0
  967.       (sprintf(buf1, " (NetBSD 1.0%c)", (char)(NetBSD1_0 - 1 + 'A')), buf1),
  968. #  else
  969.       (BSD4_4 == 0.5)? " (NetBSD before 0.9)" : " (NetBSD 1.1 or later)",
  970. #  endif
  971. #  endif
  972. #  endif
  973. #else
  974. #ifdef __FreeBSD__
  975.       (BSD4_4 == 0.5)? " (FreeBSD 1.x)" : " (FreeBSD 2.0 or later)",
  976. #else
  977. #ifdef __bsdi__
  978.       (BSD4_4 == 0.5)? " (BSD/386 1.0)" : " (BSD/386 1.1 or later)",
  979. #else
  980. #ifdef __386BSD__
  981.       (BSD4_4 == 1)? " (386BSD, post-4.4 release)" : " (386BSD)",
  982. #else
  983. #if defined(i486) || defined(__i486) || defined(__i486__)
  984.       " (Intel 486)",
  985. #else
  986. #if defined(i386) || defined(__i386) || defined(__i386__)
  987.       " (Intel 386)",
  988. #else
  989. #ifdef pyr
  990.       " (Pyramid)",
  991. #else
  992. #ifdef ultrix
  993. #  ifdef mips
  994.       " (DEC/MIPS)",
  995. #  else
  996. #  ifdef vax
  997.       " (DEC/VAX)",
  998. #  else /* __alpha? */
  999.       " (DEC/Alpha)",
  1000. #  endif
  1001. #  endif
  1002. #else
  1003. #ifdef gould
  1004.       " (Gould)",
  1005. #else
  1006. #ifdef MTS
  1007.       " (MTS)",
  1008. #else
  1009. #ifdef __convexc__
  1010.       " (Convex)",
  1011. #else
  1012. #ifdef __QNX__
  1013.       " (QNX 4)",
  1014. #else
  1015. #ifdef __QNXNTO__
  1016.       " (QNX Neutrino)",
  1017. #else
  1018.       "",
  1019. #endif /* QNX Neutrino */
  1020. #endif /* QNX 4 */
  1021. #endif /* Convex */
  1022. #endif /* MTS */
  1023. #endif /* Gould */
  1024. #endif /* DEC */
  1025. #endif /* Pyramid */
  1026. #endif /* 386 */
  1027. #endif /* 486 */
  1028. #endif /* 386BSD */
  1029. #endif /* BSDI BSD/386 */
  1030. #endif /* NetBSD */
  1031. #endif /* FreeBSD */
  1032. #endif /* SCO Xenix */
  1033. #endif /* SCO Unix */
  1034. #endif /* Minix */
  1035. #endif /* Linux */
  1036. #endif /* NeXT */
  1037. #endif /* Amdahl */
  1038. #endif /* Cray */
  1039. #endif /* RT/AIX */
  1040. #endif /* AIX */
  1041. #endif /* OSF/1 */
  1042. #endif /* HP/UX */
  1043. #endif /* Sun */
  1044. #endif /* SGI */
  1045.  
  1046. #ifdef __DATE__
  1047.       " on ", __DATE__
  1048. #else
  1049.       "", ""
  1050. #endif
  1051.     );
  1052.  
  1053.     (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  1054.  
  1055. } /* end function version() */
  1056.  
  1057. #endif /* !SFX */
  1058.  
  1059.  
  1060.  
  1061.  
  1062. #ifdef QLZIP
  1063.  
  1064. struct qdirect  {
  1065.     long            d_length __attribute__ ((packed));  /* file length */
  1066.     unsigned char   d_access __attribute__ ((packed));  /* file access type */
  1067.     unsigned char   d_type __attribute__ ((packed));    /* file type */
  1068.     long            d_datalen __attribute__ ((packed)); /* data length */
  1069.     long            d_reserved __attribute__ ((packed));/* Unused */
  1070.     short           d_szname __attribute__ ((packed));  /* size of name */
  1071.     char            d_name[36] __attribute__ ((packed));/* name area */
  1072.     long            d_update __attribute__ ((packed));  /* last update */
  1073.     long            d_refdate __attribute__ ((packed));
  1074.     long            d_backup __attribute__ ((packed));   /* EOD */
  1075. };
  1076.  
  1077. #define LONGID  "QDOS02"
  1078. #define EXTRALEN (sizeof(struct qdirect) + 8)
  1079. #define JBLONGID    "QZHD"
  1080. #define JBEXTRALEN  (sizeof(jbextra)  - 4 * sizeof(char))
  1081.  
  1082. typedef struct {
  1083.     char        eb_header[4] __attribute__ ((packed));  /* place_holder */
  1084.     char        longid[8] __attribute__ ((packed));
  1085.     struct      qdirect     header __attribute__ ((packed));
  1086. } qdosextra;
  1087.  
  1088. typedef struct {
  1089.     char        eb_header[4];                           /* place_holder */
  1090.     char        longid[4];
  1091.     struct      qdirect     header;
  1092. } jbextra;
  1093.  
  1094.  
  1095.  
  1096. /*  The following two functions SH() and LG() convert big-endian short
  1097.  *  and long numbers into native byte order.  They are some kind of
  1098.  *  counterpart to the generic UnZip's makeword() and makelong() functions.
  1099.  */
  1100. static ush SH(ush val)
  1101. {
  1102.     uch swapbuf[2];
  1103.  
  1104.     swapbuf[1] = (uch)(val & 0xff);
  1105.     swapbuf[0] = (uch)(val >> 8);
  1106.     return (*(ush *)swapbuf);
  1107. }
  1108.  
  1109.  
  1110.  
  1111. static ulg LG(ulg val)
  1112. {
  1113.     /*  convert the big-endian unsigned long number `val' to the machine
  1114.      *  dependant representation
  1115.      */
  1116.     ush swapbuf[2];
  1117.  
  1118.     swapbuf[1] = SH((ush)(val & 0xffff));
  1119.     swapbuf[0] = SH((ush)(val >> 16));
  1120.     return (*(ulg *)swapbuf);
  1121. }
  1122.  
  1123.  
  1124.  
  1125. static void qlfix(__G__ ef_ptr, ef_len)
  1126.     __GDEF
  1127.     uch *ef_ptr;
  1128.     unsigned ef_len;
  1129. {
  1130.     while (ef_len >= EB_HEADSIZE)
  1131.     {
  1132.         unsigned    eb_id  = makeword(EB_ID + ef_ptr);
  1133.         unsigned    eb_len = makeword(EB_LEN + ef_ptr);
  1134.  
  1135.         if (eb_len > (ef_len - EB_HEADSIZE)) {
  1136.             /* discovered some extra field inconsistency! */
  1137.             Trace((stderr,
  1138.               "qlfix: block length %u > rest ef_size %u\n", eb_len,
  1139.               ef_len - EB_HEADSIZE));
  1140.             break;
  1141.         }
  1142.  
  1143.         switch (eb_id) {
  1144.           case EF_QDOS:
  1145.           {
  1146.             struct _ntc_
  1147.             {
  1148.                 long id;
  1149.                 long dlen;
  1150.             } ntc;
  1151.             long dlen = 0;
  1152.  
  1153.             qdosextra   *extra = (qdosextra *)ef_ptr;
  1154.             jbextra     *jbp   = (jbextra   *)ef_ptr;
  1155.  
  1156.             if (!strncmp(extra->longid, LONGID, strlen(LONGID)))
  1157.             {
  1158.                 if (eb_len != EXTRALEN)
  1159.                     if (G.qflag)
  1160.                         Info(slide, 0x201, ((char *)slide,
  1161.                           "warning:  invalid length in Qdos field for %s\n",
  1162.                           G.filename));
  1163.                     else
  1164.                         Info(slide, 0x201, ((char *)slide,
  1165.                           "warning:  invalid length in Qdos field"));
  1166.  
  1167.                 if (extra->header.d_type)
  1168.                 {
  1169.                     dlen = extra->header.d_datalen;
  1170.                 }
  1171.             }
  1172.  
  1173.             if (!strncmp(jbp->longid, JBLONGID, strlen(JBLONGID)))
  1174.             {
  1175.                 if (eb_len != JBEXTRALEN)
  1176.                     if (G.qflag)
  1177.                         Info(slide, 0x201, ((char *)slide,
  1178.                           "warning:  invalid length in QZ field for %s\n",
  1179.                           G.filename));
  1180.                     else
  1181.                         Info(slide, 0x201, ((char *)slide,
  1182.                           "warning:  invalid length in QZ field"));
  1183.                 if(jbp->header.d_type)
  1184.                 {
  1185.                     dlen = jbp->header.d_datalen;
  1186.                 }
  1187.             }
  1188.  
  1189.             if ((long)LG(dlen) > 0)
  1190.             {
  1191.                 G.outfile = fopen(G.filename,"r+");
  1192.                 fseek(G.outfile, -8, SEEK_END);
  1193.                 fread(&ntc, 8, 1, G.outfile);
  1194.                 if(ntc.id != *(long *)"XTcc")
  1195.                 {
  1196.                     ntc.id = *(long *)"XTcc";
  1197.                     ntc.dlen = dlen;
  1198.                     fwrite (&ntc, 8, 1, G.outfile);
  1199.                 }
  1200.                 Info(slide, 0x201, ((char *)slide, "QData = %d", LG(dlen)));
  1201.                 fclose(G.outfile);
  1202.             }
  1203.             return;     /* finished, cancel further extra field scanning */
  1204.           }
  1205.  
  1206.           default:
  1207.             Trace((stderr,"qlfix: unknown extra field block, ID=%d\n",
  1208.                eb_id));
  1209.         }
  1210.  
  1211.         /* Skip this extra field block */
  1212.         ef_ptr += (eb_len + EB_HEADSIZE);
  1213.         ef_len -= (eb_len + EB_HEADSIZE);
  1214.     }
  1215. }
  1216. #endif /* QLZIP */
  1217.