/* minigzip.c -- simulate gzip using the zlib compression library
- * Copyright (C) 1995-2006, 2010 Jean-loup Gailly.
+ * Copyright (C) 1995-2005 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
-#include "zlib.h"
#include <stdio.h>
+#include "zlib.h"
#ifdef STDC
# include <string.h>
#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)
# include <unix.h> /* for fileno */
#endif
-#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
extern int unlink OF((const char *));
#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. */
-
-static char *strwinerror (error)
- DWORD error;
-{
- static char buf[1024];
-
- wchar_t *msgbuf;
- DWORD lasterr = GetLastError();
- DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_ALLOCATE_BUFFER,
- NULL,
- error,
- 0, /* Default language */
- (LPVOID)&msgbuf,
- 0,
- NULL);
- if (chars != 0) {
- /* If there is an \r\n appended, zap it. */
- if (chars >= 2
- && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
- chars -= 2;
- msgbuf[chars] = 0;
- }
-
- if (chars > sizeof (buf) - 1) {
- chars = sizeof (buf) - 1;
- msgbuf[chars] = 0;
- }
-
- wcstombs(buf, msgbuf, chars + 1);
- LocalFree(msgbuf);
- }
- else {
- sprintf(buf, "unknown win32 error (%ld)", error);
- }
-
- SetLastError(lasterr);
- return buf;
-}
-
-static void pwinerror (s)
- const char *s;
-{
- if (s && *s)
- fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
- else
- fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
-}
-
-#endif /* UNDER_CE */
#ifndef GZ_SUFFIX
# define GZ_SUFFIX ".gz"
FILE *in;
gzFile out;
- if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
- fprintf(stderr, "%s: filename too long\n", prog);
- exit(1);
- }
-
strcpy(outfile, file);
strcat(outfile, GZ_SUFFIX);
char *infile, *outfile;
FILE *out;
gzFile in;
- size_t len = strlen(file);
-
- if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
- fprintf(stderr, "%s: filename too long\n", prog);
- exit(1);
- }
+ uInt len = (uInt)strlen(file);
strcpy(buf, file);
/* ===========================================================================
- * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
- * -c : write to standard output
+ * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
* -d : decompress
* -f : compress with Z_FILTERED
* -h : compress with Z_HUFFMAN_ONLY
int argc;
char *argv[];
{
- int copyout = 0;
int uncompr = 0;
gzFile file;
- char *bname, outmode[20];
+ char outmode[20];
strcpy(outmode, "wb6 ");
prog = argv[0];
- bname = strrchr(argv[0], '/');
- if (bname)
- bname++;
- else
- bname = argv[0];
argc--, argv++;
- if (!strcmp(bname, "gunzip"))
- uncompr = 1;
- else if (!strcmp(bname, "zcat"))
- copyout = uncompr = 1;
-
while (argc > 0) {
- if (strcmp(*argv, "-c") == 0)
- copyout = 1;
- else if (strcmp(*argv, "-d") == 0)
+ if (strcmp(*argv, "-d") == 0)
uncompr = 1;
else if (strcmp(*argv, "-f") == 0)
outmode[3] = 'f';
gz_compress(stdin, file);
}
} else {
- if (copyout) {
- SET_BINARY_MODE(stdout);
- }
do {
if (uncompr) {
- if (copyout) {
- file = gzopen(*argv, "rb");
- if (file == NULL)
- fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
- else
- gz_uncompress(file, stdout);
- } else {
- file_uncompress(*argv);
- }
+ file_uncompress(*argv);
} else {
- if (copyout) {
- FILE * in = fopen(*argv, "rb");
-
- if (in == NULL) {
- perror(*argv);
- } else {
- file = gzdopen(fileno(stdout), outmode);
- if (file == NULL) error("can't gzdopen stdout");
-
- gz_compress(in, file);
- }
-
- } else {
- file_compress(*argv, outmode);
- }
+ file_compress(*argv, outmode);
}
} while (argv++, --argc);
}