home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip511.zip / unix / unix.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  31KB  |  967 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unix.c
  4.  
  5.   Unix-specific routines for use with Info-ZIP's UnZip 5.1 and later.
  6.  
  7.   Contains:  readdir()
  8.              do_wild()           <-- generic enough to put in file_io.c?
  9.              mapattr()
  10.              mapname()
  11.              checkdir()
  12.              mkdir()
  13.              close_outfile()
  14.              version()
  15.  
  16.   ---------------------------------------------------------------------------*/
  17.  
  18.  
  19. #include "unzip.h"
  20.  
  21. /* SCO Unix, AIX, DNIX, TI SysV, Coherent 4.x, ... */
  22. #if defined(__convexc__) || defined(SYSV) || defined(CRAY) || defined(BSD4_4)
  23. #  define DIRENT
  24. #endif
  25. #if defined(_AIX)
  26. #  define DIRENT
  27. #endif
  28. #ifdef COHERENT
  29. #  if defined(_I386) || (defined(__COHERENT__) && (__COHERENT__ >= 0x420))
  30. #    define DIRENT
  31. #  endif
  32. #endif
  33.  
  34. /* GRR:  may need to uncomment one or both of these for the relevant systems */
  35.  
  36. #if 0
  37. #if defined(_POSIX_VERSION)
  38. #  define DIRENT
  39. #endif
  40. #endif
  41.  
  42. #if 0
  43. #if defined(M_XENIX)
  44. #  define SYSNDIR
  45. #endif
  46. #endif
  47.  
  48. #ifdef DIRENT
  49. #  include <dirent.h>
  50. #else
  51. #  ifdef SYSV
  52. #    ifdef SYSNDIR
  53. #      include <sys/ndir.h>
  54. #    else
  55. #      include <ndir.h>
  56. #    endif
  57. #  else /* !SYSV */
  58. #    ifndef NO_SYSDIR
  59. #      include <sys/dir.h>
  60. #    endif
  61. #  endif /* ?SYSV */
  62. #  ifndef dirent
  63. #    define dirent direct
  64. #  endif
  65. #endif /* ?DIRENT */
  66.  
  67. static int created_dir;        /* used in mapname(), checkdir() */
  68. static int renamed_fullpath;   /* ditto */
  69.  
  70.  
  71. #ifndef SFX
  72. #ifdef NO_DIR                    /* for AT&T 3B1 */
  73.  
  74. #define opendir(path) fopen(path,"r")
  75. #define closedir(dir) fclose(dir)
  76. typedef FILE DIR;
  77.  
  78. /*
  79.  *  Apparently originally by Rich Salz.
  80.  *  Cleaned up and modified by James W. Birdsall.
  81.  */
  82. struct dirent *readdir(dirp)
  83.     DIR *dirp;
  84. {
  85.     static struct dirent entry;
  86.  
  87.     if (dirp == NULL) 
  88.         return NULL;
  89.  
  90.     for (;;)
  91.         if (fread(&entry, sizeof (struct dirent), 1, dirp) == 0) 
  92.             return (struct dirent *)NULL;
  93.         else if (entry.d_ino) 
  94.             return &entry;
  95.  
  96. } /* end function readdir() */
  97.  
  98. #endif /* NO_DIR */
  99.  
  100.  
  101. /**********************/
  102. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  103. /**********************/
  104.  
  105. char *do_wild(wildspec)
  106.     char *wildspec;         /* only used first time on a given dir */
  107. {
  108.     static DIR *dir = (DIR *)NULL;
  109.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  110.     static int firstcall=TRUE, have_dirname, dirnamelen;
  111.     struct dirent *file;
  112.  
  113.  
  114.     /* Even when we're just returning wildspec, we *always* do so in
  115.      * matchname[]--calling routine is allowed to append four characters
  116.      * to the returned string, and wildspec may be a pointer to argv[].
  117.      */
  118.     if (firstcall) {        /* first call:  must initialize everything */
  119.         firstcall = FALSE;
  120.  
  121.         /* break the wildspec into a directory part and a wildcard filename */
  122.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL) {
  123.             dirname = ".";
  124.             dirnamelen = 1;
  125.             have_dirname = FALSE;
  126.             wildname = wildspec;
  127.         } else {
  128.             ++wildname;     /* point at character after '/' */
  129.             dirnamelen = wildname - wildspec;
  130.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  131.                 fprintf(stderr, "warning:  can't 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()
  206. {
  207.     ulg tmp = crec.external_file_attributes;
  208.  
  209.     switch (pInfo->hostnum) {
  210.         case UNIX_:
  211.         case VMS_:
  212.             pInfo->file_attr = (unsigned)(tmp >> 16);
  213.             return 0;
  214.         case AMIGA_:
  215.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  216.             pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  217.             break;
  218.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  219.         case FS_FAT_:
  220.         case FS_HPFS_:
  221.         case FS_NTFS_:
  222.         case MAC_:
  223.         case ATARI_:             /* (used to set = 0666) */
  224.         case TOPS20_:
  225.         default:
  226.             tmp = !(tmp & 1) << 1;   /* read-only bit --> write perms bits */
  227.             pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  228.             break;
  229.     } /* end switch (host-OS-created-by) */
  230.  
  231.     /* for originating systems with no concept of "group," "other," "system": */
  232.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  233.     pInfo->file_attr &= ~tmp;
  234.  
  235.     return 0;
  236.  
  237. } /* end function mapattr() */
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /************************/
  244. /*  Function mapname()  */
  245. /************************/
  246.  
  247. int mapname(renamed)  /* return 0 if no error, 1 if caution (filename trunc), */
  248.     int renamed;      /* 2 if warning (skip file because dir doesn't exist), */
  249. {                     /* 3 if error (skip file), 10 if no memory (skip file) */
  250.     char pathcomp[FILNAMSIZ];    /* path-component buffer */
  251.     char *pp, *cp=(char *)NULL;  /* character pointers */
  252.     char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  253.     int quote = FALSE;           /* flags */
  254.     int error = 0;
  255.     register unsigned workch;    /* hold the character being tested */
  256.  
  257.  
  258. /*---------------------------------------------------------------------------
  259.     Initialize various pointers and counters and stuff.
  260.   ---------------------------------------------------------------------------*/
  261.  
  262.     if (pInfo->vollabel)
  263.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  264.  
  265.     /* can create path as long as not just freshening, or if user told us */
  266.     create_dirs = (!fflag || renamed);
  267.  
  268.     created_dir = FALSE;        /* not yet */
  269.  
  270.     /* user gave full pathname:  don't prepend rootpath */
  271.     renamed_fullpath = (renamed && (*filename == '/'));
  272.  
  273.     if (checkdir((char *)NULL, INIT) == 10)
  274.         return 10;              /* initialize path buffer, unless no memory */
  275.  
  276.     *pathcomp = '\0';           /* initialize translation buffer */
  277.     pp = pathcomp;              /* point to translation buffer */
  278.     if (jflag)                  /* junking directories */
  279.         cp = (char *)strrchr(filename, '/');
  280.     if (cp == (char *)NULL)     /* no '/' or not junking dirs */
  281.         cp = filename;          /* point to internal zipfile-member pathname */
  282.     else
  283.         ++cp;                   /* point to start of last component of path */
  284.  
  285. /*---------------------------------------------------------------------------
  286.     Begin main loop through characters in filename.
  287.   ---------------------------------------------------------------------------*/
  288.  
  289.     while ((workch = (uch)*cp++) != 0) {
  290.  
  291.         if (quote) {                 /* if character quoted, */
  292.             *pp++ = (char)workch;    /*  include it literally */
  293.             quote = FALSE;
  294.         } else
  295.             switch (workch) {
  296.             case '/':             /* can assume -j flag not given */
  297.                 *pp = '\0';
  298.                 if ((error = checkdir(pathcomp, APPEND_DIR)) > 1)
  299.                     return error;
  300.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  301.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  302.                 break;
  303.  
  304.             case ';':             /* VMS version (or DEC-20 attrib?) */
  305.                 lastsemi = pp;
  306.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  307.                 break;            /*  later, if requested */
  308.  
  309.             case '\026':          /* control-V quote for special chars */
  310.                 quote = TRUE;     /* set flag for next character */
  311.                 break;
  312.  
  313. #ifdef MTS
  314.             case ' ':             /* change spaces to underscore under */
  315.                 *pp++ = '_';      /*  MTS; leave as spaces under Unix */
  316.                 break;
  317. #endif
  318.  
  319.             default:
  320.                 /* allow European characters in filenames: */
  321.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  322.                     *pp++ = (char)workch;
  323.             } /* end switch */
  324.  
  325.     } /* end while loop */
  326.  
  327.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  328.  
  329.     /* if not saving them, remove VMS version numbers (appended ";###") */
  330.     if (!V_flag && lastsemi) {
  331.         pp = lastsemi + 1;
  332.         while (isdigit((uch)(*pp)))
  333.             ++pp;
  334.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  335.             *lastsemi = '\0';
  336.     }
  337.  
  338. /*---------------------------------------------------------------------------
  339.     Report if directory was created (and no file to create:  filename ended
  340.     in '/'), check name to be sure it exists, and combine path and name be-
  341.     fore exiting.
  342.   ---------------------------------------------------------------------------*/
  343.  
  344.     if (filename[strlen(filename) - 1] == '/') {
  345.         checkdir(filename, GETPATH);
  346.         if (created_dir && QCOND2) {
  347.             fprintf(stdout, "   creating: %s\n", filename);
  348.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  349.         }
  350.         return 2;   /* dir existed already; don't look for data to extract */
  351.     }
  352.  
  353.     if (*pathcomp == '\0') {
  354.         fprintf(stderr, "mapname:  conversion of %s failed\n", filename);
  355.         return 3;
  356.     }
  357.  
  358.     checkdir(pathcomp, APPEND_NAME);   /* returns 1 if truncated:  care? */
  359.     checkdir(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(pathcomp, flag)
  394.     char *pathcomp;
  395.     int flag;
  396. /*
  397.  * returns:  1 - (on APPEND_NAME) truncated filename
  398.  *           2 - path doesn't exist, not allowed to create
  399.  *           3 - path doesn't exist, tried to create and failed; or
  400.  *               path exists and is not a directory, but is supposed to be
  401.  *           4 - path is too long
  402.  *          10 - can't allocate memory for filename buffers
  403.  */
  404. {
  405.     static int rootlen = 0;   /* length of rootpath */
  406.     static char *rootpath;    /* user's "extract-to" directory */
  407.     static char *buildpath;   /* full path (so far) to extracted file */
  408.     static char *end;         /* pointer to end of buildpath ('\0') */
  409.  
  410. #   define FN_MASK   7
  411. #   define FUNCTION  (flag & FN_MASK)
  412.  
  413.  
  414.  
  415. /*---------------------------------------------------------------------------
  416.     APPEND_DIR:  append the path component to the path being built and check
  417.     for its existence.  If doesn't exist and we are creating directories, do
  418.     so for this one; else signal success or error as appropriate.
  419.   ---------------------------------------------------------------------------*/
  420.  
  421.     if (FUNCTION == APPEND_DIR) {
  422.         int too_long = FALSE;
  423. #ifdef SHORT_NAMES
  424.         char *old_end = end;
  425. #endif
  426.  
  427.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  428.         while ((*end = *pathcomp++) != '\0')
  429.             ++end;
  430. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  431.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  432.             *(end = old_end + FILENAME_MAX) = '\0';
  433. #endif
  434.  
  435.         /* GRR:  could do better check, see if overrunning buffer as we go:
  436.          * check end-buildpath after each append, set warning variable if
  437.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  438.          * appending.  Clear variable when begin new path. */
  439.  
  440.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  441.             too_long = TRUE;                /* check if extracting directory? */
  442.         if (stat(buildpath, &statbuf)) {    /* path doesn't exist */
  443.             if (!create_dirs) {   /* told not to create (freshening) */
  444.                 free(buildpath);
  445.                 return 2;         /* path doesn't exist:  nothing to do */
  446.             }
  447.             if (too_long) {
  448.                 fprintf(stderr, "checkdir error:  path too long: %s\n",
  449.                   buildpath);
  450.                 fflush(stderr);
  451.                 free(buildpath);
  452.                 return 4;         /* no room for filenames:  fatal */
  453.             }
  454.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  455.                 fprintf(stderr, "checkdir error:  can't create %s\n\
  456.                  unable to process %s.\n", buildpath, filename);
  457.                 fflush(stderr);
  458.                 free(buildpath);
  459.                 return 3;      /* path didn't exist, tried to create, failed */
  460.             }
  461.             created_dir = TRUE;
  462.         } else if (!S_ISDIR(statbuf.st_mode)) {
  463.             fprintf(stderr, "checkdir error:  %s exists but is not directory\n\
  464.                  unable to process %s.\n", buildpath, filename);
  465.             fflush(stderr);
  466.             free(buildpath);
  467.             return 3;          /* path existed but wasn't dir */
  468.         }
  469.         if (too_long) {
  470.             fprintf(stderr, "checkdir error:  path too long: %s\n", buildpath);
  471.             fflush(stderr);
  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.                 fprintf(stderr, "checkdir warning:  path too long; truncating\n\
  515. checkdir warning:  path too long; truncating\n\
  516.                    %s\n                -> %s\n", filename, buildpath);
  517.                 fflush(stderr);
  518.                 return 1;   /* filename truncated */
  519.             }
  520.         }
  521.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  522.         return 0;  /* could check for existence here, prompt for new name... */
  523.     }
  524.  
  525. /*---------------------------------------------------------------------------
  526.     INIT:  allocate and initialize buffer space for the file currently being
  527.     extracted.  If file was renamed with an absolute path, don't prepend the
  528.     extract-to path.
  529.   ---------------------------------------------------------------------------*/
  530.  
  531. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  532.  
  533.     if (FUNCTION == INIT) {
  534.         Trace((stderr, "initializing buildpath to "));
  535.         if ((buildpath = (char *)malloc(strlen(filename)+rootlen+1)) ==
  536.             (char *)NULL)
  537.             return 10;
  538.         if ((rootlen > 0) && !renamed_fullpath) {
  539.             strcpy(buildpath, rootpath);
  540.             end = buildpath + rootlen;
  541.         } else {
  542.             *buildpath = '\0';
  543.             end = buildpath;
  544.         }
  545.         Trace((stderr, "[%s]\n", buildpath));
  546.         return 0;
  547.     }
  548.  
  549. /*---------------------------------------------------------------------------
  550.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  551.     sary; else assume it's a zipfile member and return.  This path segment
  552.     gets used in extracting all members from every zipfile specified on the
  553.     command line.
  554.   ---------------------------------------------------------------------------*/
  555.  
  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.             int had_trailing_pathsep=FALSE;
  564.  
  565.             if (pathcomp[rootlen-1] == '/') {
  566.                 pathcomp[--rootlen] = '\0';
  567.                 had_trailing_pathsep = TRUE;
  568.             }
  569.             if (stat(pathcomp, &statbuf) || !S_ISDIR(statbuf.st_mode)) {
  570.                 /* path does not exist */
  571.                 if (!create_dirs                     /* || isshexp(pathcomp) */
  572. #ifdef OLD_EXDIR
  573.                                  || !had_trailing_pathsep
  574. #endif
  575.                                                          ) {
  576.                     rootlen = 0;
  577.                     return 2;   /* skip (or treat as stored file) */
  578.                 }
  579.                 /* create the directory (could add loop here to scan pathcomp
  580.                  * and create more than one level, but why really necessary?) */
  581.                 if (mkdir(pathcomp, 0777) == -1) {
  582.                     fprintf(stderr,
  583.                       "checkdir:  can't create extraction directory: %s\n",
  584.                       pathcomp);
  585.                     fflush(stderr);
  586.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  587.                     return 3;  /* failed:  file exists, or 2+ levels required */
  588.                 }
  589.             }
  590.             if ((rootpath = (char *)malloc(rootlen+2)) == (char *)NULL) {
  591.                 rootlen = 0;
  592.                 return 10;
  593.             }
  594.             strcpy(rootpath, pathcomp);
  595.             rootpath[rootlen++] = '/';
  596.             rootpath[rootlen] = '\0';
  597.         }
  598.         Trace((stderr, "rootpath now = [%s]\n", rootpath));
  599.         return 0;
  600.     }
  601.  
  602. /*---------------------------------------------------------------------------
  603.     END:  free rootpath, immediately prior to program exit.
  604.   ---------------------------------------------------------------------------*/
  605.  
  606.     if (FUNCTION == END) {
  607.         Trace((stderr, "freeing rootpath\n"));
  608.         if (rootlen > 0)
  609.             free(rootpath);
  610.         return 0;
  611.     }
  612.  
  613.     return 99;  /* should never reach */
  614.  
  615. } /* end function checkdir() */
  616.  
  617.  
  618.  
  619.  
  620.  
  621. #ifdef NO_MKDIR
  622.  
  623. /********************/
  624. /* Function mkdir() */
  625. /********************/
  626.  
  627. int mkdir(path, mode)
  628.     char *path;
  629.     int mode;   /* ignored */
  630. /*
  631.  * returns:   0 - successful
  632.  *           -1 - failed (errno not set, however)
  633.  */
  634. {
  635.     char command[FILNAMSIZ+40]; /* buffer for system() call */
  636.  
  637.     /* GRR 930416:  added single quotes around path to avoid bug with
  638.      * creating directories with ampersands in name; not yet tested */
  639.     sprintf(command, "IFS=\" \t\n\" /bin/mkdir '%s' 2>/dev/null", path);
  640.     if (system(command))
  641.         return -1;
  642.     return 0;
  643. }
  644.  
  645. #endif /* NO_MKDIR */
  646.  
  647.  
  648.  
  649.  
  650.  
  651. #ifndef MTS
  652.  
  653. /****************************/
  654. /* Function close_outfile() */
  655. /****************************/
  656.  
  657. void close_outfile()
  658. {
  659.     static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  660.     time_t m_time;
  661.     int yr, mo, dy, hh, mm, ss, leap, days;
  662.     struct utimbuf tp;
  663. #   define YRBASE  1970
  664. #ifndef BSD4_4
  665. #ifdef BSD
  666.     static struct timeb tbp;
  667. #else /* !BSD */
  668.     extern long timezone;
  669. #endif /* ?BSD */
  670. #endif /* !BSD4_4 */
  671.  
  672.  
  673. /*---------------------------------------------------------------------------
  674.     If symbolic links are supported, allocate a storage area, put the uncom-
  675.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  676.     link to start with, we shouldn't have to worry about overflowing unsigned
  677.     ints with unsigned longs.
  678.   ---------------------------------------------------------------------------*/
  679.  
  680. #ifdef SYMLINKS
  681.     if (symlnk) {
  682.         unsigned ucsize = (unsigned)lrec.ucsize;
  683.         char *linktarget = (char *)malloc((unsigned)lrec.ucsize+1);
  684.  
  685.         fclose(outfile);                    /* close "data" file... */
  686.         outfile = fopen(filename, FOPR);    /* ...and reopen for reading */
  687.         if (!linktarget || (fread(linktarget, 1, ucsize, outfile) != ucsize)) {
  688.             fprintf(stderr, "\nwarning:  symbolic link (%s) failed\n",
  689.               filename);
  690.             if (linktarget)
  691.                 free(linktarget);
  692.             fclose(outfile);
  693.             return;
  694.         }
  695.         fclose(outfile);                    /* close "data" file for good... */
  696.         unlink(filename);                   /* ...and delete it */
  697.         linktarget[ucsize] = '\0';
  698.         fprintf(stdout, "-> %s ", linktarget);
  699.         if (symlink(linktarget, filename))  /* create the real link */
  700.             perror("symlink error");
  701.         free(linktarget);
  702.         return;                             /* can't set time on symlinks */
  703.     }
  704. #endif /* SYMLINKS */
  705.  
  706.     fclose(outfile);
  707.  
  708. /*---------------------------------------------------------------------------
  709.     Change the file permissions from default ones to those stored in the
  710.     zipfile.
  711.   ---------------------------------------------------------------------------*/
  712.  
  713. #ifndef NO_CHMOD
  714.     if (chmod(filename, 0xffff & pInfo->file_attr))
  715.             perror("chmod (file attributes) error");
  716. #endif
  717.  
  718. /*---------------------------------------------------------------------------
  719.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  720.     time:  adjust base year from 1980 to 1970, do usual conversions from
  721.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  722.     light savings time differences.
  723.   ---------------------------------------------------------------------------*/
  724.  
  725.     yr = ((lrec.last_mod_file_date >> 9) & 0x7f) + (1980 - YRBASE);
  726.     mo = ((lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  727.     dy = (lrec.last_mod_file_date & 0x1f) - 1;
  728.     hh = (lrec.last_mod_file_time >> 11) & 0x1f;
  729.     mm = (lrec.last_mod_file_time >> 5) & 0x3f;
  730.     ss = (lrec.last_mod_file_time & 0x1f) * 2;
  731.  
  732.     /* leap = # of leap yrs from YRBASE up to but not including current year */
  733.     leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  734.  
  735.     /* how many days from YRBASE to this year? (& add expired days this year) */
  736.     days = (yr * 365) + (leap - 492) + yday[mo];
  737.  
  738.     /* if year is a leap year and month is after February, add another day */
  739.     if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  740.         ++days;   /* OK through 2199 */
  741.  
  742.     /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  743.     m_time = ((days + dy) * 86400) + (hh * 3600) + (mm * 60) + ss;
  744.  
  745.     /* adjust for local timezone */
  746. #ifdef BSD
  747. #ifdef BSD4_4
  748.     m_time -= localtime(&m_time)->tm_gmtoff;  /* seconds EAST of GMT:  subtr. */
  749. #else
  750.     ftime(&tbp);                    /* get `timezone' */
  751.     m_time += tbp.timezone * 60L;   /* seconds WEST of GMT:  add */
  752. #endif /* ?BSD4_4 */
  753. #else /* !BSD */
  754.     tzset();              /* get `timezone' */
  755.     m_time += timezone;   /* seconds WEST of GMT:  add */
  756. #endif /* ?BSD */
  757.  
  758.     /* adjust for daylight savings time (or local equivalent) */
  759. #ifndef BSD4_4  /* (DST already added to tm_gmtoff, so skip tm_isdst) */
  760.     if (localtime(&m_time)->tm_isdst)
  761.         m_time -= 60L * 60L;   /* adjust for daylight savings time */
  762. #endif
  763.  
  764.     /* set the file's access and modification times */
  765.     tp.actime = tp.modtime = m_time;
  766.     if (utime(filename, &tp)) {
  767. #ifdef AOS_VS
  768.         fprintf(stderr, "... can't set time for %s", filename);
  769. #else
  770.         fprintf(stderr, "warning:  can't set the time for %s\n", filename);
  771. #endif
  772.         FFLUSH(stderr);
  773.     }
  774.  
  775. } /* end function close_outfile() */
  776.  
  777. #endif /* !MTS */
  778.  
  779.  
  780.  
  781.  
  782. #ifndef SFX
  783.  
  784. /************************/
  785. /*  Function version()  */
  786. /************************/
  787.  
  788. void version()
  789. {
  790. #if defined(CRAY) || defined(NetBSD)
  791.     char buf1[40];
  792. #if defined(CRAY)
  793.     char buf2[40];
  794. #endif
  795. #endif
  796.  
  797.     printf(LoadFarString(CompiledWith),
  798.  
  799. #ifdef __GNUC__
  800.       "gcc ", __VERSION__,
  801. #else
  802. #  if defined(CRAY) && defined(_RELEASE)
  803.       "cc ", (sprintf(buf1, "version %d", _RELEASE), buf1),
  804. #  else
  805. #  ifdef __VERSION__
  806.       "cc ", __VERSION__,
  807. #  else
  808.       "cc", "",
  809. #  endif
  810. #  endif
  811. #endif
  812.  
  813.       "Unix",
  814.  
  815. #if defined(sgi) || defined(__sgi)
  816.       " (Silicon Graphics IRIX)",
  817. #else
  818. #ifdef sun
  819. #  ifdef sparc
  820. #    ifdef __SVR4
  821.       " (Sun Sparc/Solaris)",
  822. #    else /* may or may not be SunOS */
  823.       " (Sun Sparc)",
  824. #    endif
  825. #  else
  826. #  if defined(sun386) || defined(i386)
  827.       " (Sun 386i)",
  828. #  else
  829. #  if defined(mc68020) || defined(__mc68020__)
  830.       " (Sun 3)",
  831. #  else /* mc68010 or mc68000:  Sun 2 or earlier */
  832.       " (Sun 2)",
  833. #  endif
  834. #  endif
  835. #  endif
  836. #else
  837. #ifdef __hpux
  838.       " (HP/UX)",
  839. #else
  840. #ifdef __osf__
  841.       " (DEC OSF/1)",
  842. #else
  843. #ifdef _AIX
  844.       " (IBM AIX)",
  845. #else
  846. #ifdef aiws
  847.       " (IBM RT/AIX)",
  848. #else
  849. #if defined(CRAY) || defined(cray)
  850. #  ifdef _UNICOS
  851.       (sprintf(buf2, " (Cray UNICOS release %d)", _UNICOS), buf2),
  852. #  else
  853.       " (Cray UNICOS)",
  854. #  endif
  855. #else
  856. #if defined(uts) || defined(UTS)
  857.       " (Amdahl UTS)",
  858. #else
  859. #ifdef NeXT
  860. #  ifdef mc68000
  861.       " (NeXTStep/black)",
  862. #  else
  863.       " (NeXTStep for Intel)",
  864. #  endif
  865. #else              /* the next dozen or so are somewhat order-dependent */
  866. #ifdef LINUX
  867.       " (Linux)",
  868. #else
  869. #ifdef MINIX
  870.       " (Minix)",
  871. #else
  872. #ifdef M_UNIX
  873.       " (SCO Unix)",
  874. #else
  875. #ifdef M_XENIX
  876.       " (SCO Xenix)",
  877. #else
  878. #ifdef __NetBSD__
  879. #  ifdef NetBSD0_8
  880.       (sprintf(buf1, " (NetBSD 0.8%c)", (char)(NetBSD0_8 - 1 + 'A')), buf1),
  881. #  else
  882. #  ifdef NetBSD0_9
  883.       (sprintf(buf1, " (NetBSD 0.9%c)", (char)(NetBSD0_9 - 1 + 'A')), buf1),
  884. #  else
  885. #  ifdef NetBSD1_0
  886.       (sprintf(buf1, " (NetBSD 1.0%c)", (char)(NetBSD1_0 - 1 + 'A')), buf1),
  887. #  else
  888.       (BSD4_4 == 0.5)? " (NetBSD before 0.9)" : " (NetBSD 1.1 or later)",
  889. #  endif
  890. #  endif
  891. #  endif
  892. #else
  893. #ifdef __FreeBSD__
  894.       (BSD4_4 == 0.5)? " (FreeBSD 1.x)" : " (FreeBSD 2.0 or later)",
  895. #else
  896. #ifdef __bsdi__
  897.       (BSD4_4 == 0.5)? " (BSD/386 1.0)" : " (BSD/386 1.1 or later)",
  898. #else
  899. #ifdef __386BSD__
  900.       (BSD4_4 == 1)? " (386BSD, post-4.4 release)" : " (386BSD)",
  901. #else
  902. #if defined(i486) || defined(__i486) || defined(__i486__)
  903.       " (Intel 486)",
  904. #else
  905. #if defined(i386) || defined(__i386) || defined(__i386__)
  906.       " (Intel 386)",
  907. #else
  908. #ifdef pyr
  909.       " (Pyramid)",
  910. #else
  911. #ifdef ultrix
  912. #  ifdef mips
  913.       " (DEC/MIPS)",
  914. #  else
  915. #  ifdef vax
  916.       " (DEC/VAX)",
  917. #  else /* __alpha? */
  918.       " (DEC/Alpha)",
  919. #  endif
  920. #  endif
  921. #else
  922. #ifdef gould
  923.       " (Gould)",
  924. #else
  925. #ifdef MTS
  926.       " (MTS)",
  927. #else
  928. #ifdef __convexc__
  929.       " (Convex)",
  930. #else
  931.       "",
  932. #endif /* Convex */
  933. #endif /* MTS */
  934. #endif /* Gould */
  935. #endif /* DEC */
  936. #endif /* Pyramid */
  937. #endif /* 386 */
  938. #endif /* 486 */
  939. #endif /* 386BSD */
  940. #endif /* BSDI BSD/386 */
  941. #endif /* NetBSD */
  942. #endif /* FreeBSD */
  943. #endif /* SCO Xenix */
  944. #endif /* SCO Unix */
  945. #endif /* Minix */
  946. #endif /* Linux */
  947. #endif /* NeXT */
  948. #endif /* Amdahl */
  949. #endif /* Cray */
  950. #endif /* RT/AIX */
  951. #endif /* AIX */
  952. #endif /* OSF/1 */
  953. #endif /* HP/UX */
  954. #endif /* Sun */
  955. #endif /* SGI */
  956.  
  957. #ifdef __DATE__
  958.       " on ", __DATE__
  959. #else
  960.       "", ""
  961. #endif
  962.       );
  963.  
  964. } /* end function version() */
  965.  
  966. #endif /* !SFX */
  967.