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