Commits

Anonymous committed 4ba9b8e

Fix exit status when there is an error reading a file.
Reported by Jeramey Crawford, fix adapted from FreeBSD.
OK guenther@

Comments (0)

Files changed (3)

usr.bin/grep/grep.c

-/*	$OpenBSD: grep.c,v 1.44 2011/07/08 01:20:24 tedu Exp $	*/
+/*	$OpenBSD: grep.c,v 1.45 2012/12/29 01:32:44 millert Exp $	*/
 
 /*-
  * Copyright (c) 1999 James Howard and Dag-Erling Coďdan Smřrgrav
 /* Housekeeping */
 int	 first;		/* flag whether or not this is our first match */
 int	 tail;		/* lines left to print */
+int	 file_err;	/* file reading error */
 
 struct patfile {
 	const char		*pf_file;
 		for (c = 0; argc--; ++argv)
 			c += procfile(*argv);
 
-	exit(!c);
+	exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
 }

usr.bin/grep/grep.h

-/*	$OpenBSD: grep.h,v 1.17 2011/07/08 01:20:24 tedu Exp $	*/
+/*	$OpenBSD: grep.h,v 1.18 2012/12/29 01:32:44 millert Exp $	*/
 
 /*-
  * Copyright (c) 1999 James Howard and Dag-Erling Coďdan Smřrgrav
 		 vflag, wflag, xflag;
 extern int	 binbehave;
 
-extern int	 first, matchall, patterns, tail;
+extern int	 first, matchall, patterns, tail, file_err;
 extern char    **pattern;
 extern fastgrep_t *fg_pattern;
 extern regex_t	*r_pattern;

usr.bin/grep/util.c

-/*	$OpenBSD: util.c,v 1.44 2012/12/12 11:12:24 millert Exp $	*/
+/*	$OpenBSD: util.c,v 1.45 2012/12/29 01:32:44 millert Exp $	*/
 
 /*-
  * Copyright (c) 1999 James Howard and Dag-Erling Coďdan Smřrgrav
 		case FTS_DNR:
 			break;
 		case FTS_ERR:
-			errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
+			file_err = 1;
+			if(!sflag)
+				warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
 			break;
 		case FTS_DP:
 			break;
 		f = grep_open(fn, "r");
 	}
 	if (f == NULL) {
+		file_err = 1;
 		if (!sflag)
 			warn("%s", fn);
 		return 0;