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

  1. /*---------------------------------------------------------------------------
  2.  
  3.   fileio.c
  4.  
  5.   This file contains routines for doing direct but relatively generic input/
  6.   output, file-related sorts of things, plus some miscellaneous stuff.  Most
  7.   of the stuff has to do with opening, closing, reading and/or writing files.
  8.  
  9.   Contains:  open_input_file()
  10.              open_outfile()           (non-VMS, non-AOS/VS, non-CMS_MVS)
  11.              undefer_input()
  12.              defer_leftover_input()
  13.              readbuf()
  14.              readbyte()
  15.              fillinbuf()
  16.              flush()                  (non-VMS)
  17.              disk_error()             (non-VMS)
  18.              UzpMessagePrnt()
  19.              UzpMessageNull()         (DLL only)
  20.              UzpInput()
  21.              UzpMorePause()
  22.              handler()
  23.              dos_to_unix_time()       (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
  24.              check_for_newer()        (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
  25.              do_string()
  26.              makeword()
  27.              makelong()
  28.              memset()                 (ZMEM only)
  29.              memcpy()                 (ZMEM only)
  30.              zstrnicmp()
  31.              zstat()                  (REGULUS only)
  32.              fLoadFarString()         (SMALL_MEM only)
  33.              fLoadFarStringSmall()    (SMALL_MEM only)
  34.              fLoadFarStringSmall2()   (SMALL_MEM only)
  35.              zfstrcpy()               (SMALL_MEM only)
  36.  
  37.   ---------------------------------------------------------------------------*/
  38.  
  39.  
  40. #define FILEIO_C
  41. #define UNZIP_INTERNAL
  42. #include "unzip.h"
  43. #include "crypt.h"
  44. #include "ttyio.h"
  45. #include "ebcdic.h"   /* definition/initialization of ebcdic[] */
  46.  
  47.  
  48. /*
  49.    Note: Under Windows, the maximum size of the buffer that can be used
  50.    with any of the *printf calls is 16,384, so win_fprintf was used to
  51.    feed the fprintf clone no more than 16K chunks at a time. This should
  52.    be valid for anything up to 64K (and probably beyond, assuming your
  53.    buffers are that big.)
  54. */
  55. #ifdef MSWIN
  56. #  define WriteError(buf,len,strm) \
  57.    ((extent)win_fprintf(strm, (extent)len, (char *)buf) != (extent)(len))
  58. #else
  59. #  ifdef USE_FWRITE
  60. #    define WriteError(buf,len,strm) \
  61.      ((extent)fwrite((char *)(buf),1,(extent)(len),strm) != (extent)(len))
  62. #  else
  63. #    define WriteError(buf,len,strm) \
  64.      ((extent)write(fileno(strm),(char *)(buf),(extent)(len)) != (extent)(len))
  65. #  endif
  66. #endif /* ?MSWIN */
  67.  
  68. static int disk_error OF((__GPRO));
  69.  
  70. #ifdef MSWIN
  71.    extern int win_fprintf(FILE *file, unsigned int, char *);
  72. #endif
  73.  
  74.  
  75.  
  76. /****************************/
  77. /* Strings used in fileio.c */
  78. /****************************/
  79.  
  80. #ifdef UNIX
  81.    static char Far CannotDeleteOldFile[] = "error:  cannot delete old %s\n";
  82. #endif
  83.  
  84. static char Far CantOpenZipfile[] = "error:  can't open zipfile [ %s ]\n";
  85. #if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS))
  86.    static char Far CannotCreateFile[] = "error:  cannot create %s\n";
  87. #endif
  88. #ifdef NOVELL_BUG_FAILSAFE
  89.    static char Far NovellBug[] =
  90.      "error:  %s: stat() says does not exist, but fopen() found anyway\n";
  91. #endif
  92. static char Far ReadError[] = "error:  zipfile read error\n";
  93. static char Far DiskFullQuery[] =
  94.   "\n%s:  write error (disk full?).  Continue? (y/n/^C) ";
  95. static char Far ZipfileCorrupt[] = "error:  zipfile probably corrupt (%s)\n";
  96. static char Far FilenameTooLongTrunc[] =
  97.   "warning:  filename too long--truncating.\n";
  98. static char Far ExtraFieldTooLong[] =
  99.   "warning:  extra field too long (%d).  Ignoring...\n";
  100.  
  101. #ifndef MSWIN
  102. #  ifdef MORE
  103.      static char Far MorePrompt[] = "--More--(%lu)";
  104. #  endif
  105.    static char Far QuitPrompt[] =
  106.      "--- Press `Q' to quit, or any other key to continue ---";
  107.    static char Far HidePrompt[] = /* "\r                       \r"; */
  108.      "\r                                                         \r";
  109. #endif /* !MSWIN */
  110.  
  111.  
  112.  
  113.  
  114.  
  115. /******************************/
  116. /* Function open_input_file() */
  117. /******************************/
  118.  
  119. int open_input_file(__G)    /* return 1 if open failed */
  120.     __GDEF
  121. {
  122.     /*
  123.      *  open the zipfile for reading and in BINARY mode to prevent cr/lf
  124.      *  translation, which would corrupt the bitstreams
  125.      */
  126.  
  127. #if (defined(UNIX) || defined(TOPS20) || defined(AOS_VS))
  128.     G.zipfd = open(G.zipfn, O_RDONLY);
  129. #else /* !(UNIX || TOPS20 || AOS_VS) */
  130. #ifdef VMS
  131.     G.zipfd = open(G.zipfn, O_RDONLY, 0, "ctx=stm");
  132. #else /* !VMS */
  133. #ifdef MACOS
  134.     G.zipfd = open(G.zipfn, 0);
  135. #else /* !MACOS */
  136. #ifdef RISCOS
  137.     G.zipfd = fopen(G.zipfn, "rb");
  138. #else /* !RISCOS */
  139. #ifdef CMS_MVS
  140.     G.zipfd = vmmvs_open_infile(__G);
  141. #else /* !CMS_MVS */
  142.     G.zipfd = open(G.zipfn, O_RDONLY | O_BINARY);
  143. #endif /* ?CMS_MVS */
  144. #endif /* ?RISCOS */
  145. #endif /* ?MACOS */
  146. #endif /* ?VMS */
  147. #endif /* ?(UNIX || TOPS20 || AOS_VS) */
  148.  
  149. #ifdef USE_STRM_INPUT
  150.     if (G.zipfd == NULL)
  151. #else
  152.     if (G.zipfd < 0)
  153. #endif
  154.     {
  155.         Info(slide, 0x401, ((char *)slide, LoadFarString(CantOpenZipfile),
  156.           G.zipfn));
  157.         return 1;
  158.     }
  159.     return 0;
  160.  
  161. } /* end function open_input_file() */
  162.  
  163.  
  164.  
  165.  
  166. #if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS))
  167.  
  168. /***************************/
  169. /* Function open_outfile() */
  170. /***************************/
  171.  
  172. int open_outfile(__G)         /* return 1 if fail */
  173.     __GDEF
  174. {
  175. #ifdef DLL
  176.     if (G.redirect_data)
  177.         return redirect_outfile(__G)==FALSE;
  178. #endif
  179. #ifdef DOS_OS2_W32
  180.     if (stat(G.filename, &G.statbuf) == 0) {
  181.         Trace((stderr, "open_outfile:  stat(%s) returns 0 (file exists)\n",
  182.           G.filename));
  183.         if (!(G.statbuf.st_mode & S_IWRITE)) {
  184.             Trace((stderr, "open_outfile:  existing file %s is read-only\n",
  185.               G.filename));
  186.             chmod(G.filename, S_IREAD | S_IWRITE);
  187.             Trace((stderr, "open_outfile:  %s now writable\n", G.filename));
  188.         }
  189.         if (unlink(G.filename) != 0)
  190.             return 1;
  191.         Trace((stderr, "open_outfile:  %s now deleted\n", G.filename));
  192.     }
  193. #endif /* DOS_OS2_W32 */
  194. #ifdef UNIX
  195.     if (stat(G.filename, &G.statbuf) == 0 && unlink(G.filename) < 0) {
  196.         Info(slide, 0x401, ((char *)slide, LoadFarString(CannotDeleteOldFile),
  197.           G.filename));
  198.         return 1;
  199.     }
  200. #endif /* UNIX */
  201. #ifdef RISCOS
  202.     if (SWI_OS_File_7(G.filename,0xDEADDEAD,0xDEADDEAD,G.lrec.ucsize)!=NULL) {
  203.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  204.           G.filename));
  205.         return 1;
  206.     }
  207. #endif /* RISCOS */
  208. #ifdef TOPS20
  209.     char *tfilnam;
  210.  
  211.     if ((tfilnam = (char *)malloc(2*strlen(G.filename)+1)) == (char *)NULL)
  212.         return 1;
  213.     strcpy(tfilnam, G.filename);
  214.     upper(tfilnam);
  215.     enquote(tfilnam);
  216.     if ((G.outfile = fopen(tfilnam, FOPW)) == (FILE *)NULL) {
  217.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  218.           tfilnam));
  219.         free(tfilnam);
  220.         return 1;
  221.     }
  222.     free(tfilnam);
  223. #else /* !TOPS20 */
  224. #ifdef MTS
  225.     if (G.aflag)
  226.         G.outfile = fopen(G.filename, FOPWT);
  227.     else
  228.         G.outfile = fopen(G.filename, FOPW);
  229.     if (G.outfile == (FILE *)NULL) {
  230.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  231.           G.filename));
  232.         return 1;
  233.     }
  234. #else /* !MTS */
  235. #ifdef DEBUG
  236.     Info(slide, 1, ((char *)slide,
  237.       "open_outfile:  doing fopen(%s) for reading\n", G.filename));
  238.     if ((G.outfile = fopen(G.filename, FOPR)) == (FILE *)NULL)
  239.         Info(slide, 1, ((char *)slide,
  240.           "open_outfile:  fopen(%s) for reading failed:  does not exist\n",
  241.           G.filename));
  242.     else {
  243.         Info(slide, 1, ((char *)slide,
  244.           "open_outfile:  fopen(%s) for reading succeeded:  file exists\n",
  245.           G.filename));
  246.         fclose(G.outfile);
  247.     }
  248. #endif /* DEBUG */
  249. #ifdef NOVELL_BUG_FAILSAFE
  250.     if (G.dne && ((G.outfile = fopen(G.filename, FOPR)) != (FILE *)NULL)) {
  251.         Info(slide, 0x401, ((char *)slide, LoadFarString(NovellBug),
  252.           G.filename));
  253.         fclose(G.outfile);
  254.         return 1;   /* with "./" fix in checkdir(), should never reach here */
  255.     }
  256. #endif /* NOVELL_BUG_FAILSAFE */
  257.     Trace((stderr, "open_outfile:  doing fopen(%s) for writing\n", G.filename));
  258.     if ((G.outfile = fopen(G.filename, FOPW)) == (FILE *)NULL) {
  259.