home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / msdos / msdos.c < prev    next >
C/C++ Source or Header  |  1997-10-31  |  27KB  |  938 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Onno van der Linden, Christian Spieler and Kai Uwe Rommel.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. #include "zip.h"
  12.  
  13. #ifndef UTIL    /* little or no material in this file is used by UTIL */
  14.  
  15. #include <dos.h>
  16. #include <time.h>
  17.  
  18.  
  19. #if defined(__GO32__) || defined(__TURBOC__)
  20. #  include <dir.h> /* prototypes of find*() */
  21.    typedef struct ffblk   ff_dir;
  22. #  define FATTR (hidden_files ? FA_HIDDEN+FA_SYSTEM+FA_DIREC : FA_DIREC)
  23. #  define FFIRST(n,d,a)   findfirst(n,(struct ffblk *)d,a)
  24. #  define FNEXT(d)        findnext((struct ffblk *)d)
  25. #  if (!defined(__DJGPP__) || (__DJGPP__ < 2))
  26. #    define GetFileMode(name) bdosptr(0x43, (name), 0)
  27. #  else
  28. #    include <libc/dosio.h>
  29. #  endif
  30. #endif /* __GO32__ || __TURBOC__ */
  31.  
  32. #if defined(MSC) || defined(__WATCOMC__)
  33.    typedef struct find_t  ff_dir;
  34. #  define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR : _A_SUBDIR)
  35. #  ifndef FA_LABEL
  36. #    define FA_LABEL      _A_VOLID
  37. #  endif
  38. #  define FFIRST(n,d,a)   _dos_findfirst(n,a,(struct find_t *)d)
  39. #  define FNEXT(d)        _dos_findnext((struct find_t *)d)
  40. #  define ff_name         name
  41. #  define ff_fdate        wr_date
  42. #  define ff_ftime        wr_time
  43. #  define ff_attrib       attrib
  44. #endif /* MSC || __WATCOMC__ */
  45.  
  46. #ifdef __EMX__
  47. #  ifdef EMX_OBSOLETE           /* emx 0.9b or earlier */
  48. #    define size_t xxx_size_t
  49. #    define wchar_t xxx_wchar_t
  50. #    define tm xxx_tm
  51. #    include <sys/emx.h>
  52. #    undef size_t
  53. #    undef wchar_t
  54. #    undef tm
  55. #  else /* !EMX_OBSOLETE */     /* emx 0.9c or newer */
  56. #    include <emx/syscalls.h>
  57. #  endif /* ?EMX_OBSOLETE */
  58.    typedef struct _find   ff_dir;
  59. #  define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR : _A_SUBDIR)
  60. #  define FA_LABEL        _A_VOLID
  61. #  define FFIRST(n,d,a)   __findfirst(n,a,d)
  62. #  define FNEXT(d)        __findnext(d)
  63. #  define ff_name         name
  64. #  define ff_fdate        date
  65. #  define ff_ftime        time
  66. #  define ff_attrib       attr
  67. #  define GetFileMode(name) __chmod(name, 0, 0)
  68. #endif /* __EMX__ */
  69.  
  70.  
  71. /* Keep this =>SYNCHRONIZED<= with the corresponding definition in fileio.c */
  72. #if defined(__GO32__) || defined(__EMX__)
  73. #define MATCH shmatch
  74. #else
  75. #define MATCH dosmatch
  76. #endif /* __GO32__ */
  77.  
  78. #define PAD  0
  79. #define PATH_END '/'
  80.  
  81. /* Library functions not in (most) header files */
  82. int rmdir OF((const char *));
  83. int utime OF((char *, ztimbuf *));
  84.  
  85. /* Local functions */
  86. #ifndef GetFileMode
  87. int GetFileMode OF((char *name));
  88. #endif /* !GetFileMode */
  89.  
  90. local int  initDirSearch OF((char *name, ff_dir *ff_context_p));
  91. local char *getVolumeLabel OF((int, ulg *, ulg *, time_t *));
  92. local int  wild_recurse OF((char *, char *));
  93.  
  94. /* Module level variables */
  95. extern char *label;
  96. local ulg label_time = 0;
  97. local ulg label_mode = 0;
  98. local time_t label_utim = 0;
  99.  
  100. /* Module level constants */
  101. local ZCONST char wild_match_all[] = "*.*";
  102.  
  103.  
  104. #ifndef GetFileMode
  105. int GetFileMode(char *name)
  106. {
  107.   unsigned int attr = 0;
  108.   _dos_getfileattr(name, &attr);
  109.   return attr;
  110. }
  111. #endif /* !GetFileMode */
  112.  
  113. local int initDirSearch(name, ff_context_p)
  114.   char *name;                   /* name of directory to scan */
  115.   ff_dir *ff_context_p;         /* pointer to FFIRST/FNEXT context structure */
  116. {
  117.   int r;                        /* FFIRST return value */
  118.   char *p, *q;                  /* temporary copy of name, and aux pointer */
  119.  
  120.   if ((p = malloc(strlen(name) + 5)) == NULL)
  121.     return ZE_MEM;
  122.  
  123.   strcpy(p, name);
  124.   q = p + strlen(p);
  125.   if (q[-1] == ':')
  126.     *q++ = '.';
  127.   if ((q - p) > 0 && *(q - 1) != '/')
  128.     *q++ = '/';
  129.   strcpy(q, wild_match_all);
  130.   r = FFIRST(p, ff_context_p, FATTR);
  131.   free((zvoid *)p);
  132.  
  133.   return (r ? ZE_MISS : ZE_OK);
  134. }
  135.  
  136. local char *getVolumeLabel(drive, vtime, vmode, vutim)
  137.   int drive;    /* drive name: 'A' .. 'Z' or '\0' for current drive */
  138.   ulg *vtime;   /* volume label creation time (DOS format) */
  139.   ulg *vmode;   /* volume label file mode */
  140.   time_t *vutim;/* volume label creationtime (UNIX format) */
  141.  
  142. /* If a volume label exists for the given drive, return its name and
  143.    set its time and mode. The returned name must be static data. */
  144. {
  145.   static char vol[14];
  146.   ff_dir d;
  147.   char *p;
  148.  
  149.   if (drive) {
  150.     vol[0] = (char)drive;
  151.     strcpy(vol+1, ":/");
  152.   } else {
  153.     strcpy(vol, "/");
  154.   }
  155.   strcat(vol, wild_match_all);
  156.   if (FFIRST(vol, &d, FA_LABEL) == 0) {
  157.     strncpy(vol, d.ff_name, sizeof(vol)-1);
  158.     vol[sizeof(vol)-1] = '\0';   /* just in case */
  159.     if ((p = strchr(vol, '.')) != NULL) /* remove dot, though PKZIP doesn't */
  160.       strcpy(p, p + 1);
  161.     *vtime = ((ulg)d.ff_fdate << 16) | ((ulg)d.ff_ftime & 0xffff);
  162.     *vmode = (ulg)d.ff_attrib;
  163.     *vutim = dos2unixtime(*vtime);
  164.     return vol;
  165.   }
  166.   return NULL;
  167. }
  168.  
  169.  
  170. #define ONENAMELEN 12
  171.  
  172. /* whole is a pathname with wildcards, wildtail points somewhere in the  */
  173. /* middle of it.  All wildcards to be expanded must come AFTER wildtail. */
  174.  
  175. local int wild_recurse(whole, wildtail)
  176. char *whole;
  177. char *wildtail;
  178. {
  179.     ff_dir dir;
  180.     char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2;
  181.     ush newlen, amatch = 0;
  182.     int e = ZE_MISS;
  183.  
  184.     if (!isshexp(wildtail)) {
  185.         struct stat s;                          /* dummy buffer for stat() */
  186.  
  187.         if (!LSSTAT(whole, &s))                 /* file exists ? */
  188.             return procname(whole);
  189.         else
  190.             return ZE_MISS;                     /* woops, no wildcards! */
  191.     }
  192.  
  193.     /* back up thru path components till existing dir found */
  194.     do {
  195.         name = wildtail + strlen(wildtail) - 1;
  196.         for (;;)
  197.             if (name-- <= wildtail || *name == PATH_END) {
  198.                 subwild = name + 1;
  199.                 plug2 = *subwild;
  200.                 *subwild = 0;
  201.                 break;
  202.             }
  203.         if (glue)
  204.             *glue = plug;
  205.         glue = subwild;
  206.         plug = plug2;
  207.         e = initDirSearch(whole, &dir);
  208.     } while (e == ZE_MISS && subwild > wildtail);
  209.     wildtail = subwild;                 /* skip past non-wild components */
  210.     if (e != ZE_OK) {
  211.         if (glue)
  212.             *glue = plug;
  213.         goto ohforgetit;
  214.     }
  215.     subwild = strchr(wildtail + 1, PATH_END);
  216.     if (subwild != NULL) {
  217.         /* this "+ 1" dodges the  ^^^ hole left by *glue == 0 */
  218.         *(subwild++) = 0;               /* wildtail = one component pattern */
  219.         newlen = strlen(whole) + strlen(subwild) + ONENAMELEN + 2;
  220.     } else
  221.         newlen = strlen(whole) + ONENAMELEN + 1;
  222.     if ((newwhole = malloc(newlen)) == NULL) {
  223.         if (glue)
  224.             *glue = plug;
  225.         e = ZE_MEM;
  226.         goto ohforgetit;
  227.     }
  228.     strcpy(newwhole, whole);
  229.     newlen = strlen(newwhole);
  230.     if (glue)
  231.         *glue = plug;                           /* repair damage to whole */
  232.     if (!isshexp(wildtail)) {
  233.         e = ZE_MISS;                            /* non-wild name not found */
  234.         goto ohforgetit;
  235.     }
  236.  
  237.     do {
  238.         if (strcmp(dir.ff_name, ".") && strcmp(dir.ff_name, "..")
  239.                                   && MATCH(wildtail, dir.ff_name)) {
  240.             strcpy(newwhole + newlen, dir.ff_name);
  241.             if (subwild) {
  242.                 name = newwhole + strlen(newwhole);
  243.                 *(name++) = PATH_END;
  244.                 strcpy(name, subwild);
  245.                 e = wild_recurse(newwhole, name);
  246.             } else
  247.                 e = procname(newwhole);
  248.             newwhole[newlen] = 0;
  249.             if (e == ZE_OK)
  250.                 amatch = 1;
  251.             else if (e != ZE_MISS)
  252.                 break;
  253.         }
  254.     } while (FNEXT(&dir) == 0);
  255.  
  256.   ohforgetit:
  257.     if (subwild)
  258.         *--subwild = PATH_END;
  259.     if (newwhole)
  260.         free(newwhole);
  261.     if (e == ZE_MISS && amatch)
  262.         e = ZE_OK;
  263.     return e;
  264. }
  265.  
  266. int wild(w)
  267. char *w;                /* path/pattern to match */
  268. /* If not in exclude mode, expand the pattern based on the contents of the
  269.    file system.  Return an error code in the ZE_ class. */
  270. {
  271.     char *p;            /* path */
  272.     char *q;            /* diskless path */
  273.     int e;              /* result */
  274.  
  275.     if (volume_label == 1) {
  276.       volume_label = 2;
  277.       label = getVolumeLabel((w != NULL && w[1] == ':') ? to_up(w[0]) : '\0',
  278.                              &label_time, &label_mode, &label_utim);
  279.       if (label != NULL)
  280.         (void)newname(label, 0);
  281.       if (w == NULL || (w[1] == ':' && w[2] == '\0')) return ZE_OK;
  282.       /* "zip -$ foo a:" can be used to force drive name */
  283.     }
  284.     /* special handling of stdin request */
  285.     if (strcmp(w, "-") == 0)   /* if compressing stdin */
  286.         return newname(w, 0);
  287.  
  288.     /* Allocate and copy pattern, leaving room to add "." if needed */
  289.     if ((p = malloc(strlen(w) + 2)) == NULL)
  290.         return ZE_MEM;
  291.     strcpy(p, w);
  292.  
  293.     /* Normalize path delimiter as '/' */
  294.     for (q = p; *q; q++)                  /* use / consistently */
  295.         if (*q == '\\')
  296.             *q = '/';
  297.  
  298.     /* Separate the disk part of the path */
  299.     q = strchr(p, ':');
  300.     if (q != NULL) {
  301.         if (strchr(++q, ':'))     /* sanity check for safety of wild_recurse */
  302.             return ZE_MISS;
  303.     } else
  304.         q = p;
  305.  
  306.     /* Normalize bare disk names */
  307.     if (q > p && !*q)
  308.         strcpy(q, ".");
  309.  
  310.     /* Here we go */
  311.     e = wild_recurse(p, q);
  312.     free((zvoid *)p);
  313.     return e;
  314. }
  315.  
  316. int procname(n)
  317. char *n;                /* name to process */
  318. /* Process a name or sh expression to operate on (or exclude).  Return
  319.    an error code in the ZE_ class. */
  320. {
  321.   char *a;              /* path and name for recursion */
  322.   ff_dir *d;            /* control structure for FFIRST/FNEXT */
  323.   char *e;              /* pointer to name from readd() */
  324.   int m;                /* matched flag */
  325.   int ff_status;        /* return value of FFIRST/FNEXT */
  326.   char *p;              /* path for recursion */
  327.   struct stat s;        /* result of stat() */
  328.   struct zlist far *z;  /* steps through zfiles list */
  329.  
  330.   if (n == NULL)        /* volume_label request in freshen|delete mode ?? */
  331.     return ZE_OK;
  332.  
  333.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  334.     return newname(n, 0);
  335.   else if (*n == '\0') return ZE_MISS;
  336.   else if (LSSTAT(n, &s)
  337. #ifdef __TURBOC__
  338.            /* For this compiler, stat() succeeds on wild card names! */
  339.            || isshexp(n)
  340. #endif
  341.           )
  342.   {
  343.     /* Not a file or directory--search for shell expression in zip file */
  344.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  345.     m = 1;
  346.     for (z = zfiles; z != NULL; z = z->nxt) {
  347.       if (MATCH(p, z->iname))
  348.       {
  349.         z->mark = pcount ? filter(z->zname) : 1;
  350.         if (verbose)
  351.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  352.                z->mark ? "in" : "ex", z->name);
  353.         m = 0;
  354.       }
  355.     }
  356.     free((zvoid *)p);
  357.     return m ? ZE_MISS : ZE_OK;
  358.   }
  359.  
  360.   /* Live name--use if file, recurse if directory */
  361.   for (p = n; *p; p++)          /* use / consistently */
  362.     if (*p == '\\')
  363.       *p = '/';
  364.   if ((s.st_mode & S_IFDIR) == 0)
  365.   {
  366.     /* add or remove name of file */
  367.     if ((m = newname(n, 0)) != ZE_OK)
  368.       return m;
  369.   } else {
  370.     /* Add trailing / to the directory name */
  371.     if ((p = malloc(strlen(n)+2)) == NULL)
  372.       return ZE_MEM;
  373.     if (strcmp(n, ".") == 0 || strcmp(n, "/.") == 0) {
  374.       *p = '\0';  /* avoid "./" prefix and do not create zip entry */
  375.     } else {
  376.       strcpy(p, n);
  377.       a = p + strlen(p);
  378.       if (a[-1] != '/')
  379.         strcpy(a, "/");
  380.       if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  381.         free((zvoid *)p);
  382.         return m;
  383.       }
  384.     }
  385.     /* recurse into directory */
  386.     if (recurse)
  387.     {
  388.       if ((d = malloc(sizeof(ff_dir))) == NULL ||
  389.           (m = initDirSearch(n, d)) == ZE_MEM)
  390.       {
  391.         if (d != NULL)
  392.           free((zvoid *)d);
  393.         free((zvoid *)p);
  394.         return ZE_MEM;
  395.       }
  396.       for (e = d->ff_name, ff_status = m;
  397.            ff_status == 0;
  398.            ff_status = FNEXT(d))
  399.       {
  400.         if (strcmp(e, ".") && strcmp(e, ".."))
  401.         {
  402.           if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  403.           {
  404.             free((zvoid *)d);
  405.             free((zvoid *)p);
  406.             return ZE_MEM;
  407.           }
  408.           strcat(strcpy(a, p), e);
  409.           if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  410.           {
  411.             if (m == ZE_MISS)
  412.               zipwarn("name not matched: ", a);
  413.             else
  414.               ziperr(m, a);
  415.           }
  416.           free((zvoid *)a);
  417.         }
  418.       }
  419.       free((zvoid *)d);
  420.     }
  421.     free((zvoid *)p);
  422.   } /* (s.st_mode & S_IFDIR) == 0) */
  423.   return ZE_OK;
  424. }
  425.  
  426. char *ex2in(x, isdir, pdosflag)
  427. char *x;                /* external file name */
  428. int isdir;              /* input: x is a directory */
  429. int *pdosflag;          /* output: force MSDOS file attributes? */
  430. /* Convert the external file name to a zip file name, returning the malloc'ed
  431.    string or NULL if not enough memory. */
  432. {
  433.   char *n;              /* internal file name (malloc'ed) */
  434.   char *t;              /* shortened name */
  435.   int dosflag;
  436.  
  437.   dosflag = 1;
  438.  
  439.   /* Find starting point in name before doing malloc */
  440.   t = *x && *(x + 1) == ':' ? x + 2 : x;
  441.   while (*t == '/' || *t == '\\')
  442.     t++;
  443.   /* Skip leading "./" as well */
  444.   while (*t == '.' && (t[1] == '/' || t[1] == '\\'))
  445.     t += 2;
  446.  
  447.   /* Make changes, if any, to the copied name (leave original intact) */
  448.   for (n = t; *n; n++)
  449.     if (*n == '\\')
  450.       *n = '/';
  451.  
  452.   if (!pathput)
  453.     t = last(t, PATH_END);
  454.  
  455.   /* Malloc space for internal name and copy it */
  456.   if ((n = malloc(strlen(t) + 1)) == NULL)
  457.     return NULL;
  458.   strcpy(n, t);
  459.  
  460.   if (isdir == 42) return n;      /* avoid warning on unused variable */
  461.  
  462.   if (dosify)
  463.     msname(n);
  464.   else
  465. #if defined(__DJGPP__) && __DJGPP__ >= 2
  466.     if (_USE_LFN == 0)
  467. #endif
  468.       strlwr(n);
  469.   if (pdosflag)
  470.     *pdosflag = dosflag;
  471.   return n;
  472. }
  473.  
  474. char *in2ex(n)
  475. char *n;                /* internal file name */
  476. /* Convert the zip file name to an external file name, returning the malloc'ed
  477.    string or NULL if not enough memory. */
  478. {
  479.   char *x;              /* external file name */
  480.  
  481.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  482.       return NULL;
  483.   strcpy(x, n);
  484.  
  485.   return x;
  486. }
  487.  
  488. void stamp(f, d)
  489. char *f;                /* name of file to change */
  490. ulg d;                  /* dos-style time to change it to */
  491. /* Set last updated and accessed time of file f to the DOS time d. */
  492. {
  493. #if defined(__TURBOC__) || defined(__GO32__)
  494.   int h;                /* file handle */
  495.  
  496.   if ((h = open(f, 0)) != -1)
  497.   {
  498.     setftime(h, (struct ftime *)&d);
  499.     close(h);
  500.   }
  501. #else /* !__TURBOC__ && !__GO32__ */
  502.   ztimbuf u;            /* argument for utime() */
  503.  
  504.   /* Convert DOS time to time_t format in u.actime and u.modtime */
  505.   u.actime = u.modtime = dos2unixtime(d);
  506.  
  507.   /* Set updated and accessed times of f */
  508.   utime(f, &u);
  509. #endif /* ?(__TURBOC__ || __GO32__) */
  510. }
  511.  
  512. ulg filetime(f, a, n, t)
  513. char *f;                /* name of file to get info on */
  514. ulg *a;                 /* return value: file attributes */
  515. long *n;                /* return value: file size */
  516. iztimes *t;             /* return value: access, modific. and creation times */
  517. /* If file *f does not exist, return 0.  Else, return the file's last
  518.    modified date and time as an MSDOS date and time.  The date and
  519.    time is returned in a long with the date most significant to allow
  520.    unsigned integer comparison of absolute times.  Also, if a is not
  521.    a NULL pointer, store the file attributes there, with the high two
  522.    bytes being the Unix attributes, and the low byte being a mapping
  523.    of that to DOS attributes.  If n is not NULL, store the file size
  524.    there.  If t is not NULL, the file's access, modification and creation
  525.    times are stored there as UNIX time_t values.
  526.    If f is "-", use standard input as the file. If f is a device, return
  527.    a file size of -1 */
  528. {
  529.   struct stat s;        /* results of stat() */
  530.   char name[FNMAX];
  531.   int len = strlen(f), isstdin = !strcmp(f, "-");
  532.  
  533.   if (f == label) {
  534.     if (a != NULL)
  535.       *a = label_mode;
  536.     if (n != NULL)
  537.       *n = -2L; /* convention for a label name */
  538.     if (t != NULL)
  539.       t->atime = t->mtime = t->ctime = label_utim;
  540.     return label_time;
  541.   }
  542. #if defined(__TURBOC__)
  543.   /* Call tzset() to set timezone correctly (buggy older TC runtime libs) */
  544.   tzset();
  545. #endif /* __TURBOC__ */
  546.   strcpy(name, f);
  547.   if (name[len - 1] == '/')
  548.     name[len - 1] = '\0';
  549.   /* not all systems allow stat'ing a file with / appended */
  550.  
  551.   if (isstdin) {
  552.     if (fstat(fileno(stdin), &s) != 0)
  553.       error("fstat(stdin)");
  554.     time((time_t *)&s.st_mtime);       /* some fstat()s return time zero */
  555.   } else if (LSSTAT(name, &s) != 0)
  556.              /* Accept about any file kind including directories
  557.               * (stored with trailing / with -r option)
  558.               */
  559.     return 0;
  560.  
  561.   if (a != NULL)
  562.     *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));
  563.   if (n != NULL)
  564.     *n = (s.st_mode & S_IFREG) != 0 ? s.st_size : -1L;
  565.   if (t != NULL) {
  566.     t->atime = s.st_atime;
  567.     t->mtime = s.st_mtime;
  568.     t->ctime = s.st_ctime;
  569.   }
  570.  
  571.   return unix2dostime((time_t *)&s.st_mtime);
  572. }
  573.  
  574. int deletedir(d)
  575. char *d;                /* directory to delete */
  576. /* Delete the directory *d if it is empty, do nothing otherwise.
  577.    Return the result of rmdir(), delete(), or system().
  578.  */
  579. {
  580.     return rmdir(d);
  581. }
  582.  
  583. int set_extra_field(z, z_utim)
  584.   struct zlist far *z;
  585.   iztimes *z_utim;
  586.   /* create extra field and change z->att if desired */
  587. {
  588. #ifdef USE_EF_UT_TIME
  589.   if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
  590.     return ZE_MEM;
  591.  
  592.   z->extra[0]  = 'U';
  593.   z->extra[1]  = 'T';
  594.   z->extra[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */
  595.   z->extra[3]  = 0;
  596.   z->extra[4]  = EB_UT_FL_MTIME;
  597.   z->extra[5]  = (char)(z_utim->mtime);
  598.   z->extra[6]  = (char)(z_utim->mtime >> 8);
  599.   z->extra[7]  = (char)(z_utim->mtime >> 16);
  600.   z->extra[8]  = (char)(z_utim->mtime >> 24);
  601.  
  602.   z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));
  603.   z->cextra = z->extra;
  604.  
  605.   return ZE_OK;
  606. #else /* !USE_EF_UT_TIME */
  607.   return (int)(z-z);
  608. #endif /* ?USE_EF_UT_TIME */
  609. }
  610.  
  611.  
  612. #ifdef MY_ZCALLOC       /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */
  613.  
  614. #if defined(__TURBOC__) && !defined(OS2)
  615. /* Small and medium model are for now limited to near allocation with
  616.  * reduced MAX_WBITS and MAX_MEM_LEVEL
  617.  */
  618.  
  619. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  620.  * and farmalloc(64K) returns a pointer with an offset of 8, so we
  621.  * must fix the pointer. Warning: the pointer must be put back to its
  622.  * original form in order to free it, use zcfree().
  623.  */
  624.  
  625. #define MAX_PTR 10
  626. /* 10*64K = 640K */
  627.  
  628. local int next_ptr = 0;
  629.  
  630. typedef struct ptr_table_s {
  631.     zvoid far *org_ptr;
  632.     zvoid far *new_ptr;
  633. } ptr_table;
  634.  
  635. local ptr_table table[MAX_PTR];
  636. /* This table is used to remember the original form of pointers
  637.  * to large buffers (64K). Such pointers are normalized with a zero offset.
  638.  * Since MSDOS is not a preemptive multitasking OS, this table is not
  639.  * protected from concurrent access. This hack doesn't work anyway on
  640.  * a protected system like OS/2. Use Microsoft C instead.
  641.  */
  642.  
  643. zvoid far *zcalloc (unsigned items, unsigned size)
  644. {
  645.     zvoid far *buf;
  646.     ulg bsize = (ulg)items*size;
  647.  
  648.     if (bsize < (65536L-16L)) {
  649.         buf = farmalloc(bsize);
  650.         if (*(ush*)&buf != 0) return buf;
  651.     } else {
  652.         buf = farmalloc(bsize + 16L);
  653.     }
  654.     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  655.     table[next_ptr].org_ptr = buf;
  656.  
  657.     /* Normalize the pointer to seg:0 */
  658.     *((ush*)&buf+1) += ((ush)((uch*)buf-NULL) + 15) >> 4;
  659.     *(ush*)&buf = 0;
  660.     table[next_ptr++].new_ptr = buf;
  661.     return buf;
  662. }
  663.  
  664. zvoid zcfree (zvoid far *ptr)
  665. {
  666.     int n;
  667.     if (*(ush*)&ptr != 0) { /* object < 64K */
  668.         farfree(ptr);
  669.         return;
  670.     }
  671.     /* Find the original pointer */
  672.     for (n = next_ptr - 1; n >= 0; n--) {
  673.         if (ptr != table[n].new_ptr) continue;
  674.  
  675.         farfree(table[n].org_ptr);
  676.         while (++n < next_ptr) {
  677.             table[n-1] = table[n];
  678.         }
  679.         next_ptr--;
  680.         return;
  681.     }
  682.     ziperr(ZE_MEM, "zcfree: ptr not found");
  683. }
  684. #endif /* __TURBOC__ */
  685.  
  686. #if defined(MSC) || defined(__WATCOMC__)
  687. #if (!defined(_MSC_VER) || (_MSC_VER < 700))
  688. #  define _halloc  halloc
  689. #  define _hfree   hfree
  690. #endif
  691.  
  692. zvoid far *zcalloc (unsigned items, unsigned size)
  693. {
  694.     return (zvoid far *)_halloc((long)items, size);
  695. }
  696.  
  697. zvoid zcfree (zvoid far *ptr)
  698. {
  699.     _hfree((void huge *)ptr);
  700. }
  701. #endif /* MSC || __WATCOMC__ */
  702.  
  703. #endif /* MY_ZCALLOC */
  704.  
  705. #if (defined(__WATCOMC__) && defined(ASMV) && !defined(__386__))
  706. /* This is a hack to connect "call _exit" in match.asm to exit() */
  707. #pragma aux xit "_exit" parm caller []
  708. void xit(void)
  709. {
  710.     exit(20);
  711. }
  712. #endif
  713.  
  714. #endif /* !UTIL */
  715.  
  716.  
  717. #ifndef WINDLL
  718. /******************************/
  719. /*  Function version_local()  */
  720. /******************************/
  721.  
  722. static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  723.                         /* At module level to keep Turbo C++ 1.0 happy !! */
  724.  
  725. void version_local()
  726. {
  727. #if defined(__DJGPP__) || defined(__WATCOMC__) || \
  728.     (defined(_MSC_VER) && (_MSC_VER != 800))
  729.     char buf[80];
  730. #endif
  731.  
  732.     printf(CompiledWith,
  733.  
  734. #ifdef __GNUC__
  735. #  if defined(__DJGPP__)
  736.       (sprintf(buf, "djgpp v%d / gcc ", __DJGPP__), buf),
  737. #  elif defined(__GO32__)
  738.       "djgpp v1.x / gcc ",
  739. #  elif defined(__EMX__)            /* ...so is __EMX__ (double sigh) */
  740.       "emx+gcc ",
  741. #  else
  742.       "gcc ",
  743. #  endif
  744.       __VERSION__,
  745. #elif defined(__WATCOMC__)
  746. #  if (__WATCOMC__ % 10 > 0)
  747. /* We do this silly test because __WATCOMC__ gives two digits for the  */
  748. /* minor version, but Watcom packaging prefers to show only one digit. */
  749.       (sprintf(buf, "Watcom C/C++ %d.%02d", __WATCOMC__ / 100,
  750.                __WATCOMC__ % 100), buf), "",
  751. #  else
  752.       (sprintf(buf, "Watcom C/C++ %d.%d", __WATCOMC__ / 100,
  753.                (__WATCOMC__ % 100) / 10), buf), "",
  754. #  endif
  755. #elif defined(__TURBOC__)
  756. #  ifdef __BORLANDC__
  757.       "Borland C++",
  758. #    if (__BORLANDC__ < 0x0200)
  759.         " 1.0",
  760. #    elif (__BORLANDC__ == 0x0200)   /* James:  __TURBOC__ = 0x0297 */
  761.         " 2.0",
  762. #    elif (__BORLANDC__ == 0x0400)
  763.         " 3.0",
  764. #    elif (__BORLANDC__ == 0x0410)   /* __BCPLUSPLUS__ = 0x0310 */
  765.         " 3.1",
  766. #    elif (__BORLANDC__ == 0x0452)   /* __BCPLUSPLUS__ = 0x0320 */
  767.         " 4.0 or 4.02",
  768. #    elif (__BORLANDC__ == 0x0460)   /* __BCPLUSPLUS__ = 0x0340 */
  769.         " 4.5",
  770. #    elif (__BORLANDC__ == 0x0500)   /* __TURBOC__ = 0x0500 */
  771.         " 5.0",
  772. #    else
  773.         " later than 5.0",
  774. #    endif
  775. #  else
  776.       "Turbo C",
  777. #    if (__TURBOC__ > 0x0401)
  778.         "++ later than 3.0"
  779. #    elif (__TURBOC__ == 0x0401)     /* Kevin:  3.0 -> 0x0401 */
  780.         "++ 3.0",
  781. #    elif (__TURBOC__ == 0x0295)     /* [661] vfy'd by Kevin */
  782.         "++ 1.0",
  783. #    elif ((__TURBOC__ >= 0x018d) && (__TURBOC__ <= 0x0200)) /* James: 0x0200 */
  784.         " 2.0",
  785. #    elif (__TURBOC__ > 0x0100)
  786.         " 1.5",                    /* James:  0x0105? */
  787. #    else
  788.         " 1.0",                    /* James:  0x0100 */
  789. #    endif
  790. #  endif
  791. #elif defined(MSC)
  792.       "Microsoft C ",
  793. #  ifdef _MSC_VER
  794. #    if (_MSC_VER == 800)
  795.         "(Visual C++ v1.1)",
  796. #    elif (_MSC_VER == 850)
  797.         "(Windows NT v3.5 SDK)",
  798. #    elif (_MSC_VER == 900)
  799.         "(Visual C++ v2.0/v2.1)",
  800. #    elif (_MSC_VER > 900)
  801.         (sprintf(buf2, "(Visual C++ v%d.%d)", _MSC_VER/100 - 6,
  802.           _MSC_VER%100/10), buf2),
  803. #    else
  804.         (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf),
  805. #    endif
  806. #  else
  807.       "5.1 or earlier",
  808. #  endif
  809. #else
  810.       "unknown compiler", "",
  811. #endif
  812.  
  813.       "MS-DOS",
  814.  
  815. #if (defined(__GNUC__) || (defined(__WATCOMC__) && defined(__386__)))
  816.       " (32-bit)",
  817. #elif defined(M_I86HM) || defined(__HUGE__)
  818.       " (16-bit, huge)",
  819. #elif defined(M_I86LM) || defined(__LARGE__)
  820.       " (16-bit, large)",
  821. #elif defined(M_I86MM) || defined(__MEDIUM__)
  822.       " (16-bit, medium)",
  823. #elif defined(M_I86CM) || defined(__COMPACT__)
  824.       " (16-bit, compact)",
  825. #elif defined(M_I86SM) || defined(__SMALL__)
  826.       " (16-bit, small)",
  827. #elif defined(M_I86TM) || defined(__TINY__)
  828.       " (16-bit, tiny)",
  829. #else
  830.       " (16-bit)",
  831. #endif
  832.  
  833. #ifdef __DATE__
  834.       " on ", __DATE__
  835. #else
  836.       "", ""
  837. #endif
  838.     );
  839.  
  840. } /* end function version_local() */
  841. #endif /* !WINDLL */
  842.  
  843.  
  844. #if __DJGPP__ == 2
  845. int _is_executable (const char *p,int i,const char *q)
  846. {
  847.     return 0;
  848. }
  849. #endif
  850.  
  851. #if defined(_MSC_VER) && _MSC_VER == 700
  852.  
  853. /*
  854.  * ARGH.  MSC 7.0 libraries think times are based on 1899 Dec 31 00:00, not
  855.  *  1970 Jan 1 00:00.  So we have to diddle time_t's appropriately:  add
  856.  *  70 years' worth of seconds for localtime() wrapper function;
  857.  *  (70*365 regular days + 17 leap days + 1 1899 day) * 86400 ==
  858.  *  (25550 + 17 + 1) * 86400 == 2209075200 seconds.
  859.  *  Let time() and stat() return seconds since 1970 by using our own
  860.  *  _dtoxtime() which is the routine that is called by these two functions.
  861.  */
  862.  
  863.  
  864. #ifdef UTIL
  865. #  include <time.h>
  866. #endif
  867.  
  868. #ifndef UTIL
  869. #undef localtime
  870. struct tm *localtime(const time_t *);
  871.  
  872. struct tm *msc7_localtime(const time_t *clock)
  873. {
  874.    time_t t = *clock;
  875.  
  876.    t += 2209075200L;
  877.    return localtime(&t);
  878. }
  879. #endif /* !UTIL */
  880.  
  881.  
  882. void __tzset(void);
  883. int _isindst(struct tm *);
  884.  
  885. extern int _days[];
  886.  
  887. /* Nonzero if `y' is a leap year, else zero. */
  888. #define leap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  889.  
  890. /* Number of leap years from 1970 to `y' (not including `y' itself). */
  891. #define nleap(y) (((y) - 1969) / 4 - ((y) - 1901) / 100 + ((y) - 1601) / 400)
  892.  
  893. time_t _dtoxtime(year, month, mday, hour, min, sec)
  894. int year, month, mday, year, hour, min, sec;
  895. {
  896.    struct tm tm;
  897.    time_t t;
  898.    int days;
  899.  
  900.    days = _days[month - 1] + mday;
  901.    year += 1980;
  902.    if (leap(year) && month > 2)
  903.      ++days;
  904.    tm.tm_yday = days;
  905.    tm.tm_mon = month - 1;
  906.    tm.tm_year = year - 1900;
  907.    tm.tm_hour = hour;
  908.    __tzset();
  909.    days += 365 * (year - 1970) + nleap (year);
  910.    t = 86400L * days + 3600L * hour + 60 * min + sec + _timezone;
  911.    if (_daylight && _isindst(&tm))
  912.       t -= 3600;
  913.    return t;
  914. }
  915.  
  916. #endif /* _MSC_VER && _MSC_VER == 700 */
  917.  
  918.  
  919. #ifdef __WATCOMC__
  920.  
  921. /* This papers over a bug in Watcom 10.6's standard library... sigh */
  922. /* Apparently it applies to both the DOS and Win32 stat()s.         */
  923.  
  924. int stat_bandaid(const char *path, struct stat *buf)
  925. {
  926.   char newname[4];
  927.   if (!stat(path, buf))
  928.     return 0;
  929.   else if (!strcmp(path, ".") || (path[0] && !strcmp(path + 1, ":."))) {
  930.     strcpy(newname, path);
  931.     newname[strlen(path) - 1] = '\\';   /* stat(".") fails for root! */
  932.     return stat(newname, buf);
  933.   } else
  934.     return -1;
  935. }
  936.  
  937. #endif
  938.