1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
7 * minigzip is a minimal implementation of the gzip utility. This is
8 * only an example of using zlib and isn't meant to replace the
9 * full-featured gzip. No attempt is made to deal with file systems
10 * limiting names to 14 or 8+3 characters, etc... Error checking is
11 * very limited. So use minigzip only for testing; use gzip for the
12 * real thing. On MSDOS, use only on file names without extension
16 /* $Id: minigzip.c,v 1.9 1996/05/22 11:52:32 me Exp $ */
25 extern void exit OF((int));
29 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
32 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
34 # define SET_BINARY_MODE(file)
38 # define unlink delete
39 # define GZ_SUFFIX "-gz"
41 # define GZ_SUFFIX ".gz"
43 #define SUFFIX_LEN sizeof(GZ_SUFFIX)
45 extern int unlink OF((const char *));
48 #define MAX_NAME_LEN 1024
51 /* For MSDOS and other systems with limitation on stack size. For Unix,
58 void error OF((const char *msg));
59 void gz_compress OF((FILE *in, gzFile out));
60 void gz_uncompress OF((gzFile in, FILE *out));
61 void file_compress OF((char *file));
62 void file_uncompress OF((char *file));
63 int main OF((int argc, char *argv[]));
65 /* ===========================================================================
66 * Display error message and exit
71 fprintf(stderr, "%s: %s\n", prog, msg);
75 /* ===========================================================================
76 * Compress input to output then close both files.
78 void gz_compress(in, out)
82 local char buf[BUFLEN];
87 len = fread(buf, 1, sizeof(buf), in);
94 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
97 if (gzclose(out) != Z_OK) error("failed gzclose");
100 /* ===========================================================================
101 * Uncompress input to output then close both files.
103 void gz_uncompress(in, out)
107 local char buf[BUFLEN];
112 len = gzread(in, buf, sizeof(buf));
113 if (len < 0) error (gzerror(in, &err));
116 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
117 error("failed fwrite");
120 if (fclose(out)) error("failed fclose");
122 if (gzclose(in) != Z_OK) error("failed gzclose");
126 /* ===========================================================================
127 * Compress the given file: create a corresponding .gz file and remove the
130 void file_compress(file)
133 local char outfile[MAX_NAME_LEN];
137 strcpy(outfile, file);
138 strcat(outfile, GZ_SUFFIX);
140 in = fopen(file, "rb");
145 out = gzopen(outfile, "wb"); /* use "wb9" for maximal compression */
147 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
150 gz_compress(in, out);
156 /* ===========================================================================
157 * Uncompress the given file and remove the original.
159 void file_uncompress(file)
162 local char buf[MAX_NAME_LEN];
163 char *infile, *outfile;
166 int len = strlen(file);
170 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
173 outfile[len-3] = '\0';
177 strcat(infile, GZ_SUFFIX);
179 in = gzopen(infile, "rb");
181 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
184 out = fopen(outfile, "wb");
190 gz_uncompress(in, out);
196 /* ===========================================================================
197 * Usage: minigzip [-d] [files...]
211 uncompr = (strcmp(*argv, "-d") == 0);
217 SET_BINARY_MODE(stdin);
218 SET_BINARY_MODE(stdout);
220 file = gzdopen(fileno(stdin), "rb");
221 if (file == NULL) error("can't gzdopen stdin");
222 gz_uncompress(file, stdout);
224 file = gzdopen(fileno(stdout), "wb"); /* "wb9" for max compr. */
225 if (file == NULL) error("can't gzdopen stdout");
226 gz_compress(stdin, file);
231 file_uncompress(*argv);
233 file_compress(*argv);
235 } while (argv++, --argc);
238 return 0; /* to avoid warning */