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