aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toys/pending/gzip.c64
1 files changed, 35 insertions, 29 deletions
diff --git a/toys/pending/gzip.c b/toys/pending/gzip.c
index f6d797b5..694ef2e8 100644
--- a/toys/pending/gzip.c
+++ b/toys/pending/gzip.c
@@ -14,7 +14,6 @@ USE_ZCAT(NEWTOY(zcat, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
config GZIP
bool "gzip"
default y
- depends on TOYBOX_LIBZ
help
usage: gzip [-19cdfk] [FILE...]
@@ -31,7 +30,6 @@ config GZIP
config GUNZIP
bool "gunzip"
default y
- depends on TOYBOX_LIBZ
help
usage: gunzip [-cfk] [FILE...]
@@ -46,7 +44,6 @@ config GUNZIP
config ZCAT
bool "zcat"
default y
- depends on TOYBOX_LIBZ
help
usage: zcat [FILE...]
@@ -59,27 +56,23 @@ config ZCAT
#define FOR_gzip
#include "toys.h"
-#include <zlib.h>
-
GLOBALS(
int level;
)
-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)) perror_exit("utimensat");
-}
+// Use assembly optimized zlib code?
+#if CFG_TOYBOX_LIBZ
+#include <zlib.h>
-static int do_zlib(int in_fd, int out_fd)
+// Read fron in_fd, write to out_fd, decompress if dd else compress
+static int do_deflate(int in_fd, int out_fd, int dd, int level)
{
- int len, err = 0, dd = toys.optflags&FLAG_d;
+ int len, err = 0;
char *b = "r";
gzFile gz;
if (!dd) {
- sprintf(b = toybuf, "w%d", TT.level);
+ sprintf(b = toybuf, "w%d", level);
if (out_fd == 1) out_fd = xdup(out_fd);
}
if (!(gz = gzdopen(dd ? in_fd : out_fd, b))) perror_exit("gzdopen");
@@ -101,6 +94,22 @@ static int do_zlib(int in_fd, int out_fd)
return err;
}
+// Use toybox's builtin lib/deflate.c
+#else
+
+// Read from in_fd, write to out_fd, decompress if dd else compress
+static int do_deflate(int in_fd, int out_fd, int dd, int level)
+{
+ int x;
+
+ if (dd) WOULD_EXIT(x, gunzip_fd(in_fd, out_fd));
+ else WOULD_EXIT(x, gzip_fd(in_fd, out_fd));
+
+ return x;
+}
+
+#endif
+
static void do_gzip(int in_fd, char *arg)
{
struct stat sb;
@@ -110,26 +119,20 @@ static void do_gzip(int in_fd, char *arg)
// Are we writing to stdout?
if (!in_fd || (toys.optflags&FLAG_c)) out_fd = 1;
if (isatty(in_fd)) {
- if (!(toys.optflags&FLAG_f)) {
- error_msg("%s:need -f to read TTY"+3*!!in_fd, arg);
- return;
- } else out_fd = 1;
+ if (!(toys.optflags&FLAG_f))
+ return error_msg("%s:need -f to read TTY"+3*!!in_fd, arg);
+ else out_fd = 1;
}
// Are we reading file.gz to write to file?
if (!out_fd) {
- if (fstat(in_fd, &sb)) {
- perror_msg("%s", arg);
- return;
- }
+ if (fstat(in_fd, &sb)) return perror_msg("%s", arg);
if (!(toys.optflags&FLAG_d)) out_name = xmprintf("%s%s", arg, ".gz");
else {
// "gunzip x.gz" will decompress "x.gz" to "x".
- if ((len = strlen(arg))<4 || strcmp(arg+len-3, ".gz")) {
- error_msg("no .gz: %s", arg);
- return;
- }
+ if ((len = strlen(arg))<4 || strcmp(arg+len-3, ".gz"))
+ return error_msg("no .gz: %s", arg);
out_name = xstrdup(arg);
out_name[len-3] = 0;
}
@@ -139,12 +142,14 @@ static void do_gzip(int in_fd, char *arg)
if (out_fd == -1) return;
}
-// if (CFG_TOYBOX_LIBZ)
- if (do_zlib(in_fd, out_fd) && out_name) arg = out_name;
+ if (do_deflate(in_fd, out_fd, toys.optflags&FLAG_d, TT.level) && out_name)
+ arg = out_name;
if (out_fd != 1) close(out_fd);
if (out_name) {
- fix_time(out_name, &sb);
+ struct timespec times[] = { sb.st_atim, sb.st_mtim };
+
+ if (utimensat(AT_FDCWD, out_name, times, 0)) perror_exit("utimensat");
if (!(toys.optflags&FLAG_k)) if (unlink(arg)) perror_msg("unlink %s", arg);
free(out_name);
}
@@ -152,6 +157,7 @@ static void do_gzip(int in_fd, char *arg)
void gzip_main(void)
{
+ // This depends on 1-9 being at the end of the option list
for (TT.level = 0; TT.level<9; TT.level++)
if ((toys.optflags>>TT.level)&1) break;
if (!(TT.level = 9-TT.level)) TT.level = 6;