home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / SOURCES / UNZIP52.ZIP / msdos / msdos.c (.txt) < prev    next >
C/C++ Source or Header  |  1996-04-23  |  54KB  |  1,559 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   msdos.c
  4.  
  5.   MSDOS-specific routines for use with Info-ZIP's UnZip 5.2 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.              dateformat()
  18.              version()
  19.              _dos_getcountryinfo()          (djgpp, emx)
  20.             [_dos_getftime()                (djgpp, emx)   to be added]
  21.              _dos_setftime()                (djgpp, emx)
  22.              _dos_setfileattr()             (djgpp, emx)
  23.              _dos_getdrive()                (djgpp, emx)
  24.              _dos_creat()                   (djgpp, emx)
  25.              _dos_close()                   (djgpp, emx)
  26.              volumelabel()                  (djgpp, emx)
  27.              __crt0_glob_function()         (djgpp 2.x)
  28.              __crt_load_environment_file()  (djgpp 2.x)
  29.  
  30.   ---------------------------------------------------------------------------*/
  31.  
  32.  
  33.  
  34. #define UNZIP_INTERNAL
  35. #include "unzip.h"
  36.  
  37. #ifndef USE_VFAT
  38. static void map2fat OF((char *pathcomp, char *last_dot));
  39. #endif
  40. static int isfloppy OF((int nDrive));
  41. static int volumelabel OF((char *newlabel));
  42.  
  43. static int created_dir;        /* used by mapname(), checkdir() */
  44. static int renamed_fullpath;   /* ditto */
  45. static unsigned nLabelDrive;   /* ditto, plus volumelabel() */
  46.  
  47. /*****************************/
  48. /*  Strings used in msdos.c  */
  49. /*****************************/
  50.  
  51. #ifndef SFX
  52.   static char Far CantAllocateWildcard[] =
  53.     "warning:  can't allocate wildcard buffers\n";
  54. #endif
  55. static char Far Creating[] = "   creating: %s\n";
  56. static char Far ConversionFailed[] = "mapname:  conversion of %s failed\n";
  57. static char Far Labelling[] = "labelling %c: %-22s\n";
  58. static char Far ErrSetVolLabel[] = "mapname:  error setting volume label\n";
  59. static char Far PathTooLong[] = "checkdir error:  path too long: %s\n";
  60. static char Far CantCreateDir[] = "checkdir error:  can't create %s\n\
  61.                  unable to process %s.\n";
  62. static char Far DirIsntDirectory[] =
  63.   "checkdir error:  %s exists but is not directory\n\
  64.                  unable to process %s.\n";
  65. static char Far PathTooLongTrunc[] =
  66.   "checkdir warning:  path too long; truncating\n                   %s\n\
  67.                 -> %s\n";
  68. #if (!defined(SFX) || defined(SFX_EXDIR))
  69.    static char Far CantCreateExtractDir[] =
  70.      "checkdir:  can't create extraction directory: %s\n";
  71. #endif
  72. #ifdef __TURBOC__
  73.    static char Far AttribsMayBeWrong[] =
  74.      "\nwarning:  file attributes may not be correct\n";
  75. #endif
  76.  
  77. #ifdef WATCOMC_386
  78. #  define WREGS(v,r) (v##.w.##r)
  79. #  define int86x int386x
  80. #else
  81. #  define WREGS(v,r) (v##.x.##r)
  82. #endif
  83.  
  84. #if (defined(__GO32__) || defined(__EMX__))
  85. #  include <dirent.h>        /* use readdir() */
  86. #  define MKDIR(path,mode)   mkdir(path,mode)
  87. #  define Opendir  opendir
  88. #  define Readdir  readdir
  89. #  define Closedir closedir
  90. #  define zdirent  dirent
  91. #  define zDIR     DIR
  92. #  ifdef __EMX__
  93. #    include <dos.h>
  94. #    define GETDRIVE(d)      d = _getdrive()
  95. #    define FA_LABEL         A_LABEL
  96. #  else
  97. #    define GETDRIVE(d)      _dos_getdrive(&d)
  98. #  endif
  99. #else /* !(__GO32__ || __EMX__) */
  100. #  define MKDIR(path,mode)   mkdir(path)
  101. #  ifdef __TURBOC__
  102. #    define FATTR            FA_HIDDEN+FA_SYSTEM+FA_DIREC
  103. #    define FVOLID           FA_VOLID
  104. #    define FFIRST(n,d,a)    findfirst(n,(struct ffblk *)d,a)
  105. #    define FNEXT(d)         findnext((struct ffblk *)d)
  106. #    define GETDRIVE(d)      d=getdisk()+1
  107. #    include <dir.h>
  108. #  else /* !__TURBOC__ */
  109. #    define FATTR            _A_HIDDEN+_A_SYSTEM+_A_SUBDIR
  110. #    define FVOLID           _A_VOLID
  111. #    define FFIRST(n,d,a)    _dos_findfirst(n,a,(struct find_t *)d)
  112. #    define FNEXT(d)         _dos_findnext((struct find_t *)d)
  113. #    define GETDRIVE(d)      _dos_getdrive(&d)
  114. #    include <direct.h>
  115. #  endif /* ?__TURBOC__ */
  116.    typedef struct zdirent {
  117.        char d_reserved[30];
  118.        char d_name[13];
  119.        int d_first;
  120.    } zDIR;
  121.    zDIR *Opendir OF((const char *));
  122.    struct zdirent *Readdir OF((zDIR *));
  123. #  define Closedir free
  124.  
  125.  
  126.  
  127.  
  128. #ifndef SFX
  129.  
  130. /**********************/   /* Borland C++ 3.x has its own opendir/readdir */
  131. /* Function Opendir() */   /*  library routines, but earlier versions don't, */
  132. /**********************/   /*  so use ours regardless */
  133.  
  134. zDIR *Opendir(name)
  135.     const char *name;        /* name of directory to open */
  136. {
  137.     zDIR *dirp;              /* malloc'd return value */
  138.     char *nbuf;              /* malloc'd temporary string */
  139.     int len = strlen(name);  /* path length to avoid strlens and strcats */
  140.  
  141.  
  142.     if ((dirp = (zDIR *)malloc(sizeof(zDIR))) == (zDIR *)NULL)
  143.         return (zDIR *)NULL;
  144.     if ((nbuf = malloc(len + 5)) == (char *)NULL) {
  145.         free(dirp);
  146.         return (zDIR *)NULL;
  147.     }
  148.     strcpy(nbuf, name);
  149.     if (nbuf[len-1] == ':') {
  150.         nbuf[len++] = '.';
  151.     } else if (nbuf[len-1] == '/' || nbuf[len-1] == '\\')
  152.         --len;
  153.     strcpy(nbuf+len, "/*.*");
  154.     Trace((stderr, "Opendir:  nbuf = [%s]\n", nbuf));
  155.  
  156.     if (FFIRST(nbuf, dirp, FATTR)) {
  157.         free((zvoid *)nbuf);
  158.         return (zDIR *)NULL;
  159.     }
  160.     free((zvoid *)nbuf);
  161.     dirp->d_first = 1;
  162.     return dirp;
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /**********************/
  170. /* Function Readdir() */
  171. /**********************/
  172.  
  173. struct zdirent *Readdir(d)
  174.     zDIR *d;        /* directory stream from which to read */
  175. {
  176.     /* Return pointer to first or next directory entry, or NULL if end. */
  177.  
  178.     if (d->d_first)
  179.         d->d_first = 0;
  180.     else
  181.         if (FNEXT(d))
  182.             return (struct zdirent *)NULL;
  183.     return (struct zdirent *)d;
  184. }
  185.  
  186. #endif /* !SFX */
  187. #endif /* ?(__GO32__ || __EMX__) */
  188.  
  189.  
  190.  
  191.  
  192.  
  193. #ifndef SFX
  194.  
  195. /************************/
  196. /*  Function do_wild()  */   /* identical to OS/2 version */
  197. /************************/
  198.  
  199. char *do_wild(__G__ wildspec)
  200.     __GDEF
  201.     char *wildspec;          /* only used first time on a given dir */
  202. {
  203.     static zDIR *dir = (zDIR *)NULL;
  204.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  205.     static int firstcall=TRUE, have_dirname, dirnamelen;
  206.     struct zdirent *file;
  207.  
  208.  
  209.     /* Even when we're just returning wildspec, we *always* do so in
  210.      * matchname[]--calling routine is allowed to append four characters
  211.      * to the returned string, and wildspec may be a pointer to argv[].
  212.      */
  213.     if (firstcall) {        /* first call:  must initialize everything */
  214.         firstcall = FALSE;
  215.  
  216.         if (!iswild(wildspec)) {
  217.             strcpy(matchname, wildspec);
  218.             have_dirname = FALSE;
  219.             dir = NULL;
  220.             return matchname;
  221.         }
  222.  
  223.         /* break the wildspec into a directory part and a wildcard filename */
  224.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL &&
  225.             (wildname = strrchr(wildspec, ':')) == (char *)NULL) {
  226.             dirname = ".";
  227.             dirnamelen = 1;
  228.             have_dirname = FALSE;
  229.             wildname = wildspec;
  230.         } else {
  231.             ++wildname;     /* point at character after '/' or ':' */
  232.             dirnamelen = (int)(wildname - wildspec);
  233.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  234.                 Info(slide, 1, ((char *)slide,
  235.                   LoadFarString(CantAllocateWildcard)));
  236.                 strcpy(matchname, wildspec);
  237.                 return matchname;   /* but maybe filespec was not a wildcard */
  238.             }
  239. /* GRR:  can't strip trailing char for opendir since might be "d:/" or "d:"
  240.  *       (would have to check for "./" at end--let opendir handle it instead) */
  241.             strncpy(dirname, wildspec, dirnamelen);
  242.             dirname[dirnamelen] = '\0';       /* terminate for strcpy below */
  243.             have_dirname = TRUE;
  244.         }
  245.         Trace((stderr, "do_wild:  dirname = [%s]\n", dirname));
  246.  
  247.         if ((dir = Opendir(dirname)) != (zDIR *)NULL) {
  248.             while ((file = Readdir(dir)) != (struct zdirent *)NULL) {
  249.                 Trace((stderr, "do_wild:  readdir retu