util.c revision 1.1 1 1.1 cjep /*-
2 1.1 cjep * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
3 1.1 cjep * All rights reserved.
4 1.1 cjep *
5 1.1 cjep * Redistribution and use in source and binary forms, with or without
6 1.1 cjep * modification, are permitted provided that the following conditions
7 1.1 cjep * are met:
8 1.1 cjep * 1. Redistributions of source code must retain the above copyright
9 1.1 cjep * notice, this list of conditions and the following disclaimer.
10 1.1 cjep * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cjep * notice, this list of conditions and the following disclaimer in the
12 1.1 cjep * documentation and/or other materials provided with the distribution.
13 1.1 cjep *
14 1.1 cjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 1.1 cjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 1.1 cjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 1.1 cjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 1.1 cjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 1.1 cjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 1.1 cjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 1.1 cjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 1.1 cjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 1.1 cjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 1.1 cjep * SUCH DAMAGE.
25 1.1 cjep *
26 1.1 cjep * $Id: util.c,v 1.1 2004/01/02 14:58:43 cjep Exp $
27 1.1 cjep */
28 1.1 cjep
29 1.1 cjep #include <sys/types.h>
30 1.1 cjep #include <sys/stat.h>
31 1.1 cjep
32 1.1 cjep #include <ctype.h>
33 1.1 cjep #include <err.h>
34 1.1 cjep #include <errno.h>
35 1.1 cjep #include <fts.h>
36 1.1 cjep #include <regex.h>
37 1.1 cjep #include <stdio.h>
38 1.1 cjep #include <stdlib.h>
39 1.1 cjep #include <string.h>
40 1.1 cjep #include <unistd.h>
41 1.1 cjep #include <zlib.h>
42 1.1 cjep
43 1.1 cjep #include "grep.h"
44 1.1 cjep
45 1.1 cjep /*
46 1.1 cjep * Process a file line by line...
47 1.1 cjep */
48 1.1 cjep
49 1.1 cjep static int linesqueued;
50 1.1 cjep static int procline(str_t *l);
51 1.1 cjep
52 1.1 cjep int
53 1.1 cjep grep_tree(char **argv)
54 1.1 cjep {
55 1.1 cjep FTS *fts;
56 1.1 cjep FTSENT *p;
57 1.1 cjep int c, fts_flags;
58 1.1 cjep
59 1.1 cjep c = fts_flags = 0;
60 1.1 cjep
61 1.1 cjep if (Hflag)
62 1.1 cjep fts_flags = FTS_COMFOLLOW;
63 1.1 cjep if (Pflag)
64 1.1 cjep fts_flags = FTS_PHYSICAL;
65 1.1 cjep if (Sflag)
66 1.1 cjep fts_flags = FTS_LOGICAL;
67 1.1 cjep
68 1.1 cjep fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
69 1.1 cjep
70 1.1 cjep if (!(fts = fts_open(argv, fts_flags, (int (*) ()) NULL)))
71 1.1 cjep err(1, NULL);
72 1.1 cjep while ((p = fts_read(fts)) != NULL) {
73 1.1 cjep switch (p->fts_info) {
74 1.1 cjep case FTS_DNR:
75 1.1 cjep break;
76 1.1 cjep case FTS_ERR:
77 1.1 cjep errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
78 1.1 cjep break;
79 1.1 cjep case FTS_DP:
80 1.1 cjep break;
81 1.1 cjep default:
82 1.1 cjep c += procfile(p->fts_path);
83 1.1 cjep break;
84 1.1 cjep }
85 1.1 cjep }
86 1.1 cjep
87 1.1 cjep return c;
88 1.1 cjep }
89 1.1 cjep
90 1.1 cjep int
91 1.1 cjep procfile(char *fn)
92 1.1 cjep {
93 1.1 cjep str_t ln;
94 1.1 cjep file_t *f;
95 1.1 cjep int c, t, z;
96 1.1 cjep
97 1.1 cjep if (fn == NULL) {
98 1.1 cjep fn = "(standard input)";
99 1.1 cjep f = grep_fdopen(STDIN_FILENO, "r");
100 1.1 cjep } else {
101 1.1 cjep f = grep_open(fn, "r");
102 1.1 cjep }
103 1.1 cjep if (f == NULL) {
104 1.1 cjep if (!sflag)
105 1.1 cjep warn("%s", fn);
106 1.1 cjep return 0;
107 1.1 cjep }
108 1.1 cjep if (aflag && grep_bin_file(f)) {
109 1.1 cjep grep_close(f);
110 1.1 cjep return 0;
111 1.1 cjep }
112 1.1 cjep
113 1.1 cjep ln.file = fn;
114 1.1 cjep ln.line_no = 0;
115 1.1 cjep linesqueued = 0;
116 1.1 cjep ln.off = -1;
117 1.1 cjep
118 1.1 cjep if (Bflag > 0)
119 1.1 cjep initqueue();
120 1.1 cjep for (c = 0; !(lflag && c);) {
121 1.1 cjep ln.off += ln.len + 1;
122 1.1 cjep if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
123 1.1 cjep break;
124 1.1 cjep if (ln.len > 0 && ln.dat[ln.len - 1] == '\n')
125 1.1 cjep --ln.len;
126 1.1 cjep ln.line_no++;
127 1.1 cjep
128 1.1 cjep z = tail;
129 1.1 cjep
130 1.1 cjep if ((t = procline(&ln)) == 0 && Bflag > 0 && z == 0) {
131 1.1 cjep enqueue(&ln);
132 1.1 cjep linesqueued++;
133 1.1 cjep }
134 1.1 cjep c += t;
135 1.1 cjep }
136 1.1 cjep if (Bflag > 0)
137 1.1 cjep clearqueue();
138 1.1 cjep grep_close(f);
139 1.1 cjep
140 1.1 cjep if (cflag) {
141 1.1 cjep if (!hflag)
142 1.1 cjep printf("%s:", ln.file);
143 1.1 cjep printf("%u\n", c);
144 1.1 cjep }
145 1.1 cjep if (lflag && c != 0)
146 1.1 cjep printf("%s\n", fn);
147 1.1 cjep if (Lflag && c == 0)
148 1.1 cjep printf("%s\n", fn);
149 1.1 cjep return c;
150 1.1 cjep }
151 1.1 cjep
152 1.1 cjep
153 1.1 cjep /*
154 1.1 cjep * Process an individual line in a file. Return non-zero if it matches.
155 1.1 cjep */
156 1.1 cjep
157 1.1 cjep #define isword(x) (isalnum(x) || (x) == '_')
158 1.1 cjep
159 1.1 cjep static int
160 1.1 cjep procline(str_t *l)
161 1.1 cjep {
162 1.1 cjep regmatch_t pmatch;
163 1.1 cjep int c, i, r, t;
164 1.1 cjep
165 1.1 cjep if (matchall) {
166 1.1 cjep c = !vflag;
167 1.1 cjep goto print;
168 1.1 cjep }
169 1.1 cjep
170 1.1 cjep t = vflag ? REG_NOMATCH : 0;
171 1.1 cjep pmatch.rm_so = 0;
172 1.1 cjep pmatch.rm_eo = l->len;
173 1.1 cjep for (c = i = 0; i < patterns; i++) {
174 1.1 cjep r = regexec(&r_pattern[i], l->dat, 0, &pmatch, eflags);
175 1.1 cjep if (r == REG_NOMATCH && t == 0)
176 1.1 cjep continue;
177 1.1 cjep if (r == 0) {
178 1.1 cjep if (wflag) {
179 1.1 cjep if ((pmatch.rm_so != 0 && isword(l->dat[pmatch.rm_so - 1]))
180 1.1 cjep || (pmatch.rm_eo != l->len && isword(l->dat[pmatch.rm_eo])))
181 1.1 cjep r = REG_NOMATCH;
182 1.1 cjep }
183 1.1 cjep if (xflag) {
184 1.1 cjep if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
185 1.1 cjep r = REG_NOMATCH;
186 1.1 cjep }
187 1.1 cjep }
188 1.1 cjep if (r == t) {
189 1.1 cjep c++;
190 1.1 cjep break;
191 1.1 cjep }
192 1.1 cjep }
193 1.1 cjep
194 1.1 cjep print:
195 1.1 cjep if ((tail > 0 || c) && !cflag && !qflag) {
196 1.1 cjep if (c) {
197 1.1 cjep if (first > 0 && tail == 0 && (Bflag < linesqueued) && (Aflag || Bflag))
198 1.1 cjep printf("--\n");
199 1.1 cjep first = 1;
200 1.1 cjep tail = Aflag;
201 1.1 cjep if (Bflag > 0)
202 1.1 cjep printqueue();
203 1.1 cjep linesqueued = 0;
204 1.1 cjep printline(l, ':');
205 1.1 cjep } else {
206 1.1 cjep printline(l, '-');
207 1.1 cjep tail--;
208 1.1 cjep }
209 1.1 cjep }
210 1.1 cjep return c;
211 1.1 cjep }
212 1.1 cjep
213 1.1 cjep void *
214 1.1 cjep grep_malloc(size_t size)
215 1.1 cjep {
216 1.1 cjep void *ptr;
217 1.1 cjep
218 1.1 cjep if ((ptr = malloc(size)) == NULL)
219 1.1 cjep err(1, "malloc");
220 1.1 cjep return ptr;
221 1.1 cjep }
222 1.1 cjep
223 1.1 cjep void *
224 1.1 cjep grep_realloc(void *ptr, size_t size)
225 1.1 cjep {
226 1.1 cjep if ((ptr = realloc(ptr, size)) == NULL)
227 1.1 cjep err(1, "realloc");
228 1.1 cjep return ptr;
229 1.1 cjep }
230 1.1 cjep
231 1.1 cjep void
232 1.1 cjep printline(str_t *line, int sep)
233 1.1 cjep {
234 1.1 cjep int n;
235 1.1 cjep
236 1.1 cjep n = 0;
237 1.1 cjep if (!hflag) {
238 1.1 cjep fputs(line->file, stdout);
239 1.1 cjep ++n;
240 1.1 cjep }
241 1.1 cjep if (nflag) {
242 1.1 cjep if (n)
243 1.1 cjep putchar(sep);
244 1.1 cjep printf("%d", line->line_no);
245 1.1 cjep ++n;
246 1.1 cjep }
247 1.1 cjep if (bflag) {
248 1.1 cjep if (n)
249 1.1 cjep putchar(sep);
250 1.1 cjep printf("%lu", (unsigned long)line->off);
251 1.1 cjep }
252 1.1 cjep if (n)
253 1.1 cjep putchar(sep);
254 1.1 cjep fwrite(line->dat, line->len, 1, stdout);
255 1.1 cjep putchar('\n');
256 1.1 cjep }
257