1 /* gzip.c - gzip/gunzip/zcat 2 * 3 * Copyright 2017 The Android Open Source Project 4 * 5 * GZIP RFC: http://www.ietf.org/rfc/rfc1952.txt 6 7 // Existing implementations allow all options for all commands. 8 USE_GZIP(NEWTOY(gzip, "cdfk123456789", TOYFLAG_USR|TOYFLAG_BIN)) 9 USE_GUNZIP(NEWTOY(gunzip, "cdfk123456789", TOYFLAG_USR|TOYFLAG_BIN)) 10 USE_ZCAT(NEWTOY(zcat, "cdfk123456789", TOYFLAG_USR|TOYFLAG_BIN)) 11 12 config GZIP 13 bool "gzip" 14 default y 15 depends on TOYBOX_LIBZ 16 help 17 usage: gzip [-19cdfk] [FILE...] 18 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. 22 23 -c Output to stdout 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) 28 29 config GUNZIP 30 bool "gunzip" 31 default y 32 depends on TOYBOX_LIBZ 33 help 34 usage: gunzip [-cfk] [FILE...] 35 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. 39 40 -c Output to stdout (act as zcat) 41 -f Force: allow read from tty 42 -k Keep input files (default is to remove) 43 44 config ZCAT 45 bool "zcat" 46 default y 47 depends on TOYBOX_LIBZ 48 help 49 usage: zcat [FILE...] 50 51 Decompress files to stdout. Like `gzip -dc`. 52 53 -c Output to stdout (default) 54 -f Force: allow read from tty 55 */ 56 57 #include <zlib.h> 58 59 #define FORCE_FLAGS 60 #define FOR_gzip 61 #include "toys.h" 62 63 GLOBALS( 64 int level; 65 ) 66 67 static void fix_time(const char *path, struct stat *sb) 68 { 69 struct timespec times[] = { sb->st_atim, sb->st_mtim }; 70 71 if (utimensat(AT_FDCWD, path, times, 0)) perror_exit("utimensat"); 72 } 73 74 static void gzerror_exit(gzFile f, char *what) 75 { 76 int err; 77 const char *msg = gzerror(f, &err); 78 79 ((err == Z_ERRNO) ? perror_exit : error_exit)("%s: %s", what, msg); 80 } 81 82 static void do_gunzip(int in_fd, char *arg) 83 { 84 struct stat sb; 85 int len, both_files; 86 char *in_name, *out_name; 87 gzFile in; 88 FILE *out; 89 90 // "gunzip x.gz" will decompress "x.gz" to "x". 91 len = strlen(arg); 92 if (len > 3 && !strcmp(arg+len-3, ".gz")) { 93 in_name = strdup(arg); 94 out_name = strdup(arg); 95 out_name[len-3] = '\0'; 96 } else if (!strcmp(arg, "-")) { 97 // "-" means stdin; assume output to stdout. 98 // TODO: require -f to read compressed data from tty? 99 in_name = strdup("-"); 100 out_name = strdup("-"); 101 } else error_exit("unknown suffix: %s", arg); 102 103 if (toys.optflags&FLAG_c) { 104 free(out_name); 105 out_name = strdup("-"); 106 } 107 108 both_files = strcmp(in_name, "-") && strcmp(out_name, "-"); 109 if (both_files) xstat(in_name, &sb); 110 111 in = gzdopen(in_fd, "r"); 112 if (in == NULL) perror_exit("gzdopen"); 113 if (!strcmp(out_name, "-")) out = stdout; 114 else { 115 int out_fd = xcreate(out_name, 116 O_CREAT|O_WRONLY|((toys.optflags&FLAG_f)?0:O_EXCL), 117 both_files?sb.st_mode:0666); 118 119 out = xfdopen(out_fd, "w"); 120 } 121 122 while ((len = gzread(in, toybuf, sizeof(toybuf))) > 0) { 123 if (fwrite(toybuf, 1, len, out) != (size_t) len) perror_exit("writing"); 124 } 125 if (len < 0) gzerror_exit(in, "gzread"); 126 if (out != stdout && fclose(out)) perror_exit("writing"); 127 if (gzclose(in) != Z_OK) error_exit("gzclose"); 128 129 if (both_files) fix_time(out_name, &sb); 130 if (!(toys.optflags&(FLAG_c|FLAG_k))) unlink(in_name); 131 free(in_name); 132 free(out_name); 133 } 134 135 static void do_gzip(int in_fd, char *in_name) 136 { 137 size_t len; 138 char *out_name; 139 FILE *in = xfdopen(in_fd, "r"); 140 gzFile out; 141 struct stat sb; 142 int both_files, out_fd; 143 144 out_name = (toys.optflags&FLAG_c) ? strdup("-") : xmprintf("%s.gz", in_name); 145 both_files = strcmp(in_name, "-") && strcmp(out_name, "-"); 146 if (both_files) xstat(in_name, &sb); 147 148 if (!strcmp(out_name, "-")) out_fd = dup(1); 149 else { 150 out_fd = open(out_name, O_CREAT|O_WRONLY|((toys.optflags&FLAG_f)?0:O_EXCL), 151 both_files?sb.st_mode:0); 152 } 153 if (out_fd == -1) perror_exit("open %s", out_name); 154 155 snprintf(toybuf, sizeof(toybuf), "w%d", TT.level); 156 out = gzdopen(out_fd, toybuf); 157 if (out == NULL) perror_exit("gzdopen %s", out_name); 158 159 while ((len = fread(toybuf, 1, sizeof(toybuf), in)) > 0) { 160 if (gzwrite(out, toybuf, len) != (int) len) gzerror_exit(out, "gzwrite"); 161 } 162 if (ferror(in)) perror_exit("fread"); 163 if (fclose(in)) perror_exit("fclose"); 164 if (gzclose(out) != Z_OK) error_exit("gzclose"); 165 166 if (both_files) fix_time(out_name, &sb); 167 if (!(toys.optflags&(FLAG_c|FLAG_k))) unlink(in_name); 168 free(out_name); 169 } 170 171 static void do_gz(int fd, char *name) 172 { 173 if (toys.optflags&FLAG_d) do_gunzip(fd, name); 174 else do_gzip(fd, name); 175 } 176 177 void gzip_main(void) 178 { 179 int i = (toys.optflags&0x1ff); 180 181 for (TT.level = (i == 0) ? 6 : 10; i; i >>= 1) --TT.level; 182 183 // With no arguments, go from stdin to stdout. 184 if (!*toys.optargs) toys.optflags |= FLAG_c; 185 186 loopfiles(toys.optargs, do_gz); 187 } 188 189 void gunzip_main(void) 190 { 191 toys.optflags |= FLAG_d; 192 gzip_main(); 193 } 194 195 void zcat_main(void) 196 { 197 toys.optflags |= (FLAG_c|FLAG_d); 198 gzip_main(); 199 } 200