Deleted Added
full compact
util.c (210479) util.c (210578)
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>
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 $");
31__FBSDID("$FreeBSD: head/usr.bin/grep/util.c 210578 2010-07-29 00:11:14Z 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>
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 <stdbool.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
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47#include <wchar.h>
48#include <wctype.h>
49
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}
74
75bool
76dir_matching(const char *dname)
77{
78 bool ret;
79
80 ret = dinclude ? false : true;
81
82 for (unsigned int i = 0; i < dpatterns; ++i) {
83 if (dname != NULL &&
84 fnmatch(dname, dpattern[i].pat, 0) == 0) {
85 if (dpattern[i].mode == EXCL_PAT)
86 return (false);
87 else
88 ret = true;
89 }
90 }
91 return (ret);
92}
93
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;
94/*
95 * Processes a directory when a recursive search is performed with
96 * the -R option. Each appropriate file is passed to procfile().
97 */
98int
99grep_tree(char **argv)
100{
101 FTS *fts;
102 FTSENT *p;
103 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;
104 int c, fts_flags;
105 bool ok;
106
107 c = fts_flags = 0;
108
109 switch(linkbehave) {
110 case LINK_EXPLICIT:
111 fts_flags = FTS_COMFOLLOW;

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

136 case FTS_DC:
137 /* Print a warning for recursive directory loop */
138 warnx("warning: %s: recursive directory loop",
139 p->fts_path);
140 break;
141 default:
142 /* Check for file exclusion/inclusion */
143 ok = true;
105 if (exclflag) {
144 if (dexclude || dinclude) {
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 }
145 if ((d = strrchr(p->fts_path, '/')) != NULL) {
146 dir = grep_malloc(sizeof(char) *
147 (d - p->fts_path + 2));
148 strlcpy(dir, p->fts_path,
149 (d - p->fts_path + 1));
150 }
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 }
151 ok = dir_matching(dir);
126 free(dir);
127 dir = NULL;
128 }
152 free(dir);
153 dir = NULL;
154 }
155 if (fexclude || finclude)
156 ok &= file_matching(p->fts_path);
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/*
157
158 if (ok)
159 c += procfile(p->fts_path);
160 break;
161 }
162 }
163
164 fts_close(fts);

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

432{
433
434 if ((ptr = realloc(ptr, size)) == NULL)
435 err(2, "realloc");
436 return (ptr);
437}
438
439/*
440 * Safe strdup() for internal use.
441 */
442char *
443grep_strdup(const char *str)
444{
445 char *ret;
446
447 if ((ret = strdup(str)) == NULL)
448 err(2, "strdup");
449 return (ret);
450}
451
452/*
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 ---
453 * Prints a matching line according to the command line options.
454 */
455void
456printline(struct str *line, int sep, regmatch_t *matches, int m)
457{
458 size_t a = 0;
459 int i, n = 0;
460

--- 51 unchanged lines hidden ---