home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
SOFTWARE
/
SOURCES
/
UNZIP52.ZIP
/
fileio.c
(
.txt
)
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-25
|
49KB
|
1,585 lines
/*---------------------------------------------------------------------------
fileio.c
This file contains routines for doing direct but relatively generic input/
output, file-related sorts of things, plus some miscellaneous stuff. Most
of the stuff has to do with opening, closing, reading and/or writing files.
Contains: open_input_file()
open_outfile() (non-VMS, non-AOS/VS, non-CMS_MVS)
undefer_input()
defer_leftover_input()
readbuf()
readbyte()
fillinbuf()
flush() (non-VMS)
disk_error() (non-VMS)
UzpMessagePrnt()
UzpMessageNull() (DLL only)
UzpInput()
UzpMorePause()
handler()
dos_to_unix_time() (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
check_for_newer() (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
do_string()
makeword()
makelong()
memset() (ZMEM only)
memcpy() (ZMEM only)
zstrnicmp()
zstat() (REGULUS only)
fLoadFarString() (SMALL_MEM only)
fLoadFarStringSmall() (SMALL_MEM only)
fLoadFarStringSmall2() (SMALL_MEM only)
zfstrcpy() (SMALL_MEM only)
---------------------------------------------------------------------------*/
#define FILEIO_C
#define UNZIP_INTERNAL
#include "unzip.h"
#include "crypt.h"
#include "ttyio.h"
#include "ebcdic.h" /* definition/initialization of ebcdic[] */
/*
Note: Under Windows, the maximum size of the buffer that can be used
with any of the *printf calls is 16,384, so win_fprintf was used to
feed the fprintf clone no more than 16K chunks at a time. This should
be valid for anything up to 64K (and probably beyond, assuming your
buffers are that big.)
*/
#ifdef MSWIN
# define WriteError(buf,len,strm) \
((extent)win_fprintf(strm, (extent)len, (char *)buf) != (extent)(len))
#else
# ifdef USE_FWRITE
# define WriteError(buf,len,strm) \
((extent)fwrite((char *)(buf),1,(extent)(len),strm) != (extent)(len))
# else
# define WriteError(buf,len,strm) \
((extent)write(fileno(strm),(char *)(buf),(extent)(len)) != (extent)(len))
# endif
#endif /* ?MSWIN */
static int disk_error OF((__GPRO));
#ifdef MSWIN
extern int win_fprintf(FILE *file, unsigned int, char *);
#endif
/****************************/
/* Strings used in fileio.c */
/****************************/
#ifdef UNIX
static char Far CannotDeleteOldFile[] = "error: cannot delete old %s\n";
#endif
static char Far CantOpenZipfile[] = "error: can't open zipfile [ %s ]\n";
#if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS))
static char Far CannotCreateFile[] = "error: cannot create %s\n";
#endif
#ifdef NOVELL_BUG_FAILSAFE
static char Far NovellBug[] =
"error: %s: stat() says does not exist, but fopen() found anyway\n";
#endif
static char Far ReadError[] = "error: zipfile read error\n";
static char Far DiskFullQuery[] =
"\n%s: write error (disk full?). Continue? (y/n/^C) ";
static char Far ZipfileCorrupt[] = "error: zipfile probably corrupt (%s)\n";
static char Far FilenameTooLongTrunc[] =
"warning: filename too long--truncating.\n";
static char Far ExtraFieldTooLong[] =
"warning: extra field too long (%d). Ignoring...\n";
#ifndef MSWIN
# ifdef MORE
static char Far MorePrompt[] = "--More--(%lu)";
# endif
static char Far QuitPrompt[] =
"--- Press `Q' to quit, or any other key to continue ---";
static char Far HidePrompt[] = /* "\r \r"; */
"\r \r";
#endif /* !MSWIN */
/******************************/
/* Function open_input_file() */
/******************************/
int open_input_file(__G) /* return 1 if open failed */
__GDEF
{
/*
* open the zipfile for reading and in BINARY mode to prevent cr/lf
* translation, which would corrupt the bitstreams
*/
#if (defined(UNIX) || defined(TOPS20) || defined(AOS_VS))
G.zipfd = open(G.zipfn, O_RDONLY);
#else /* !(UNIX || TOPS20 || AOS_VS) */
#ifdef VMS
G.zipfd = open(G.zipfn, O_RDONLY, 0, "ctx=stm");
#else /* !VMS */
#ifdef MACOS
G.zipfd = open(G.zipfn, 0);
#else /* !MACOS */
#ifdef RISCOS
G.zipfd = fopen(G.zipfn, "rb");
#else /* !RISCOS */
#ifdef CMS_MVS
G.zipfd = vmmvs_open_infile(__G);
#else /* !CMS_MVS */
G.zipfd = open(G.zipfn, O_RDONLY | O_BINARY);
#endif /* ?CMS_MVS */
#endif /* ?RISCOS */
#endif /* ?MACOS */
#endif /* ?VMS */
#endif /* ?(UNIX || TOPS20 || AOS_VS) */
#ifdef USE_STRM_INPUT
if (G.zipfd == NULL)
#else
if (G.zipfd < 0)
#endif
{
Info(slide, 0x401, ((char *)slide, LoadFarString(CantOpenZipfile),
G.zipfn));
return 1;
}
return 0;
} /* end function open_input_file() */
#if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS))
/***************************/
/* Function open_outfile() */
/***************************/
int open_outfile(__G) /* return 1 if fail */
__GDEF
{
#ifdef DLL
if (G.redirect_data)
return redirect_outfile(__G)==FALSE;
#endif
#ifdef DOS_OS2_W32
if (stat(G.filename, &G.statbuf) == 0) {
Trace((stderr, "open_outfile: stat(%s) returns 0 (file exists)\n",
G.filename));
if (!(G.statbuf.st_mode & S_IWRITE)) {
Trace((stderr, "open_outfile: existing file %s is read-only\n",
G.filename));
chmod(G.filename, S_IREAD | S_IWRITE);
Trace((stderr, "open_outfile: %s now writable\n", G.filename));
}
if (unlink(G.filename) != 0)
return 1;
Trace((stderr, "open_outfile: %s now deleted\n", G.filename));
}
#endif /* DOS_OS2_W32 */
#ifdef UNIX
if (stat(G.filename, &G.statbuf) == 0 && unlink(G.filename) < 0) {
Info(slide, 0x401, ((char *)slide, LoadFarString(CannotDeleteOldFile),
G.filename));
return 1;
}
#endif /* UNIX */
#ifdef RISCOS
if (SWI_OS_File_7(G.filename,0xDEADDEAD,0xDEADDEAD,G.lrec.ucsize)!=NULL) {
Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
G.filename));
return 1;
}
#endif /* RISCOS */
#ifdef TOPS20
char *tfilnam;
if ((tfilnam = (char *)malloc(2*strlen(G.filename)+1)) == (char *)NULL)
return 1;
strcpy(tfilnam, G.filename);
upper(tfilnam);
enquote(tfilnam);
if ((G.outfile = fopen(tfilnam, FOPW)) == (FILE *)NULL) {
Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
tfilnam));
free(tfilnam);
return 1;
}
free(tfilnam);
#else /* !TOPS20 */
#ifdef MTS
if (G.aflag)
G.outfile = fopen(G.filename, FOPWT);
else
G.outfile = fopen(G.filename, FOPW);
if (G.outfile == (FILE *)NULL) {
Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
G.filename));
return 1;
}
#else /* !MTS */
#ifdef DEBUG
Info(slide, 1, ((char *)slide,
"open_outfile: doing fopen(%s) for reading\n", G.filename));
if ((G.outfile = fopen(G.filename, FOPR)) == (FILE *)NULL)
Info(slide, 1, ((char *)slide,
"open_outfile: fopen(%s) for reading failed: does not exist\n",
G.filename));
else {
Info(slide, 1, ((char *)slide,
"open_outfile: fopen(%s) for reading succeeded: file exists\n",
G.filename));
fclose(G.outfile);
}
#endif /* DEBUG */
#ifdef NOVELL_BUG_FAILSAFE
if (G.dne && ((G.outfile = fopen(G.filename, FOPR)) != (FILE *)NULL)) {
Info(slide, 0x401, ((char *)slide, LoadFarString(NovellBug),
G.filename));
fclose(G.outfile);
return 1; /* with "./" fix in checkdir(), should never reach here */
}
#endif /* NOVELL_BUG_FAILSAFE */
Trace((stderr, "open_outfile: doing fopen(%s) for writing\n", G.filename));
if ((G.outfile = fopen(G.filename, FOPW)) == (FILE *)NULL) {