home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip52.zip / unix / unix.c < prev    next >
C/C++ Source or Header  |  1996-04-16  |  32KB  |  989 lines

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