home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip540.zip / msdos / msdos.c < prev    next >
C/C++ Source or Header  |  1998-10-29  |  71KB  |  2,057 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   msdos.c
  4.  
  5.   MSDOS-specific routines for use with Info-ZIP's UnZip 5.3 and later.
  6.  
  7.   Contains:  Opendir()                      (from zip)
  8.              Readdir()                      (from zip)
  9.              do_wild()
  10.              mapattr()
  11.              mapname()
  12.              map2fat()
  13.              checkdir()
  14.              isfloppy()
  15.              z_dos_chmod()
  16.              volumelabel()                  (non-djgpp, non-emx)
  17.              close_outfile()
  18.              stamp_file()                   (TIMESTAMP only)
  19.              dateformat()
  20.              version()
  21.              _dos_getcountryinfo()          (djgpp 1.x, emx)
  22.             [_dos_getftime()                (djgpp 1.x, emx)   to be added]
  23.              _dos_setftime()                (djgpp 1.x, emx)
  24.              _dos_setfileattr()             (djgpp 1.x, emx)
  25.              _dos_getdrive()                (djgpp 1.x, emx)
  26.              _dos_creat()                   (djgpp 1.x, emx)
  27.              _dos_close()                   (djgpp 1.x, emx)
  28.              volumelabel()                  (djgpp, emx)
  29.              _dos_getcountryinfo()          (djgpp 2.x)
  30.              _is_executable()               (djgpp 2.x)
  31.              __crt0_glob_function()         (djgpp 2.x)
  32.              __crt0_load_environment_file() (djgpp 2.x)
  33.              screenlines()                  (emx)
  34.              screencolumns()                (emx)
  35.              int86x_realmode()              (Watcom 32-bit)
  36.              stat_bandaid()                 (Watcom)
  37.  
  38.   ---------------------------------------------------------------------------*/
  39.  
  40.  
  41.  
  42. #define UNZIP_INTERNAL
  43. #include "unzip.h"
  44.  
  45. #ifdef MAYBE_PLAIN_FAT
  46.    static void map2fat OF((char *pathcomp, char *last_dot));
  47. #endif
  48. static int isfloppy OF((int nDrive));
  49. static int z_dos_chmod OF((__GPRO__ ZCONST char *fname, int attributes));
  50. static int volumelabel OF((char *newlabel));
  51.  
  52. static int created_dir;        /* used by mapname(), checkdir() */
  53. static int renamed_fullpath;   /* ditto */
  54. static unsigned nLabelDrive;   /* ditto, plus volumelabel() */
  55.  
  56.  
  57.  
  58. /*****************************/
  59. /*  Strings used in msdos.c  */
  60. /*****************************/
  61.  
  62. #ifndef SFX
  63.   static ZCONST char Far CantAllocateWildcard[] =
  64.     "warning:  cannot allocate wildcard buffers\n";
  65. #endif
  66. static ZCONST char Far Creating[] = "   creating: %s\n";
  67. static ZCONST char Far ConversionFailed[] =
  68.   "mapname:  conversion of %s failed\n";
  69. static ZCONST char Far Labelling[] = "labelling %c: %-22s\n";
  70. static ZCONST char Far ErrSetVolLabel[] =
  71.   "mapname:  error setting volume label\n";
  72. static ZCONST char Far PathTooLong[] = "checkdir error:  path too long: %s\n";
  73. static ZCONST char Far CantCreateDir[] = "checkdir error:  cannot create %s\n\
  74.                  unable to process %s.\n";
  75. static ZCONST char Far DirIsntDirectory[] =
  76.   "checkdir error:  %s exists but is not directory\n\
  77.                  unable to process %s.\n";
  78. static ZCONST char Far PathTooLongTrunc[] =
  79.   "checkdir warning:  path too long; truncating\n                   %s\n\
  80.                 -> %s\n";
  81. #if (!defined(SFX) || defined(SFX_EXDIR))
  82.    static ZCONST char Far CantCreateExtractDir[] =
  83.      "checkdir:  cannot create extraction directory: %s\n";
  84. #endif
  85. static ZCONST char Far AttribsMayBeWrong[] =
  86.   "\nwarning:  file attributes may not be correct\n";
  87.  
  88.  
  89.  
  90. /****************************/
  91. /*  Macros used in msdos.c  */
  92. /****************************/
  93.  
  94. #ifdef WATCOMC_386
  95. #  define WREGS(v,r) (v##.w.##r)
  96. #  define int86x int386x
  97.    static int int86x_realmode(int inter_no, union REGS *in,
  98.                               union REGS *out, struct SREGS *seg);
  99. #  define F_intdosx(ir,or,sr) int86x_realmode(0x21, ir, or, sr)
  100. #  define XXX__MK_FP_IS_BROKEN
  101. #else
  102. #  define WREGS(v,r) (v##.x.##r)
  103. #  define F_intdosx(ir,or,sr) intdosx(ir, or, sr)
  104. #endif
  105.  
  106. #if (defined(__GO32__) || defined(__EMX__))
  107. #  include <dirent.h>        /* use readdir() */
  108. #  define MKDIR(path,mode)   mkdir(path,mode)
  109. #  define Opendir  opendir
  110. #  define Readdir  readdir
  111. #  define Closedir closedir
  112. #  define zdirent  dirent
  113. #  define zDIR     DIR
  114. #  ifdef __EMX__
  115. #    include <dos.h>
  116. #    define GETDRIVE(d)      d = _getdrive()
  117. #    define FA_LABEL         A_LABEL
  118. #  else
  119. #    define GETDRIVE(d)      _dos_getdrive(&d)
  120. #  endif
  121. #  if defined(_A_SUBDIR)     /* MSC dos.h and compatibles */
  122. #    define FSUBDIR          _A_SUBDIR
  123. #  elif defined(FA_DIREC)    /* Borland dos.h and compatible variants */
  124. #    define FSUBDIR          FA_DIREC
  125. #  elif defined(A_DIR)       /* EMX dir.h (and dirent.h) */
  126. #    define FSUBDIR          A_DIR
  127. #  else                      /* fallback definition */
  128. #    define FSUBDIR          0x10
  129. #  endif
  130. #  if defined(_A_VOLID)      /* MSC dos.h and compatibles */
  131. #    define FVOLID           _A_VOLID
  132. #  elif defined(FA_LABEL)    /* Borland dos.h and compatible variants */
  133. #    define FVOLID           FA_LABEL
  134. #  elif defined(A_LABEL)     /* EMX dir.h (and dirent.h) */
  135. #    define FVOLID           A_LABEL
  136. #  else
  137. #    define FVOLID           0x08
  138. #  endif
  139. #else /* !(__GO32__ || __EMX__) */
  140. #  define MKDIR(path,mode)   mkdir(path)
  141. #  ifdef __TURBOC__
  142. #    define FATTR            FA_HIDDEN+FA_SYSTEM+FA_DIREC
  143. #    define FVOLID           FA_LABEL
  144. #    define FSUBDIR          FA_DIREC
  145. #    define FFIRST(n,d,a)    findfirst(n,(struct ffblk *)d,a)
  146. #    define FNEXT(d)         findnext((struct ffblk *)d)
  147. #    define GETDRIVE(d)      d=getdisk()+1
  148. #    include <dir.h>
  149. #  else /* !__TURBOC__ */
  150. #    define FATTR            _A_HIDDEN+_A_SYSTEM+_A_SUBDIR
  151. #    define FVOLID           _A_VOLID
  152. #    define FSUBDIR          _A_SUBDIR
  153. #    define FFIRST(n,d,a)    _dos_findfirst(n,a,(struct find_t *)d)
  154. #    define FNEXT(d)         _dos_findnext((struct find_t *)d)
  155. #    define GETDRIVE(d)      _dos_getdrive(&d)
  156. #    include <direct.h>
  157. #  endif /* ?__TURBOC__ */
  158.    typedef struct zdirent {
  159.        char d_reserved[30];
  160.        char d_name[13];
  161.        int d_first;
  162.    } zDIR;
  163.    zDIR *Opendir OF((const char *));
  164.    struct zdirent *Readdir OF((zDIR *));
  165. #  define Closedir free
  166.  
  167.  
  168.  
  169.  
  170. #ifndef SFX
  171.  
  172. /**********************/   /* Borland C++ 3.x has its own opendir/readdir */
  173. /* Function Opendir() */   /*  library routines, but earlier versions don't, */
  174. /**********************/   /*  so use ours regardless */
  175.  
  176. zDIR *Opendir(name)
  177.     const char *name;           /* name of directory to open */
  178. {
  179.     zDIR *dirp;                 /* malloc'd return value */
  180.     char *nbuf;                 /* malloc'd temporary string */
  181.     extent len = strlen(name);  /* path length to avoid strlens and strcats */
  182.  
  183.  
  184.     if ((dirp = (zDIR *)malloc(sizeof(zDIR))) == (zDIR *)NULL)
  185.         return (zDIR *)NULL;
  186.     if ((nbuf = malloc(len + 6)) == (char *)NULL) {
  187.         free(dirp);
  188.         return (zDIR *)NULL;
  189.     }
  190.     strcpy(nbuf, name);
  191.     if (len > 0) {
  192.         if (nbuf[len-1] == ':') {
  193.             nbuf[len++] = '.';
  194.         } else if (nbuf[len-1] == '/' || nbuf[len-1] == '\\')
  195.             --len;
  196.     }
  197.     strcpy(nbuf+len, "/*.*");
  198.     Trace((stderr, "Opendir:  nbuf = [%s]\n", nbuf));
  199.  
  200.     if (FFIRST(nbuf, dirp, FATTR)) {
  201.         free((zvoid *)nbuf);
  202.         return (zDIR *)NULL;
  203.     }
  204.     free((zvoid *)nbuf);
  205.     dirp->d_first = 1;
  206.     return dirp;
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213. /**********************/
  214. /* Function Readdir() */
  215. /**********************/
  216.  
  217. struct zdirent *Readdir(d)
  218.     zDIR *d;        /* directory stream from which to read */
  219. {
  220.     /* Return pointer to first or next directory entry, or NULL if end. */
  221.  
  222.     if (d->d_first)
  223.         d->d_first = 0;
  224.     else
  225.         if (FNEXT(d))
  226.             return (struct zdirent *)NULL;
  227.     return (struct zdirent *)d;
  228. }
  229.  
  230. #endif /* !SFX */
  231. #endif /* ?(__GO32__ || __EMX__) */
  232.  
  233.  
  234.  
  235.  
  236.  
  237. #ifndef SFX
  238.  
  239. /************************/
  240. /*  Function do_wild()  */   /* identical to OS/2 version */
  241. /************************/
  242.  
  243. char *do_wild(__G__ wildspec)
  244.     __GDEF
  245.     char *wildspec;          /* only used first time on a given dir */
  246. {
  247.     static zDIR *dir = (zDIR *)NULL;
  248.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  249.     static int firstcall=TRUE, have_dirname, dirnamelen;
  250.     char *fnamestart;
  251.     struct zdirent *file;
  252.  
  253.  
  254.     /* Even when we're just returning wildspec, we *always* do so in
  255.      * matchname[]--calling routine is allowed to append four characters
  256.      * to the returned string, and wildspec may be a pointer to argv[].
  257.      */
  258.     if (firstcall) {        /* first call:  must initialize everything */
  259.         firstcall = FALSE;
  260.  
  261.         if (!iswild(wildspec)) {
  262.             strcpy(matchname, wildspec);
  263.             have_dirname = FALSE;
  264.             dir = NULL;
  265.             return matchname;
  266.         }
  267.  
  268.         /* break the wildspec into a directory part and a wildcard filename */
  269.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL &&
  270.             (wildname = strrchr(wildspec, ':')) == (char *)NULL) {
  271.             dirname = ".";
  272.             dirnamelen = 1;
  273.             have_dirname = FALSE;
  274.             wildname = wildspec;
  275.         } else {
  276.             ++wildname;     /* point at character after '/' or ':' */
  277.             dirnamelen = (int)(wildname - wildspec);
  278.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  279.                 Info(slide, 1, ((char *)slide,
  280.                   LoadFarString(CantAllocateWildcard)));
  281.                 strcpy(matchname, wildspec);
  282.                 return matchname;   /* but maybe filespec was not a wildcard */
  283.             }
  284. /* GRR:  can't strip trailing char for opendir since might be "d:/" or "d:"
  285.  *       (would have to check for "./" at end--let opendir handle it instead) */
  286.             strncpy(dirname, wildspec, dirnamelen);
  287.             dirname[dirnamelen] = '\0';       /* terminate for strcpy below */
  288.             have_dirname = TRUE;
  289.         }
  290.         Trace((stderr, "do_wild:  dirname = [%s]\n", dirname));
  291.  
  292.         if ((dir = Opendir(dirname)) != (zDIR *)NULL) {
  293.             if (have_dirname) {
  294.                 strcpy(matchname, dirname);
  295.                 fnamestart = matchname + dirnamelen;
  296.             } else
  297.                 fnamestart = matchname;
  298.             while ((file = Readdir(dir)) != (struct zdirent *)NULL) {
  299.                 Trace((stderr, "do_wild:  readdir returns %s\n", file->d_name));
  300.                 strcpy(fnamestart, file->d_name);
  301.                 if (strrchr(fnamestart, '.') == (char *)NULL)
  302.                     strcat(fnamestart, ".");
  303.                 if (match(fnamestart, wildname, 1) &&  /* 1 == ignore case */
  304.                     /* skip "." and ".." directory entries */
  305.                     strcmp(fnamestart, ".") && strcmp(fnamestart, "..")) {
  306.                     Trace((stderr, "do_wild:  match() succeeds\n"));
  307.                     /* remove trailing dot */
  308.                     fnamestart += strlen(fnamestart) - 1;
  309.                     if (*fnamestart == '.')
  310.                         *fnamestart = '\0';
  311.                     return matchname;
  312.                 }
  313.             }
  314.             /* if we get to here directory is exhausted, so close it */
  315.             Closedir(dir);
  316.             dir = (zDIR *)NULL;
  317.         }
  318. #ifdef DEBUG
  319.         else {
  320.             Trace((stderr, "do_wild:  Opendir(%s) returns NULL\n", dirname));
  321.         }
  322. #endif /* DEBUG */
  323.  
  324.         /* return the raw wildspec in case that works (e.g., directory not
  325.          * searchable, but filespec was not wild and file is readable) */
  326.         strcpy(matchname, wildspec);
  327.         return matchname;
  328.     }
  329.  
  330.     /* last time through, might have failed opendir but returned raw wildspec */
  331.     if (dir == (zDIR *)NULL) {
  332.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  333.         if (have_dirname)
  334.             free(dirname);
  335.         return (char *)NULL;
  336.     }
  337.  
  338.     /* If we've gotten this far, we've read and matched at least one entry
  339.      * successfully (in a previous call), so dirname has been copied into
  340.      * matchname already.
  341.      */
  342.     if (have_dirname) {
  343.         /* strcpy(matchname, dirname); */
  344.         fnamestart = matchname + dirnamelen;
  345.     } else
  346.         fnamestart = matchname;
  347.     while ((file = Readdir(dir)) != (struct zdirent *)NULL) {
  348.         Trace((stderr, "do_wild:  readdir returns %s\n", file->d_name));
  349.         strcpy(fnamestart, file->d_name);
  350.         if (strrchr(fnamestart, '.') == (char *)NULL)
  351.             strcat(fnamestart, ".");
  352.         if (match(fnamestart, wildname, 1)) {   /* 1 == ignore case */
  353.             Trace((stderr, "do_wild:  match() succeeds\n"));
  354.             /* remove trailing dot */
  355.             fnamestart += strlen(fnamestart) - 1;
  356.             if (*fnamestart == '.')
  357.                 *fnamestart = '\0';
  358.             return matchname;
  359.         }
  360.     }
  361.  
  362.     Closedir(dir);     /* have read at least one dir entry; nothing left */
  363.     dir = (zDIR *)NULL;
  364.     firstcall = TRUE;  /* reset for new wildspec */
  365.     if (have_dirname)
  366.         free(dirname);
  367.     return (char *)NULL;
  368.  
  369. } /* end function do_wild() */
  370.  
  371. #endif /* !SFX */
  372.  
  373.  
  374.  
  375.  
  376. /**********************/
  377. /* Function mapattr() */
  378. /**********************/
  379.  
  380. int mapattr(__G)
  381.     __GDEF
  382. {
  383.     /* set archive bit for file entries (file is not backed up): */
  384.     G.pInfo->file_attr = ((unsigned)G.crec.external_file_attributes |
  385.       (G.crec.external_file_attributes & FSUBDIR ? 0 : 32)) & 0xff;
  386.     return 0;
  387.  
  388. } /* end function mapattr() */
  389.  
  390.  
  391.  
  392.  
  393.  
  394. /************************/
  395. /*  Function mapname()  */
  396. /************************/
  397.                              /* return 0 if no error, 1 if caution (filename */
  398. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  399.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  400.     int renamed;             /*  or 10 if out of memory (skip file) */
  401. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  402.     char pathcomp[FILNAMSIZ];      /* path-component buffer */
  403.     char *pp, *cp=(char *)NULL;    /* character pointers */
  404.     char *lastsemi=(char *)NULL;   /* pointer to last semi-colon in pathcomp */
  405. #ifdef MAYBE_PLAIN_FAT
  406.     char *last_dot=(char *)NULL;   /* last dot not converted to underscore */
  407.     int dotname = FALSE;           /* path component begins with dot? */
  408. # ifdef USE_LFN
  409.     int use_lfn = USE_LFN;         /* file system supports long filenames? */
  410. # endif
  411. #endif
  412.     int error = 0;
  413.     register unsigned workch;      /* hold the character being tested */
  414.  
  415.  
  416. /*---------------------------------------------------------------------------
  417.     Initialize various pointers and counters and stuff.
  418.   ---------------------------------------------------------------------------*/
  419.  
  420.     /* can create path as long as not just freshening, or if user told us */
  421.     G.create_dirs = (!uO.fflag || renamed);
  422.  
  423.     created_dir = FALSE;        /* not yet */
  424.     renamed_fullpath = FALSE;
  425.  
  426.     if (renamed) {
  427.         cp = G.filename - 1;    /* point to beginning of renamed name... */
  428.         while (*++cp)
  429.             if (*cp == '\\')    /* convert backslashes to forward */
  430.                 *cp = '/';
  431.         cp = G.filename;
  432.         /* use temporary rootpath if user gave full pathname */
  433.         if (G.filename[0] == '/') {
  434.             renamed_fullpath = TRUE;
  435.             pathcomp[0] = '/';  /* copy the '/' and terminate */
  436.             pathcomp[1] = '\0';
  437.             ++cp;
  438.         } else if (isalpha(G.filename[0]) && G.filename[1] == ':') {
  439.             renamed_fullpath = TRUE;
  440.             pp = pathcomp;
  441.             *pp++ = *cp++;      /* copy the "d:" (+ '/', possibly) */
  442.             *pp++ = *cp++;
  443.             if (*cp == '/')
  444.                 *pp++ = *cp++;  /* otherwise add "./"? */
  445.             *pp = '\0';
  446.         }
  447.     }
  448.  
  449.     /* pathcomp is ignored unless renamed_fullpath is TRUE: */
  450.     if ((error = checkdir(__G__ pathcomp, INIT)) != 0) /* initialize path buf */
  451.         return error;           /* ...unless no mem or vol label on hard disk */
  452.  
  453.     *pathcomp = '\0';           /* initialize translation buffer */
  454.     pp = pathcomp;              /* point to translation buffer */
  455.     if (!renamed) {             /* cp already set if renamed */
  456.         if (uO.jflag)           /* junking directories */
  457.             cp = (char *)strrchr(G.filename, '/');
  458.         if (cp == (char *)NULL) /* no '/' or not junking dirs */
  459.             cp = G.filename;    /* point to internal zipfile-member pathname */
  460.         else
  461.             ++cp;               /* point to start of last component of path */
  462.     }
  463.  
  464. /*---------------------------------------------------------------------------
  465.     Begin main loop through characters in filename.
  466.   ---------------------------------------------------------------------------*/
  467.  
  468.     while ((workch = (uch)*cp++) != 0) {
  469.  
  470.         switch (workch) {
  471.             case '/':             /* can assume -j flag not given */
  472.                 *pp = '\0';
  473. #ifdef MAYBE_PLAIN_FAT
  474. # ifdef USE_LFN
  475.                 if (!use_lfn)
  476. # endif
  477.                 {
  478.                     map2fat(pathcomp, last_dot);   /* 8.3 trunc. (in place) */
  479.                     last_dot = (char *)NULL;
  480.                 }
  481. #endif
  482.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  483.                     return error;
  484.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  485.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  486.                 break;
  487.  
  488.             /* drive names are not stored in zipfile, so no colons allowed;
  489.              *  no brackets or most other punctuation either (all of which
  490.              *  can appear in Unix-created archives; backslash is particularly
  491.              *  bad unless all necessary directories exist) */
  492. #ifdef MAYBE_PLAIN_FAT
  493.             case '[':          /* these punctuation characters forbidden */
  494.             case ']':          /*  only on plain FAT file systems */
  495.             case '+':
  496.             case ',':
  497.             case '=':
  498. # ifdef USE_LFN
  499.                 if (use_lfn)
  500.                     *pp++ = (char)workch;
  501.                 else
  502.                     *pp++ = '_';
  503.                 break;
  504. # endif
  505. #endif
  506.             case ':':           /* special shell characters of command.com */
  507.             case '\\':          /*  (device and directory limiters, wildcard */
  508.             case '"':           /*  characters, stdin/stdout redirection and */
  509.             case '<':           /*  pipe indicators and the quote sign) are */
  510.             case '>':           /*  never allowed in filenames on (V)FAT */
  511.             case '|':
  512.             case '*':
  513.             case '?':
  514.                 *pp++ = '_';
  515.                 break;
  516.  
  517. #ifdef MAYBE_PLAIN_FAT
  518.             case '.':
  519. # ifdef USE_LFN
  520.                 if (use_lfn) {
  521.                     *pp++ = (char)workch;
  522.                     break;
  523.                 }
  524. # endif
  525.                 if (pp == pathcomp) {     /* nothing appended yet... */
  526.                     if (*cp == '/') {     /* don't bother appending a "./" */
  527.                         ++cp;             /*  component to the path:  skip */
  528.                         break;            /*  to next char after the '/' */
  529.                     } else if (*cp == '.' && cp[1] == '/') {   /* "../" */
  530.                         *pp++ = '.';      /* add first dot, unchanged... */
  531.                         ++cp;             /* skip second dot, since it will */
  532.                     } else {              /*  be "added" at end of if-block */
  533.                         *pp++ = '_';      /* FAT doesn't allow null filename */
  534.                         dotname = TRUE;   /*  bodies, so map .exrc -> _.exrc */
  535.                     }                     /*  (extra '_' now, "dot" below) */
  536.                 } else if (dotname) {     /* found a second dot, but still */
  537.                     dotname = FALSE;      /*  have extra leading underscore: */
  538.                     *pp = '\0';           /*  remove it by shifting chars */
  539.                     pp = pathcomp + 1;    /*  left one space (e.g., .p1.p2: */
  540.                     while (pp[1]) {       /*  __p1 -> _p1_p2 -> _p1.p2 when */
  541.                         *pp = pp[1];      /*  finished) [opt.:  since first */
  542.                         ++pp;             /*  two chars are same, can start */
  543.                     }                     /*  shifting at second position] */
  544.                 }
  545.                 last_dot = pp;    /* point at last dot so far... */
  546.                 *pp++ = '_';      /* convert dot to underscore for now */
  547.                 break;
  548. #endif /* MAYBE_PLAIN_FAT */
  549.  
  550.             case ';':             /* start of VMS version? */
  551.                 lastsemi = pp;
  552. #ifdef MAYBE_PLAIN_FAT
  553. # ifdef USE_LFN
  554.                 if (use_lfn)
  555.                     *pp++ = ';';  /* keep for now; remove VMS ";##" later */
  556. # endif
  557. #else
  558.                 *pp++ = ';';      /* keep for now; remove VMS ";##" later */
  559. #endif
  560.                 break;
  561.  
  562. #ifdef MAYBE_PLAIN_FAT
  563.             case ' ':                      /* change spaces to underscores */
  564. # ifdef USE_LFN
  565.                 if (!use_lfn && uO.sflag)  /*  only if requested and NO lfn! */
  566. # else
  567.                 if (uO.sflag)              /*  only if requested */
  568. # endif
  569.                     *pp++ = '_';
  570.                 else
  571.                     *pp++ = (char)workch;
  572.                 break;
  573. #endif /* MAYBE_PLAIN_FAT */
  574.  
  575.             default:
  576.                 /* allow ASCII 255 and European characters in filenames: */
  577.                 if (isprint(workch) || workch >= 127)
  578.                     *pp++ = (char)workch;
  579.  
  580.         } /* end switch */
  581.     } /* end while loop */
  582.  
  583.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  584.  
  585.     /* if not saving them, remove VMS version numbers (appended ";###") */
  586.     if (!uO.V_flag && lastsemi) {
  587. #ifndef MAYBE_PLAIN_FAT
  588.         pp = lastsemi + 1;
  589. #else
  590. # ifdef USE_LFN
  591.         if (use_lfn)
  592.             pp = lastsemi + 1;
  593.         else
  594.             pp = lastsemi;        /* semi-colon was omitted:  expect all #'s */
  595. # else
  596.         pp = lastsemi;            /* semi-colon was omitted:  expect all #'s */
  597. # endif
  598. #endif
  599.         while (isdigit((uch)(*pp)))
  600.             ++pp;
  601.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  602.             *lastsemi = '\0';
  603.     }
  604.  
  605.     if (G.pInfo->vollabel) {
  606.         if (strlen(pathcomp) > 11)
  607.             pathcomp[11] = '\0';
  608.     }
  609.  
  610. #ifdef MAYBE_PLAIN_FAT
  611. # ifdef USE_LFN
  612.     if (!use_lfn)
  613.         map2fat(pathcomp, last_dot);  /* 8.3 truncation (in place) */
  614. # else
  615.     map2fat(pathcomp, last_dot);  /* 8.3 truncation (in place) */
  616. # endif
  617. #endif
  618.  
  619. /*---------------------------------------------------------------------------
  620.     Report if directory was created (and no file to create:  filename ended
  621.     in '/'), check name to be sure it exists, and combine path and name be-
  622.     fore exiting.
  623.   ---------------------------------------------------------------------------*/
  624.  
  625.     if (G.filename[strlen(G.filename) - 1] == '/') {
  626.         checkdir(__G__ G.filename, GETPATH);
  627.         if (created_dir) {
  628.             if (QCOND2) {
  629.                 Info(slide, 0, ((char *)slide, LoadFarString(Creating),
  630.                   FnFilter1(G.filename)));
  631.             }
  632.  
  633.             /* set file attributes: */
  634.             z_dos_chmod(__G__ G.filename, G.pInfo->file_attr);
  635.  
  636.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  637.         } else if (uO.overwrite_all) {
  638.             /* overwrite attributes of existing directory on user's request */
  639.  
  640.             /* set file attributes: */
  641.             z_dos_chmod(__G__ G.filename, G.pInfo->file_attr);
  642.         }
  643.         return 2;   /* dir existed already; don't look for data to extract */
  644.     }
  645.  
  646.     if (*pathcomp == '\0') {
  647.         Info(slide, 1, ((char *)slide, LoadFarString(ConversionFailed),
  648.           FnFilter1(G.filename)));
  649.         return 3;
  650.     }
  651.  
  652.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  653.     checkdir(__G__ G.filename, GETPATH);
  654.  
  655.     if (G.pInfo->vollabel) {    /* set the volume label now */
  656.         if (QCOND2)
  657.             Info(slide, 0, ((char *)slide, LoadFarString(Labelling),
  658.               (nLabelDrive + 'a' - 1),
  659.               FnFilter1(G.filename)));
  660.         if (volumelabel(G.filename)) {
  661.             Info(slide, 1, ((char *)slide, LoadFarString(ErrSetVolLabel)));
  662.             return 3;
  663.         }
  664.         return 2;   /* success:  skip the "extraction" quietly */
  665.     }
  666.  
  667.     return error;
  668.  
  669. } /* end function mapname() */
  670.  
  671.  
  672.  
  673.  
  674.  
  675. #ifdef MAYBE_PLAIN_FAT
  676.  
  677. /**********************/
  678. /* Function map2fat() */
  679. /**********************/
  680.  
  681. static void map2fat(pathcomp, last_dot)
  682.     char *pathcomp, *last_dot;
  683. {
  684.     char *pEnd = pathcomp + strlen(pathcomp);
  685.  
  686. /*---------------------------------------------------------------------------
  687.     Case 1:  filename has no dot, so figure out if we should add one.  Note
  688.     that the algorithm does not try to get too fancy:  if there are no dots
  689.     already, the name either gets truncated at 8 characters or the last un-
  690.     derscore is converted to a dot (only if more characters are saved that
  691.     way).  In no case is a dot inserted between existing characters.
  692.  
  693.               GRR:  have problem if filename is volume label??
  694.  
  695.   ---------------------------------------------------------------------------*/
  696.  
  697.     if (last_dot == (char *)NULL) {   /* no dots:  check for underscores... */
  698.         char *plu = strrchr(pathcomp, '_');   /* pointer to last underscore */
  699.  
  700.         if (plu == (char *)NULL) {  /* no dots, no underscores:  truncate at */
  701.             if (pEnd > pathcomp+8)  /* 8 chars (could insert '.' and keep 11) */
  702.                 *(pEnd = pathcomp+8) = '\0';
  703.         } else if (MIN(plu - pathcomp, 8) + MIN(pEnd - plu - 1, 3) > 8) {
  704.             last_dot = plu;       /* be lazy:  drop through to next if-block */
  705.         } else if ((pEnd - pathcomp) > 8)    /* more fits into just basename */
  706.             pathcomp[8] = '\0';    /* than if convert last underscore to dot */
  707.         /* else whole thing fits into 8 chars or less:  no change */
  708.     }
  709.  
  710. /*---------------------------------------------------------------------------
  711.     Case 2:  filename has dot in it, so truncate first half at 8 chars (shift
  712.     extension if necessary) and second half at three.
  713.   ---------------------------------------------------------------------------*/
  714.  
  715.     if (last_dot != (char *)NULL) {   /* one dot (or two, in the case of */
  716.         *last_dot = '.';              /*  "..") is OK:  put it back in */
  717.  
  718.         if ((last_dot - pathcomp) > 8) {
  719.             char *p, *q;
  720.             int i;
  721.  
  722.             p = last_dot;
  723.             q = last_dot = pathcomp + 8;
  724.             for (i = 0;  (i < 4) && *p;  ++i)  /* too many chars in basename: */
  725.                 *q++ = *p++;                   /*  shift extension left and */
  726.             *q = '\0';                         /*  truncate/terminate it */
  727.         } else if ((pEnd - last_dot) > 4)
  728.             last_dot[4] = '\0';                /* too many chars in extension */
  729.         /* else filename is fine as is:  no change */
  730.  
  731.         if ((last_dot - pathcomp) > 0 && last_dot[-1] == ' ')
  732.             last_dot[-1] = '_';                /* NO blank in front of '.'! */
  733.     }
  734. } /* end function map2fat() */
  735.  
  736. #endif /* MAYBE_PLAIN_FAT */
  737.  
  738.  
  739.  
  740.  
  741.  
  742. /***********************/
  743. /* Function checkdir() */
  744. /***********************/
  745.  
  746. int checkdir(__G__ pathcomp, flag)
  747.     __GDEF
  748.     char *pathcomp;
  749.     int flag;
  750. /*
  751.  * returns:  1 - (on APPEND_NAME) truncated filename
  752.  *           2 - path doesn't exist, not allowed to create
  753.  *           3 - path doesn't exist, tried to create and failed; or
  754.  *               path exists and is not a directory, but is supposed to be
  755.  *           4 - path is too long
  756.  *          10 - can't allocate memory for filename buffers
  757.  */
  758. {
  759.     static int rootlen = 0;   /* length of rootpath */
  760.     static char *rootpath;    /* user's "extract-to" directory */
  761.     static char *buildpath;   /* full path (so far) to extracted file */
  762.     static char *end;         /* pointer to end of buildpath ('\0') */
  763. #ifdef MSC
  764.     int attrs;                /* work around MSC stat() bug */
  765. #endif
  766.  
  767. #   define FN_MASK   7
  768. #   define FUNCTION  (flag & FN_MASK)
  769.  
  770.  
  771.  
  772. /*---------------------------------------------------------------------------
  773.     APPEND_DIR:  append the path component to the path being built and check
  774.     for its existence.  If doesn't exist and we are creating directories, do
  775.     so for this one; else signal success or error as appropriate.
  776.   ---------------------------------------------------------------------------*/
  777.  
  778.     if (FUNCTION == APPEND_DIR) {
  779.         int too_long = FALSE;
  780.  
  781.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  782.         while ((*end = *pathcomp++) != '\0')
  783.             ++end;
  784.  
  785.         /* GRR:  could do better check, see if overrunning buffer as we go:
  786.          * check end-buildpath after each append, set warning variable if
  787.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  788.          * appending.  Clear variable when begin new path. */
  789.  
  790.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  791.             too_long = TRUE;                /* check if extracting directory? */
  792. #ifdef MSC /* MSC 6.00 bug:  stat(non-existent-dir) == 0 [exists!] */
  793.         if (_dos_getfileattr(buildpath, &attrs) || stat(buildpath, &G.statbuf))
  794. #else
  795.         if (SSTAT(buildpath, &G.statbuf))    /* path doesn't exist */
  796. #endif
  797.         {
  798.             if (!G.create_dirs) { /* told not to create (freshening) */
  799.                 free(buildpath);
  800.                 return 2;         /* path doesn't exist:  nothing to do */
  801.             }
  802.             if (too_long) {
  803.                 Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong),
  804.                   FnFilter1(buildpath)));
  805.                 free(buildpath);
  806.                 return 4;         /* no room for filenames:  fatal */
  807.             }
  808.             if (MKDIR(buildpath, 0777) == -1) {   /* create the directory */
  809.                 Info(slide, 1, ((char *)slide, LoadFarString(CantCreateDir),
  810.                   FnFilter2(buildpath), FnFilter1(G.filename)));
  811.                 free(buildpath);
  812.                 return 3;      /* path didn't exist, tried to create, failed */
  813.             }
  814.             created_dir = TRUE;
  815.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  816.             Info(slide, 1, ((char *)slide, LoadFarString(DirIsntDirectory),
  817.               FnFilter2(buildpath), FnFilter1(G.filename)));
  818.             free(buildpath);
  819.             return 3;          /* path existed but wasn't dir */
  820.         }
  821.         if (too_long) {
  822.             Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong),
  823.               FnFilter1(buildpath)));
  824.             free(buildpath);
  825.             return 4;         /* no room for filenames:  fatal */
  826.         }
  827.         *end++ = '/';
  828.         *end = '\0';
  829.         Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  830.         return 0;
  831.  
  832.     } /* end if (FUNCTION == APPEND_DIR) */
  833.  
  834. /*---------------------------------------------------------------------------
  835.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  836.     buildpath.
  837.   ---------------------------------------------------------------------------*/
  838.  
  839.     if (FUNCTION == GETPATH) {
  840.         strcpy(pathcomp, buildpath);
  841.         Trace((stderr, "getting and freeing path [%s]\n",
  842.           FnFilter1(pathcomp)));
  843.         free(buildpath);
  844.         buildpath = end = (char *)NULL;
  845.         return 0;
  846.     }
  847.  
  848. /*---------------------------------------------------------------------------
  849.     APPEND_NAME:  assume the path component is the filename; append it and
  850.     return without checking for existence.
  851.   ---------------------------------------------------------------------------*/
  852.  
  853.     if (FUNCTION == APPEND_NAME) {
  854. #ifdef NOVELL_BUG_WORKAROUND
  855.         if (end == buildpath && !G.pInfo->vollabel) {
  856.             /* work-around for Novell's "overwriting executables" bug:
  857.                prepend "./" to name when no path component is specified */
  858.             *end++ = '.';
  859.             *end++ = '/';
  860.         }
  861. #endif /* NOVELL_BUG_WORKAROUND */
  862.         Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  863.         while ((*end = *pathcomp++) != '\0') {
  864.             ++end;
  865.             if ((end-buildpath) >= FILNAMSIZ) {
  866.                 *--end = '\0';
  867.                 Info(slide, 1, ((char *)slide, LoadFarString(PathTooLongTrunc),
  868.                   FnFilter1(G.filename), FnFilter2(buildpath)));
  869.                 return 1;   /* filename truncated */
  870.             }
  871.         }
  872.         Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  873.         return 0;  /* could check for existence here, prompt for new name... */
  874.     }
  875.  
  876. /*---------------------------------------------------------------------------
  877.     INIT:  allocate and initialize buffer space for the file currently being
  878.     extracted.  If file was renamed with an absolute path, don't prepend the
  879.     extract-to path.
  880.   ---------------------------------------------------------------------------*/
  881.  
  882.     if (FUNCTION == INIT) {
  883.         Trace((stderr, "initializing buildpath to "));
  884.         /* allocate space for full filename, root path, and maybe "./" */
  885.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+3)) ==
  886.             (char *)NULL)
  887.             return 10;
  888.         if (G.pInfo->vollabel) {
  889. /* GRR:  for network drives, do strchr() and return IZ_VOL_LABEL if not [1] */
  890.             if (renamed_fullpath && pathcomp[1] == ':')
  891.                 *buildpath = (char)ToLower(*pathcomp);
  892.             else if (!renamed_fullpath && rootlen > 1 && rootpath[1] == ':')
  893.                 *buildpath = (char)ToLower(*rootpath);
  894.             else {
  895.                 GETDRIVE(nLabelDrive);   /* assumed that a == 1, b == 2, etc. */
  896.                 *buildpath = (char)(nLabelDrive - 1 + 'a');
  897.             }
  898.             nLabelDrive = *buildpath - 'a' + 1;        /* save for mapname() */
  899.             if (uO.volflag == 0 || *buildpath < 'a' || /* no label/bogus disk */
  900.                (uO.volflag == 1 && !isfloppy(nLabelDrive))) /* -$:  no fixed */
  901.             {
  902.                 free(buildpath);
  903.                 return IZ_VOL_LABEL;     /* skipping with message */
  904.             }
  905.             *buildpath = '\0';
  906.             end = buildpath;
  907.         } else if (renamed_fullpath) {   /* pathcomp = valid data */
  908.             end = buildpath;
  909.             while ((*end = *pathcomp++) != '\0')
  910.                 ++end;
  911.         } else if (rootlen > 0) {
  912.             strcpy(buildpath, rootpath);
  913.             end = buildpath + rootlen;
  914.         } else {
  915.             *buildpath = '\0';
  916.             end = buildpath;
  917.         }
  918.         Trace((stderr, "[%s]\n", FnFilter1(buildpath)));
  919.         return 0;
  920.     }
  921.  
  922. /*---------------------------------------------------------------------------
  923.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  924.     sary; else assume it's a zipfile member and return.  This path segment
  925.     gets used in extracting all members from every zipfile specified on the
  926.     command line.  Note that under OS/2 and MS-DOS, if a candidate extract-to
  927.     directory specification includes a drive letter (leading "x:"), it is
  928.     treated just as if it had a trailing '/'--that is, one directory level
  929.     will be created if the path doesn't exist, unless this is otherwise pro-
  930.     hibited (e.g., freshening).
  931.   ---------------------------------------------------------------------------*/
  932.  
  933. #if (!defined(SFX) || defined(SFX_EXDIR))
  934.     if (FUNCTION == ROOT) {
  935.         Trace((stderr, "initializing root path to [%s]\n",
  936.           FnFilter1(pathcomp)));
  937.         if (pathcomp == (char *)NULL) {
  938.             rootlen = 0;
  939.             return 0;
  940.         }
  941.         if ((rootlen = strlen(pathcomp)) > 0) {
  942.             int had_trailing_pathsep=FALSE, has_drive=FALSE, xtra=2;
  943.  
  944.             if (isalpha(pathcomp[0]) && pathcomp[1] == ':')
  945.                 has_drive = TRUE;   /* drive designator */
  946.             if (pathcomp[rootlen-1] == '/' || pathcomp[rootlen-1] == '\\') {
  947.                 pathcomp[--rootlen] = '\0';
  948.                 had_trailing_pathsep = TRUE;
  949.             }
  950.             if (has_drive && (rootlen == 2)) {
  951.                 if (!had_trailing_pathsep)   /* i.e., original wasn't "x:/" */
  952.                     xtra = 3;      /* room for '.' + '/' + 0 at end of "x:" */
  953.             } else if (rootlen > 0) {     /* need not check "x:." and "x:/" */
  954. #ifdef MSC
  955.                 /* MSC 6.00 bug:  stat(non-existent-dir) == 0 [exists!] */
  956.                 if (_dos_getfileattr(pathcomp, &attrs) ||
  957.                     SSTAT(pathcomp,&G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  958. #else
  959.                 if (SSTAT(pathcomp,&G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  960. #endif
  961.                 {
  962.                     /* path does not exist */
  963.                     if (!G.create_dirs /* || iswild(pathcomp) */ ) {
  964.                         rootlen = 0;
  965.                         return 2;   /* treat as stored file */
  966.                     }
  967. /* GRR:  scan for wildcard characters?  OS-dependent...  if find any, return 2:
  968.  * treat as stored file(s) */
  969.                     /* create directory (could add loop here to scan pathcomp
  970.                      * and create more than one level, but really necessary?) */
  971.                     if (MKDIR(pathcomp, 0777) == -1) {
  972.                         Info(slide, 1, ((char *)slide,
  973.                           LoadFarString(CantCreateExtractDir),
  974.                           FnFilter1(pathcomp)));
  975.                         rootlen = 0;   /* path didn't exist, tried to create, */
  976.                         return 3;  /* failed:  file exists, or need 2+ levels */
  977.                     }
  978.                 }
  979.             }
  980.             if ((rootpath = (char *)malloc(rootlen+xtra)) == (char *)NULL) {
  981.                 rootlen = 0;
  982.                 return 10;
  983.             }
  984.             strcpy(rootpath, pathcomp);
  985.             if (xtra == 3)                  /* had just "x:", make "x:." */
  986.                 rootpath[rootlen++] = '.';
  987.             rootpath[rootlen++] = '/';
  988.             rootpath[rootlen] = '\0';
  989.             Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath)));
  990.         }
  991.         return 0;
  992.     }
  993. #endif /* !SFX || SFX_EXDIR */
  994.  
  995. /*---------------------------------------------------------------------------
  996.     END:  free rootpath, immediately prior to program exit.
  997.   ---------------------------------------------------------------------------*/
  998.  
  999.     if (FUNCTION == END) {
  1000.         Trace((stderr, "freeing rootpath\n"));
  1001.         if (rootlen > 0) {
  1002.             free(rootpath);
  1003.             rootlen = 0;
  1004.         }
  1005.         return 0;
  1006.     }
  1007.  
  1008.     return 99;  /* should never reach */
  1009.  
  1010. } /* end function checkdir() */
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017. /***********************/
  1018. /* Function isfloppy() */
  1019. /***********************/
  1020.  
  1021. static int isfloppy(nDrive)  /* more precisely, is it removable? */
  1022.     int nDrive;
  1023. {
  1024.     union REGS regs;
  1025.  
  1026.     regs.h.ah = 0x44;
  1027.     regs.h.al = 0x08;
  1028.     regs.h.bl = (uch)nDrive;
  1029. #ifdef __EMX__
  1030.     _int86(0x21, ®s, ®s);
  1031.     if (WREGS(regs,flags) & 1)
  1032. #else
  1033.     intdos(®s, ®s);
  1034.     if (WREGS(regs,cflag))        /* error:  do default a/b check instead */
  1035. #endif
  1036.     {
  1037.         Trace((stderr,
  1038.           "error in DOS function 0x44 (AX = 0x%04x):  guessing instead...\n",
  1039.           WREGS(regs,ax)));
  1040.         return (nDrive == 1 || nDrive == 2)? TRUE : FALSE;
  1041.     } else
  1042.         return WREGS(regs,ax)? FALSE : TRUE;
  1043. }
  1044.  
  1045.  
  1046.  
  1047.  
  1048. /**************************/
  1049. /* Function z_dos_chmod() */
  1050. /**************************/
  1051.  
  1052. static int z_dos_chmod(__G__ fname, attributes)
  1053.     __GDEF
  1054.     ZCONST char *fname;
  1055.     int attributes;
  1056. {
  1057.     char *name;
  1058.     unsigned fnamelength;
  1059.     int errv;
  1060.  
  1061.     /* set file attributes:
  1062.        The DOS `chmod' system call requires to mask out the
  1063.        directory and volume_label attribute bits.
  1064.        And, a trailing '/' has to be removed from the directory name,
  1065.        the DOS `chmod' system call does not accept it. */
  1066.     fnamelength = strlen(fname);
  1067.     if (fnamelength > 1 && fname[fnamelength-1] == '/' &&
  1068.         fname[fnamelength-2] != ':' &&
  1069.         (name = (char *)malloc(fnamelength)) != (char *)NULL) {
  1070.         strncpy(name, fname, fnamelength-1);
  1071.         name[fnamelength-1] = '\0';
  1072.     } else {
  1073.         name = (char *)fname;
  1074.         fnamelength = 0;
  1075.     }
  1076.  
  1077. #if defined(__TURBOC__) || (defined(__DJGPP__) && (__DJGPP__ >= 2))
  1078. #   if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x0452))
  1079. #     define Chmod  _rtl_chmod
  1080. #   else
  1081. #     define Chmod  _chmod
  1082. #   endif
  1083.     errv = (Chmod(name, 1, attributes & (~FSUBDIR & ~FVOLID)) !=
  1084.             (attributes & (~FSUBDIR & ~FVOLID)));
  1085. #   undef Chmod
  1086. #else /* !(__TURBOC__ || (__DJGPP__ && __DJGPP__ >= 2)) */
  1087.     errv = (_dos_setfileattr(name, attributes & (~FSUBDIR & ~FVOLID)) != 0);
  1088. #endif /* ?(__TURBOC__ || (__DJGPP__ && __DJGPP__ >= 2)) */
  1089.     if (errv)
  1090.         Info(slide, 1, ((char *)slide, LoadFarString(AttribsMayBeWrong)));
  1091.  
  1092.     if (fnamelength > 0)
  1093.         free(name);
  1094.     return errv;
  1095. } /* end function z_dos_chmod() */
  1096.  
  1097.  
  1098.  
  1099.  
  1100. #if (!defined(__GO32__) && !defined(__EMX__))
  1101.  
  1102. typedef struct dosfcb {
  1103.     uch  flag;        /* ff to indicate extended FCB */
  1104.     char res[5];      /* reserved */
  1105.     uch  vattr;       /* attribute */
  1106.     uch  drive;       /* drive (1=A, 2=B, ...) */
  1107.     uch  vn[11];      /* file or volume name */
  1108.     char dmmy[5];
  1109.     uch  nn[11];      /* holds new name if renaming (else reserved) */
  1110.     char dmmy2[9];
  1111. } dos_fcb;
  1112.  
  1113. /**************************/
  1114. /* Function volumelabel() */
  1115. /**************************/
  1116.  
  1117. static int volumelabel(newlabel)
  1118.     char *newlabel;
  1119. {
  1120. #ifdef DEBUG
  1121.     char *p;
  1122. #endif
  1123.     int len = strlen(newlabel);
  1124.     int fcbseg, dtaseg, fcboff, dtaoff, retv;
  1125.     dos_fcb  fcb, dta, far *pfcb=&fcb, far *pdta=&dta;
  1126.     struct SREGS sregs;
  1127.     union REGS regs;
  1128.  
  1129.  
  1130. /*---------------------------------------------------------------------------
  1131.     Label the diskette specified by nLabelDrive using FCB calls.  (Old ver-
  1132.     sions of MS-DOS and OS/2 DOS boxes can't use DOS function 3Ch to create
  1133.     labels.)  Must use far pointers for MSC FP_* macros to work; must pad
  1134.     FCB filenames with spaces; and cannot include dot in 8th position.  May
  1135.     or may not need to zero out FCBs before using; do so just in case.
  1136.   ---------------------------------------------------------------------------*/
  1137.  
  1138. #ifdef WATCOMC_386
  1139.     int truseg;
  1140.  
  1141.     memset(&sregs, 0, sizeof(sregs));
  1142.     memset(®s, 0, sizeof(regs));
  1143.     /* PMODE/W does not support extended versions of any dos FCB functions, */
  1144.     /* so we have to use brute force, allocating real mode memory for them. */
  1145.     regs.w.ax = 0x0100;
  1146.     regs.w.bx = (2 * sizeof(dos_fcb) + 15) >> 4;   /* size in paragraphs */
  1147.     int386(0x31, ®s, ®s);            /* DPMI allocate DOS memory */
  1148.     if (regs.w.cflag)
  1149.         return DF_MDY;                     /* no memory, return default */
  1150.     truseg = regs.w.dx;                    /* protected mode selector */
  1151.     dtaseg = regs.w.ax;                    /* real mode paragraph */
  1152.     fcboff = 0;
  1153.     dtaoff = sizeof(dos_fcb);
  1154. #ifdef XXX__MK_FP_IS_BROKEN
  1155.     /* XXX  This code may not be trustworthy in general, though it is   */
  1156.     /* valid with DOS/4GW and PMODE/w, which is all we support for now. */
  1157.     regs.w.ax = 6;
  1158.     regs.w.bx = truseg;
  1159.     int386(0x31, ®s, ®s);            /* convert seg to linear address */
  1160.     pfcb = (dos_fcb far *) (((ulg) regs.w.cx << 16) | regs.w.dx);
  1161.     /* pfcb = (dos_fcb far *) ((ulg) dtaseg << 4); */
  1162.     pdta = pfcb + 1;
  1163. #else
  1164.     pfcb = MK_FP(truseg, fcboff);
  1165.     pdta = MK_FP(truseg, dtaoff);
  1166. #endif
  1167.     _fmemset((char far *)pfcb, 0, 2 * sizeof(dos_fcb));
  1168.     /* we pass the REAL MODE paragraph to the dos interrupts: */
  1169.     fcbseg = dtaseg;
  1170.  
  1171. #else /* !WATCOMC_386 */
  1172.  
  1173.     memset((char *)&dta, 0, sizeof(dos_fcb));
  1174.     memset((char *)&fcb, 0, sizeof(dos_fcb));
  1175.     fcbseg = FP_SEG(pfcb);
  1176.     fcboff = FP_OFF(pfcb);
  1177.     dtaseg = FP_SEG(pdta);
  1178.     dtaoff = FP_OFF(pdta);
  1179. #endif /* ?WATCOMC_386 */
  1180.  
  1181. #ifdef DEBUG
  1182.     for (p = (char *)&dta; (p - (char *)&dta) < sizeof(dos_fcb); ++p)
  1183.         if (*p)
  1184.             fprintf(stderr, "error:  dta[%d] = %x\n", (p - (char *)&dta), *p);
  1185.     for (p = (char *)&fcb; (p - (char *)&fcb) < sizeof(dos_fcb); ++p)
  1186.         if (*p)
  1187.             fprintf(stderr, "error:  fcb[%d] = %x\n", (p - (char *)&fcb), *p);
  1188.     printf("testing pointer macros:\n");
  1189.     segread(&sregs);
  1190.     printf("cs = %x, ds = %x, es = %x, ss = %x\n", sregs.cs, sregs.ds, sregs.es,
  1191.       sregs.ss);
  1192. #endif /* DEBUG */
  1193.  
  1194. #if 0
  1195. #ifdef __TURBOC__
  1196.     bdosptr(0x1a, dta, DO_NOT_CARE);
  1197. #else
  1198.     (intdosx method below)
  1199. #endif
  1200. #endif /* 0 */
  1201.  
  1202.     /* set the disk transfer address for subsequent FCB calls */
  1203.     sregs.ds = dtaseg;
  1204.     WREGS(regs,dx) = dtaoff;
  1205.     Trace((stderr, "segment:offset of pdta = %x:%x\n", dtaseg, dtaoff));
  1206.     Trace((stderr, "&dta = %lx, pdta = %lx\n", (ulg)&dta, (ulg)pdta));
  1207.     regs.h.ah = 0x1a;
  1208.     F_intdosx(®s, ®s, &sregs);
  1209.  
  1210.     /* fill in the FCB */
  1211.     sregs.ds = fcbseg;
  1212.     WREGS(regs,dx) = fcboff;
  1213.     pfcb->flag = 0xff;          /* extended FCB */
  1214.     pfcb->vattr = 0x08;         /* attribute:  disk volume label */
  1215.     pfcb->drive = (uch)nLabelDrive;
  1216.  
  1217. #ifdef DEBUG
  1218.     Trace((stderr, "segment:offset of pfcb = %x:%x\n", sregs.ds, WREGS(regs,dx)));
  1219.     Trace((stderr, "&fcb = %lx, pfcb = %lx\n", (ulg)&fcb, (ulg)pfcb));
  1220.     Trace((stderr, "(2nd check:  labelling drive %c:)\n", pfcb->drive-1+'A'));
  1221.     if (pfcb->flag != fcb.flag)
  1222.         fprintf(stderr, "error:  pfcb->flag = %d, fcb.flag = %d\n",
  1223.           pfcb->flag, fcb.flag);
  1224.     if (pfcb->drive != fcb.drive)
  1225.         fprintf(stderr, "error:  pfcb->drive = %d, fcb.drive = %d\n",
  1226.           pfcb->drive, fcb.drive);
  1227.     if (pfcb->vattr != fcb.vattr)
  1228.         fprintf(stderr, "error:  pfcb->vattr = %d, fcb.vattr = %d\n",
  1229.           pfcb->vattr, fcb.vattr);
  1230. #endif /* DEBUG */
  1231.  
  1232.     /* check for existing label */
  1233.     Trace((stderr, "searching for existing label via FCBs\n"));
  1234.     regs.h.ah = 0x11;      /* FCB find first */
  1235. #ifdef WATCOMC_386
  1236.     _fstrncpy((char far *)&pfcb->vn, "???????????", 11);
  1237. #else
  1238.     strncpy((char *)fcb.vn, "???????????", 11);   /* i.e., "*.*" */
  1239. #endif /* ?WATCOMC_386 */
  1240.     Trace((stderr, "fcb.vn = %lx\n", (ulg)fcb.vn));
  1241.     Trace((stderr, "regs.h.ah = %x, regs.x.dx = %04x, sregs.ds = %04x\n",
  1242.       regs.h.ah, WREGS(regs,dx), sregs.ds));
  1243.     Trace((stderr, "flag = %x, drive = %d, vattr = %x, vn = %s = %s.\n",
  1244.       fcb.flag, fcb.drive, fcb.vattr, fcb.vn, pfcb->vn));
  1245.     F_intdosx(®s, ®s, &sregs);
  1246.  
  1247. /*---------------------------------------------------------------------------
  1248.     If not previously labelled, write a new label.  Otherwise just rename,
  1249.     since MS-DOS 2.x has a bug that damages the FAT when the old label is
  1250.     deleted.
  1251.   ---------------------------------------------------------------------------*/
  1252.  
  1253.     if (regs.h.al) {
  1254.         Trace((stderr, "no label found\n\n"));
  1255.         regs.h.ah = 0x16;                 /* FCB create file */
  1256. #ifdef WATCOMC_386
  1257.         _fstrncpy((char far *)pfcb->vn, newlabel, len);
  1258.         if (len < 11)
  1259.             _fstrncpy((char far *)(pfcb->vn+len), "           ", 11-len);
  1260. #else
  1261.         strncpy((char *)fcb.vn, newlabel, len);
  1262.         if (len < 11)   /* fill with spaces */
  1263.             strncpy((char *)(fcb.vn+len), "           ", 11-len);
  1264. #endif
  1265.         Trace((stderr, "fcb.vn = %lx  pfcb->vn = %lx\n", (ulg)fcb.vn,
  1266.           (ulg)pfcb->vn));
  1267.         Trace((stderr, "flag = %x, drive = %d, vattr = %x\n", fcb.flag,
  1268.           fcb.drive, fcb.vattr));
  1269.         Trace((stderr, "vn = %s = %s.\n", fcb.vn, pfcb->vn));
  1270.         F_intdosx(®s, ®s, &sregs);
  1271.         regs.h.ah = 0x10;                 /* FCB close file */
  1272.         if (regs.h.al) {
  1273.             Trace((stderr, "unable to write volume name (AL = %x)\n",
  1274.               regs.h.al));
  1275.             F_intdosx(®s, ®s, &sregs);
  1276.             retv = 1;
  1277.         } else {
  1278.             F_intdosx(®s, ®s, &sregs);
  1279.             Trace((stderr, "new volume label [%s] written\n", newlabel));
  1280.             retv = 0;
  1281.         }
  1282.     } else {
  1283.         Trace((stderr, "found old label [%s]\n\n", dta.vn));  /* not term. */
  1284.         regs.h.ah = 0x17;                 /* FCB rename */
  1285. #ifdef WATCOMC_386
  1286.         _fstrncpy((char far *)pfcb->vn, (char far *)pdta->vn, 11);
  1287.         _fstrncpy((char far *)pfcb->nn, newlabel, len);
  1288.         if (len < 11)
  1289.             _fstrncpy((char far *)(pfcb->nn+len), "           ", 11-len);
  1290. #else
  1291.         strncpy((char *)fcb.vn, (char *)dta.vn, 11);
  1292.         strncpy((char *)fcb.nn, newlabel, len);
  1293.         if (len < 11)                     /* fill with spaces */
  1294.             strncpy((char *)(fcb.nn+len), "           ", 11-len);
  1295. #endif
  1296.         Trace((stderr, "fcb.vn = %lx  pfcb->vn = %lx\n", (ulg)fcb.vn,
  1297.           (ulg)pfcb->vn));
  1298.         Trace((stderr, "fcb.nn = %lx  pfcb->nn = %lx\n", (ulg)fcb.nn,
  1299.           (ulg)pfcb->nn));
  1300.         Trace((stderr, "flag = %x, drive = %d, vattr = %x\n", fcb.flag,
  1301.           fcb.drive, fcb.vattr));
  1302.         Trace((stderr, "vn = %s = %s.\n", fcb.vn, pfcb->vn));
  1303.         Trace((stderr, "nn = %s = %s.\n", fcb.nn, pfcb->nn));
  1304.         F_intdosx(®s, ®s, &sregs);
  1305.         if (regs.h.al) {
  1306.             Trace((stderr, "Unable to change volume name (AL = %x)\n",
  1307.               regs.h.al));
  1308.             retv = 1;
  1309.         } else {
  1310.             Trace((stderr, "volume label changed to [%s]\n", newlabel));
  1311.             retv = 0;
  1312.         }
  1313.     }
  1314. #ifdef WATCOMC_386
  1315.     regs.w.ax = 0x0101;                    /* free dos memory */
  1316.     regs.w.dx = truseg;
  1317.     int386(0x31, ®s, ®s);
  1318. #endif
  1319.     return retv;
  1320.  
  1321. } /* end function volumelabel() */
  1322.  
  1323. #endif /* !__GO32__ && !__EMX__ */
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329. #if defined(USE_EF_UT_TIME) || defined(TIMESTAMP)
  1330. /* The following DOS date/time structure is machine-dependent as it
  1331.  * assumes "little-endian" byte order.  For MSDOS-specific code, which
  1332.  * is run on x86 CPUs (or emulators), this assumption is valid; but
  1333.  * care should be taken when using this code as template for other ports.
  1334.  */
  1335. typedef union {
  1336.     ulg z_dostime;
  1337. # ifdef __TURBOC__
  1338.     struct ftime ft;            /* system file time record */
  1339. # endif
  1340.     struct {                    /* date and time words */
  1341.         ush ztime;              /* DOS file modification time word */
  1342.         ush zdate;              /* DOS file modification date word */
  1343.     } zft;
  1344.     struct {                    /* DOS date/time components bitfield */
  1345.         unsigned zt_se : 5;
  1346.         unsigned zt_mi : 6;
  1347.         unsigned zt_hr : 5;
  1348.         unsigned zd_dy : 5;
  1349.         unsigned zd_mo : 4;
  1350.         unsigned zd_yr : 7;
  1351.     } z_dtf;
  1352. } dos_fdatetime;
  1353. #endif /* USE_EF_UT_TIME || TIMESTAMP */
  1354.  
  1355.  
  1356. /****************************/
  1357. /* Function close_outfile() */
  1358. /****************************/
  1359.  
  1360. void close_outfile(__G)
  1361.     __GDEF
  1362.  /*
  1363.   * MS-DOS VERSION
  1364.   *
  1365.   * Set the output file date/time stamp according to information from the
  1366.   * zipfile directory record for this member, then close the file and set
  1367.   * its permissions (archive, hidden, read-only, system).  Aside from closing
  1368.   * the file, this routine is optional (but most compilers support it).
  1369.   */
  1370. {
  1371. #ifdef USE_EF_UT_TIME
  1372.     dos_fdatetime dos_dt;
  1373.     iztimes z_utime;
  1374.     struct tm *t;
  1375. #endif /* USE_EF_UT_TIME */
  1376.  
  1377.  
  1378. /*---------------------------------------------------------------------------
  1379.     Copy and/or convert time and date variables, if necessary; then set the
  1380.     file time/date.  WEIRD BORLAND "BUG":  if output is buffered, and if run
  1381.     under at least some versions of DOS (e.g., 6.0), and if files are smaller
  1382.     than DOS physical block size (i.e., 512 bytes) (?), then files MAY NOT
  1383.     get timestamped correctly--apparently setftime() occurs before any data
  1384.     are written to the file, and when file is closed and buffers are flushed,
  1385.     timestamp is overwritten with current time.  Even with a 32K buffer, this
  1386.     does not seem to occur with larger files.  UnZip output is now unbuffered,
  1387.     but if it were not, could still avoid problem by adding "fflush(outfile)"
  1388.     just before setftime() call.  Weird, huh?
  1389.   ---------------------------------------------------------------------------*/
  1390.  
  1391. #ifdef USE_EF_UT_TIME
  1392.     if (G.extra_field &&
  1393. #ifdef IZ_CHECK_TZ
  1394.         G.tz_is_valid &&
  1395. #endif
  1396.         (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  1397.                           G.lrec.last_mod_dos_datetime, &z_utime, NULL)
  1398.          & EB_UT_FL_MTIME))
  1399.     {
  1400.         TTrace((stderr, "close_outfile:  Unix e.f. modif. time = %ld\n",
  1401.           z_utime.mtime));
  1402.         /* round up (down if "up" overflows) to even seconds */
  1403.         if (z_utime.mtime & 1)
  1404.             z_utime.mtime = (z_utime.mtime + 1 > z_utime.mtime) ?
  1405.                              z_utime.mtime + 1 : z_utime.mtime - 1;
  1406.         TIMET_TO_NATIVE(z_utime.mtime)   /* NOP unless MSC 7.0 or Macintosh */
  1407.         t = localtime(&(z_utime.mtime));
  1408.     } else
  1409.         t = (struct tm *)NULL;
  1410.     if (t != (struct tm *)NULL) {
  1411.         if (t->tm_year < 80) {
  1412.             dos_dt.z_dtf.zt_se = 0;
  1413.             dos_dt.z_dtf.zt_mi = 0;
  1414.             dos_dt.z_dtf.zt_hr = 0;
  1415.             dos_dt.z_dtf.zd_dy = 1;
  1416.             dos_dt.z_dtf.zd_mo = 1;
  1417.             dos_dt.z_dtf.zd_yr = 0;
  1418.         } else {
  1419.             dos_dt.z_dtf.zt_se = t->tm_sec >> 1;
  1420.             dos_dt.z_dtf.zt_mi = t->tm_min;
  1421.             dos_dt.z_dtf.zt_hr = t->tm_hour;
  1422.             dos_dt.z_dtf.zd_dy = t->tm_mday;
  1423.             dos_dt.z_dtf.zd_mo = t->tm_mon + 1;
  1424.             dos_dt.z_dtf.zd_yr = t->tm_year - 80;
  1425.         }
  1426.     } else {
  1427.         dos_dt.z_dostime = G.lrec.last_mod_dos_datetime;
  1428.     }
  1429. # ifdef __TURBOC__
  1430.     setftime(fileno(G.outfile), &dos_dt.ft);
  1431. # else
  1432.     _dos_setftime(fileno(G.outfile), dos_dt.zft.zdate, dos_dt.zft.ztime);
  1433. # endif
  1434. #else /* !USE_EF_UT_TIME */
  1435. # ifdef __TURBOC__
  1436.     setftime(fileno(G.outfile),
  1437.              (struct ftime *)(&(G.lrec.last_mod_dos_datetime)));
  1438. # else
  1439.     _dos_setftime(fileno(G.outfile), (ush)(G.lrec.last_mod_dos_datetime >> 16),
  1440.                                      (ush)(G.lrec.last_mod_dos_datetime));
  1441. # endif
  1442. #endif /* ?USE_EF_UT_TIME */
  1443.  
  1444. /*---------------------------------------------------------------------------
  1445.     And finally we can close the file...at least everybody agrees on how to
  1446.     do *this*.  I think...  Also change the mode according to the stored file
  1447.     attributes, since we didn't do that when we opened the dude.
  1448.   ---------------------------------------------------------------------------*/
  1449.  
  1450.     fclose(G.outfile);
  1451.  
  1452.     z_dos_chmod(__G__ G.filename, G.pInfo->file_attr);
  1453.  
  1454. } /* end function close_outfile() */
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460. #ifdef TIMESTAMP
  1461.  
  1462. /*************************/
  1463. /* Function stamp_file() */
  1464. /*************************/
  1465.  
  1466. int stamp_file(fname, modtime)
  1467.     ZCONST char *fname;
  1468.     time_t modtime;
  1469. {
  1470.     dos_fdatetime dos_dt;
  1471.     time_t t_even;
  1472.     struct tm *t;
  1473.     int fd;                             /* file handle */
  1474.  
  1475.     /* round up (down if "up" overflows) to even seconds */
  1476.     t_even = ((modtime + 1 > modtime) ? modtime + 1 : modtime) & (~1);
  1477.     TIMET_TO_NATIVE(t_even)             /* NOP unless MSC 7.0 or Macintosh */
  1478.     t = localtime(&t_even);
  1479.     if (t == (struct tm *)NULL)
  1480.         return -1;                      /* time conversion error */
  1481.     if (t->tm_year < 80) {
  1482.         dos_dt.z_dtf.zt_se = 0;
  1483.         dos_dt.z_dtf.zt_mi = 0;
  1484.         dos_dt.z_dtf.zt_hr = 0;
  1485.         dos_dt.z_dtf.zd_dy = 1;
  1486.         dos_dt.z_dtf.zd_mo = 1;
  1487.         dos_dt.z_dtf.zd_yr = 0;
  1488.     } else {
  1489.         dos_dt.z_dtf.zt_se = t->tm_sec >> 1;
  1490.         dos_dt.z_dtf.zt_mi = t->tm_min;
  1491.         dos_dt.z_dtf.zt_hr = t->tm_hour;
  1492.         dos_dt.z_dtf.zd_dy = t->tm_mday;
  1493.         dos_dt.z_dtf.zd_mo = t->tm_mon + 1;
  1494.         dos_dt.z_dtf.zd_yr = t->tm_year - 80;
  1495.     }
  1496.     if (((fd = open((char *)fname, 0)) == -1) ||
  1497. # ifdef __TURBOC__
  1498.         (setftime(fd, &dos_dt.ft)))
  1499. # else
  1500.         (_dos_setftime(fd, dos_dt.zft.zdate, dos_dt.zft.ztime)))
  1501. # endif
  1502.     {
  1503.         if (fd != -1)
  1504.             close(fd);
  1505.         return -1;
  1506.     }
  1507.     close(fd);
  1508.     return 0;
  1509.  
  1510. } /* end function stamp_file() */
  1511.  
  1512. #endif /* TIMESTAMP */
  1513.  
  1514.  
  1515.  
  1516.  
  1517. #ifndef SFX
  1518.  
  1519. /*************************/
  1520. /* Function dateformat() */
  1521. /*************************/
  1522.  
  1523. int dateformat()
  1524. {
  1525.  
  1526. /*---------------------------------------------------------------------------
  1527.     For those operating systems that support it, this function returns a
  1528.     value that tells how national convention says that numeric dates are
  1529.     displayed.  Return values are DF_YMD, DF_DMY and DF_MDY (the meanings
  1530.     should be fairly obvious).
  1531.   ---------------------------------------------------------------------------*/
  1532.  
  1533. #ifndef WINDLL
  1534.     ush CountryInfo[18];
  1535. #if (!defined(__GO32__) && !defined(__EMX__))
  1536.     ush far *_CountryInfo = CountryInfo;
  1537.     struct SREGS sregs;
  1538.     union REGS regs;
  1539. #ifdef WATCOMC_386
  1540.     ush seg, para;
  1541.  
  1542.     memset(&sregs, 0, sizeof(sregs));
  1543.     memset(®s, 0, sizeof(regs));
  1544.     /* PMODE/W does not support an extended version of dos function 38,   */
  1545.     /* so we have to use brute force, allocating real mode memory for it. */
  1546.     regs.w.ax = 0x0100;
  1547.     regs.w.bx = 3;                         /* 36 bytes rounds up to 48 */
  1548.     int386(0x31, ®s, ®s);            /* DPMI allocate DOS memory */
  1549.     if (regs.w.cflag)
  1550.         return DF_MDY;                     /* no memory, return default */
  1551.     seg = regs.w.dx;
  1552.     para = regs.w.ax;
  1553.  
  1554. #ifdef XXX__MK_FP_IS_BROKEN
  1555.     /* XXX  This code may not be trustworthy in general, though it is
  1556.      * valid with DOS/4GW and PMODE/w, which is all we support for now. */
  1557.  /* _CountryInfo = (ush far *) (para << 4); */ /* works for some extenders */
  1558.     regs.w.ax = 6;
  1559.     regs.w.bx = seg;
  1560.     int386(0x31, ®s, ®s);            /* convert seg to linear address */
  1561.     _CountryInfo = (ush far *) (((ulg) regs.w.cx << 16) | regs.w.dx);
  1562. #else
  1563.     _CountryInfo = (ush far *) MK_FP(seg, 0);
  1564. #endif
  1565.  
  1566.     sregs.ds = para;                       /* real mode paragraph */
  1567.     regs.w.dx = 0;                         /* no offset from segment */
  1568.     regs.w.ax = 0x3800;
  1569.     int86x_realmode(0x21, ®s, ®s, &sregs);
  1570.     CountryInfo[0] = regs.w.cflag ? 0 : _CountryInfo[0];
  1571.     regs.w.ax = 0x0101;
  1572.     regs.w.dx = seg;
  1573.     int386(0x31, ®s, ®s);              /* DPMI free DOS memory */
  1574.  
  1575. #else /* !WATCOMC_386 */
  1576.  
  1577.     sregs.ds  = FP_SEG(_CountryInfo);
  1578.     regs.x.dx = FP_OFF(_CountryInfo);
  1579.     regs.x.ax = 0x3800;
  1580.     intdosx(®s, ®s, &sregs);
  1581. #endif /* ?WATCOMC_386 */
  1582.  
  1583. #else /* __GO32__ || __EMX__ */
  1584.     _dos_getcountryinfo(CountryInfo);
  1585. #endif /* ?(__GO32__ || __EMX__) */
  1586.  
  1587.     switch(CountryInfo[0]) {
  1588.         case 0:
  1589.             return DF_MDY;
  1590.         case 1:
  1591.             return DF_DMY;
  1592.         case 2:
  1593.             return DF_YMD;
  1594.     }
  1595. #endif /* !WINDLL && !WATCOMC_386 */
  1596.  
  1597.     return DF_MDY;   /* default for systems without locale info */
  1598.  
  1599. } /* end function dateformat() */
  1600.  
  1601.  
  1602.  
  1603.  
  1604. #ifndef WINDLL
  1605.  
  1606. /************************/
  1607. /*  Function version()  */
  1608. /************************/
  1609.  
  1610. void version(__G)
  1611.     __GDEF
  1612. {
  1613.     int len;
  1614. #if defined(__DJGPP__) || defined(__WATCOMC__) || \
  1615.     (defined(_MSC_VER) && (_MSC_VER != 800))
  1616.     char buf[80];
  1617. #endif
  1618.  
  1619.     len = sprintf((char *)slide, LoadFarString(CompiledWith),
  1620.  
  1621. #if defined(__GNUC__)
  1622. #  if defined(__DJGPP__)
  1623.       (sprintf(buf, "djgpp v%d.%02d / gcc ", __DJGPP__, __DJGPP_MINOR__), buf),
  1624. #  elif defined(__GO32__)         /* __GO32__ is defined as "1" only (sigh) */
  1625.       "djgpp v1.x / gcc ",
  1626. #  elif defined(__EMX__)          /* ...so is __EMX__ (double sigh) */
  1627.       "emx+gcc ",
  1628. #  else
  1629.       "gcc ",
  1630. #  endif
  1631.       __VERSION__,
  1632. #elif defined(__WATCOMC__)
  1633. #  if (__WATCOMC__ % 10 != 0)
  1634.       "Watcom C/C++", (sprintf(buf, " %d.%02d", __WATCOMC__ / 100,
  1635.                                __WATCOMC__ % 100), buf),
  1636. #  else
  1637.       "Watcom C/C++", (sprintf(buf, " %d.%d", __WATCOMC__ / 100,
  1638.                                (__WATCOMC__ % 100) / 10), buf),
  1639. #  endif
  1640. #elif defined(__TURBOC__)
  1641. #  ifdef __BORLANDC__
  1642.       "Borland C++",
  1643. #    if (__BORLANDC__ < 0x0200)
  1644.         " 1.0",
  1645. #    elif (__BORLANDC__ == 0x0200)   /* James:  __TURBOC__ = 0x0297 */
  1646.         " 2.0",
  1647. #    elif (__BORLANDC__ == 0x0400)
  1648.         " 3.0",
  1649. #    elif (__BORLANDC__ == 0x0410)   /* __BCPLUSPLUS__ = 0x0310 */
  1650.         " 3.1",
  1651. #    elif (__BORLANDC__ == 0x0452)   /* __BCPLUSPLUS__ = 0x0320 */
  1652.         " 4.0 or 4.02",
  1653. #    elif (__BORLANDC__ == 0x0460)   /* __BCPLUSPLUS__ = 0x0340 */
  1654.         " 4.5",
  1655. #    elif (__BORLANDC__ == 0x0500)
  1656.         " 5.0",
  1657. #    else
  1658.         " later than 5.0",
  1659. #    endif
  1660. #  else
  1661.       "Turbo C",
  1662. #    if (__TURBOC__ > 0x0401)        /* Kevin:  3.0 -> 0x0401 */
  1663.         "++ later than 3.0",
  1664. #    elif (__TURBOC__ >= 0x0400)
  1665.         "++ 3.0",
  1666. #    elif (__TURBOC__ == 0x0295)     /* [661] vfy'd by Kevin */
  1667.         "++ 1.0",
  1668. #    elif ((__TURBOC__ >= 0x018d) && (__TURBOC__ <= 0x0200)) /* James: 0x0200 */
  1669.         " 2.0",
  1670. #    elif (__TURBOC__ > 0x0100)
  1671.         " 1.5",                      /* James:  0x0105? */
  1672. #    else
  1673.         " 1.0",                      /* James:  0x0100 */
  1674. #    endif
  1675. #  endif
  1676. #elif defined(MSC)
  1677. #  if defined(_QC) && !defined(_MSC_VER)
  1678.       "MS Quick C ", "2.0 or earlier",      /* _QC is defined as 1 */
  1679. #  elif defined(_QC) && (_MSC_VER == 600)
  1680.       "MS Quick C ", "2.5 (MSC 6.00)",
  1681. #  else
  1682.       "Microsoft C ",
  1683. #    ifdef _MSC_VER
  1684. #      if (_MSC_VER == 800)
  1685.         "8.0/8.0c (Visual C++ 1.0/1.5)",
  1686. #      else
  1687.         (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf),
  1688. #      endif
  1689. #    else
  1690.       "5.1 or earlier",
  1691. #    endif
  1692. #  endif
  1693. #else
  1694.       "unknown compiler", "",
  1695. #endif /* ?compilers */
  1696.  
  1697.       "MS-DOS",
  1698.  
  1699. #if (defined(__GNUC__) || defined(WATCOMC_386))
  1700.       " (32-bit)",
  1701. #else
  1702. #  if defined(M_I86HM) || defined(__HUGE__)
  1703.       " (16-bit, huge)",
  1704. #  elif defined(M_I86LM) || defined(__LARGE__)
  1705.       " (16-bit, large)",
  1706. #  elif defined(M_I86MM) || defined(__MEDIUM__)
  1707.       " (16-bit, medium)",
  1708. #  elif defined(M_I86CM) || defined(__COMPACT__)
  1709.       " (16-bit, compact)",
  1710. #  elif defined(M_I86SM) || defined(__SMALL__)
  1711.       " (16-bit, small)",
  1712. #  elif defined(M_I86TM) || defined(__TINY__)
  1713.       " (16-bit, tiny)",
  1714. #  else
  1715.       " (16-bit)",
  1716. #  endif
  1717. #endif
  1718.  
  1719. #ifdef __DATE__
  1720.       " on ", __DATE__
  1721. #else
  1722.       "", ""
  1723. #endif
  1724.     );
  1725.  
  1726.     (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  1727.                                 /* MSC can't handle huge macro expansion */
  1728.  
  1729.     /* temporary debugging code for Borland compilers only */
  1730. #if (defined(__TURBOC__) && defined(DEBUG))
  1731.     Info(slide, 0, ((char *)slide, "\tdebug(__TURBOC__ = 0x%04x = %d)\n",
  1732.       __TURBOC__, __TURBOC__));
  1733. #ifdef __BORLANDC__
  1734.     Info(slide, 0, ((char *)slide, "\tdebug(__BORLANDC__ = 0x%04x)\n",
  1735.       __BORLANDC__));
  1736. #else
  1737.     Info(slide, 0, ((char *)slide, "\tdebug(__BORLANDC__ not defined)\n"));
  1738. #endif
  1739. #ifdef __TCPLUSPLUS__
  1740.     Info(slide, 0, ((char *)slide, "\tdebug(__TCPLUSPLUS__ = 0x%04x)\n",
  1741.       __TCPLUSPLUS__));
  1742. #else
  1743.     Info(slide, 0, ((char *)slide, "\tdebug(__TCPLUSPLUS__ not defined)\n"));
  1744. #endif
  1745. #ifdef __BCPLUSPLUS__
  1746.     Info(slide, 0, ((char *)slide, "\tdebug(__BCPLUSPLUS__ = 0x%04x)\n\n",
  1747.       __BCPLUSPLUS__));
  1748. #else
  1749.     Info(slide, 0, ((char *)slide, "\tdebug(__BCPLUSPLUS__ not defined)\n\n"));
  1750. #endif
  1751. #endif /* __TURBOC__ && DEBUG */
  1752.  
  1753. } /* end function version() */
  1754.  
  1755. #endif /* !WINDLL */
  1756. #endif /* !SFX */
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762. #if (defined(__GO32__) || defined(__EMX__))
  1763.  
  1764. #if (!defined(__DJGPP__) || (__DJGPP__ < 2) || \
  1765.      ((__DJGPP__ == 2) && (__DJGPP_MINOR__ < 2)))
  1766. int volatile _doserrno;
  1767. #endif /* not "djgpp v2.02 or newer" */
  1768.  
  1769. #if (!defined(__DJGPP__) || (__DJGPP__ < 2))
  1770.  
  1771. unsigned _dos_getcountryinfo(void *countrybuffer)
  1772. {
  1773.     asm("movl %0, %%edx": : "g" (countrybuffer));
  1774.     asm("movl $0x3800, %eax");
  1775.     asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1776.     _doserrno = 0;
  1777.     asm("jnc 1f");
  1778.     asm("movl %%eax, %0": "=m" (_doserrno));
  1779.     asm("1:");
  1780.     return (unsigned)_doserrno;
  1781. }
  1782.  
  1783. unsigned _dos_setftime(int fd, ush dosdate, ush dostime)
  1784. {
  1785.     asm("movl %0, %%ebx": : "g" (fd));
  1786.     asm("movl %0, %%ecx": : "g" (dostime));
  1787.     asm("movl %0, %%edx": : "g" (dosdate));
  1788.     asm("movl $0x5701, %eax");
  1789.     asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1790.     _doserrno = 0;
  1791.     asm("jnc 1f");
  1792.     asm("movl %%eax, %0": "=m" (_doserrno));
  1793.     errno = EBADF;
  1794.     asm("1:");
  1795.     return (unsigned)_doserrno;
  1796. }
  1797.  
  1798. unsigned _dos_setfileattr(char *name, unsigned attr)
  1799. {
  1800. #if 0   /* stripping of trailing '/' is not needed for unzip-internal use */
  1801.     unsigned namlen = strlen(name);
  1802.     char *i_name = alloca(namlen + 1);
  1803.  
  1804.     strcpy(i_name, name);
  1805.     if (namlen > 1 && i_name[namlen-1] == '/' && i_name[namlen-2] != ':')
  1806.         i_name[namlen-1] = '\0';
  1807.     asm("movl %0, %%edx": : "g" (i_name));
  1808. #else
  1809.     asm("movl %0, %%edx": : "g" (name));
  1810. #endif
  1811.     asm("movl %0, %%ecx": : "g" (attr));
  1812.     asm("movl $0x4301, %eax");
  1813.     asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1814.     _doserrno = 0;
  1815.     asm("jnc 1f");
  1816.     asm("movl %%eax, %0": "=m" (_doserrno));
  1817.     switch (_doserrno) {
  1818.     case 2:
  1819.     case 3:
  1820.            errno = ENOENT;
  1821.            break;
  1822.     case 5:
  1823.            errno = EACCES;
  1824.            break;
  1825.     }
  1826.     asm("1:");
  1827.     return (unsigned)_doserrno;
  1828. }
  1829.  
  1830. void _dos_getdrive(unsigned *d)
  1831. {
  1832.     asm("movl $0x1900, %eax");
  1833.     asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1834.     asm("xorb %ah, %ah");
  1835.     asm("incb %al");
  1836.     asm("movl %%eax, %0": "=a" (*d));
  1837. }
  1838.  
  1839. unsigned _dos_creat(char *path, unsigned attr, int *fd)
  1840. {
  1841.     asm("movl $0x3c00, %eax");
  1842.     asm("movl %0, %%edx": :"g" (path));
  1843.     asm("movl %0, %%ecx": :"g" (attr));
  1844.     asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1845.     asm("movl %%eax, %0": "=a" (*fd));
  1846.     _doserrno = 0;
  1847.     asm("jnc 1f");
  1848.     _doserrno = *fd;
  1849.     switch (_doserrno) {
  1850.     case 3:
  1851.            errno = ENOENT;
  1852.            break;
  1853.     case 4:
  1854.            errno = EMFILE;
  1855.            break;
  1856.     case 5:
  1857.            errno = EACCES;
  1858.            break;
  1859.     }
  1860.     asm("1:");
  1861.     return (unsigned)_doserrno;
  1862. }
  1863.  
  1864. unsigned _dos_close(int fd)
  1865. {
  1866.     asm("movl %0, %%ebx": : "g" (fd));
  1867.     asm("movl $0x3e00, %eax");
  1868.     asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  1869.     _doserrno = 0;
  1870.     asm("jnc 1f");
  1871.     asm ("movl %%eax, %0": "=m" (_doserrno));
  1872.     if (_doserrno == 6) {
  1873.           errno = EBADF;
  1874.     }
  1875.     asm("1:");
  1876.     return (unsigned)_doserrno;
  1877. }
  1878.  
  1879. #endif /* !__DJGPP__ || (__DJGPP__ < 2) */
  1880.  
  1881.  
  1882. static int volumelabel(char *name)
  1883. {
  1884.     int fd;
  1885.  
  1886.     return _dos_creat(name, FA_LABEL, &fd) ? fd : _dos_close(fd);
  1887. }
  1888.  
  1889.  
  1890. #if (defined(__DJGPP__) && (__DJGPP__ >= 2))
  1891.  
  1892. #include <dpmi.h>               /* These includes for the country info */
  1893. #include <go32.h>
  1894. #include <sys/farptr.h>
  1895.  
  1896. /* The above _dos_getcountryinfo function doesn't work with djgpp v2, presumably
  1897.  * because ds is not set correctly (does it really work at all?). Note that
  1898.  * this version only sets the date (ie. CountryInfo[0]).
  1899.  */
  1900. unsigned _dos_getcountryinfo(void *countrybuffer)
  1901. {
  1902.    __dpmi_regs regs;
  1903.  
  1904.    regs.x.ax = 0x3800;
  1905.    regs.x.dx = __tb & 0x0f;
  1906.    regs.x.ds = (__tb >> 4) & 0xffff;
  1907.    _doserrno = __dpmi_int(0x21, ®s);
  1908.  
  1909.    *(ush*)countrybuffer = _farpeekw(_dos_ds, __tb & 0xfffff);
  1910.  
  1911.    return (unsigned)_doserrno;
  1912. }
  1913.  
  1914.  
  1915. /* Disable determination of "x" bit in st_mode field for [f]stat() calls. */
  1916. int _is_executable (const char *path, int fhandle, const char *ext)
  1917. {
  1918.     return 0;
  1919. }
  1920.  
  1921. /* Prevent globbing of filenames.  This gives the same functionality as
  1922.  * "stubedit <program> globbing=no" did with DJGPP v1.
  1923.  */
  1924. char **__crt0_glob_function(char *_arg)
  1925. {
  1926.     return NULL;
  1927. }
  1928.  
  1929. /* Reduce the size of the executable and remove the functionality to read
  1930.  * the program's environment from whatever $DJGPP points to.
  1931.  */
  1932. void __crt0_load_environment_file(char *_app_name)
  1933. {
  1934. }
  1935.  
  1936. #endif /* __DJGPP__ >= 2 */
  1937. #endif /* __GO32__ || __EMX__ */
  1938.  
  1939.  
  1940.  
  1941. #ifdef __EMX__
  1942. #ifdef MORE
  1943.  
  1944. /**************************/
  1945. /* Function screenlines() */
  1946. /**************************/
  1947.  
  1948. int screenlines()
  1949. {
  1950.     int scr_dimen[2];           /* scr_dimen[0]: columns, src_dimen[1]: rows */
  1951.  
  1952.     _scrsize(scr_dimen);
  1953.     return (scr_dimen[1]);
  1954. }
  1955.  
  1956.  
  1957. /****************************/
  1958. /* Function screencolumns() */
  1959. /****************************/
  1960.  
  1961. int screencolumns()
  1962. {
  1963.     int scr_dimen[2];           /* scr_dimen[0]: columns, src_dimen[1]: rows */
  1964.  
  1965.     _scrsize(scr_dimen);
  1966.     return (scr_dimen[0]);
  1967. }
  1968.  
  1969. #endif /* MORE */
  1970. #endif /* __EMX__ */
  1971.  
  1972.  
  1973.  
  1974. #ifdef WATCOMC_386
  1975.  
  1976. static struct RMINFO {
  1977.     ulg edi, esi, ebp;
  1978.     ulg reserved;
  1979.     ulg ebx, edx, ecx, eax;
  1980.     ush flags;
  1981.     ush es,ds,fs,gs;
  1982.     ush ip_ignored,cs_ignored;
  1983.     ush sp,ss;
  1984. };
  1985.  
  1986. /* This function is used to call dos interrupts that may not be supported
  1987.  * by some particular 32-bit DOS extender.  It uses DPMI function 300h to
  1988.  * simulate a real mode call of the interrupt.  The caller is responsible
  1989.  * for providing real mode addresses of any buffer areas used.  The docs
  1990.  * for PMODE/W imply that this should not be necessary for calling the DOS
  1991.  * interrupts that it doesn't extend, but it crashes when this isn't used. */
  1992.  
  1993. static int int86x_realmode(int inter_no, union REGS *in,
  1994.                             union REGS *out, struct SREGS *seg)
  1995. {
  1996.     union REGS local;
  1997.     struct SREGS localseg;
  1998.     struct RMINFO rmi;
  1999.     int r;
  2000.  
  2001.     rmi.eax = in->x.eax;
  2002.     rmi.ebx = in->x.ebx;
  2003.     rmi.ecx = in->x.ecx;
  2004.     rmi.edx = in->x.edx;
  2005.     rmi.edi = in->x.edi;
  2006.     rmi.esi = in->x.esi;
  2007.     rmi.ebp = rmi.reserved = 0L;
  2008.     rmi.es = seg->es;
  2009.     rmi.ds = seg->ds;
  2010.     rmi.fs = seg->fs;
  2011.     rmi.gs = seg->gs;
  2012.     rmi.sp = rmi.ss = rmi.ip_ignored = rmi.cs_ignored = rmi.flags = 0;
  2013.     memset(&local, 0, sizeof(local));
  2014.     memset(&localseg, 0, sizeof(localseg));
  2015.     local.w.ax = 0x0300;
  2016.     local.h.bl = inter_no;
  2017.     local.h.bh = 0;
  2018.     local.w.cx = 0;
  2019.     localseg.es = FP_SEG(&rmi);
  2020.     local.x.edi = FP_OFF(&rmi);
  2021.     r = int386x(0x31, &local, &local, &localseg);
  2022.     out->x.eax = rmi.eax;
  2023.     out->x.ebx = rmi.ebx;
  2024.     out->x.ecx = rmi.ecx;
  2025.     out->x.edx = rmi.edx;
  2026.     out->x.edi = rmi.edi;
  2027.     out->x.esi = rmi.esi;
  2028.     out->x.cflag = rmi.flags & INTR_CF;
  2029.     return r;
  2030. }
  2031.  
  2032. #endif /* WATCOMC_386 */
  2033.  
  2034.  
  2035.  
  2036.  
  2037. #ifdef DOS_STAT_BANDAID
  2038.  
  2039. /* This papers over a bug in Watcom 10.6's standard library...sigh.
  2040.  * Apparently it applies to both the DOS and Win32 stat()s. */
  2041.  
  2042. int stat_bandaid(const char *path, struct stat *buf)
  2043. {
  2044.     char newname[4];
  2045.  
  2046.     if (!stat(path, buf))
  2047.         return 0;
  2048.     else if (!strcmp(path, ".") || (path[0] && !strcmp(path + 1, ":."))) {
  2049.         strcpy(newname, path);
  2050.         newname[strlen(path) - 1] = '\\';   /* stat(".") fails for root! */
  2051.         return stat(newname, buf);
  2052.     } else
  2053.         return -1;
  2054. }
  2055.  
  2056. #endif /* DOS_STAT_BANDAID */
  2057.