home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / msdos / msdos.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  24KB  |  861 lines

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