home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip51.zip / unix / unix.c < prev   
C/C++ Source or Header  |  1994-01-27  |  31KB  |  890 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.  
  15.   ---------------------------------------------------------------------------*/
  16.  
  17.  
  18. #include "unzip.h"
  19.  
  20. /* SCO Unix, DNIX, TI SysV, Coherent 4.x, ... */
  21. #if defined(__convexc__) || defined(SYSV) || defined(CRAY)
  22. #  define DIRENT
  23. #endif
  24. #ifdef COHERENT
  25. #  if defined(_I386) || (defined(__COHERENT__) && (__COHERENT__ >= 0x420))
  26. #    define DIRENT
  27. #  endif
  28. #endif
  29.  
  30. /* GRR:  may need to uncomment one or both of these for the relevant systems */
  31.  
  32. #if 0
  33. #if defined(__386BSD__) || defined(_POSIX_VERSION)
  34. #  define DIRENT
  35. #endif
  36. #endif
  37.  
  38. #if 0
  39. #if defined(M_XENIX)
  40. #  define SYSNDIR
  41. #endif
  42. #endif
  43.  
  44. #ifdef DIRENT
  45. #  include <dirent.h>
  46. #else
  47. #  ifdef SYSV
  48. #    ifdef SYSNDIR
  49. #      include <sys/ndir.h>
  50. #    else
  51. #      include <ndir.h>
  52. #    endif
  53. #  else /* !SYSV */
  54. #    ifndef NO_SYSDIR
  55. #      include <sys/dir.h>
  56. #    endif
  57. #  endif /* ?SYSV */
  58. #  ifndef dirent
  59. #    define dirent direct
  60. #  endif
  61. #endif /* ?DIRENT */
  62.  
  63. static int created_dir;        /* used in mapname(), checkdir() */
  64. static int renamed_fullpath;   /* ditto */
  65.  
  66.  
  67. #ifndef SFX
  68. #ifdef NO_DIR                    /* for AT&T 3B1 */
  69.  
  70. #define opendir(path) fopen(path,"r")
  71. #define closedir(dir) fclose(dir)
  72. typedef FILE DIR;
  73.  
  74. /*
  75.  *  Apparently originally by Rich Salz.
  76.  *  Cleaned up and modified by James W. Birdsall.
  77.  */
  78. struct dirent *readdir(dirp)
  79.     DIR *dirp;
  80. {
  81.     static struct dirent entry;
  82.  
  83.     if (dirp == NULL) 
  84.         return NULL;
  85.  
  86.     for (;;)
  87.         if (fread(&entry, sizeof (struct dirent), 1, dirp) == 0) 
  88.             return (struct dirent *)NULL;
  89.         else if (entry.d_ino) 
  90.             return &entry;
  91.  
  92. } /* end function readdir() */
  93.  
  94. #endif /* NO_DIR */
  95.  
  96.  
  97. /**********************/
  98. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  99. /**********************/
  100.  
  101. char *do_wild(wildspec)
  102.     char *wildspec;         /* only used first time on a given dir */
  103. {
  104.     static 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, '/')) == 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)) == NULL) {
  127.                 fprintf(stderr, "warning:  can't allocate wildcard buffers\n");
  128.                 strcpy(matchname, wildspec);
  129.                 return matchname;   /* but maybe filespec was not a wildcard */
  130.             }
  131.             strncpy(dirname, wildspec, dirnamelen);
  132.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  133.             have_dirname = TRUE;
  134.         }
  135.  
  136.         if ((dir = opendir(dirname)) != NULL) {
  137.             while ((file = readdir(dir)) != NULL) {
  138.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  139.                     continue;  /* Unix:  '*' and '?' do not match leading dot */
  140.                 if (match(file->d_name, wildname, 0)) {  /* 0 == case sens. */
  141.                     if (have_dirname) {
  142.                         strcpy(matchname, dirname);
  143.                         strcpy(matchname+dirnamelen, file->d_name);
  144.                     } else
  145.                         strcpy(matchname, file->d_name);
  146.                     return matchname;
  147.                 }
  148.             }
  149.             /* if we get to here directory is exhausted, so close it */
  150.             closedir(dir);
  151.             dir = NULL;
  152.         }
  153.  
  154.         /* return the raw wildspec in case that works (e.g., directory not
  155.          * searchable, but filespec was not wild and file is readable) */
  156.         strcpy(matchname, wildspec);
  157.         return matchname;
  158.     }
  159.  
  160.     /* last time through, might have failed opendir but returned raw wildspec */
  161.     if (dir == NULL) {
  162.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  163.         if (have_dirname)
  164.             free(dirname);
  165.         return (char *)NULL;
  166.     }
  167.  
  168.     /* If we've gotten this far, we've read and matched at least one entry
  169.      * successfully (in a previous call), so dirname has been copied into
  170.      * matchname already.
  171.      */
  172.     while ((file = readdir(dir)) != NULL)
  173.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  174.             if (have_dirname) {
  175.                 /* strcpy(matchname, dirname); */
  176.                 strcpy(matchname+dirnamelen, file->d_name);
  177.             } else
  178.                 strcpy(matchname, file->d_name);
  179.             return matchname;
  180.         }
  181.  
  182.     closedir(dir);     /* have read at least one dir entry; nothing left */
  183.     dir = NULL;
  184.     firstcall = TRUE;  /* reset for new wildspec */
  185.     if (have_dirname)
  186.         free(dirname);
  187.     return (char *)NULL;
  188.  
  189. } /* end function do_wild() */
  190.  
  191. #endif /* !SFX */
  192.  
  193.  
  194.  
  195.  
  196.  
  197. /**********************/
  198. /* Function mapattr() */
  199. /**********************/
  200.  
  201. int mapattr()
  202. {
  203.     ulg tmp = crec.external_file_attributes;
  204.  
  205.     switch (pInfo->hostnum) {
  206.         case UNIX_:
  207.         case VMS_:
  208.             pInfo->file_attr = (unsigned)(tmp >> 16);
  209.             return 0;
  210.         case AMIGA_:
  211.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  212.             pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  213.             break;
  214.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  215.         case FS_FAT_:
  216.         case FS_HPFS_:
  217.         case FS_NTFS_:
  218.         case MAC_:
  219.         case ATARI_:             /* (used to set = 0666) */
  220.         case TOPS20_:
  221.         default:
  222.             tmp = !(tmp & 1) << 1;   /* read-only bit --> write perms bits */
  223.             pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  224.             break;
  225.     } /* end switch (host-OS-created-by) */
  226.  
  227.     /* for originating systems with no concept of "group," "other," "system": */
  228.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  229.     pInfo->file_attr &= ~tmp;
  230.  
  231.     return 0;
  232.  
  233. } /* end function mapattr() */
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /************************/
  240. /*  Function mapname()  */
  241. /************************/
  242.  
  243. int mapname(renamed)  /* return 0 if no error, 1 if caution (filename trunc), */
  244.     int renamed;      /* 2 if warning (skip file because dir doesn't exist), */
  245. {                     /* 3 if error (skip file), 10 if no memory (skip file) */
  246.     char pathcomp[FILNAMSIZ];   /* path-component buffer */
  247.     char *pp, *cp=NULL;         /* character pointers */
  248.     char *lastsemi = NULL;      /* pointer to last semi-colon in pathcomp */
  249.     int quote = FALSE;          /* flags */
  250.     int error = 0;
  251.     register unsigned workch;   /* hold the character being tested */
  252.  
  253.  
  254. /*---------------------------------------------------------------------------
  255.     Initialize various pointers and counters and stuff.
  256.   ---------------------------------------------------------------------------*/
  257.  
  258.     if (pInfo->vollabel)
  259.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  260.  
  261.     /* can create path as long as not just freshening, or if user told us */
  262.     create_dirs = (!fflag || renamed);
  263.  
  264.     created_dir = FALSE;        /* not yet */
  265.  
  266.     /* user gave full pathname:  don't prepend rootpath */
  267.     renamed_fullpath = (renamed && (*filename == '/'));
  268.  
  269.     if (checkdir((char *)NULL, INIT) == 10)
  270.         return 10;              /* initialize path buffer, unless no memory */
  271.  
  272.     *pathcomp = '\0';           /* initialize translation buffer */
  273.     pp = pathcomp;              /* point to translation buffer */
  274.     if (jflag)                  /* junking directories */
  275.         cp = (char *)strrchr(filename, '/');
  276.     if (cp == NULL)             /* no '/' or not junking dirs */
  277.         cp = filename;          /* point to internal zipfile-member pathname */
  278.     else
  279.         ++cp;                   /* point to start of last component of path */
  280.  
  281. /*---------------------------------------------------------------------------
  282.     Begin main loop through characters in filename.
  283.   ---------------------------------------------------------------------------*/
  284.  
  285.     while ((workch = (uch)*cp++) != 0) {
  286.  
  287.         if (quote) {                 /* if character quoted, */
  288.             *pp++ = (char)workch;    /*  include it literally */
  289.             quote = FALSE;
  290.         } else
  291.             switch (workch) {
  292.             case '/':             /* can assume -j flag not given */
  293.                 *pp = '\0';
  294.                 if ((error = checkdir(pathcomp, APPEND_DIR)) > 1)
  295.                     return error;
  296.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  297.                 lastsemi = NULL;  /* leave directory semi-colons alone */
  298.                 break;
  299.  
  300.             case ';':             /* VMS version (or DEC-20 attrib?) */
  301.                 lastsemi = pp;
  302.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  303.                 break;            /*  later, if requested */
  304.  
  305.             case '\026':          /* control-V quote for special chars */
  306.                 quote = TRUE;     /* set flag for next character */
  307.                 break;
  308.  
  309. #ifdef MTS
  310.             case ' ':             /* change spaces to underscore under */
  311.                 *pp++ = '_';      /*  MTS; leave as spaces under Unix */
  312.                 break;
  313. #endif
  314.  
  315.             default:
  316.                 /* allow European characters in filenames: */
  317.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  318.                     *pp++ = (char)workch;
  319.             } /* end switch */
  320.  
  321.     } /* end while loop */
  322.  
  323.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  324.  
  325.     /* if not saving them, remove VMS version numbers (appended ";###") */
  326.     if (!V_flag && lastsemi) {
  327.         pp = lastsemi + 1;
  328.         while (isdigit((uch)(*pp)))
  329.             ++pp;
  330.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  331.             *lastsemi = '\0';
  332.     }
  333.  
  334. /*---------------------------------------------------------------------------
  335.     Report if directory was created (and no file to create:  filename ended
  336.     in '/'), check name to be sure it exists, and combine path and name be-
  337.     fore exiting.
  338.   ---------------------------------------------------------------------------*/
  339.  
  340.     if (filename[strlen(filename) - 1] == '/') {
  341.         checkdir(filename, GETPATH);
  342.         if (created_dir && QCOND2) {
  343.             fprintf(stdout, "   creating: %s\n", filename);
  344.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  345.         }
  346.         return 2;   /* dir existed already; don't look for data to extract */
  347.     }
  348.  
  349.     if (*pathcomp == '\0') {
  350.         fprintf(stderr, "mapname:  conversion of %s failed\n", filename);
  351.         return 3;
  352.     }
  353.  
  354.     checkdir(pathcomp, APPEND_NAME);   /* returns 1 if truncated:  care? */
  355.     checkdir(filename, GETPATH);
  356.  
  357.     return error;
  358.  
  359. } /* end function mapname() */
  360.  
  361.  
  362.  
  363.  
  364. #if 0  /*========== NOTES ==========*/
  365.  
  366.   extract-to dir:      a:path/
  367.   buildpath:           path1/path2/ ...   (NULL-terminated)
  368.   pathcomp:                filename 
  369.  
  370.   mapname():
  371.     loop over chars in zipfile member name
  372.       checkdir(path component, COMPONENT | CREATEDIR) --> map as required?
  373.         (d:/tmp/unzip/)                    (disk:[tmp.unzip.)
  374.         (d:/tmp/unzip/jj/)                 (disk:[tmp.unzip.jj.)
  375.         (d:/tmp/unzip/jj/temp/)            (disk:[tmp.unzip.jj.temp.)
  376.     finally add filename itself and check for existence? (could use with rename)
  377.         (d:/tmp/unzip/jj/temp/msg.outdir)  (disk:[tmp.unzip.jj.temp]msg.outdir)
  378.     checkdir(name, COPYFREE)     -->  copy path to name and free space
  379.  
  380. #endif /* 0 */
  381.  
  382.  
  383.  
  384.  
  385. /***********************/
  386. /* Function checkdir() */
  387. /***********************/
  388.  
  389. int checkdir(pathcomp, flag)
  390.     char *pathcomp;
  391.     int flag;
  392. /*
  393.  * returns:  1 - (on APPEND_NAME) truncated filename
  394.  *           2 - path doesn't exist, not allowed to create
  395.  *           3 - path doesn't exist, tried to create and failed; or
  396.  *               path exists and is not a directory, but is supposed to be
  397.  *           4 - path is too long
  398.  *          10 - can't allocate memory for filename buffers
  399.  */
  400. {
  401.     static int rootlen = 0;   /* length of rootpath */
  402.     static char *rootpath;    /* user's "extract-to" directory */
  403.     static char *buildpath;   /* full path (so far) to extracted file */
  404.     static char *end;         /* pointer to end of buildpath ('\0') */
  405.  
  406. #   define FN_MASK   7
  407. #   define FUNCTION  (flag & FN_MASK)
  408.  
  409.  
  410.  
  411. /*---------------------------------------------------------------------------
  412.     APPEND_DIR:  append the path component to the path being built and check
  413.     for its existence.  If doesn't exist and we are creating directories, do
  414.     so for this one; else signal success or error as appropriate.
  415.   ---------------------------------------------------------------------------*/
  416.  
  417.     if (FUNCTION == APPEND_DIR) {
  418.         int too_long = FALSE;
  419. #ifdef SHORT_NAMES
  420.         char *old_end = end;
  421. #endif
  422.  
  423.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  424.         while ((*end = *pathcomp++) != '\0')
  425.             ++end;
  426. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  427.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  428.             *(end = old_end + FILENAME_MAX) = '\0';
  429. #endif
  430.  
  431.         /* GRR:  could do better check, see if overrunning buffer as we go:
  432.          * check end-buildpath after each append, set warning variable if
  433.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  434.          * appending.  Clear variable when begin new path. */
  435.  
  436.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  437.             too_long = TRUE;                /* check if extracting directory? */
  438.         if (stat(buildpath, &statbuf)) {    /* path doesn't exist */
  439.             if (!create_dirs) {   /* told not to create (freshening) */
  440.                 free(buildpath);
  441.                 return 2;         /* path doesn't exist:  nothing to do */
  442.             }
  443.             if (too_long) {
  444.                 fprintf(stderr, "checkdir error:  path too long: %s\n",
  445.                   buildpath);
  446.                 fflush(stderr);
  447.                 free(buildpath);
  448.                 return 4;         /* no room for filenames:  fatal */
  449.             }
  450.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  451.                 fprintf(stderr, "checkdir error:  can't create %s\n\
  452.                  unable to process %s.\n", buildpath, filename);
  453.                 fflush(stderr);
  454.                 free(buildpath);
  455.                 return 3;      /* path didn't exist, tried to create, failed */
  456.             }
  457.             created_dir = TRUE;
  458.         } else if (!S_ISDIR(statbuf.st_mode)) {
  459.             fprintf(stderr, "checkdir error:  %s exists but is not directory\n\
  460.                  unable to process %s.\n", buildpath, filename);
  461.             fflush(stderr);
  462.             free(buildpath);
  463.             return 3;          /* path existed but wasn't dir */
  464.         }
  465.         if (too_long) {
  466.             fprintf(stderr, "checkdir error:  path too long: %s\n", buildpath);
  467.             fflush(stderr);
  468.             free(buildpath);
  469.             return 4;         /* no room for filenames:  fatal */
  470.         }
  471.         *end++ = '/';
  472.         *end = '\0';
  473.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  474.         return 0;
  475.  
  476.     } /* end if (FUNCTION == APPEND_DIR) */
  477.  
  478. /*---------------------------------------------------------------------------
  479.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  480.     buildpath.
  481.   ---------------------------------------------------------------------------*/
  482.  
  483.     if (FUNCTION == GETPATH) {
  484.         strcpy(pathcomp, buildpath);
  485.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  486.         free(buildpath);
  487.         buildpath = end = NULL;
  488.         return 0;
  489.     }
  490.  
  491. /*---------------------------------------------------------------------------
  492.     APPEND_NAME:  assume the path component is the filename; append it and
  493.     return without checking for existence.
  494.   ---------------------------------------------------------------------------*/
  495.  
  496.     if (FUNCTION == APPEND_NAME) {
  497. #ifdef SHORT_NAMES
  498.         char *old_end = end;
  499. #endif
  500.  
  501.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  502.         while ((*end = *pathcomp++) != '\0') {
  503.             ++end;
  504. #ifdef SHORT_NAMES  /* truncate name at 14 characters, typically */
  505.             if ((end-old_end) > FILENAME_MAX)      /* GRR:  proper constant? */
  506.                 *(end = old_end + FILENAME_MAX) = '\0';
  507. #endif
  508.             if ((end-buildpath) >= FILNAMSIZ) {
  509.                 *--end = '\0';
  510.                 fprintf(stderr, "checkdir warning:  path too long; truncating\n\
  511. checkdir warning:  path too long; truncating\n\
  512.                    %s\n                -> %s\n", filename, buildpath);
  513.                 fflush(stderr);
  514.                 return 1;   /* filename truncated */
  515.             }
  516.         }
  517.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  518.         return 0;  /* could check for existence here, prompt for new name... */
  519.     }
  520.  
  521. /*---------------------------------------------------------------------------
  522.     INIT:  allocate and initialize buffer space for the file currently being
  523.     extracted.  If file was renamed with an absolute path, don't prepend the
  524.     extract-to path.
  525.   ---------------------------------------------------------------------------*/
  526.  
  527. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  528.  
  529.     if (FUNCTION == INIT) {
  530.         Trace((stderr, "initializing buildpath to "));
  531.         if ((buildpath = (char *)malloc(strlen(filename)+rootlen+1)) == NULL)
  532.             return 10;
  533.         if ((rootlen > 0) && !renamed_fullpath) {
  534.             strcpy(buildpath, rootpath);
  535.             end = buildpath + rootlen;
  536.         } else {
  537.             *buildpath = '\0';
  538.             end = buildpath;
  539.         }
  540.         Trace((stderr, "[%s]\n", buildpath));
  541.         return 0;
  542.     }
  543.  
  544. /*---------------------------------------------------------------------------
  545.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  546.     sary; else assume it's a zipfile member and return.  This path segment
  547.     gets used in extracting all members from every zipfile specified on the
  548.     command line.
  549.   ---------------------------------------------------------------------------*/
  550.  
  551.     if (FUNCTION == ROOT) {
  552.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  553.         if (pathcomp == NULL) {
  554.             rootlen = 0;
  555.             return 0;
  556.         }
  557.         if ((rootlen = strlen(pathcomp)) > 0) {
  558.             int had_trailing_pathsep=FALSE;
  559.  
  560.             if (pathcomp[rootlen-1] == '/') {
  561.                 pathcomp[--rootlen] = '\0';
  562.                 had_trailing_pathsep = TRUE;
  563.             }
  564.             if (stat(pathcomp, &statbuf) || !S_ISDIR(statbuf.st_mode)) {
  565.                 /* path does not exist */
  566.                 if (!create_dirs                     /* || isshexp(pathcomp) */
  567. #ifdef OLD_EXDIR
  568.                                  || !had_trailing_pathsep
  569. #endif
  570.                                                          ) {
  571.                     rootlen = 0;
  572.                     return 2;   /* skip (or treat as stored file) */
  573.                 }
  574.                 /* create the directory (could add loop here to scan pathcomp
  575.                  * and create more than one level, but why really necessary?) */
  576.                 if (mkdir(pathcomp, 0777) == -1) {
  577.                     fprintf(stderr,
  578.                       "checkdir:  can't create extraction directory: %s\n",
  579.                       pathcomp);
  580.                     fflush(stderr);
  581.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  582.                     return 3;  /* failed:  file exists, or 2+ levels required */
  583.                 }
  584.             }
  585.             if ((rootpath = (char *)malloc(rootlen+2)) == NULL) {
  586.                 rootlen = 0;
  587.                 return 10;
  588.             }
  589.             strcpy(rootpath, pathcomp);
  590.             rootpath[rootlen++] = '/';
  591.             rootpath[rootlen] = '\0';
  592.         }
  593.         Trace((stderr, "rootpath now = [%s]\n", rootpath));
  594.         return 0;
  595.     }
  596.  
  597. /*---------------------------------------------------------------------------
  598.     END:  free rootpath, immediately prior to program exit.
  599.   ---------------------------------------------------------------------------*/
  600.  
  601.     if (FUNCTION == END) {
  602.         Trace((stderr, "freeing rootpath\n"));
  603.         if (rootlen > 0)
  604.             free(rootpath);
  605.         return 0;
  606.     }
  607.  
  608.     return 99;  /* should never reach */
  609.  
  610. } /* end function checkdir() */
  611.  
  612.  
  613.  
  614.  
  615.  
  616. #ifdef NO_MKDIR
  617.  
  618. /********************/
  619. /* Function mkdir() */
  620. /********************/
  621.  
  622. int mkdir(path, mode)
  623.     char *path;
  624.     int mode;   /* ignored */
  625. /*
  626.  * returns:   0 - successful
  627.  *           -1 - failed (errno not set, however)
  628.  */
  629. {
  630.     char command[FILNAMSIZ+40]; /* buffer for system() call */
  631.  
  632.     /* GRR 930416:  added single quotes around path to avoid bug with
  633.      * creating directories with ampersands in name; not yet tested */
  634.     sprintf(command, "IFS=\" \t\n\" /bin/mkdir '%s' 2>/dev/null", path);
  635.     if (system(command))
  636.         return -1;
  637.     return 0;
  638. }
  639.  
  640. #endif /* NO_MKDIR */
  641.  
  642.  
  643.  
  644.  
  645.  
  646. #ifndef MTS
  647.  
  648. /****************************/
  649. /* Function close_outfile() */
  650. /****************************/
  651.  
  652. void close_outfile()
  653. {
  654.     static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  655.     long m_time;
  656.     int yr, mo, dy, hh, mm, ss, leap, days;
  657.     struct utimbuf tp;
  658. #   define YRBASE  1970
  659. #ifndef __386BSD__
  660. #ifdef BSD
  661.     static struct timeb tbp;
  662. #else /* !BSD */
  663.     extern long timezone;
  664. #endif /* ?BSD */
  665. #endif /* !__386BSD__ */
  666.  
  667.  
  668. /*---------------------------------------------------------------------------
  669.     If symbolic links are supported, allocate a storage area, put the uncom-
  670.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  671.     link to start with, we shouldn't have to worry about overflowing unsigned
  672.     ints with unsigned longs.
  673.   ---------------------------------------------------------------------------*/
  674.  
  675. #ifdef SYMLINKS
  676.     if (symlnk) {
  677.         unsigned ucsize = (unsigned)lrec.ucsize;
  678.         char *linktarget = (char *)malloc((unsigned)lrec.ucsize+1);
  679.  
  680.         fclose(outfile);                    /* close "data" file... */
  681.         outfile = fopen(filename, FOPR);    /* ...and reopen for reading */
  682.         if (!linktarget || (fread(linktarget, 1, ucsize, outfile) != ucsize)) {
  683.             fprintf(stderr, "\nwarning:  symbolic link (%s) failed\n",
  684.               filename);
  685.             if (linktarget)
  686.                 free(linktarget);
  687.             fclose(outfile);
  688.             return;
  689.         }
  690.         fclose(outfile);                    /* close "data" file for good... */
  691.         unlink(filename);                   /* ...and delete it */
  692.         linktarget[ucsize] = '\0';
  693.         fprintf(stdout, "-> %s ", linktarget);
  694.         if (symlink(linktarget, filename))  /* create the real link */
  695.             perror("symlink error");
  696.         free(linktarget);
  697.         return;                             /* can't set time on symlinks */
  698.     }
  699. #endif /* SYMLINKS */
  700.  
  701.     fclose(outfile);
  702.  
  703. /*---------------------------------------------------------------------------
  704.     Change the file permissions from default ones to those stored in the
  705.     zipfile.
  706.   ---------------------------------------------------------------------------*/
  707.  
  708. #ifndef NO_CHMOD
  709.     if (chmod(filename, 0xffff & pInfo->file_attr))
  710.             perror("chmod (file attributes) error");
  711. #endif
  712.  
  713. /*---------------------------------------------------------------------------
  714.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  715.     time:  adjust base year from 1980 to 1970, do usual conversions from
  716.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  717.     light savings time differences.
  718.   ---------------------------------------------------------------------------*/
  719.  
  720.     yr = ((lrec.last_mod_file_date >> 9) & 0x7f) + (1980 - YRBASE);
  721.     mo = ((lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  722.     dy = (lrec.last_mod_file_date & 0x1f) - 1;
  723.     hh = (lrec.last_mod_file_time >> 11) & 0x1f;
  724.     mm = (lrec.last_mod_file_time >> 5) & 0x3f;
  725.     ss = (lrec.last_mod_file_time & 0x1f) * 2;
  726.  
  727.     /* leap = # of leap yrs from YRBASE up to but not including current year */
  728.     leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  729.  
  730.     /* how many days from YRBASE to this year? (& add expired days this year) */
  731.     days = (yr * 365) + (leap - 492) + yday[mo];
  732.  
  733.     /* if year is a leap year and month is after February, add another day */
  734.     if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  735.         ++days;   /* OK through 2199 */
  736.  
  737.     /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  738.     m_time = ((days + dy) * 86400) + (hh * 3600) + (mm * 60) + ss;
  739.  
  740.     /* adjust for local timezone */
  741. #ifdef BSD
  742. #if (defined(__386BSD__) || defined(__bsdi__))
  743.     m_time -= localtime(&m_time)->tm_gmtoff;  /* seconds EAST of GMT:  subtr. */
  744. #else
  745.     ftime(&tbp);                    /* get `timezone' */
  746.     m_time += tbp.timezone * 60L;   /* seconds WEST of GMT:  add */
  747. #endif
  748. #else /* !BSD */
  749.     tzset();              /* get `timezone' */
  750.     m_time += timezone;   /* seconds WEST of GMT:  add */
  751. #endif /* ?BSD */
  752.  
  753.     /* adjust for daylight savings time (or local equivalent) */
  754. #ifndef __386BSD__  /* (DST already added to tm_gmtoff, so skip tm_isdst) */
  755.     if (localtime(&m_time)->tm_isdst)
  756.         m_time -= 60L * 60L;   /* adjust for daylight savings time */
  757. #endif
  758.  
  759.     /* set the file's access and modification times */
  760.     tp.actime = tp.modtime = m_time;
  761.     if (utime(filename, &tp)) {
  762. #ifdef AOS_VS
  763.         fprintf(stderr, "... can't set time for %s", filename);
  764. #else
  765.         fprintf(stderr, "warning:  can't set the time for %s\n", filename);
  766. #endif
  767.         FFLUSH(stderr);
  768.     }
  769.  
  770. } /* end function close_outfile() */
  771.  
  772. #endif /* !MTS */
  773.  
  774.  
  775.  
  776.  
  777. #if 0
  778.  
  779. /**************************************/
  780. /* Function set_file_time_and_close() */
  781. /**************************************/
  782.  
  783. void set_file_time_and_close()
  784. {
  785.     static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  786.     long m_time;
  787.     int yr, mo, dy, hh, mm, ss, leap, days;
  788.     struct utimbuf tp;
  789. #   define YRBASE  1970
  790. #ifndef __386BSD__
  791. #ifdef BSD
  792.     static struct timeb tbp;
  793. #else /* !BSD */
  794.     extern long timezone;
  795. #endif /* ?BSD */
  796. #endif /* !__386BSD__ */
  797.  
  798.  
  799. /*---------------------------------------------------------------------------
  800.     If symbolic links are supported, allocate a storage area, put the uncom-
  801.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  802.     link to start with, we shouldn't have to worry about overflowing unsigned
  803.     ints with unsigned longs.
  804.   ---------------------------------------------------------------------------*/
  805.  
  806. #ifdef SYMLINKS
  807.     if (symlnk) {
  808.         unsigned ucsize = (unsigned)lrec.ucsize;
  809.         char *linktarget = (char *)malloc((unsigned)lrec.ucsize+1);
  810.  
  811.         close(outfd);                       /* close "data" file... */
  812.         outfd = open(filename, O_RDONLY);   /* ...and reopen for reading */
  813.         if (!linktarget || (read(outfd, linktarget, ucsize) != ucsize)) {
  814.             fprintf(stderr, "\nwarning:  symbolic link (%s) failed\n",
  815.               filename);
  816.             if (linktarget)
  817.                 free(linktarget);
  818.             close(outfd);
  819.             return;
  820.         }
  821.         close(outfd);                       /* close "data" file for good... */
  822.         unlink(filename);                   /* ...and delete it */
  823.         linktarget[ucsize] = '\0';
  824.         fprintf(stdout, "-> %s ", linktarget);
  825.         if (symlink(linktarget, filename))  /* create the real link */
  826.             perror("symlink error");
  827.         free(linktarget);
  828.         return;                             /* can't set time on symlinks */
  829.     }
  830. #endif /* SYMLINKS */
  831.  
  832.     close(outfd);
  833.     if (cflag)          /* can't set time on stdout */
  834.         return;
  835.  
  836. /*---------------------------------------------------------------------------
  837.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  838.     time:  adjust base year from 1980 to 1970, do usual conversions from
  839.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  840.     light savings time differences.
  841.   ---------------------------------------------------------------------------*/
  842.  
  843.     yr = ((lrec.last_mod_file_date >> 9) & 0x7f) + (1980 - YRBASE);
  844.     mo = ((lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  845.     dy = (lrec.last_mod_file_date & 0x1f) - 1;
  846.     hh = (lrec.last_mod_file_time >> 11) & 0x1f;
  847.     mm = (lrec.last_mod_file_time >> 5) & 0x3f;
  848.     ss = (lrec.last_mod_file_time & 0x1f) * 2;
  849.  
  850.     /* leap = # of leap yrs from YRBASE up to but not including current year */
  851.     leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  852.  
  853.     /* how many days from YRBASE to this year? (& add expired days this year) */
  854.     days = (yr * 365) + (leap - 492) + yday[mo];
  855.  
  856.     /* if year is a leap year and month is after February, add another day */
  857.     if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  858.         ++days;   /* OK through 2199 */
  859.  
  860.     /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  861.     m_time = ((days + dy) * 86400) + (hh * 3600) + (mm * 60) + ss;
  862.  
  863.     /* adjust for local timezone */
  864. #ifdef BSD
  865. #ifdef __386BSD__
  866.     m_time -= localtime(&m_time)->tm_gmtoff;  /* seconds EAST of GMT:  subtr. */
  867. #else
  868.     ftime(&tbp);                    /* get `timezone' */
  869.     m_time += tbp.timezone * 60L;   /* seconds WEST of GMT:  add */
  870. #endif
  871. #else /* !BSD */
  872.     tzset();              /* get `timezone' */
  873.     m_time += timezone;   /* seconds WEST of GMT:  add */
  874. #endif /* ?BSD */
  875.  
  876.     /* adjust for daylight savings time (or local equivalent) */
  877. #ifndef __386BSD__  /* (DST already added to tm_gmtoff, so skip tm_isdst) */
  878.     if (localtime(&m_time)->tm_isdst)
  879.         m_time -= 60L * 60L;   /* adjust for daylight savings time */
  880. #endif
  881.  
  882.     /* set the file's access and modification times */
  883.     tp.actime = tp.modtime = m_time;
  884.     if (utime(filename, &tp))
  885.         fprintf(stderr, "warning:  can't set the time for %s\n", filename);
  886.  
  887. } /* end function set_file_time_and_close() */
  888.  
  889. #endif /* 0 */
  890.