athena/extern/zlib/test/minigzip.c

744 lines
15 KiB
C
Raw Normal View History

2015-04-14 03:10:10 +00:00
/* minigzip.c -- simulate gzip using the zlib compression library
* Copyright (C) 1995-2006, 2010, 2011 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/*
* minigzip is a minimal implementation of the gzip utility. This is
* only an example of using zlib and isn't meant to replace the
* full-featured gzip. No attempt is made to deal with file systems
* limiting names to 14 or 8+3 characters, etc... Error checking is
* very limited. So use minigzip only for testing; use gzip for the
* real thing. On MSDOS, use only on file names without extension
* or in pipe mode.
*/
/* @(#) $Id$ */
#include "zlib.h"
#include <stdio.h>
#ifdef STDC
# include <string.h>
# include <stdlib.h>
#endif
#ifdef USE_MMAP
# include <sys/types.h>
# include <sys/mman.h>
# include <sys/stat.h>
#endif
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# ifdef UNDER_CE
# include <stdlib.h>
# endif
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
#ifdef VMS
# define unlink delete
# define GZ_SUFFIX "-gz"
#endif
#ifdef RISCOS
# define unlink remove
# define GZ_SUFFIX "-gz"
# define fileno(file) file->__file
#endif
#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
# include <unix.h> /* for fileno */
#endif
#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
2015-05-19 03:24:56 +00:00
extern int unlink OF((const char*));
2015-04-14 03:10:10 +00:00
#endif
#endif
#if defined(UNDER_CE)
# include <windows.h>
# define perror(s) pwinerror(s)
/* Map the Windows error number in ERROR to a locale-dependent error
message string and return a pointer to it. Typically, the values
for ERROR come from GetLastError.
The string pointed to shall not be modified by the application,
but may be overwritten by a subsequent call to strwinerror
The strwinerror function does not change the current setting
of GetLastError. */
2015-05-19 03:24:56 +00:00
static char* strwinerror(error)
DWORD error;
2015-04-14 03:10:10 +00:00
{
static char buf[1024];
2015-05-19 03:24:56 +00:00
wchar_t* msgbuf;
2015-04-14 03:10:10 +00:00
DWORD lasterr = GetLastError();
DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
2015-05-19 03:24:56 +00:00
| FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
error,
0, /* Default language */
(LPVOID)&msgbuf,
0,
NULL);
if (chars != 0)
{
2015-04-14 03:10:10 +00:00
/* If there is an \r\n appended, zap it. */
if (chars >= 2
2015-05-19 03:24:56 +00:00
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
{
2015-04-14 03:10:10 +00:00
chars -= 2;
msgbuf[chars] = 0;
}
2015-05-19 03:24:56 +00:00
if (chars > sizeof(buf) - 1)
{
chars = sizeof(buf) - 1;
2015-04-14 03:10:10 +00:00
msgbuf[chars] = 0;
}
wcstombs(buf, msgbuf, chars + 1);
LocalFree(msgbuf);
}
2015-05-19 03:24:56 +00:00
else
{
2015-04-14 03:10:10 +00:00
sprintf(buf, "unknown win32 error (%ld)", error);
}
SetLastError(lasterr);
return buf;
}
2015-05-19 03:24:56 +00:00
static void pwinerror(s)
const char* s;
2015-04-14 03:10:10 +00:00
{
if (s && *s)
2015-05-19 03:24:56 +00:00
fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError()));
2015-04-14 03:10:10 +00:00
else
2015-05-19 03:24:56 +00:00
fprintf(stderr, "%s\n", strwinerror(GetLastError()));
2015-04-14 03:10:10 +00:00
}
#endif /* UNDER_CE */
#ifndef GZ_SUFFIX
# define GZ_SUFFIX ".gz"
#endif
#define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
#define BUFLEN 16384
#define MAX_NAME_LEN 1024
#ifdef MAXSEG_64K
# define local static
2015-05-19 03:24:56 +00:00
/* Needed for systems with limitation on stack size. */
2015-04-14 03:10:10 +00:00
#else
# define local
#endif
#ifdef Z_SOLO
/* for Z_SOLO, create simplified gz* functions using deflate and inflate */
#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
# include <unistd.h> /* for unlink() */
#endif
2015-05-19 03:24:56 +00:00
void* myalloc OF((void*, unsigned, unsigned));
void myfree OF((void*, void*));
2015-04-14 03:10:10 +00:00
2015-05-19 03:24:56 +00:00
void* myalloc(q, n, m)
void* q;
unsigned n, m;
2015-04-14 03:10:10 +00:00
{
q = Z_NULL;
return calloc(n, m);
}
void myfree(q, p)
2015-05-19 03:24:56 +00:00
void* q, *p;
2015-04-14 03:10:10 +00:00
{
q = Z_NULL;
free(p);
}
2015-05-19 03:24:56 +00:00
typedef struct gzFile_s
{
FILE* file;
2015-04-14 03:10:10 +00:00
int write;
int err;
2015-05-19 03:24:56 +00:00
char* msg;
2015-04-14 03:10:10 +00:00
z_stream strm;
2015-05-19 03:24:56 +00:00
}* gzFile;
2015-04-14 03:10:10 +00:00
2015-05-19 03:24:56 +00:00
gzFile gzopen OF((const char*, const char*));
gzFile gzdopen OF((int, const char*));
gzFile gz_open OF((const char*, int, const char*));
2015-04-14 03:10:10 +00:00
gzFile gzopen(path, mode)
2015-05-19 03:24:56 +00:00
const char* path;
const char* mode;
2015-04-14 03:10:10 +00:00
{
return gz_open(path, -1, mode);
}
gzFile gzdopen(fd, mode)
int fd;
2015-05-19 03:24:56 +00:00
const char* mode;
2015-04-14 03:10:10 +00:00
{
return gz_open(NULL, fd, mode);
}
gzFile gz_open(path, fd, mode)
2015-05-19 03:24:56 +00:00
const char* path;
int fd;
const char* mode;
2015-04-14 03:10:10 +00:00
{
gzFile gz;
int ret;
gz = malloc(sizeof(struct gzFile_s));
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz == NULL)
return NULL;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz->write = strchr(mode, 'w') != NULL;
gz->strm.zalloc = myalloc;
gz->strm.zfree = myfree;
gz->strm.opaque = Z_NULL;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz->write)
ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
2015-05-19 03:24:56 +00:00
else
{
2015-04-14 03:10:10 +00:00
gz->strm.next_in = 0;
gz->strm.avail_in = Z_NULL;
ret = inflateInit2(&(gz->strm), 15 + 16);
}
2015-05-19 03:24:56 +00:00
if (ret != Z_OK)
{
2015-04-14 03:10:10 +00:00
free(gz);
return NULL;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
2015-05-19 03:24:56 +00:00
fopen(path, gz->write ? "wb" : "rb");
if (gz->file == NULL)
{
2015-04-14 03:10:10 +00:00
gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
free(gz);
return NULL;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz->err = 0;
gz->msg = "";
return gz;
}
2015-05-19 03:24:56 +00:00
int gzwrite OF((gzFile, const void*, unsigned));
2015-04-14 03:10:10 +00:00
int gzwrite(gz, buf, len)
2015-05-19 03:24:56 +00:00
gzFile gz;
const void* buf;
unsigned len;
2015-04-14 03:10:10 +00:00
{
2015-05-19 03:24:56 +00:00
z_stream* strm;
2015-04-14 03:10:10 +00:00
unsigned char out[BUFLEN];
if (gz == NULL || !gz->write)
return 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm = &(gz->strm);
2015-05-19 03:24:56 +00:00
strm->next_in = (void*)buf;
2015-04-14 03:10:10 +00:00
strm->avail_in = len;
2015-05-19 03:24:56 +00:00
do
{
2015-04-14 03:10:10 +00:00
strm->next_out = out;
strm->avail_out = BUFLEN;
(void)deflate(strm, Z_NO_FLUSH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
2015-05-19 03:24:56 +00:00
}
while (strm->avail_out == 0);
2015-04-14 03:10:10 +00:00
return len;
}
2015-05-19 03:24:56 +00:00
int gzread OF((gzFile, void*, unsigned));
2015-04-14 03:10:10 +00:00
int gzread(gz, buf, len)
2015-05-19 03:24:56 +00:00
gzFile gz;
void* buf;
unsigned len;
2015-04-14 03:10:10 +00:00
{
int ret;
unsigned got;
unsigned char in[1];
2015-05-19 03:24:56 +00:00
z_stream* strm;
2015-04-14 03:10:10 +00:00
if (gz == NULL || gz->write)
return 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz->err)
return 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm = &(gz->strm);
2015-05-19 03:24:56 +00:00
strm->next_out = (void*)buf;
2015-04-14 03:10:10 +00:00
strm->avail_out = len;
2015-05-19 03:24:56 +00:00
do
{
2015-04-14 03:10:10 +00:00
got = fread(in, 1, 1, gz->file);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (got == 0)
break;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm->next_in = in;
strm->avail_in = 1;
ret = inflate(strm, Z_NO_FLUSH);
2015-05-19 03:24:56 +00:00
if (ret == Z_DATA_ERROR)
{
2015-04-14 03:10:10 +00:00
gz->err = Z_DATA_ERROR;
gz->msg = strm->msg;
return 0;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (ret == Z_STREAM_END)
inflateReset(strm);
2015-05-19 03:24:56 +00:00
}
while (strm->avail_out);
2015-04-14 03:10:10 +00:00
return len - strm->avail_out;
}
int gzclose OF((gzFile));
int gzclose(gz)
2015-05-19 03:24:56 +00:00
gzFile gz;
2015-04-14 03:10:10 +00:00
{
2015-05-19 03:24:56 +00:00
z_stream* strm;
2015-04-14 03:10:10 +00:00
unsigned char out[BUFLEN];
if (gz == NULL)
return Z_STREAM_ERROR;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm = &(gz->strm);
2015-05-19 03:24:56 +00:00
if (gz->write)
{
2015-04-14 03:10:10 +00:00
strm->next_in = Z_NULL;
strm->avail_in = 0;
2015-05-19 03:24:56 +00:00
do
{
2015-04-14 03:10:10 +00:00
strm->next_out = out;
strm->avail_out = BUFLEN;
(void)deflate(strm, Z_FINISH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
2015-05-19 03:24:56 +00:00
}
while (strm->avail_out == 0);
2015-04-14 03:10:10 +00:00
deflateEnd(strm);
}
else
inflateEnd(strm);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
fclose(gz->file);
free(gz);
return Z_OK;
}
2015-05-19 03:24:56 +00:00
const char* gzerror OF((gzFile, int*));
2015-04-14 03:10:10 +00:00
2015-05-19 03:24:56 +00:00
const char* gzerror(gz, err)
gzFile gz;
int* err;
2015-04-14 03:10:10 +00:00
{
*err = gz->err;
return gz->msg;
}
#endif
2015-05-19 03:24:56 +00:00
char* prog;
2015-04-14 03:10:10 +00:00
2015-05-19 03:24:56 +00:00
void error OF((const char* msg));
void gz_compress OF((FILE* in, gzFile out));
2015-04-14 03:10:10 +00:00
#ifdef USE_MMAP
2015-05-19 03:24:56 +00:00
int gz_compress_mmap OF((FILE* in, gzFile out));
2015-04-14 03:10:10 +00:00
#endif
2015-05-19 03:24:56 +00:00
void gz_uncompress OF((gzFile in, FILE* out));
void file_compress OF((char* file, char* mode));
void file_uncompress OF((char* file));
int main OF((int argc, char* argv[]));
2015-04-14 03:10:10 +00:00
/* ===========================================================================
* Display error message and exit
*/
void error(msg)
2015-05-19 03:24:56 +00:00
const char* msg;
2015-04-14 03:10:10 +00:00
{
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
/* ===========================================================================
* Compress input to output then close both files.
*/
void gz_compress(in, out)
2015-05-19 03:24:56 +00:00
FILE* in;
gzFile out;
2015-04-14 03:10:10 +00:00
{
local char buf[BUFLEN];
int len;
int err;
#ifdef USE_MMAP
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
/* Try first compressing with mmap. If mmap fails (minigzip used in a
* pipe), use the normal fread loop.
*/
if (gz_compress_mmap(in, out) == Z_OK) return;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
#endif
2015-05-19 03:24:56 +00:00
for (;;)
{
2015-04-14 03:10:10 +00:00
len = (int)fread(buf, 1, sizeof(buf), in);
2015-05-19 03:24:56 +00:00
if (ferror(in))
{
2015-04-14 03:10:10 +00:00
perror("fread");
exit(1);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (len == 0) break;
if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
fclose(in);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gzclose(out) != Z_OK) error("failed gzclose");
}
#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
/* Try compressing the input file at once using mmap. Return Z_OK if
* if success, Z_ERRNO otherwise.
*/
int gz_compress_mmap(in, out)
2015-05-19 03:24:56 +00:00
FILE* in;
gzFile out;
2015-04-14 03:10:10 +00:00
{
int len;
int err;
int ifd = fileno(in);
caddr_t buf; /* mmap'ed buffer for the entire input file */
off_t buf_len; /* length of the input file */
struct stat sb;
/* Determine the size of the file, needed for mmap: */
if (fstat(ifd, &sb) < 0) return Z_ERRNO;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
buf_len = sb.st_size;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (buf_len <= 0) return Z_ERRNO;
/* Now do the actual mmap: */
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (buf == (caddr_t)(-1)) return Z_ERRNO;
/* Compress the whole file at once: */
2015-05-19 03:24:56 +00:00
len = gzwrite(out, (char*)buf, (unsigned)buf_len);
2015-04-14 03:10:10 +00:00
if (len != (int)buf_len) error(gzerror(out, &err));
munmap(buf, buf_len);
fclose(in);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gzclose(out) != Z_OK) error("failed gzclose");
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
return Z_OK;
}
#endif /* USE_MMAP */
/* ===========================================================================
* Uncompress input to output then close both files.
*/
void gz_uncompress(in, out)
2015-05-19 03:24:56 +00:00
gzFile in;
FILE* out;
2015-04-14 03:10:10 +00:00
{
local char buf[BUFLEN];
int len;
int err;
2015-05-19 03:24:56 +00:00
for (;;)
{
2015-04-14 03:10:10 +00:00
len = gzread(in, buf, sizeof(buf));
2015-05-19 03:24:56 +00:00
if (len < 0) error(gzerror(in, &err));
2015-04-14 03:10:10 +00:00
if (len == 0) break;
2015-05-19 03:24:56 +00:00
if ((int)fwrite(buf, 1, (unsigned)len, out) != len)
{
2015-04-14 03:10:10 +00:00
error("failed fwrite");
}
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (fclose(out)) error("failed fclose");
if (gzclose(in) != Z_OK) error("failed gzclose");
}
/* ===========================================================================
* Compress the given file: create a corresponding .gz file and remove the
* original.
*/
void file_compress(file, mode)
2015-05-19 03:24:56 +00:00
char* file;
char* mode;
2015-04-14 03:10:10 +00:00
{
local char outfile[MAX_NAME_LEN];
2015-05-19 03:24:56 +00:00
FILE* in;
2015-04-14 03:10:10 +00:00
gzFile out;
2015-05-19 03:24:56 +00:00
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile))
{
2015-04-14 03:10:10 +00:00
fprintf(stderr, "%s: filename too long\n", prog);
exit(1);
}
strcpy(outfile, file);
strcat(outfile, GZ_SUFFIX);
in = fopen(file, "rb");
2015-05-19 03:24:56 +00:00
if (in == NULL)
{
2015-04-14 03:10:10 +00:00
perror(file);
exit(1);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
out = gzopen(outfile, mode);
2015-05-19 03:24:56 +00:00
if (out == NULL)
{
2015-04-14 03:10:10 +00:00
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
exit(1);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz_compress(in, out);
unlink(file);
}
/* ===========================================================================
* Uncompress the given file and remove the original.
*/
void file_uncompress(file)
2015-05-19 03:24:56 +00:00
char* file;
2015-04-14 03:10:10 +00:00
{
local char buf[MAX_NAME_LEN];
2015-05-19 03:24:56 +00:00
char* infile, *outfile;
FILE* out;
2015-04-14 03:10:10 +00:00
gzFile in;
size_t len = strlen(file);
2015-05-19 03:24:56 +00:00
if (len + strlen(GZ_SUFFIX) >= sizeof(buf))
{
2015-04-14 03:10:10 +00:00
fprintf(stderr, "%s: filename too long\n", prog);
exit(1);
}
strcpy(buf, file);
2015-05-19 03:24:56 +00:00
if (len > SUFFIX_LEN && strcmp(file + len - SUFFIX_LEN, GZ_SUFFIX) == 0)
{
2015-04-14 03:10:10 +00:00
infile = file;
outfile = buf;
2015-05-19 03:24:56 +00:00
outfile[len - 3] = '\0';
}
else
{
2015-04-14 03:10:10 +00:00
outfile = file;
infile = buf;
strcat(infile, GZ_SUFFIX);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
in = gzopen(infile, "rb");
2015-05-19 03:24:56 +00:00
if (in == NULL)
{
2015-04-14 03:10:10 +00:00
fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
exit(1);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
out = fopen(outfile, "wb");
2015-05-19 03:24:56 +00:00
if (out == NULL)
{
2015-04-14 03:10:10 +00:00
perror(file);
exit(1);
}
gz_uncompress(in, out);
unlink(infile);
}
/* ===========================================================================
* Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
* -c : write to standard output
* -d : decompress
* -f : compress with Z_FILTERED
* -h : compress with Z_HUFFMAN_ONLY
* -r : compress with Z_RLE
* -1 to -9 : compression level
*/
int main(argc, argv)
2015-05-19 03:24:56 +00:00
int argc;
char* argv[];
2015-04-14 03:10:10 +00:00
{
int copyout = 0;
int uncompr = 0;
gzFile file;
2015-05-19 03:24:56 +00:00
char* bname, outmode[20];
2015-04-14 03:10:10 +00:00
strcpy(outmode, "wb6 ");
prog = argv[0];
bname = strrchr(argv[0], '/');
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (bname)
2015-05-19 03:24:56 +00:00
bname++;
2015-04-14 03:10:10 +00:00
else
2015-05-19 03:24:56 +00:00
bname = argv[0];
2015-04-14 03:10:10 +00:00
argc--, argv++;
if (!strcmp(bname, "gunzip"))
2015-05-19 03:24:56 +00:00
uncompr = 1;
2015-04-14 03:10:10 +00:00
else if (!strcmp(bname, "zcat"))
2015-05-19 03:24:56 +00:00
copyout = uncompr = 1;
while (argc > 0)
{
if (strcmp(*argv, "-c") == 0)
copyout = 1;
else if (strcmp(*argv, "-d") == 0)
uncompr = 1;
else if (strcmp(*argv, "-f") == 0)
outmode[3] = 'f';
else if (strcmp(*argv, "-h") == 0)
outmode[3] = 'h';
else if (strcmp(*argv, "-r") == 0)
outmode[3] = 'R';
else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
(*argv)[2] == 0)
outmode[2] = (*argv)[1];
else
break;
2015-04-14 03:10:10 +00:00
2015-05-19 03:24:56 +00:00
argc--, argv++;
2015-04-14 03:10:10 +00:00
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (outmode[3] == ' ')
outmode[3] = 0;
2015-05-19 03:24:56 +00:00
if (argc == 0)
{
2015-04-14 03:10:10 +00:00
SET_BINARY_MODE(stdin);
SET_BINARY_MODE(stdout);
2015-05-19 03:24:56 +00:00
if (uncompr)
{
2015-04-14 03:10:10 +00:00
file = gzdopen(fileno(stdin), "rb");
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (file == NULL) error("can't gzdopen stdin");
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz_uncompress(file, stdout);
2015-05-19 03:24:56 +00:00
}
else
{
2015-04-14 03:10:10 +00:00
file = gzdopen(fileno(stdout), outmode);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (file == NULL) error("can't gzdopen stdout");
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz_compress(stdin, file);
}
2015-05-19 03:24:56 +00:00
}
else
{
if (copyout)
{
2015-04-14 03:10:10 +00:00
SET_BINARY_MODE(stdout);
}
2015-05-19 03:24:56 +00:00
do
{
if (uncompr)
{
if (copyout)
{
2015-04-14 03:10:10 +00:00
file = gzopen(*argv, "rb");
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (file == NULL)
fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
else
gz_uncompress(file, stdout);
2015-05-19 03:24:56 +00:00
}
else
{
2015-04-14 03:10:10 +00:00
file_uncompress(*argv);
}
2015-05-19 03:24:56 +00:00
}
else
{
if (copyout)
{
FILE* in = fopen(*argv, "rb");
if (in == NULL)
{
2015-04-14 03:10:10 +00:00
perror(*argv);
2015-05-19 03:24:56 +00:00
}
else
{
2015-04-14 03:10:10 +00:00
file = gzdopen(fileno(stdout), outmode);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (file == NULL) error("can't gzdopen stdout");
gz_compress(in, file);
}
2015-05-19 03:24:56 +00:00
}
else
{
2015-04-14 03:10:10 +00:00
file_compress(*argv, outmode);
}
}
2015-05-19 03:24:56 +00:00
}
while (argv++, --argc);
2015-04-14 03:10:10 +00:00
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
return 0;
}