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 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> |
42#include <stdbool.h> |
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 |
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; |
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; |
144 if (dexclude || dinclude) { |
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 } |
151 ok = dir_matching(dir); |
152 free(dir); 153 dir = NULL; 154 } |
155 if (fexclude || finclude) 156 ok &= file_matching(p->fts_path); |
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/* |
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 --- |