1 From cd09e81520b7917adebcffd7c361671f913325eb Mon Sep 17 00:00:00 2001
2 From: Natanael Copa <ncopa@alpinelinux.org>
3 Date: Thu, 23 Feb 2012 14:20:22 +0000
4 Subject: [PATCH] grep: support for -x, match whole line
7 http://pubs.opengroup.org/onlinepubs/009604499/utilities/grep.html
9 Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
10 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
12 findutils/grep.c | 12 +++++++++---
13 1 file changed, 9 insertions(+), 3 deletions(-)
15 diff --git a/findutils/grep.c b/findutils/grep.c
16 index 5f42242..f14d6e6 100644
17 --- a/findutils/grep.c
18 +++ b/findutils/grep.c
20 //usage: "\n -r Recurse"
21 //usage: "\n -i Ignore case"
22 //usage: "\n -w Match whole words only"
23 +//usage: "\n -x Match whole lines only"
24 //usage: "\n -F PATTERN is a literal (not regexp)"
25 //usage: IF_FEATURE_GREP_EGREP_ALIAS(
26 //usage: "\n -E PATTERN is an extended regexp"
28 //usage:#define fgrep_full_usage ""
31 - "lnqvscFiHhe:f:Lorm:w" \
32 + "lnqvscFiHhe:f:Lorm:wx" \
33 IF_FEATURE_GREP_CONTEXT("A:B:C:") \
34 IF_FEATURE_GREP_EGREP_ALIAS("E") \
35 IF_EXTRA_COMPAT("z") \
36 @@ -138,6 +139,7 @@ enum {
37 OPTBIT_r, /* recurse dirs */
38 OPTBIT_m, /* -m MAX_MATCHES */
39 OPTBIT_w, /* -w whole word match */
40 + OPTBIT_x, /* -x whole line match */
41 IF_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */
42 IF_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */
43 IF_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */
44 @@ -160,6 +162,7 @@ enum {
45 OPT_r = 1 << OPTBIT_r,
46 OPT_m = 1 << OPTBIT_m,
47 OPT_w = 1 << OPTBIT_w,
48 + OPT_x = 1 << OPTBIT_x,
49 OPT_A = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0,
50 OPT_B = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0,
51 OPT_C = IF_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0,
52 @@ -370,9 +373,12 @@ static int grep_file(FILE *file)
53 &gl->matched_range) >= 0
56 - if (!(option_mask32 & OPT_w))
57 + if (option_mask32 & OPT_x) {
58 + found = (gl->matched_range.rm_so == 0
59 + && line[gl->matched_range.rm_eo] == '\0');
60 + } else if (!(option_mask32 & OPT_w)) {
65 if (gl->matched_range.rm_so)
66 c = line[gl->matched_range.rm_so - 1];