home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip512.zip / atari / atari.c next >
C/C++ Source or Header  |  1994-08-16  |  27KB  |  720 lines

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