1/* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav
5 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 14 unchanged lines hidden (view full) ---
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $");
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <fnmatch.h>
--- 10 unchanged lines hidden (view full) ---
50#include "grep.h"
51
52static int linesqueued;
53static int procline(struct str *l, int);
54
55bool
56file_matching(const char *fname)
57{
58 bool ret;
59
60 ret = finclude ? false : true;
61
62 for (unsigned int i = 0; i < fpatterns; ++i) {
63 if (fnmatch(fpattern[i].pat,
64 fname, 0) == 0 || fnmatch(fpattern[i].pat,
65 basename(fname), 0) == 0) {
66 if (fpattern[i].mode == EXCL_PAT)
67 return (false);
68 else
69 ret = true;
70 }
71 }
72 return (ret);
73}
--- 198 unchanged lines hidden (view full) ---
272
273/*
274 * Processes a line comparing it with the specified patterns. Each pattern
275 * is looped to be compared along with the full string, saving each and every
276 * match, which is necessary to colorize the output and to count the
277 * matches. The matching lines are passed to printline() to display the
278 * appropriate output.
279 */
280static inline int
281procline(struct str *l, int nottext)
282{
283 regmatch_t matches[MAX_LINE_MATCHES];
284 regmatch_t pmatch;
285 size_t st = 0;
286 unsigned int i;
287 int c = 0, m = 0, r = 0;
288
--- 24 unchanged lines hidden (view full) ---
313 if (r == REG_NOMATCH)
314 continue;
315 /* Check for full match */
316 if (r == 0 && xflag)
317 if (pmatch.rm_so != 0 ||
318 (size_t)pmatch.rm_eo != l->len)
319 r = REG_NOMATCH;
320 /* Check for whole word match */
321 if (r == 0 && wflag && pmatch.rm_so != 0) {
322 wint_t wbegin, wend;
323
324 wbegin = wend = L' ';
325 if (pmatch.rm_so != 0 &&
326 sscanf(&l->dat[pmatch.rm_so - 1],
327 "%lc", &wbegin) != 1)
328 r = REG_NOMATCH;
329 else if ((size_t)pmatch.rm_eo != l->len &&
--- 173 unchanged lines hidden ---
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav
5 * Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 14 unchanged lines hidden (view full) ---
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $");
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <fnmatch.h>
--- 10 unchanged lines hidden (view full) ---
50#include "grep.h"
51
52static int linesqueued;
53static int procline(struct str *l, int);
54
55bool
56file_matching(const char *fname)
57{
58 bool ret;
59
60 ret = finclude ? false : true;
61
62 for (unsigned int i = 0; i < fpatterns; ++i) {
63 if (fnmatch(fpattern[i].pat,
64 fname, 0) == 0 || fnmatch(fpattern[i].pat,
65 basename(fname), 0) == 0) {
66 if (fpattern[i].mode == EXCL_PAT)
67 return (false);
68 else
69 ret = true;
70 }
71 }
72 return (ret);
73}
--- 198 unchanged lines hidden (view full) ---
272
273/*
274 * Processes a line comparing it with the specified patterns. Each pattern
275 * is looped to be compared along with the full string, saving each and every
276 * match, which is necessary to colorize the output and to count the
277 * matches. The matching lines are passed to printline() to display the
278 * appropriate output.
279 */
280static inline int
281procline(struct str *l, int nottext)
282{
283 regmatch_t matches[MAX_LINE_MATCHES];
284 regmatch_t pmatch;
285 size_t st = 0;
286 unsigned int i;
287 int c = 0, m = 0, r = 0;
288
--- 24 unchanged lines hidden (view full) ---
313 if (r == REG_NOMATCH)
314 continue;
315 /* Check for full match */
316 if (r == 0 && xflag)
317 if (pmatch.rm_so != 0 ||
318 (size_t)pmatch.rm_eo != l->len)
319 r = REG_NOMATCH;
320 /* Check for whole word match */
321 if (r == 0 && wflag && pmatch.rm_so != 0) {
322 wint_t wbegin, wend;
323
324 wbegin = wend = L' ';
325 if (pmatch.rm_so != 0 &&
326 sscanf(&l->dat[pmatch.rm_so - 1],
327 "%lc", &wbegin) != 1)
328 r = REG_NOMATCH;
329 else if ((size_t)pmatch.rm_eo != l->len &&
--- 173 unchanged lines hidden ---