util.c (210389) | util.c (210430) |
---|---|
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 210389 2010-07-22 19:11:57Z gabor $"); | 31__FBSDID("$FreeBSD: head/usr.bin/grep/util.c 210430 2010-07-23 19:36:11Z delphij $"); |
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> --- 15 unchanged lines hidden (view full) --- 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; | 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> --- 15 unchanged lines hidden (view full) --- 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; | 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: --- 5 unchanged lines hidden (view full) --- 77 default: 78 fts_flags = FTS_LOGICAL; 79 80 } 81 82 fts_flags |= FTS_NOSTAT | FTS_NOCHDIR; 83 84 if (!(fts = fts_open(argv, fts_flags, 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: --- 5 unchanged lines hidden (view full) --- 77 default: 78 fts_flags = FTS_LOGICAL; 79 80 } 81 82 fts_flags |= FTS_NOSTAT | FTS_NOCHDIR; 83 84 if (!(fts = fts_open(argv, fts_flags, NULL))) |
85 err(2, NULL); | 85 err(2, "fts_open"); |
86 while ((p = fts_read(fts)) != NULL) { 87 switch (p->fts_info) { 88 case FTS_DNR: 89 /* FALLTHROUGH */ 90 case FTS_ERR: 91 errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno)); 92 break; 93 case FTS_D: --- 4 unchanged lines hidden (view full) --- 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) { | 86 while ((p = fts_read(fts)) != NULL) { 87 switch (p->fts_info) { 88 case FTS_DNR: 89 /* FALLTHROUGH */ 90 case FTS_ERR: 91 errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno)); 92 break; 93 case FTS_D: --- 4 unchanged lines hidden (view full) --- 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 d = strrchr(p->fts_path, '/'); 107 dir = grep_malloc(sizeof(char) * 108 (d - p->fts_path + 2)); 109 strlcpy(dir, p->fts_path, 110 (d - p->fts_path + 1)); | 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 } |
111 for (i = 0; i < epatterns; ++i) { 112 switch(epattern[i].type) { 113 case FILE_PAT: 114 if (fnmatch(epattern[i].pat, 115 basename(p->fts_path), 0) == 0) 116 ok = epattern[i].mode != EXCL_PAT; 117 break; 118 case DIR_PAT: | 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: |
119 if (strstr(dir, | 120 if (dir != NULL && strstr(dir, |
120 epattern[i].pat) != NULL) 121 ok = epattern[i].mode != EXCL_PAT; 122 break; 123 } 124 } | 121 epattern[i].pat) != NULL) 122 ok = epattern[i].mode != EXCL_PAT; 123 break; 124 } 125 } |
125 free(dir); | 126 free(dir); 127 dir = NULL; |
126 } 127 128 if (ok) 129 c += procfile(p->fts_path); 130 break; 131 } 132 } 133 | 128 } 129 130 if (ok) 131 c += procfile(p->fts_path); 132 break; 133 } 134 } 135 |
136 fts_close(fts); |
|
134 return (c); 135} 136 137/* 138 * Opens a file and processes it. Each file is processed line-by-line 139 * passing the lines to procline(). 140 */ 141int --- 49 unchanged lines hidden (view full) --- 191 } 192 if (ln.len > 0 && ln.dat[ln.len - 1] == '\n') 193 --ln.len; 194 ln.line_no++; 195 196 /* Return if we need to skip a binary file */ 197 if (f->binary && binbehave == BINFILE_SKIP) { 198 grep_close(f); | 137 return (c); 138} 139 140/* 141 * Opens a file and processes it. Each file is processed line-by-line 142 * passing the lines to procline(). 143 */ 144int --- 49 unchanged lines hidden (view full) --- 194 } 195 if (ln.len > 0 && ln.dat[ln.len - 1] == '\n') 196 --ln.len; 197 ln.line_no++; 198 199 /* Return if we need to skip a binary file */ 200 if (f->binary && binbehave == BINFILE_SKIP) { 201 grep_close(f); |
202 free(ln.file); |
|
199 free(f); 200 return (0); 201 } 202 /* Process the file line-by-line */ 203 if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) { 204 enqueue(&ln); 205 linesqueued++; 206 } --- 18 unchanged lines hidden (view full) --- 225 if (lflag && c != 0) 226 printf("%s\n", fn); 227 if (Lflag && c == 0) 228 printf("%s\n", fn); 229 if (c && !cflag && !lflag && !Lflag && 230 binbehave == BINFILE_BIN && f->binary && !qflag) 231 printf(getstr(9), fn); 232 | 203 free(f); 204 return (0); 205 } 206 /* Process the file line-by-line */ 207 if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) { 208 enqueue(&ln); 209 linesqueued++; 210 } --- 18 unchanged lines hidden (view full) --- 229 if (lflag && c != 0) 230 printf("%s\n", fn); 231 if (Lflag && c == 0) 232 printf("%s\n", fn); 233 if (c && !cflag && !lflag && !Lflag && 234 binbehave == BINFILE_BIN && f->binary && !qflag) 235 printf(getstr(9), fn); 236 |
237 free(ln.file); |
|
233 free(f); 234 return (c); 235} 236 237#define iswword(x) (iswalnum((x)) || (x) == L'_') 238 239/* 240 * Processes a line comparing it with the specified patterns. Each pattern --- 224 unchanged lines hidden --- | 238 free(f); 239 return (c); 240} 241 242#define iswword(x) (iswalnum((x)) || (x) == L'_') 243 244/* 245 * Processes a line comparing it with the specified patterns. Each pattern --- 224 unchanged lines hidden --- |