7 static int gzipf(char*, int);
8 static int gzip(char*, long, int, Biobuf*);
9 static int crcread(void *fd, void *buf, int n);
10 static int gzwrite(void *bout, void *buf, int n);
24 fprint(2, "usage: gzip [-vcD] [-1-9] [file ...]\n");
29 main(int argc, char *argv[])
43 * gnu tar expects gzip -d to decompress
46 /* remove -d from command line - magic! */
47 if(strcmp(argv[0], "-d") == 0){
51 memmove(_args-1, _args, strlen(_args)+1);
52 exec("gunzip", oargv);
53 sysfatal("exec gunzip failed");
64 case '1': case '2': case '3': case '4':
65 case '5': case '6': case '7': case '8': case '9':
73 crctab = mkcrctab(GZCRCPOLY);
76 sysfatal("deflateinit failed: %s\n", flateerr(ok));
79 Binit(&bout, 1, OWRITE);
80 ok = gzip(nil, time(0), 0, &bout);
84 for(i = 0; i < argc; i++)
85 ok &= gzipf(argv[i], stdout);
87 exits(ok ? nil: "errors");
91 gzipf(char *file, int stdout)
94 char ofile[256], *f, *s;
97 ifd = open(file, OREAD);
99 fprint(2, "gzip: can't open %s: %r\n", file);
104 fprint(2, "gzip: can't stat %s: %r\n", file);
108 if(dir->mode & DMDIR){
109 fprint(2, "gzip: can't compress a directory\n");
117 strcpy(ofile, "<stdout>");
119 f = strrchr(file, '/');
125 if(s != nil && s != ofile && strcmp(s, ".tar") == 0){
127 snprint(ofile, sizeof(ofile), "%s.tgz", f);
129 snprint(ofile, sizeof(ofile), "%s.gz", f);
130 ofd = create(ofile, OWRITE, 0666);
132 fprint(2, "gzip: can't open %s: %r\n", ofile);
139 fprint(2, "compressing %s to %s\n", file, ofile);
141 Binit(&bout, ofd, OWRITE);
142 ok = gzip(file, dir->mtime, ifd, &bout);
143 if(!ok || Bflush(&bout) < 0){
144 fprint(2, "gzip: error writing %s: %r\n", ofile);
156 gzip(char *file, long mtime, int ifd, Biobuf *bout)
161 Bputc(bout, GZMAGIC1);
162 Bputc(bout, GZMAGIC2);
163 Bputc(bout, GZDEFLATE);
170 Bputc(bout, mtime>>8);
171 Bputc(bout, mtime>>16);
172 Bputc(bout, mtime>>24);
175 Bputc(bout, GZOSINFERNO);
178 Bwrite(bout, file, strlen(file)+1);
183 err = deflate(bout, gzwrite, (void*)ifd, crcread, level, debug);
185 fprint(2, "gzip: deflate failed: %s\n", flateerr(err));
191 Bputc(bout, crc>>16);
192 Bputc(bout, crc>>24);
195 Bputc(bout, totr>>8);
196 Bputc(bout, totr>>16);
197 Bputc(bout, totr>>24);
203 crcread(void *fd, void *buf, int n)
208 for(; !eof && n > 0; n -= m){
209 m = read((int)fd, (char*)buf+nr, n);
218 crc = blockcrc(crctab, crc, buf, nr);
224 gzwrite(void *bout, void *buf, int n)
226 if(n != Bwrite(bout, buf, n)){