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)
186 return gz_open(path, -1, 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) {
244 if (gz ==
NULL || !gz->write)
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);
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);
int write(int fd, const char *buf, int nbytes)
GLdouble GLdouble GLdouble GLdouble q
local gzFile gz_open(void *path, int fd, const char *mode) const
int ZEXPORT gzclose(gzFile file)
int ZEXPORT inflateReset(z_streamp strm)
int unlink OF((const char *))
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
int ZEXPORT deflateEnd(z_streamp strm)
void error(char *msg) const
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
#define inflateInit2(strm, windowBits)
GLenum GLuint GLenum GLsizei const GLchar * buf
void file_compress(char *file, char *mode)
GLsizei const GLchar ** path
#define SET_BINARY_MODE(file)
local int out(void *out_desc, unsigned char *buf, unsigned len)
int ZEXPORT inflate(z_streamp strm, int flush)
gzFile ZEXPORT gzdopen(int fd, const char *mode)
void file_uncompress(char *file)
int ZEXPORT deflate(z_streamp strm, int flush)
const char *ZEXPORT gzerror(gzFile file, int *errnum)
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
void gz_compress(FILE *in, gzFile out)
void gz_uncompress(gzFile in, FILE *out)
gzFile ZEXPORT gzopen(char *path, const char *mode) const
int ZEXPORT gzread(gzFile file, voidp buf, unsigned len)
int ZEXPORT inflateEnd(z_streamp strm)