summaryrefslogtreecommitdiffstats
blob: 62c4518305644f61214f0f53a742d1583d6a1c0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* gzip.c - gzip/gunzip/zcat tools for gzip data
 *
 * Copyright 2017 The Android Open Source Project
 *
 * GZIP RFC: http://www.ietf.org/rfc/rfc1952.txt

TODO: port to toybox.

*/

#define _GNU_SOURCE

#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include <zlib.h>

// toybox-style flags/globals.
#define FLAG_c 1
#define FLAG_d 2
#define FLAG_f 4
#define FLAG_k 8
static struct {
  int optflags;
} toys;
static struct {
  int level;
} TT;

static void xstat(const char *path, struct stat *sb)
{
  if (stat(path, sb)) error(1, errno, "stat %s", path);
}

static void fix_time(const char *path, struct stat *sb)
{
  struct timespec times[] = { sb->st_atim, sb->st_mtim };

  if (utimensat(AT_FDCWD, path, times, 0)) error(1, errno, "utimes");
}

static FILE *xfdopen(const char *name, int flags, mode_t open_mode,
    const char *mode)
{
  FILE *fp;
  int fd;

  if (!strcmp(name, "-")) fd = dup((*mode == 'r') ? 0 : 1);
  else fd = open(name, flags, open_mode);

  if (fd == -1) error(1, errno, "open %s (%s)", name, mode);
  fp = fdopen(fd, mode);
  if (fp == NULL) error(1, errno, "fopen %s (%s)", name, mode);
  return fp;
}

static gzFile xgzopen(const char *name, int flags, mode_t open_mode,
    const char *mode)
{
  gzFile f;
  int fd;

  if (!strcmp(name, "-")) fd = dup((*mode == 'r') ? 0 : 1);
  else fd = open(name, flags, open_mode);

  if (fd == -1) error(1, errno, "open %s (%s)", name, mode);
  f = gzdopen(fd, mode);
  if (f == NULL) error(1, errno, "gzdopen %s (%s)", name, mode);
  return f;
}

static void gzfatal(gzFile f, char *what)
{
  int err;
  const char *msg = gzerror(f, &err);

  error(1, (err == Z_ERRNO) ? errno : 0, "%s: %s", what, msg);
}

static void gunzip(char *arg)
{
  struct stat sb;
  char buf[BUFSIZ];
  int len, both_files;
  char *in_name, *out_name;
  gzFile in;
  FILE *out;

  // "gunzip x.gz" will decompress "x.gz" to "x".
  len = strlen(arg);
  if (len > 3 && !strcmp(arg+len-3, ".gz")) {
    in_name = strdup(arg);
    out_name = strdup(arg);
    out_name[len-3] = '\0';
  } else if (!strcmp(arg, "-")) {
    // "-" means stdin; assume output to stdout.
    // TODO: require -f to read compressed data from tty?
    in_name = strdup("-");
    out_name = strdup("-");
  } else error(1, 0, "unknown suffix");

  if (toys.optflags&FLAG_c) {
    free(out_name);
    out_name = strdup("-");
  }

  both_files = strcmp(in_name, "-") && strcmp(out_name, "-");
  if (both_files) xstat(in_name, &sb);

  in = xgzopen(in_name, O_RDONLY, 0, "r");
  out = xfdopen(out_name, O_CREAT|O_WRONLY|((toys.optflags&FLAG_f)?0:O_EXCL),
      both_files?sb.st_mode:0666, "w");

  while ((len = gzread(in, buf, sizeof(buf))) > 0) {
    if (fwrite(buf, 1, len, out) != (size_t) len) error(1, errno, "fwrite");
  }
  if (len < 0) gzfatal(in, "gzread");
  if (fclose(out)) error(1, errno, "fclose");
  if (gzclose(in) != Z_OK) error(1, 0, "gzclose");

  if (both_files) fix_time(out_name, &sb);
  if (!(toys.optflags&(FLAG_c|FLAG_k))) unlink(in_name);
  free(in_name);
  free(out_name);
}

static void gzip(char *in_name)
{
  char buf[BUFSIZ];
  size_t len;
  char *out_name;
  FILE *in;
  gzFile out;
  struct stat sb;
  int both_files;

  if (toys.optflags&FLAG_c) {
    out_name = strdup("-");
  } else {
    if (asprintf(&out_name, "%s.gz", in_name) == -1) {
      error(1, errno, "asprintf");
    }
  }

  both_files = strcmp(in_name, "-") && strcmp(out_name, "-");
  if (both_files) xstat(in_name, &sb);

  snprintf(buf, sizeof(buf), "w%d", TT.level);
  in = xfdopen(in_name, O_RDONLY, 0, "r");
  out = xgzopen(out_name, O_CREAT|O_WRONLY|((toys.optflags&FLAG_f)?0:O_EXCL),
      both_files?sb.st_mode:0, buf);

  while ((len = fread(buf, 1, sizeof(buf), in)) > 0) {
    if (gzwrite(out, buf, len) != (int) len) gzfatal(out, "gzwrite");
  }
  if (ferror(in)) error(1, errno, "fread");
  if (fclose(in)) error(1, errno, "fclose");
  if (gzclose(out) != Z_OK) error(1, 0, "gzclose");

  if (both_files) fix_time(out_name, &sb);
  if (!(toys.optflags&(FLAG_c|FLAG_k))) unlink(in_name);
  free(out_name);
}

static void do_file(char *arg)
{
  if (toys.optflags&FLAG_d) gunzip(arg);
  else gzip(arg);
}

static void usage()
{
  char *cmd = basename(getprogname());

  printf("usage: %s [-c] [-d] [-f] [-#] [FILE...]\n", cmd);
  printf("\n");
  if (!strcmp(cmd, "zcat")) {
    printf("Decompress files to stdout. Like `gzip -dc`.\n");
    printf("\n");
    printf("-c\tOutput to stdout\n");
    printf("-f\tForce: allow read from tty\n");
  } else if (!strcmp(cmd, "gunzip")) {
    printf("Decompress files. With no files, decompresses stdin to stdout.\n");
    printf("On success, the input files are removed and replaced by new\n");
    printf("files without the .gz suffix.\n");
    printf("\n");
    printf("-c\tOutput to stdout\n");
    printf("-f\tForce: allow read from tty\n");
    printf("-k\tKeep input files (don't remove)\n");
  } else { // gzip
    printf("Compress files. With no files, compresses stdin to stdout.\n");
    printf("On success, the input files are removed and replaced by new\n");
    printf("files with the .gz suffix.\n");
    printf("\n");
    printf("-c\tOutput to stdout\n");
    printf("-d\tDecompress (act as gunzip)\n");
    printf("-f\tForce: allow overwrite of output file\n");
    printf("-k\tKeep input files (don't remove)\n");
    printf("-#\tCompression level 1-9 (1:fastest, 6:default, 9:best)\n");
  }
  printf("\n");
}

int main(int argc, char *argv[])
{
  char *cmd = basename(argv[0]);
  int opt_ch;

  toys.optflags = 0;
  TT.level = 6;

  if (!strcmp(cmd, "gunzip")) {
    // gunzip == gzip -d
    toys.optflags = FLAG_d;
  } else if (!strcmp(cmd, "zcat")) {
    // zcat == gzip -dc
    toys.optflags = (FLAG_c|FLAG_d);
  }

  while ((opt_ch = getopt(argc, argv, "cdfhk123456789")) != -1) {
    switch (opt_ch) {
    case 'c': toys.optflags |= FLAG_c; break;
    case 'd': toys.optflags |= FLAG_d; break;
    case 'f': toys.optflags |= FLAG_f; break;
    case 'k': toys.optflags |= FLAG_k; break;

    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      TT.level = opt_ch - '0';
      break;

    default:
      usage();
      return 1;
    }
  }

  if (optind == argc) {
    // With no arguments, we go from stdin to stdout.
    toys.optflags |= FLAG_c;
    do_file("-");
    return 0;
  }

  // Otherwise process each file in turn.
  while (optind < argc) do_file(argv[optind++]);
  return 0;
}