From 411ce63e597b04ffe79478758e272d555965459e Mon Sep 17 00:00:00 2001
From: Assaf Gordon <assafgordon@gmail.com>
Date: Mon, 11 Apr 2016 02:03:49 -0400
Subject: [PATCH] grep: silently ignore EPIPE errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If SIGPIPE is set to 'ignore', exit silently instead of reporting
'write error: Broken pipe' - closely mimicking default SIGPIPE
behaviour. Based on similar method in coreutils' seq by Pádraig Brady
(http://lists.gnu.org/archive/html/coreutils/2016-04/msg00004.html).

* src/grep.c: (prline): if errno=EPIPE, exit silently signalling success
  instead of reporting write error.
* tests/write-error-msg: test with SIGPIPE set to 'ignore'.
---
 src/grep.c            |  7 ++++++-
 tests/write-error-msg | 10 +++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index 8baca5a..f375ab6 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1235,7 +1235,12 @@ prline (char *beg, char *lim, char sep)
     fflush_errno ();
 
   if (stdout_errno)
-    error (EXIT_TROUBLE, stdout_errno, _("write error"));
+    {
+      if (stdout_errno!=EPIPE)
+        error (EXIT_TROUBLE, stdout_errno, _("write error"));
+      /* silently ignore EPIPE errors and exit */
+      exit (EXIT_SUCCESS);
+    }
 
   lastout = lim;
 }
diff --git a/tests/write-error-msg b/tests/write-error-msg
index 130648b..b56f233 100755
--- a/tests/write-error-msg
+++ b/tests/write-error-msg
@@ -1,5 +1,6 @@
 #!/bin/sh
-# Ensure that output errors are reported with errno information.
+# Ensure that output errors are reported with errno information,
+# except SIGPIPE which is silently ignored.
 
 # Copyright 2016 Free Software Foundation, Inc.
 
@@ -52,4 +53,11 @@ done
 compare err1 err2 \
     || { warn_ "err1,err2 contain different error messages" ; fail=1 ; }
 
+
+# With SIGPIPE ignored, grep should detect EPIPE, yet exit silently.
+( trap '' PIPE ;
+  sh -c 'grep --line-buffered 1 <in 2>err3 | head -n1 >/dev/null' ) \
+    || framework_failure_
+compare /dev/null err3 || fail=1
+
 Exit $fail
-- 
2.7.0

