1 /* gzip.c - gzip/gunzip/zcat
3 * Copyright 2017 The Android Open Source Project
5 * GZIP RFC: http://www.ietf.org/rfc/rfc1952.txt
7 // Existing implementations allow all options for all commands.
8 USE_GZIP(NEWTOY(gzip, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
9 USE_GUNZIP(NEWTOY(gunzip, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
10 USE_ZCAT(NEWTOY(zcat, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
15 depends on TOYBOX_LIBZ
17 usage: gzip [-19cdfk] [FILE...]
19 Compress files. With no files, compresses stdin to stdout.
20 On success, the input files are removed and replaced by new
21 files with the .gz suffix.
24 -d Decompress (act as gunzip)
25 -f Force: allow overwrite of output file
26 -k Keep input files (default is to remove)
27 -# Compression level 1-9 (1:fastest, 6:default, 9:best)
32 depends on TOYBOX_LIBZ
34 usage: gunzip [-cfk] [FILE...]
36 Decompress files. With no files, decompresses stdin to stdout.
37 On success, the input files are removed and replaced by new
38 files without the .gz suffix.
40 -c Output to stdout (act as zcat)
41 -f Force: allow read from tty
42 -k Keep input files (default is to remove)
47 depends on TOYBOX_LIBZ
51 Decompress files to stdout. Like `gzip -dc`.
53 -c Output to stdout (default)
54 -f Force: allow read from tty
67 static void fix_time(const char *path, struct stat *sb)
69 struct timespec times[] = { sb->st_atim, sb->st_mtim };
71 if (utimensat(AT_FDCWD, path, times, 0)) perror_exit("utimensat");
74 static void gzerror_msg(gzFile f, char *what)
77 const char *msg = gzerror(f, &err);
79 ((err == Z_ERRNO) ? perror_msg : error_msg)("%s: %s", what, msg);
83 static int zlib_inflate(int in_fd, int out_fd)
88 if (!(in = gzdopen(in_fd, "r"))) perror_exit("gzdopen");
89 while ((len = gzread(in, toybuf, sizeof(toybuf))) > 0)
90 if (len != writeall(out_fd, toybuf, len)) break;
92 if (len>0) perror_msg("write");
93 if (len<0) gzerror_msg(in, "gzread");
94 if (gzclose(in) != Z_OK) perror_msg("gzclose"), err++;
99 static void do_gunzip(int in_fd, char *arg)
105 // Are we writing to stderr?
106 if (!in_fd || (toys.optflags&FLAG_c)) out_fd = 1;
108 if (!(toys.optflags&FLAG_f)) {
109 error_msg("%s:need -f to read TTY"+3*!!in_fd, arg);
114 // Are we reading file.gz to write to file?
116 // "gunzip x.gz" will decompress "x.gz" to "x".
117 if ((len = strlen(arg))<4 || strcmp(arg+len-3, ".gz")) {
118 error_msg("no .gz: %s", arg);
121 if (!stat(arg, &sb)) {
122 perror_msg("%s", arg);
126 out_name = xstrdup(arg);
128 out_fd = xcreate(out_name,
129 O_CREAT|O_WRONLY|WARN_ONLY|(O_EXCL*!(toys.optflags&FLAG_f)), sb.st_mode);
130 if (out_fd == -1) return;
134 if (zlib_inflate(in_fd, out_fd) && out_name) arg = out_name;
138 fix_time(out_name, &sb);
139 if (!(toys.optflags&FLAG_k)) if (unlink(arg)) perror_msg("unlink %s", arg);
144 static void do_gzip(int in_fd, char *in_name)
148 FILE *in = xfdopen(in_fd, "r");
151 int both_files, out_fd;
153 out_name = (toys.optflags&FLAG_c) ? strdup("-") : xmprintf("%s.gz", in_name);
154 both_files = strcmp(in_name, "-") && strcmp(out_name, "-");
155 if (both_files) xstat(in_name, &sb);
157 if (!strcmp(out_name, "-")) out_fd = dup(1);
159 out_fd = open(out_name, O_CREAT|O_WRONLY|((toys.optflags&FLAG_f)?0:O_EXCL),
160 both_files?sb.st_mode:0);
162 if (out_fd == -1) perror_exit("open %s", out_name);
164 snprintf(toybuf, sizeof(toybuf), "w%d", TT.level);
165 out = gzdopen(out_fd, toybuf);
166 if (out == NULL) perror_exit("gzdopen %s", out_name);
168 while ((len = fread(toybuf, 1, sizeof(toybuf), in)) > 0) {
169 if (gzwrite(out, toybuf, len) != (int) len) {
170 gzerror_msg(out, "gzwrite");
174 if (ferror(in)) perror_exit("fread");
175 if (fclose(in)) perror_exit("fclose");
176 if (gzclose(out) != Z_OK) error_exit("gzclose");
178 if (both_files) fix_time(out_name, &sb);
179 if (!(toys.optflags&(FLAG_c|FLAG_k))) unlink(in_name);
185 for (TT.level = 0; TT.level<9; TT.level++)
186 if ((toys.optflags>>TT.level)&1) break;
187 if (!(TT.level = 9-TT.level)) TT.level = 6;
189 loopfiles(toys.optargs, (toys.optflags&FLAG_d) ? do_gunzip : do_gzip);
192 void gunzip_main(void)
194 toys.optflags |= FLAG_d;
200 toys.optflags |= (FLAG_c|FLAG_d);