diff options
Diffstat (limited to 'minigzip.c')
-rw-r--r-- | minigzip.c | 90 |
1 files changed, 45 insertions, 45 deletions
@@ -13,7 +13,7 @@ | |||
13 | * or in pipe mode. | 13 | * or in pipe mode. |
14 | */ | 14 | */ |
15 | 15 | ||
16 | /* $Id: minigzip.c,v 1.4 1995/05/02 15:54:22 jloup Exp $ */ | 16 | /* $Id: minigzip.c,v 1.5 1995/05/03 17:27:11 jloup Exp $ */ |
17 | 17 | ||
18 | #include <stdio.h> | 18 | #include <stdio.h> |
19 | #include "zlib.h" | 19 | #include "zlib.h" |
@@ -75,14 +75,14 @@ void gz_compress(in, out) | |||
75 | int err; | 75 | int err; |
76 | 76 | ||
77 | for (;;) { | 77 | for (;;) { |
78 | len = fread(buf, 1, sizeof(buf), in); | 78 | len = fread(buf, 1, sizeof(buf), in); |
79 | if (ferror(in)) { | 79 | if (ferror(in)) { |
80 | perror("fread"); | 80 | perror("fread"); |
81 | exit(1); | 81 | exit(1); |
82 | } | 82 | } |
83 | if (len == 0) break; | 83 | if (len == 0) break; |
84 | 84 | ||
85 | if (gzwrite(out, buf, len) != len) error(gzerror(out, &err)); | 85 | if (gzwrite(out, buf, len) != len) error(gzerror(out, &err)); |
86 | } | 86 | } |
87 | fclose(in); | 87 | fclose(in); |
88 | if (gzclose(out) != Z_OK) error("failed gzclose"); | 88 | if (gzclose(out) != Z_OK) error("failed gzclose"); |
@@ -100,11 +100,11 @@ void gz_uncompress(in, out) | |||
100 | int err; | 100 | int err; |
101 | 101 | ||
102 | for (;;) { | 102 | for (;;) { |
103 | len = gzread(in, buf, sizeof(buf)); | 103 | len = gzread(in, buf, sizeof(buf)); |
104 | if (len < 0) error (gzerror(in, &err)); | 104 | if (len < 0) error (gzerror(in, &err)); |
105 | if (len == 0) break; | 105 | if (len == 0) break; |
106 | 106 | ||
107 | if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite"); | 107 | if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite"); |
108 | } | 108 | } |
109 | if (fclose(out)) error("failed fclose"); | 109 | if (fclose(out)) error("failed fclose"); |
110 | 110 | ||
@@ -128,13 +128,13 @@ void file_compress(file) | |||
128 | 128 | ||
129 | in = fopen(file, "rb"); | 129 | in = fopen(file, "rb"); |
130 | if (in == NULL) { | 130 | if (in == NULL) { |
131 | perror(file); | 131 | perror(file); |
132 | exit(1); | 132 | exit(1); |
133 | } | 133 | } |
134 | out = gzopen(outfile, "wb"); | 134 | out = gzopen(outfile, "wb"); |
135 | if (out == NULL) { | 135 | if (out == NULL) { |
136 | fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); | 136 | fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); |
137 | exit(1); | 137 | exit(1); |
138 | } | 138 | } |
139 | gz_compress(in, out); | 139 | gz_compress(in, out); |
140 | 140 | ||
@@ -157,23 +157,23 @@ void file_uncompress(file) | |||
157 | strcpy(buf, file); | 157 | strcpy(buf, file); |
158 | 158 | ||
159 | if (len > 3 && strcmp(file+len-3, ".gz") == 0) { | 159 | if (len > 3 && strcmp(file+len-3, ".gz") == 0) { |
160 | infile = file; | 160 | infile = file; |
161 | outfile = buf; | 161 | outfile = buf; |
162 | outfile[len-3] = '\0'; | 162 | outfile[len-3] = '\0'; |
163 | } else { | 163 | } else { |
164 | outfile = file; | 164 | outfile = file; |
165 | infile = buf; | 165 | infile = buf; |
166 | strcat(infile, ".gz"); | 166 | strcat(infile, ".gz"); |
167 | } | 167 | } |
168 | in = gzopen(infile, "rb"); | 168 | in = gzopen(infile, "rb"); |
169 | if (in == NULL) { | 169 | if (in == NULL) { |
170 | fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); | 170 | fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); |
171 | exit(1); | 171 | exit(1); |
172 | } | 172 | } |
173 | out = fopen(outfile, "wb"); | 173 | out = fopen(outfile, "wb"); |
174 | if (out == NULL) { | 174 | if (out == NULL) { |
175 | perror(file); | 175 | perror(file); |
176 | exit(1); | 176 | exit(1); |
177 | } | 177 | } |
178 | 178 | ||
179 | gz_uncompress(in, out); | 179 | gz_uncompress(in, out); |
@@ -197,31 +197,31 @@ void main(argc, argv) | |||
197 | argc--, argv++; | 197 | argc--, argv++; |
198 | 198 | ||
199 | if (argc > 0) { | 199 | if (argc > 0) { |
200 | uncompr = (strcmp(*argv, "-d") == 0); | 200 | uncompr = (strcmp(*argv, "-d") == 0); |
201 | if (uncompr) { | 201 | if (uncompr) { |
202 | argc--, argv++; | 202 | argc--, argv++; |
203 | } | 203 | } |
204 | } | 204 | } |
205 | if (argc == 0) { | 205 | if (argc == 0) { |
206 | SET_BINARY_MODE(stdin); | 206 | SET_BINARY_MODE(stdin); |
207 | SET_BINARY_MODE(stdout); | 207 | SET_BINARY_MODE(stdout); |
208 | if (uncompr) { | 208 | if (uncompr) { |
209 | file = gzdopen(fileno(stdin), "rb"); | 209 | file = gzdopen(fileno(stdin), "rb"); |
210 | if (file == NULL) error("can't gzdopen stdin"); | 210 | if (file == NULL) error("can't gzdopen stdin"); |
211 | gz_uncompress(file, stdout); | 211 | gz_uncompress(file, stdout); |
212 | } else { | 212 | } else { |
213 | file = gzdopen(fileno(stdout), "wb"); | 213 | file = gzdopen(fileno(stdout), "wb"); |
214 | if (file == NULL) error("can't gzdopen stdout"); | 214 | if (file == NULL) error("can't gzdopen stdout"); |
215 | gz_compress(stdin, file); | 215 | gz_compress(stdin, file); |
216 | } | 216 | } |
217 | } else { | 217 | } else { |
218 | do { | 218 | do { |
219 | if (uncompr) { | 219 | if (uncompr) { |
220 | file_uncompress(*argv); | 220 | file_uncompress(*argv); |
221 | } else { | 221 | } else { |
222 | file_compress(*argv); | 222 | file_compress(*argv); |
223 | } | 223 | } |
224 | } while (argv++, --argc); | 224 | } while (argv++, --argc); |
225 | } | 225 | } |
226 | exit(0); | 226 | exit(0); |
227 | } | 227 | } |