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 unlink delete
45 # define GZ_SUFFIX "-gz"
48 # define unlink remove
49 # define GZ_SUFFIX "-gz"
50 # define fileno(file) file->__file
52 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
56 #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
58 extern int unlink
OF((
const char *));
64 # define perror(s) pwinerror(s)
76 static char *strwinerror (
error)
79 static char buf[1024];
82 DWORD lasterr = GetLastError();
83 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
84 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
94 && msgbuf[chars - 2] ==
'\r' && msgbuf[chars - 1] ==
'\n') {
99 if (chars >
sizeof (buf) - 1) {
100 chars =
sizeof (
buf) - 1;
104 wcstombs(buf, msgbuf, chars + 1);
108 sprintf(buf,
"unknown win32 error (%ld)", error);
111 SetLastError(lasterr);
115 static void pwinerror (
s)
119 fprintf(stderr,
"%s: %s\n", s, strwinerror(GetLastError ()));
121 fprintf(stderr,
"%s\n", strwinerror(GetLastError ()));
127 # define GZ_SUFFIX ".gz"
129 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
132 #define MAX_NAME_LEN 1024
135 # define local static
143 void error
OF((
const char *msg));
146 int gz_compress_mmap
OF((FILE *
in,
gzFile out));
151 int main OF((
int argc,
char *argv[]));
159 fprintf(stderr,
"%s: %s\n", prog, msg);
179 if (gz_compress_mmap(in, out) ==
Z_OK)
return;
182 len = (
int)fread(buf, 1,
sizeof(buf),
in);
200 int gz_compress_mmap(
in, out)
206 int ifd = fileno(in);
212 if (fstat(ifd, &sb) < 0)
return Z_ERRNO;
213 buf_len = sb.st_size;
214 if (buf_len <= 0)
return Z_ERRNO;
217 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
218 if (buf == (caddr_t)(-1))
return Z_ERRNO;
221 len =
gzwrite(out, (
char *)buf, (
unsigned)buf_len);
225 munmap(buf, buf_len);
244 len =
gzread(in, buf,
sizeof(buf));
248 if ((
int)fwrite(buf, 1, (
unsigned)len, out) != len) {
249 error(
"failed fwrite");
252 if (fclose(out))
error(
"failed fclose");
270 if (strlen(file) + strlen(
GZ_SUFFIX) >=
sizeof(outfile)) {
271 fprintf(stderr,
"%s: filename too long\n", prog);
275 strcpy(outfile, file);
278 in = fopen(file,
"rb");
283 out =
gzopen(outfile, mode);
285 fprintf(stderr,
"%s: can't gzopen %s\n", prog, outfile);
301 char *infile, *outfile;
304 size_t len = strlen(file);
306 if (len + strlen(
GZ_SUFFIX) >=
sizeof(buf)) {
307 fprintf(stderr,
"%s: filename too long\n", prog);
316 outfile[len-3] =
'\0';
322 in =
gzopen(infile,
"rb");
324 fprintf(stderr,
"%s: can't gzopen %s\n", prog, infile);
327 out = fopen(outfile,
"wb");
356 char *bname, outmode[20];
358 strcpy(outmode,
"wb6 ");
361 bname = strrchr(argv[0],
'/');
368 if (!strcmp(bname,
"gunzip"))
370 else if (!strcmp(bname,
"zcat"))
371 copyout = uncompr = 1;
374 if (strcmp(*argv,
"-c") == 0)
376 else if (strcmp(*argv,
"-d") == 0)
378 else if (strcmp(*argv,
"-f") == 0)
380 else if (strcmp(*argv,
"-h") == 0)
382 else if (strcmp(*argv,
"-r") == 0)
384 else if ((*argv)[0] ==
'-' && (*argv)[1] >=
'1' && (*argv)[1] <=
'9' &&
386 outmode[2] = (*argv)[1];
391 if (outmode[3] ==
' ')
397 file =
gzdopen(fileno(stdin),
"rb");
398 if (file ==
NULL)
error(
"can't gzdopen stdin");
401 file =
gzdopen(fileno(stdout), outmode);
402 if (file ==
NULL)
error(
"can't gzdopen stdout");
412 file =
gzopen(*argv,
"rb");
414 fprintf(stderr,
"%s: can't gzopen %s\n", prog, *argv);
422 FILE * in = fopen(*argv,
"rb");
427 file =
gzdopen(fileno(stdout), outmode);
428 if (file ==
NULL)
error(
"can't gzdopen stdout");
437 }
while (argv++, --argc);
int main(int argc, char **argv)
gzFile ZEXPORT gzdopen(int fd, const char *mode)
int ZEXPORT gzclose(gzFile file)
int ZEXPORT gzread(gzFile file, voidp buf, unsigned len)
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
gzFile ZEXPORT gzopen(char *path, const char *mode) const
void file_compress(char *file, char *mode)
#define SET_BINARY_MODE(file)
const char *ZEXPORT gzerror(gzFile file, int *errnum)
void file_uncompress(char *file)
typedef LPVOID(WINAPI *PFNWGLCREATEIMAGEBUFFERI3DPROC)(HDC hDC
GLenum GLuint GLsizei const GLchar * buf
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
void gz_compress(FILE *in, gzFile out)
void gz_uncompress(gzFile in, FILE *out)