From 0fcaa65199a64239a3552ca4bbbdd7fb1edaca71 Mon Sep 17 00:00:00 2001
From: Reuben Thomas <rrt@sc3d.org>
Date: Fri, 21 Jan 2011 13:21:32 +0000
Subject: [PATCH] Add --decompress flag for transparent decompression using zlib.

---
 configure.ac |   14 ++++++++++++++
 src/main.c   |   26 +++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 1 deletions(-)

diff --git a/configure.ac b/configure.ac
index aca529a..2ddd006 100644
--- a/configure.ac
+++ b/configure.ac
@@ -73,6 +73,15 @@ AM_SILENT_RULES([yes]) # make --enable-silent-rules the default.
 
 AC_CONFIG_HEADERS([config.h:config.hin])
 
+dnl Check for arguments
+AC_ARG_ENABLE(decompress,
+ [  --disable-decompress            disable decompress],
+ [case "${enableval}" in
+  yes) testzlib=yes ;;
+  no)  testzlib=no ;;
+  *)   AC_MSG_ERROR(bad value ${enableval} for --disable-decompress) ;;
+ esac],[testzlib=yes])
+
 dnl Checks for programs.
 AC_CANONICAL_HOST
 AC_PROG_AWK
@@ -199,6 +208,11 @@ fi
 
 gl_FUNC_PCRE
 
+# support for zlib
+if test x"$testzlib" = x"yes"; then
+        AC_CHECK_LIB(z, gzdopen)
+fi
+
 AC_CONFIG_FILES([
   Makefile
   lib/Makefile
diff --git a/src/main.c b/src/main.c
index f964adb..26fec76 100644
--- a/src/main.c
+++ b/src/main.c
@@ -30,6 +30,9 @@
 #include <wctype.h>
 #include <fcntl.h>
 #include <stdio.h>
+#if defined HAVE_LIBZ
+# include <zlib.h>
+#endif
 #include "system.h"
 
 #include "argmatch.h"
@@ -83,6 +86,9 @@ static int color_option;
 /* If nonzero, show only the part of a line matching the expression. */
 static int only_matching;
 
+/* If nonzero, auto-decompress the input. */
+static int do_decompress;
+
 /* If nonzero, make sure first content char in a line is on a tab stop. */
 static int align_tabs;
 
@@ -286,7 +292,8 @@ enum
   LABEL_OPTION,
   EXCLUDE_DIRECTORY_OPTION,
   GROUP_SEPARATOR_OPTION,
-  MMAP_OPTION
+  MMAP_OPTION,
+  DECOMPRESS_OPTION
 };
 
 /* Long options equivalences. */
@@ -305,6 +312,7 @@ static struct option const long_options[] =
   {"color", optional_argument, NULL, COLOR_OPTION},
   {"colour", optional_argument, NULL, COLOR_OPTION},
   {"count", no_argument, NULL, 'c'},
+  {"decompress", no_argument, NULL, DECOMPRESS_OPTION},
   {"devices", required_argument, NULL, 'D'},
   {"directories", required_argument, NULL, 'd'},
   {"exclude", required_argument, NULL, EXCLUDE_OPTION},
@@ -435,6 +443,7 @@ static off_t bufoffset;		/* Read offset; defined on regular files.  */
 static off_t after_last_match;	/* Pointer after last matching line that
                                    would have been output if we were
                                    outputting characters. */
+static gzFile bufgz;		/* zlib compressed file descriptor. */
 
 /* Return VAL aligned to the next multiple of ALIGNMENT.  VAL can be
    an integer or a pointer.  Both args must be free of side effects.  */
@@ -1252,6 +1261,21 @@ grepfile (char const *file, struct stats *stats)
           return 1;
         }
 
+#if defined HAVE_LIBZ
+      if (do_decompress)
+	{
+	  bufgz = dl_gzdopen (fd, "rb");
+	  if (bufgz == NULL)
+	    {
+	      if (errno != 0)
+		{
+		  error (0, errno, "gzdopen");
+		  return 1;
+		}
+	    }
+	}
+#endif
+
       filename = file;
     }
 
-- 
1.7.1

