home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip51.zip / atari / atari.c next >
C/C++ Source or Header  |  1994-01-03  |  31KB  |  826 lines

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