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[])
45 case '1': case '2': case '3': case '4':
46 case '5': case '6': case '7': case '8': case '9':
54 crctab = mkcrctab(GZCRCPOLY);
57 sysfatal("deflateinit failed: %s", flateerr(ok));
60 Binit(&bout, 1, OWRITE);
61 ok = gzip(nil, time(0), 0, &bout);
65 for(i = 0; i < argc; i++)
66 ok &= gzipf(argv[i], stdout);
68 exits(ok ? nil: "errors");
72 gzipf(char *file, int stdout)
75 char ofile[256], *f, *s;
78 ifd = open(file, OREAD);
80 fprint(2, "gzip: can't open %s: %r\n", file);
85 fprint(2, "gzip: can't stat %s: %r\n", file);
89 if(dir->mode & DMDIR){
90 fprint(2, "gzip: can't compress a directory\n");
98 strcpy(ofile, "<stdout>");
100 f = strrchr(file, '/');
106 if(s != nil && s != ofile && strcmp(s, ".tar") == 0){
108 snprint(ofile, sizeof(ofile), "%s.tgz", f);
110 snprint(ofile, sizeof(ofile), "%s.gz", f);
111 ofd = create(ofile, OWRITE, 0666);
113 fprint(2, "gzip: can't open %s: %r\n", ofile);
120 fprint(2, "compressing %s to %s\n", file, ofile);
122 Binit(&bout, ofd, OWRITE);
123 ok = gzip(file, dir->mtime, ifd, &bout);
124 if(!ok || Bflush(&bout) < 0){
125 fprint(2, "gzip: error writing %s: %r\n", ofile);
137 gzip(char *file, long mtime, int ifd, Biobuf *bout)
142 Bputc(bout, GZMAGIC1);
143 Bputc(bout, GZMAGIC2);
144 Bputc(bout, GZDEFLATE);
151 Bputc(bout, mtime>>8);
152 Bputc(bout, mtime>>16);
153 Bputc(bout, mtime>>24);
156 Bputc(bout, GZOSINFERNO);
159 Bwrite(bout, file, strlen(file)+1);
164 err = deflate(bout, gzwrite, (void*)ifd, crcread, level, debug);
166 fprint(2, "gzip: deflate failed: %s\n", flateerr(err));
172 Bputc(bout, crc>>16);
173 Bputc(bout, crc>>24);
176 Bputc(bout, totr>>8);
177 Bputc(bout, totr>>16);
178 Bputc(bout, totr>>24);
184 crcread(void *fd, void *buf, int n)
189 for(; !eof && n > 0; n -= m){
190 m = read((int)(uintptr)fd, (char*)buf+nr, n);
199 crc = blockcrc(crctab, crc, buf, nr);
205 gzwrite(void *bout, void *buf, int n)
207 if(n != Bwrite(bout, buf, n)){