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>
71 #include "pathnames.h"
73 static char _path_words[] = _PATH_WORDS;
81 char *binary_search(wchar_t *, unsigned char *, unsigned char *);
82 int compare(wchar_t *, unsigned char *, unsigned char *);
83 char *linear_search(wchar_t *, unsigned char *, unsigned char *);
84 int look(wchar_t *, unsigned char *, unsigned char *);
85 wchar_t *prepkey(const char *, wchar_t);
86 void print_from(wchar_t *, unsigned char *, unsigned char *);
88 static void usage(void);
91 main(int argc, char *argv[])
96 unsigned char *back, *front;
97 unsigned const char *file;
100 (void) setlocale(LC_CTYPE, "");
104 while ((ch = getopt(argc, argv, "dft:")) != -1)
113 if (mbrtowc(&termchar, optarg, MB_LEN_MAX, NULL) !=
115 errx(2, "invalid termination character");
126 if (argc == 1) /* But set -df by default. */
128 key = prepkey(*argv++, termchar);
135 if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
137 if (sb.st_size > SIZE_T_MAX)
138 errx(2, "%s: %s", file, strerror(EFBIG));
139 if (sb.st_size == 0) {
143 if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED)
145 back = front + sb.st_size;
146 match *= (look(key, front, back));
148 } while (argc-- > 2 && (file = *argv++));
154 prepkey(const char *string, wchar_t termchar)
157 wchar_t *key, *writep;
162 * Reformat search string and convert to wide character representation
163 * to avoid doing it multiple times later.
165 if ((key = malloc(sizeof(wchar_t) * (strlen(string) + 1))) == NULL)
169 while ((clen = mbrtowc(&ch, readp, MB_LEN_MAX, NULL)) != 0) {
170 if (clen == (size_t)-1 || clen == (size_t)-2)
171 errc(2, EILSEQ, NULL);
174 if (!dflag || iswalnum(ch))
179 if (termchar != L'\0' && (writep = wcschr(key, termchar)) != NULL)
185 look(wchar_t *string, unsigned char *front, unsigned char *back)
188 front = binary_search(string, front, back);
189 front = linear_search(string, front, back);
192 print_from(string, front, back);
193 return (front ? 0 : 1);
198 * Binary search for "string" in memory between "front" and "back".
200 * This routine is expected to return a pointer to the start of a line at
201 * *or before* the first word matching "string". Relaxing the constraint
202 * this way simplifies the algorithm.
205 * front points to the beginning of a line at or before the first
208 * back points to the beginning of a line at or after the first
211 * Base of the Invariants.
215 * Advancing the Invariants:
217 * p = first newline after halfway point from front to back.
219 * If the string at "p" is not greater than the string to match,
220 * p is the new front. Otherwise it is the new back.
224 * The definition of the routine allows it return at any point,
225 * since front is always at or before the line to print.
227 * In fact, it returns when the chosen "p" equals "back". This
228 * implies that there exists a string is least half as long as
229 * (back - front), which in turn implies that a linear search will
230 * be no more expensive than the cost of simply printing a string or two.
232 * Trying to continue with binary search at this point would be
233 * more trouble than it's worth.
235 #define SKIP_PAST_NEWLINE(p, back) \
236 while (p < back && *p++ != '\n');
239 binary_search(wchar_t *string, unsigned char *front, unsigned char *back)
243 p = front + (back - front) / 2;
244 SKIP_PAST_NEWLINE(p, back);
247 * If the file changes underneath us, make sure we don't
250 while (p < back && back > front) {
251 if (compare(string, p, back) == GREATER)
255 p = front + (back - front) / 2;
256 SKIP_PAST_NEWLINE(p, back);
262 * Find the first line that starts with string, linearly searching from front
265 * Return NULL for no such line.
267 * This routine assumes:
269 * o front points at the first character in a line.
270 * o front is before or at the first line to be printed.
273 linear_search(wchar_t *string, unsigned char *front, unsigned char *back)
275 while (front < back) {
276 switch (compare(string, front, back)) {
277 case EQUAL: /* Found it. */
279 case LESS: /* No such string. */
281 case GREATER: /* Keep going. */
284 SKIP_PAST_NEWLINE(front, back);
290 * Print as many lines as match string, starting at front.
293 print_from(wchar_t *string, unsigned char *front, unsigned char *back)
295 for (; front < back && compare(string, front, back) == EQUAL; ++front) {
296 for (; front < back && *front != '\n'; ++front)
297 if (putchar(*front) == EOF)
299 if (putchar('\n') == EOF)
305 * Return LESS, GREATER, or EQUAL depending on how the string1 compares with
306 * string2 (s1 ??? s2).
308 * o Matches up to len(s1) are EQUAL.
309 * o Matches up to len(s2) are GREATER.
311 * Compare understands about the -f and -d flags, and treats comparisons
314 * The string "s1" is null terminated. The string s2 is '\n' terminated (or
315 * "back" terminated).
318 compare(wchar_t *s1, unsigned char *s2, unsigned char *back)
323 for (; *s1 && s2 < back && *s2 != '\n'; ++s1, s2 += len2) {
325 len2 = mbrtowc(&ch2, s2, back - s2, NULL);
326 if (len2 == (size_t)-1 || len2 == (size_t)-2) {
332 if (dflag && !iswalnum(ch2)) {
333 /* Ignore character in comparison. */
338 return (ch1 < ch2 ? LESS : GREATER);
340 return (*s1 ? GREATER : EQUAL);
346 (void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n");