Deleted Added
sdiff udiff text old ( 210479 ) new ( 210578 )
full compact
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 210479 2010-07-25 18:57:48Z gabor $");
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>
40#include <fts.h>
41#include <libgen.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46#include <wchar.h>
47#include <wctype.h>
48
49#include "grep.h"
50
51static int linesqueued;
52static int procline(struct str *l, int);
53
54/*
55 * Processes a directory when a recursive search is performed with
56 * the -R option. Each appropriate file is passed to procfile().
57 */
58int
59grep_tree(char **argv)
60{
61 FTS *fts;
62 FTSENT *p;
63 char *d, *dir = NULL;
64 unsigned int i;
65 int c, fts_flags;
66 bool ok;
67
68 c = fts_flags = 0;
69
70 switch(linkbehave) {
71 case LINK_EXPLICIT:
72 fts_flags = FTS_COMFOLLOW;

--- 24 unchanged lines hidden (view full) ---

97 case FTS_DC:
98 /* Print a warning for recursive directory loop */
99 warnx("warning: %s: recursive directory loop",
100 p->fts_path);
101 break;
102 default:
103 /* Check for file exclusion/inclusion */
104 ok = true;
105 if (exclflag) {
106 if ((d = strrchr(p->fts_path, '/')) != NULL) {
107 dir = grep_malloc(sizeof(char) *
108 (d - p->fts_path + 2));
109 strlcpy(dir, p->fts_path,
110 (d - p->fts_path + 1));
111 }
112 for (i = 0; i < epatterns; ++i) {
113 switch(epattern[i].type) {
114 case FILE_PAT:
115 if (fnmatch(epattern[i].pat,
116 basename(p->fts_path), 0) == 0)
117 ok = epattern[i].mode != EXCL_PAT;
118 break;
119 case DIR_PAT:
120 if (dir != NULL && strstr(dir,
121 epattern[i].pat) != NULL)
122 ok = epattern[i].mode != EXCL_PAT;
123 break;
124 }
125 }
126 free(dir);
127 dir = NULL;
128 }
129
130 if (ok)
131 c += procfile(p->fts_path);
132 break;
133 }
134 }
135
136 fts_close(fts);

--- 267 unchanged lines hidden (view full) ---

404{
405
406 if ((ptr = realloc(ptr, size)) == NULL)
407 err(2, "realloc");
408 return (ptr);
409}
410
411/*
412 * Prints a matching line according to the command line options.
413 */
414void
415printline(struct str *line, int sep, regmatch_t *matches, int m)
416{
417 size_t a = 0;
418 int i, n = 0;
419

--- 51 unchanged lines hidden ---