comparison src/zlib/minigzip.c @ 10:1040ca591f2e

First entry of Paradise Server 2.9 patch 10 Beta
author darius
date Sat, 06 Dec 1997 04:37:18 +0000
parents
children
comparison
equal deleted inserted replaced
9:331055a97a9d 10:1040ca591f2e
1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 /*
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
13 * or in pipe mode.
14 */
15
16 /* $Id: minigzip.c,v 1.1.1.1 1997/12/06 04:37:17 darius Exp $ */
17
18 #include <stdio.h>
19 #include "zlib.h"
20
21 #ifndef __GO32__
22 extern void exit __P((int));
23 #endif
24 extern int unlink __P((const char *));
25
26 #ifdef STDC
27 # include <string.h>
28 #endif
29
30 #ifdef MSDOS
31 # include <fcntl.h>
32 # include <io.h>
33 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
34 #else
35 # define SET_BINARY_MODE(file)
36 #endif
37
38 #define BUFLEN 4096
39 #define MAX_NAME_LEN 1024
40
41 #define local static
42 /* For MSDOS and other systems with limitation on stack size. For Unix,
43 #define local
44 works also.
45 */
46
47 char *prog;
48
49 void error __P((char *msg));
50 void gz_compress __P((FILE *in, gzFile out));
51 void gz_uncompress __P((gzFile in, FILE *out));
52 void file_compress __P((char *file));
53 void file_uncompress __P((char *file));
54 void main __P((int argc, char *argv[]));
55
56 /* ===========================================================================
57 * Display error message and exit
58 */
59 void error(msg)
60 char *msg;
61 {
62 fprintf(stderr, "%s: %s\n", prog, msg);
63 exit(1);
64 }
65
66 /* ===========================================================================
67 * Compress input to output then close both files.
68 */
69 void gz_compress(in, out)
70 FILE *in;
71 gzFile out;
72 {
73 local char buf[BUFLEN];
74 int len;
75 int err;
76
77 for (;;) {
78 len = fread(buf, 1, sizeof(buf), in);
79 if (ferror(in)) {
80 perror("fread");
81 exit(1);
82 }
83 if (len == 0) break;
84
85 if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
86 }
87 fclose(in);
88 if (gzclose(out) != Z_OK) error("failed gzclose");
89 }
90
91 /* ===========================================================================
92 * Uncompress input to output then close both files.
93 */
94 void gz_uncompress(in, out)
95 gzFile in;
96 FILE *out;
97 {
98 local char buf[BUFLEN];
99 int len;
100 int err;
101
102 for (;;) {
103 len = gzread(in, buf, sizeof(buf));
104 if (len < 0) error (gzerror(in, &err));
105 if (len == 0) break;
106
107 if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
108 }
109 if (fclose(out)) error("failed fclose");
110
111 if (gzclose(in) != Z_OK) error("failed gzclose");
112 }
113
114
115 /* ===========================================================================
116 * Compress the given file: create a corresponding .gz file and remove the
117 * original.
118 */
119 void file_compress(file)
120 char *file;
121 {
122 local char outfile[MAX_NAME_LEN];
123 FILE *in;
124 gzFile out;
125
126 strcpy(outfile, file);
127 strcat(outfile, ".gz");
128
129 in = fopen(file, "rb");
130 if (in == NULL) {
131 perror(file);
132 exit(1);
133 }
134 out = gzopen(outfile, "wb");
135 if (out == NULL) {
136 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
137 exit(1);
138 }
139 gz_compress(in, out);
140
141 unlink(file);
142 }
143
144
145 /* ===========================================================================
146 * Uncompress the given file and remove the original.
147 */
148 void file_uncompress(file)
149 char *file;
150 {
151 local char buf[MAX_NAME_LEN];
152 char *infile, *outfile;
153 FILE *out;
154 gzFile in;
155 int len = strlen(file);
156
157 strcpy(buf, file);
158
159 if (len > 3 && strcmp(file+len-3, ".gz") == 0) {
160 infile = file;
161 outfile = buf;
162 outfile[len-3] = '\0';
163 } else {
164 outfile = file;
165 infile = buf;
166 strcat(infile, ".gz");
167 }
168 in = gzopen(infile, "rb");
169 if (in == NULL) {
170 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
171 exit(1);
172 }
173 out = fopen(outfile, "wb");
174 if (out == NULL) {
175 perror(file);
176 exit(1);
177 }
178
179 gz_uncompress(in, out);
180
181 unlink(infile);
182 }
183
184
185 /* ===========================================================================
186 * Usage: minigzip [-d] [files...]
187 */
188
189 void main(argc, argv)
190 int argc;
191 char *argv[];
192 {
193 int uncompr = 0;
194 gzFile file;
195
196 prog = argv[0];
197 argc--, argv++;
198
199 if (argc > 0) {
200 uncompr = (strcmp(*argv, "-d") == 0);
201 if (uncompr) {
202 argc--, argv++;
203 }
204 }
205 if (argc == 0) {
206 SET_BINARY_MODE(stdin);
207 SET_BINARY_MODE(stdout);
208 if (uncompr) {
209 file = gzdopen(fileno(stdin), "rb");
210 if (file == NULL) error("can't gzdopen stdin");
211 gz_uncompress(file, stdout);
212 } else {
213 file = gzdopen(fileno(stdout), "wb");
214 if (file == NULL) error("can't gzdopen stdout");
215 gz_compress(stdin, file);
216 }
217 } else {
218 do {
219 if (uncompr) {
220 file_uncompress(*argv);
221 } else {
222 file_compress(*argv);
223 }
224 } while (argv++, --argc);
225 }
226 exit(0);
227 }