From jbeich@tormail.net  Mon Nov 28 22:25:13 2011
Return-Path: <jbeich@tormail.net>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id D62D51065670
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Nov 2011 22:25:13 +0000 (UTC)
	(envelope-from jbeich@tormail.net)
Received: from server2.hudsonvalleyhost.com (server2.hudsonvalleyhost.com [66.7.195.77])
	by mx1.freebsd.org (Postfix) with ESMTP id 932DA8FC12
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Nov 2011 22:25:12 +0000 (UTC)
Received: from politkovskaja.torservers.net ([77.247.181.165]:64525 helo=internal.tormail.net)
	by server2.hudsonvalleyhost.com with esmtpsa (TLSv1:RC4-SHA:128)
	(Exim 4.69)
	(envelope-from <jbeich@tormail.net>)
	id 1RV9dP-004B6O-0Y
	for FreeBSD-gnats-submit@freebsd.org; Mon, 28 Nov 2011 17:25:12 -0500
Received: from jbeich by internal.tormail.net with local (Exim 4.63)
	(envelope-from <jbeich@tormail.net>)
	id 1RV9cG-000Bne-CG
	for FreeBSD-gnats-submit@freebsd.org; Mon, 28 Nov 2011 22:24:03 +0000
Message-Id: <1RV9cG-000Bne-CG@internal.tormail.net>
Date: Tue, 29 Nov 2011 06:21:06 +0800
From: Jan Beich <jbeich@tormail.net>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [patch] bsdgrep(1): match gnugrep(1) in exit code when open() fails

>Number:         162930
>Category:       bin
>Synopsis:       [patch] bsdgrep(1): match gnugrep(1) in exit code when open() fails
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gabor
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Nov 28 22:30:14 UTC 2011
>Closed-Date:    Tue Feb 07 17:43:07 UTC 2012
>Last-Modified:  Tue Feb 07 17:43:07 UTC 2012
>Originator:     Jan Beich
>Release:        FreeBSD 10.0-CURRENT r228005M amd64
>Organization:
>Environment:
>Description:
Any error should have exit code > 1 (from man page).

`notfound' should probably be renamed to a more generic name.
It lost its purpose since bin/162907. A few examples

  $ grep -hro 'exit(.*err.*)' /usr/src/*bin | sort | uniq -c | sort -r | head -2
     9 exit(errs)
     5 exit(error)     
>How-To-Repeat:
$ bsdgrep foo /dev/mem /dev/ctty
bsdgrep: /dev/mem: Permission denied
bsdgrep: /dev/ctty: Device not configured
Exit 1

$ gnugrep foo /dev/mem /dev/ctty
gnugrep: /dev/mem: Permission denied
gnugrep: /dev/ctty: Device not configured
Exit 2
>Fix:
--- error.diff begins here ---
Index: usr.bin/grep/util.c
===================================================================
--- usr.bin/grep/util.c	(revision 228104)
+++ usr.bin/grep/util.c	(working copy)
@@ -195,10 +195,9 @@ procfile(const char *fn)
 		f = grep_open(fn);
 	}
 	if (f == NULL) {
+		notfound = true;
 		if (!sflag)
 			warn("%s", fn);
-		if (errno == ENOENT)
-			notfound = true;
 		return (0);
 	}
 
--- error.diff ends here ---
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched 
State-Changed-By: gabor 
State-Changed-When: Wed Dec 7 12:25:42 UTC 2011 
State-Changed-Why:  
Committed to HEAD, thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162930 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/162930: commit references a PR
Date: Wed,  7 Dec 2011 12:25:42 +0000 (UTC)

 Author: gabor
 Date: Wed Dec  7 12:25:28 2011
 New Revision: 228319
 URL: http://svn.freebsd.org/changeset/base/228319
 
 Log:
   - Match GNU behavior of exit code
   - Rename variable that has a different meaning now
   
   PR:		bin/162930
   Submitted by:	Jan Beich <jbeich@tormail.net>
   MFC after:	1 week
 
 Modified:
   head/usr.bin/grep/grep.c
   head/usr.bin/grep/grep.h
   head/usr.bin/grep/util.c
 
 Modified: head/usr.bin/grep/grep.c
 ==============================================================================
 --- head/usr.bin/grep/grep.c	Wed Dec  7 11:06:18 2011	(r228318)
 +++ head/usr.bin/grep/grep.c	Wed Dec  7 12:25:28 2011	(r228319)
 @@ -148,7 +148,7 @@ static inline const char	*init_color(con
  bool	 first = true;	/* flag whether we are processing the first match */
  bool	 prev;		/* flag whether or not the previous line matched */
  int	 tail;		/* lines left to print */
 -bool	 notfound;	/* file not found */
 +bool	 file_err;	/* file reading error */
  
  /*
   * Prints usage information and returns 2.
 @@ -728,5 +728,5 @@ main(int argc, char *argv[])
  
  	/* Find out the correct return value according to the
  	   results and the command line option. */
 -	exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
 +	exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
  }
 
 Modified: head/usr.bin/grep/grep.h
 ==============================================================================
 --- head/usr.bin/grep/grep.h	Wed Dec  7 11:06:18 2011	(r228318)
 +++ head/usr.bin/grep/grep.h	Wed Dec  7 12:25:28 2011	(r228319)
 @@ -119,7 +119,7 @@ extern char	*label;
  extern const char *color;
  extern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;
  
 -extern bool	 first, matchall, notfound, prev;
 +extern bool	 file_err, first, matchall, prev;
  extern int	 tail;
  extern unsigned int dpatterns, fpatterns, patterns;
  extern struct pat *pattern;
 
 Modified: head/usr.bin/grep/util.c
 ==============================================================================
 --- head/usr.bin/grep/util.c	Wed Dec  7 11:06:18 2011	(r228318)
 +++ head/usr.bin/grep/util.c	Wed Dec  7 12:25:28 2011	(r228319)
 @@ -130,7 +130,7 @@ grep_tree(char **argv)
  		case FTS_DNR:
  			/* FALLTHROUGH */
  		case FTS_ERR:
 -			notfound = true;
 +			file_err = true;
  			if(!sflag)
  				warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
  			break;
 @@ -195,10 +195,9 @@ procfile(const char *fn)
  		f = grep_open(fn);
  	}
  	if (f == NULL) {
 +		file_err = true;
  		if (!sflag)
  			warn("%s", fn);
 -		if (errno == ENOENT)
 -			notfound = true;
  		return (0);
  	}
  
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: gabor 
State-Changed-When: Tue Feb 7 17:42:45 UTC 2012 
State-Changed-Why:  
It has already been MFC'd to 9.X. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162930 
>Unformatted:
