1 | /* $NetBSD: look.c,v 1.7 1995/08/31 22:41:02 jtc Exp $ */ |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1991, 1993 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * This code is derived from software contributed to Berkeley by |
---|
8 | * David Hitz of Auspex Systems, Inc. |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * 3. All advertising materials mentioning features or use of this software |
---|
19 | * must display the following acknowledgement: |
---|
20 | * This product includes software developed by the University of |
---|
21 | * California, Berkeley and its contributors. |
---|
22 | * 4. Neither the name of the University nor the names of its contributors |
---|
23 | * may be used to endorse or promote products derived from this software |
---|
24 | * without specific prior written permission. |
---|
25 | * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
36 | * SUCH DAMAGE. |
---|
37 | */ |
---|
38 | |
---|
39 | #ifndef lint |
---|
40 | static char copyright[] = |
---|
41 | "@(#) Copyright (c) 1991, 1993\n\ |
---|
42 | The Regents of the University of California. All rights reserved.\n"; |
---|
43 | #endif /* not lint */ |
---|
44 | |
---|
45 | #ifndef lint |
---|
46 | #if 0 |
---|
47 | static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95"; |
---|
48 | #endif |
---|
49 | static char rcsid[] = "$NetBSD: look.c,v 1.7 1995/08/31 22:41:02 jtc Exp $"; |
---|
50 | #endif /* not lint */ |
---|
51 | |
---|
52 | /* |
---|
53 | * look -- find lines in a sorted list. |
---|
54 | * |
---|
55 | * The man page said that TABs and SPACEs participate in -d comparisons. |
---|
56 | * In fact, they were ignored. This implements historic practice, not |
---|
57 | * the manual page. |
---|
58 | */ |
---|
59 | |
---|
60 | #include <sys/types.h> |
---|
61 | #include <sys/mman.h> |
---|
62 | #include <sys/stat.h> |
---|
63 | |
---|
64 | #include <ctype.h> |
---|
65 | #include <errno.h> |
---|
66 | #include <fcntl.h> |
---|
67 | #include <limits.h> |
---|
68 | #include <stdio.h> |
---|
69 | #include <stdlib.h> |
---|
70 | #include <string.h> |
---|
71 | #include <unistd.h> |
---|
72 | |
---|
73 | #include "pathnames.h" |
---|
74 | |
---|
75 | /* |
---|
76 | * FOLD and DICT convert characters to a normal form for comparison, |
---|
77 | * according to the user specified flags. |
---|
78 | * |
---|
79 | * DICT expects integers because it uses a non-character value to |
---|
80 | * indicate a character which should not participate in comparisons. |
---|
81 | */ |
---|
82 | #define EQUAL 0 |
---|
83 | #define GREATER 1 |
---|
84 | #define LESS (-1) |
---|
85 | #define NO_COMPARE (-2) |
---|
86 | |
---|
87 | #define FOLD(c) (isascii(c) && isupper(c) ? tolower(c) : (c)) |
---|
88 | #define DICT(c) (isascii(c) && isalnum(c) ? (c) : NO_COMPARE) |
---|
89 | |
---|
90 | int dflag, fflag; |
---|
91 | |
---|
92 | char *binary_search(char *, char *, char *); |
---|
93 | int compare(char *, char *, char *); |
---|
94 | char *linear_search(char *, char *, char *); |
---|
95 | int look(char *, char *, char *); |
---|
96 | void print_from(char *, char *, char *); |
---|
97 | void usage(void); |
---|
98 | |
---|
99 | int |
---|
100 | main(argc, argv) |
---|
101 | int argc; |
---|
102 | char *argv[]; |
---|
103 | { |
---|
104 | struct stat sb; |
---|
105 | int ch, fd, termchar; |
---|
106 | char *back, *file, *front, *string, *p; |
---|
107 | |
---|
108 | file = _PATH_WORDS; |
---|
109 | termchar = '\0'; |
---|
110 | while ((ch = getopt(argc, argv, "dft:")) != EOF) |
---|
111 | switch(ch) { |
---|
112 | case 'd': |
---|
113 | dflag = 1; |
---|
114 | break; |
---|
115 | case 'f': |
---|
116 | fflag = 1; |
---|
117 | break; |
---|
118 | case 't': |
---|
119 | termchar = *optarg; |
---|
120 | break; |
---|
121 | case '?': |
---|
122 | default: |
---|
123 | usage(); |
---|
124 | } |
---|
125 | argc -= optind; |
---|
126 | argv += optind; |
---|
127 | |
---|
128 | switch (argc) { |
---|
129 | case 2: /* Don't set -df for user. */ |
---|
130 | string = *argv++; |
---|
131 | file = *argv; |
---|
132 | break; |
---|
133 | case 1: /* But set -df by default. */ |
---|
134 | dflag = fflag = 1; |
---|
135 | string = *argv; |
---|
136 | break; |
---|
137 | default: |
---|
138 | usage(); |
---|
139 | } |
---|
140 | |
---|
141 | if (termchar != '\0' && (p = strchr(string, termchar)) != NULL) |
---|
142 | *++p = '\0'; |
---|
143 | |
---|
144 | if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) { |
---|
145 | fprintf(stderr, "look: %s: %s\n", file, strerror(errno)); |
---|
146 | exit(2); |
---|
147 | } |
---|
148 | if ((front = mmap(NULL, |
---|
149 | (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, |
---|
150 | (off_t)0)) == NULL || front == (void *) -1) { |
---|
151 | fprintf(stderr, "look: %s: %s\n", file, strerror(errno)); |
---|
152 | exit(2); |
---|
153 | } |
---|
154 | back = front + sb.st_size; |
---|
155 | exit(look(string, front, back)); |
---|
156 | } |
---|
157 | |
---|
158 | int |
---|
159 | look(string, front, back) |
---|
160 | char *string, *front, *back; |
---|
161 | { |
---|
162 | register int ch; |
---|
163 | register char *readp, *writep; |
---|
164 | |
---|
165 | /* Reformat string string to avoid doing it multiple times later. */ |
---|
166 | for (readp = writep = string; ch = *readp++;) { |
---|
167 | if (fflag) |
---|
168 | ch = FOLD(ch); |
---|
169 | if (dflag) |
---|
170 | ch = DICT(ch); |
---|
171 | if (ch != NO_COMPARE) |
---|
172 | *(writep++) = ch; |
---|
173 | } |
---|
174 | *writep = '\0'; |
---|
175 | |
---|
176 | front = binary_search(string, front, back); |
---|
177 | front = linear_search(string, front, back); |
---|
178 | |
---|
179 | if (front) |
---|
180 | print_from(string, front, back); |
---|
181 | return (front ? 0 : 1); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | /* |
---|
186 | * Binary search for "string" in memory between "front" and "back". |
---|
187 | * |
---|
188 | * This routine is expected to return a pointer to the start of a line at |
---|
189 | * *or before* the first word matching "string". Relaxing the constraint |
---|
190 | * this way simplifies the algorithm. |
---|
191 | * |
---|
192 | * Invariants: |
---|
193 | * front points to the beginning of a line at or before the first |
---|
194 | * matching string. |
---|
195 | * |
---|
196 | * back points to the beginning of a line at or after the first |
---|
197 | * matching line. |
---|
198 | * |
---|
199 | * Base of the Invariants. |
---|
200 | * front = NULL; |
---|
201 | * back = EOF; |
---|
202 | * |
---|
203 | * Advancing the Invariants: |
---|
204 | * |
---|
205 | * p = first newline after halfway point from front to back. |
---|
206 | * |
---|
207 | * If the string at "p" is not greater than the string to match, |
---|
208 | * p is the new front. Otherwise it is the new back. |
---|
209 | * |
---|
210 | * Termination: |
---|
211 | * |
---|
212 | * The definition of the routine allows it return at any point, |
---|
213 | * since front is always at or before the line to print. |
---|
214 | * |
---|
215 | * In fact, it returns when the chosen "p" equals "back". This |
---|
216 | * implies that there exists a string is least half as long as |
---|
217 | * (back - front), which in turn implies that a linear search will |
---|
218 | * be no more expensive than the cost of simply printing a string or two. |
---|
219 | * |
---|
220 | * Trying to continue with binary search at this point would be |
---|
221 | * more trouble than it's worth. |
---|
222 | */ |
---|
223 | #define SKIP_PAST_NEWLINE(p, back) \ |
---|
224 | while (p < back && *p++ != '\n'); |
---|
225 | |
---|
226 | char * |
---|
227 | binary_search(string, front, back) |
---|
228 | register char *string, *front, *back; |
---|
229 | { |
---|
230 | register char *p; |
---|
231 | |
---|
232 | p = front + (back - front) / 2; |
---|
233 | SKIP_PAST_NEWLINE(p, back); |
---|
234 | |
---|
235 | /* |
---|
236 | * If the file changes underneath us, make sure we don't |
---|
237 | * infinitely loop. |
---|
238 | */ |
---|
239 | while (p < back && back > front) { |
---|
240 | if (compare(string, p, back) == GREATER) |
---|
241 | front = p; |
---|
242 | else |
---|
243 | back = p; |
---|
244 | p = front + (back - front) / 2; |
---|
245 | SKIP_PAST_NEWLINE(p, back); |
---|
246 | } |
---|
247 | return (front); |
---|
248 | } |
---|
249 | |
---|
250 | /* |
---|
251 | * Find the first line that starts with string, linearly searching from front |
---|
252 | * to back. |
---|
253 | * |
---|
254 | * Return NULL for no such line. |
---|
255 | * |
---|
256 | * This routine assumes: |
---|
257 | * |
---|
258 | * o front points at the first character in a line. |
---|
259 | * o front is before or at the first line to be printed. |
---|
260 | */ |
---|
261 | char * |
---|
262 | linear_search(string, front, back) |
---|
263 | char *string, *front, *back; |
---|
264 | { |
---|
265 | while (front < back) { |
---|
266 | switch (compare(string, front, back)) { |
---|
267 | case EQUAL: /* Found it. */ |
---|
268 | return (front); |
---|
269 | break; |
---|
270 | case LESS: /* No such string. */ |
---|
271 | return (NULL); |
---|
272 | break; |
---|
273 | case GREATER: /* Keep going. */ |
---|
274 | break; |
---|
275 | } |
---|
276 | SKIP_PAST_NEWLINE(front, back); |
---|
277 | } |
---|
278 | return (NULL); |
---|
279 | } |
---|
280 | |
---|
281 | /* |
---|
282 | * Print as many lines as match string, starting at front. |
---|
283 | */ |
---|
284 | void |
---|
285 | print_from(string, front, back) |
---|
286 | register char *string, *front, *back; |
---|
287 | { |
---|
288 | for (; front < back && compare(string, front, back) == EQUAL; ++front) { |
---|
289 | for (; front < back && *front != '\n'; ++front) |
---|
290 | if (putchar(*front) == EOF) { |
---|
291 | perror("look: stdout"); |
---|
292 | exit(2); |
---|
293 | } |
---|
294 | if (putchar('\n') == EOF) { |
---|
295 | perror("look: stdout"); |
---|
296 | exit(2); |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | /* |
---|
302 | * Return LESS, GREATER, or EQUAL depending on how the string1 compares with |
---|
303 | * string2 (s1 ??? s2). |
---|
304 | * |
---|
305 | * o Matches up to len(s1) are EQUAL. |
---|
306 | * o Matches up to len(s2) are GREATER. |
---|
307 | * |
---|
308 | * Compare understands about the -f and -d flags, and treats comparisons |
---|
309 | * appropriately. |
---|
310 | * |
---|
311 | * The string "s1" is null terminated. The string s2 is '\n' terminated (or |
---|
312 | * "back" terminated). |
---|
313 | */ |
---|
314 | int |
---|
315 | compare(s1, s2, back) |
---|
316 | register char *s1, *s2, *back; |
---|
317 | { |
---|
318 | register int ch; |
---|
319 | |
---|
320 | for (; *s1 && s2 < back && *s2 != '\n'; ++s1, ++s2) { |
---|
321 | ch = *s2; |
---|
322 | if (fflag) |
---|
323 | ch = FOLD(ch); |
---|
324 | if (dflag) |
---|
325 | ch = DICT(ch); |
---|
326 | |
---|
327 | if (ch == NO_COMPARE) { |
---|
328 | ++s2; /* Ignore character in comparison. */ |
---|
329 | continue; |
---|
330 | } |
---|
331 | if (*s1 != ch) |
---|
332 | return (*s1 < ch ? LESS : GREATER); |
---|
333 | } |
---|
334 | return (*s1 ? GREATER : EQUAL); |
---|
335 | } |
---|
336 | |
---|
337 | void |
---|
338 | usage() |
---|
339 | { |
---|
340 | (void)fprintf(stderr, "usage: look [-df] [-t char] string [file]\n"); |
---|
341 | exit(2); |
---|
342 | } |
---|