27 # include <sys/types.h> 28 # include <sys/mman.h> 29 # include <sys/stat.h> 32 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 38 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 40 # define SET_BINARY_MODE(file) 44 # define snprintf _snprintf 48 # define unlink delete 49 # define GZ_SUFFIX "-gz" 52 # define unlink remove 53 # define GZ_SUFFIX "-gz" 54 # define fileno(file) file->__file 56 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 60 #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) 62 extern int unlink
OF((
const char *));
68 # define perror(s) pwinerror(s) 80 static char *strwinerror (
error)
83 static char buf[1024];
86 DWORD lasterr = GetLastError();
87 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
88 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
98 && msgbuf[chars - 2] ==
'\r' && msgbuf[chars - 1] ==
'\n') {
103 if (chars >
sizeof (buf) - 1) {
104 chars =
sizeof (buf) - 1;
108 wcstombs(buf, msgbuf, chars + 1);
112 sprintf(buf,
"unknown win32 error (%ld)", error);
115 SetLastError(lasterr);
119 static void pwinerror (s)
123 fprintf(stderr,
"%s: %s\n", s, strwinerror(GetLastError ()));
125 fprintf(stderr,
"%s\n", strwinerror(GetLastError ()));
131 # define GZ_SUFFIX ".gz" 133 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1) 136 #define MAX_NAME_LEN 1024 139 # define local static 148 #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) 152 void *myalloc
OF((
void *,
unsigned,
unsigned));
153 void myfree
OF((
void *,
void *));
155 void *myalloc(q, n, m)
178 gzFile
gzopen OF((
const char *,
const char *));
180 gzFile
gz_open OF((
const char *,
int,
const char *));
186 return gz_open(path, -1, mode);
193 return gz_open(NULL, fd, mode);
207 gz->write = strchr(mode,
'w') != NULL;
208 gz->strm.zalloc = myalloc;
209 gz->strm.zfree = myfree;
214 gz->strm.next_in = 0;
215 gz->strm.avail_in =
Z_NULL;
222 gz->file = path == NULL ? fdopen(fd, gz->write ?
"wb" :
"rb") :
223 fopen(path, gz->
write ?
"wb" :
"rb");
224 if (gz->file == NULL) {
234 int gzwrite OF((gzFile,
const void *,
unsigned));
244 if (gz == NULL || !gz->write)
258 int gzread OF((gzFile,
void *,
unsigned));
270 if (gz == NULL || gz->write)
278 got = fread(in, 1, 1, gz->file);
301 unsigned char out[
BUFLEN];
338 void error
OF((
const char *msg));
341 int gz_compress_mmap
OF((FILE *in, gzFile out));
346 int main OF((
int argc,
char *argv[]));
354 fprintf(stderr,
"%s: %s\n", prog, msg);
374 if (gz_compress_mmap(in, out) ==
Z_OK)
return;
377 len = (int)fread(buf, 1,
sizeof(buf),
in);
395 int gz_compress_mmap(in, out)
401 int ifd = fileno(in);
407 if (fstat(ifd, &sb) < 0)
return Z_ERRNO;
408 buf_len = sb.st_size;
409 if (buf_len <= 0)
return Z_ERRNO;
412 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
413 if (buf == (caddr_t)(-1))
return Z_ERRNO;
416 len =
gzwrite(out, (
char *)buf, (
unsigned)buf_len);
420 munmap(buf, buf_len);
439 len =
gzread(in, buf,
sizeof(buf));
443 if ((
int)fwrite(buf, 1, (
unsigned)len, out) != len) {
444 error(
"failed fwrite");
447 if (fclose(out))
error(
"failed fclose");
465 if (strlen(file) + strlen(
GZ_SUFFIX) >=
sizeof(outfile)) {
466 fprintf(stderr,
"%s: filename too long\n", prog);
470 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 471 snprintf(outfile,
sizeof(outfile),
"%s%s", file,
GZ_SUFFIX);
473 strcpy(outfile, file);
477 in = fopen(file,
"rb");
482 out =
gzopen(outfile, mode);
484 fprintf(stderr,
"%s: can't gzopen %s\n", prog, outfile);
500 char *infile, *outfile;
503 size_t len = strlen(file);
505 if (len + strlen(
GZ_SUFFIX) >=
sizeof(buf)) {
506 fprintf(stderr,
"%s: filename too long\n", prog);
510 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 511 snprintf(buf,
sizeof(buf),
"%s", file);
519 outfile[len-3] =
'\0';
523 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 524 snprintf(buf + len,
sizeof(buf) - len,
"%s",
GZ_SUFFIX);
529 in =
gzopen(infile,
"rb");
531 fprintf(stderr,
"%s: can't gzopen %s\n", prog, infile);
534 out = fopen(outfile,
"wb");
563 char *bname, outmode[20];
565 #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 566 snprintf(outmode,
sizeof(outmode),
"%s",
"wb6 ");
568 strcpy(outmode,
"wb6 ");
572 bname = strrchr(argv[0],
'/');
579 if (!strcmp(bname,
"gunzip"))
581 else if (!strcmp(bname,
"zcat"))
582 copyout = uncompr = 1;
585 if (strcmp(*argv,
"-c") == 0)
587 else if (strcmp(*argv,
"-d") == 0)
589 else if (strcmp(*argv,
"-f") == 0)
591 else if (strcmp(*argv,
"-h") == 0)
593 else if (strcmp(*argv,
"-r") == 0)
595 else if ((*argv)[0] ==
'-' && (*argv)[1] >=
'1' && (*argv)[1] <=
'9' &&
597 outmode[2] = (*argv)[1];
602 if (outmode[3] ==
' ')
608 file =
gzdopen(fileno(stdin),
"rb");
609 if (file == NULL)
error(
"can't gzdopen stdin");
612 file =
gzdopen(fileno(stdout), outmode);
613 if (file == NULL)
error(
"can't gzdopen stdout");
623 file =
gzopen(*argv,
"rb");
625 fprintf(stderr,
"%s: can't gzopen %s\n", prog, *argv);
633 FILE * in = fopen(*argv,
"rb");
638 file =
gzdopen(fileno(stdout), outmode);
639 if (file == NULL)
error(
"can't gzdopen stdout");
648 }
while (argv++, --argc);
void gz_compress(FILE *in, gzFile out)
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
int deflateEnd(z_streamp strm)
int gzread(gzFile file, voidp buf, unsigned len)
gzFile gzopen(char *path, char *mode)
int unlink OF((const char *))
void error(char *msg) const
void gz_uncompress(gzFile in, FILE *out)
#define inflateInit2(strm, windowBits)
void file_compress(char *file, char *mode)
int write(ozstream &zs, const T *x, Items items)
#define SET_BINARY_MODE(file)
static int out(void *out_desc, unsigned char *buf, unsigned len)
void file_uncompress(char *file)
gzFile gzdopen(int fd, char *mode)
int inflateReset(z_streamp strm)
static unsigned in(void *in_desc, z_const unsigned char **buf)
int inflateEnd(z_streamp strm)
int inflate(z_streamp strm, int flush)
int gzwrite(gzFile file, voidpc buf, unsigned len)
char * gzerror(gzFile file, int *errnum)
int deflate(z_streamp strm, int flush)