? INSTALL
? automake.patch
? config.guess
? config.status.lineno
? config.sub
? depcomp
? foo
? install-sh
? mkinstalldirs
? doc/mdate-sh
? doc/texinfo.tex
Index: configure.ac.in
===================================================================
RCS file: /sources/grep/grep/configure.ac.in,v
retrieving revision 1.14
diff -u -r1.14 configure.ac.in
--- configure.ac.in	10 Feb 2009 05:51:34 -0000	1.14
+++ configure.ac.in	18 Aug 2009 10:24:45 -0000
@@ -37,6 +37,14 @@
   *)   AC_MSG_ERROR(bad value ${enableval} for --disable-perl-regexp) ;;
  esac],[testpcre=yes])
 
+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
@@ -160,6 +168,11 @@
 	AC_CHECK_LIB(pcre, pcre_exec)
 fi
 
+# support for zlib
+if test x"$testzlib" = x"yes"; then
+	AC_CHECK_LIB(z, gzdopen)
+fi
+
 AC_CONFIG_FILES([Makefile
 				 lib/Makefile
 				 lib/posix/Makefile
Index: src/grep.c
===================================================================
RCS file: /sources/grep/grep/src/grep.c,v
retrieving revision 1.132
diff -u -r1.132 grep.c
--- src/grep.c	1 Feb 2009 22:09:26 -0000	1.132
+++ src/grep.c	18 Aug 2009 10:24:46 -0000
@@ -37,6 +37,10 @@
 # include <wctype.h>
 #endif
 #include <stdio.h>
+#if defined(HAVE_LIBZ)
+# include <zlib.h>
+# include <dlfcn.h>
+#endif
 #include "system.h"
 #include "getopt.h"
 #include "getpagesize.h"
@@ -48,6 +52,28 @@
 #include "exclude.h"
 #include "closeout.h"
 
+
+#if defined(HAVE_LIBZ)
+static int (*dl_gzdopen)(int fd, const char *mode);
+
+static int map_libz(void)
+{
+  static int library_mapped = 0;
+  void *library;
+
+  if (library_mapped)
+    return 0;
+
+  library = dlopen("libz.so", RTLD_NOW);
+
+  if (!(dl_gzdopen = dlsym(library, "gzdopen")))
+    return -1;
+
+  library_mapped = 1;
+  return 0;
+}
+#endif
+
 #undef MAX
 #define MAX(A,B) ((A) > (B) ? (A) : (B))
 
@@ -82,6 +108,9 @@
 /* If nonzero, show only the part of a line matching the expression. */
 static int only_matching;
 
+/* Auto-decompress. */
+static int do_decompress;
+
 /* If nonzero, make sure first content char in a line is on a tab stop. */
 static int align_tabs;
 
@@ -284,7 +313,8 @@
   EXCLUDE_FROM_OPTION,
   LINE_BUFFERED_OPTION,
   LABEL_OPTION,
-  EXCLUDE_DIRECTORY_OPTION
+  EXCLUDE_DIRECTORY_OPTION,
+  DECOMPRESS_OPTION
 };
 
 /* Long options equivalences. */
@@ -337,6 +367,7 @@
   {"text", no_argument, NULL, 'a'},
   {"binary", no_argument, NULL, 'U'},
   {"unix-byte-offsets", no_argument, NULL, 'u'},
+  {"decompress", no_argument, NULL, DECOMPRESS_OPTION},
   {"version", no_argument, NULL, 'V'},
   {"with-filename", no_argument, NULL, 'H'},
   {"word-regexp", no_argument, NULL, 'w'},
@@ -421,6 +452,7 @@
 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. */
 
 #if defined(HAVE_MMAP)
 static int bufmapped;		/* True if buffer is memory-mapped.  */
@@ -478,6 +510,22 @@
       bufmapped = 0;
 #endif
     }
+
+#if defined(HAVE_LIBZ)
+  if (do_decompress)
+    {
+      if (map_libz())
+          error (2, 0, _("--decompress is not supported"));
+      bufgz = dl_gzdopen (fd, "rb");
+      if (bufgz == NULL)
+        {
+          if (errno != 0)
+            error (0, errno, "gzdopen");
+          return 0;
+        }
+    }
+#endif
+
   return 1;
 }
 
@@ -594,7 +642,7 @@
   if (! fillsize)
     {
       ssize_t bytesread;
-      while ((bytesread = read (bufdesc, readbuf, readsize)) < 0
+      while ((bytesread = ((bufgz ? gzread (bufgz, readbuf, readsize) : read (bufdesc, readbuf, readsize)))) < 0
 	     && errno == EINTR)
 	continue;
       if (bytesread < 0)
@@ -1321,7 +1369,7 @@
 	    error (0, errno, "%s", filename);
 	}
       else
-	while (close (desc) != 0)
+	while ((bufgz ? gzclose (bufgz) : close (desc)) != 0)
 	  if (errno != EINTR)
 	    {
 	      error (0, errno, "%s", file);
@@ -1329,6 +1377,8 @@
 	    }
     }
 
+  bufgz = NULL;
+
   return status;
 }
 
@@ -1464,6 +1514,7 @@
                             TYPE is `binary', `text', or `without-match'\n\
   -a, --text                equivalent to --binary-files=text\n\
   -I                        equivalent to --binary-files=without-match\n\
+      --decompress          decompress compressed input\n\
   -d, --directories=ACTION  how to handle directories;\n\
                             ACTION is `read', `recurse', or `skip'\n\
   -D, --devices=ACTION      how to handle devices, FIFOs and sockets;\n\
@@ -1909,6 +1960,10 @@
 
       case 'X': /* undocumented on purpose */
 	setmatcher (optarg);
+        break;
+
+      case DECOMPRESS_OPTION:
+        do_decompress = 1;
 	break;
 #endif /* GREP_PROGRAM */
 
