2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * David Hitz of Auspex Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1991, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * look -- find lines in a sorted list.
50 * The man page said that TABs and SPACEs participate in -d comparisons.
51 * In fact, they were ignored. This implements historic practice, not
55 #include <sys/types.h>
72 #include "pathnames.h"
74 static char _path_words[] = _PATH_WORDS;
80 static int dflag, fflag;
82 static char *binary_search(wchar_t *, unsigned char *, unsigned char *);
83 static int compare(wchar_t *, unsigned char *, unsigned char *);
84 static char *linear_search(wchar_t *, unsigned char *, unsigned char *);
85 static int look(wchar_t *, unsigned char *, unsigned char *);
86 static wchar_t *prepkey(const char *, wchar_t);
87 static void print_from(wchar_t *, unsigned char *, unsigned char *);
89 static void usage(void);
92 main(int argc, char *argv[])
97 unsigned char *back, *front;
98 unsigned const char *file;
101 (void) setlocale(LC_CTYPE, "");
105 while ((ch = getopt(argc, argv, "dft:")) != -1)
114 if (mbrtowc(&termchar, optarg, MB_LEN_MAX, NULL) !=
116 errx(2, "invalid termination character");
127 if (argc == 1) /* But set -df by default. */
129 key = prepkey(*argv++, termchar);
136 if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
138 if ((uintmax_t)sb.st_size > (uintmax_t)SIZE_T_MAX)
139 errx(2, "%s: %s", file, strerror(EFBIG));
140 if (sb.st_size == 0) {
144 if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED)
146 back = front + sb.st_size;
147 match *= (look(key, front, back));
149 } while (argc-- > 2 && (file = *argv++));
155 prepkey(const char *string, wchar_t termchar)
158 wchar_t *key, *writep;
163 * Reformat search string and convert to wide character representation
164 * to avoid doing it multiple times later.
166 if ((key = malloc(sizeof(wchar_t) * (strlen(string) + 1))) == NULL)
170 while ((clen = mbrtowc(&ch, readp, MB_LEN_MAX, NULL)) != 0) {
171 if (clen == (size_t)-1 || clen == (size_t)-2)
172 errc(2, EILSEQ, NULL);
175 if (!dflag || iswalnum(ch))
180 if (termchar != L'\0' && (writep = wcschr(key, termchar)) != NULL)
186 look(wchar_t *string, unsigned char *front, unsigned char *back)
189 front = binary_search(string, front, back);
190 front = linear_search(string, front, back);
193 print_from(string, front, back);
194 return (front ? 0 : 1);
199 * Binary search for "string" in memory between "front" and "back".
201 * This routine is expected to return a pointer to the start of a line at
202 * *or before* the first word matching "string". Relaxing the constraint
203 * this way simplifies the algorithm.
206 * front points to the beginning of a line at or before the first
209 * back points to the beginning of a line at or after the first
212 * Base of the Invariants.
216 * Advancing the Invariants:
218 * p = first newline after halfway point from front to back.
220 * If the string at "p" is not greater than the string to match,
221 * p is the new front. Otherwise it is the new back.
225 * The definition of the routine allows it return at any point,
226 * since front is always at or before the line to print.
228 * In fact, it returns when the chosen "p" equals "back". This
229 * implies that there exists a string is least half as long as
230 * (back - front), which in turn implies that a linear search will
231 * be no more expensive than the cost of simply printing a string or two.
233 * Trying to continue with binary search at this point would be
234 * more trouble than it's worth.
236 #define SKIP_PAST_NEWLINE(p, back) \
237 while (p < back && *p++ != '\n');
240 binary_search(wchar_t *string, unsigned char *front, unsigned char *back)
244 p = front + (back - front) / 2;
245 SKIP_PAST_NEWLINE(p, back);
248 * If the file changes underneath us, make sure we don't
251 while (p < back && back > front) {
252 if (compare(string, p, back) == GREATER)
256 p = front + (back - front) / 2;
257 SKIP_PAST_NEWLINE(p, back);
263 * Find the first line that starts with string, linearly searching from front
266 * Return NULL for no such line.
268 * This routine assumes:
270 * o front points at the first character in a line.
271 * o front is before or at the first line to be printed.
274 linear_search(wchar_t *string, unsigned char *front, unsigned char *back)
276 while (front < back) {
277 switch (compare(string, front, back)) {
278 case EQUAL: /* Found it. */
280 case LESS: /* No such string. */
282 case GREATER: /* Keep going. */
285 SKIP_PAST_NEWLINE(front, back);
291 * Print as many lines as match string, starting at front.
294 print_from(wchar_t *string, unsigned char *front, unsigned char *back)
296 for (; front < back && compare(string, front, back) == EQUAL; ++front) {
297 for (; front < back && *front != '\n'; ++front)
298 if (putchar(*front) == EOF)
300 if (putchar('\n') == EOF)
306 * Return LESS, GREATER, or EQUAL depending on how the string1 compares with
307 * string2 (s1 ??? s2).
309 * o Matches up to len(s1) are EQUAL.
310 * o Matches up to len(s2) are GREATER.
312 * Compare understands about the -f and -d flags, and treats comparisons
315 * The string "s1" is null terminated. The string s2 is '\n' terminated (or
316 * "back" terminated).
319 compare(wchar_t *s1, unsigned char *s2, unsigned char *back)
324 for (; *s1 && s2 < back && *s2 != '\n'; ++s1, s2 += len2) {
326 len2 = mbrtowc(&ch2, s2, back - s2, NULL);
327 if (len2 == (size_t)-1 || len2 == (size_t)-2) {
333 if (dflag && !iswalnum(ch2)) {
334 /* Ignore character in comparison. */
339 return (ch1 < ch2 ? LESS : GREATER);
341 return (*s1 ? GREATER : EQUAL);
347 (void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n");