2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * David Hitz of Auspex Systems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1991, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * look -- find lines in a sorted list.
52 * The man page said that TABs and SPACEs participate in -d comparisons.
53 * In fact, they were ignored. This implements historic practice, not
57 #include <sys/types.h>
75 #include "pathnames.h"
77 static char _path_words[] = _PATH_WORDS;
83 static int dflag, fflag;
85 static char *binary_search(wchar_t *, unsigned char *, unsigned char *);
86 static int compare(wchar_t *, unsigned char *, unsigned char *);
87 static char *linear_search(wchar_t *, unsigned char *, unsigned char *);
88 static int look(wchar_t *, unsigned char *, unsigned char *);
89 static wchar_t *prepkey(const char *, wchar_t);
90 static void print_from(wchar_t *, unsigned char *, unsigned char *);
92 static void usage(void);
94 static struct option longopts[] = {
95 { "alternative",no_argument, NULL, 'a' },
96 { "alphanum", no_argument, NULL, 'd' },
97 { "ignore-case",no_argument, NULL, 'i' },
98 { "terminate", required_argument, NULL, 't'},
103 main(int argc, char *argv[])
108 unsigned char *back, *front;
109 unsigned const char *file;
112 (void) setlocale(LC_CTYPE, "");
116 while ((ch = getopt_long(argc, argv, "+adft:", longopts, NULL)) != -1)
128 if (mbrtowc(&termchar, optarg, MB_LEN_MAX, NULL) !=
130 errx(2, "invalid termination character");
141 if (argc == 1) /* But set -df by default. */
143 key = prepkey(*argv++, termchar);
150 if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
152 if ((uintmax_t)sb.st_size > (uintmax_t)SIZE_T_MAX)
153 errx(2, "%s: %s", file, strerror(EFBIG));
154 if (sb.st_size == 0) {
158 if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED)
160 back = front + sb.st_size;
161 match *= (look(key, front, back));
163 } while (argc-- > 2 && (file = *argv++));
169 prepkey(const char *string, wchar_t termchar)
172 wchar_t *key, *writep;
177 * Reformat search string and convert to wide character representation
178 * to avoid doing it multiple times later.
180 if ((key = malloc(sizeof(wchar_t) * (strlen(string) + 1))) == NULL)
184 while ((clen = mbrtowc(&ch, readp, MB_LEN_MAX, NULL)) != 0) {
185 if (clen == (size_t)-1 || clen == (size_t)-2)
186 errc(2, EILSEQ, NULL);
189 if (!dflag || iswalnum(ch))
194 if (termchar != L'\0' && (writep = wcschr(key, termchar)) != NULL)
200 look(wchar_t *string, unsigned char *front, unsigned char *back)
203 front = binary_search(string, front, back);
204 front = linear_search(string, front, back);
207 print_from(string, front, back);
208 return (front ? 0 : 1);
213 * Binary search for "string" in memory between "front" and "back".
215 * This routine is expected to return a pointer to the start of a line at
216 * *or before* the first word matching "string". Relaxing the constraint
217 * this way simplifies the algorithm.
220 * front points to the beginning of a line at or before the first
223 * back points to the beginning of a line at or after the first
226 * Base of the Invariants.
230 * Advancing the Invariants:
232 * p = first newline after halfway point from front to back.
234 * If the string at "p" is not greater than the string to match,
235 * p is the new front. Otherwise it is the new back.
239 * The definition of the routine allows it return at any point,
240 * since front is always at or before the line to print.
242 * In fact, it returns when the chosen "p" equals "back". This
243 * implies that there exists a string is least half as long as
244 * (back - front), which in turn implies that a linear search will
245 * be no more expensive than the cost of simply printing a string or two.
247 * Trying to continue with binary search at this point would be
248 * more trouble than it's worth.
250 #define SKIP_PAST_NEWLINE(p, back) \
251 while (p < back && *p++ != '\n');
254 binary_search(wchar_t *string, unsigned char *front, unsigned char *back)
258 p = front + (back - front) / 2;
259 SKIP_PAST_NEWLINE(p, back);
262 * If the file changes underneath us, make sure we don't
265 while (p < back && back > front) {
266 if (compare(string, p, back) == GREATER)
270 p = front + (back - front) / 2;
271 SKIP_PAST_NEWLINE(p, back);
277 * Find the first line that starts with string, linearly searching from front
280 * Return NULL for no such line.
282 * This routine assumes:
284 * o front points at the first character in a line.
285 * o front is before or at the first line to be printed.
288 linear_search(wchar_t *string, unsigned char *front, unsigned char *back)
290 while (front < back) {
291 switch (compare(string, front, back)) {
292 case EQUAL: /* Found it. */
294 case LESS: /* No such string. */
296 case GREATER: /* Keep going. */
299 SKIP_PAST_NEWLINE(front, back);
305 * Print as many lines as match string, starting at front.
308 print_from(wchar_t *string, unsigned char *front, unsigned char *back)
310 for (; front < back && compare(string, front, back) == EQUAL; ++front) {
311 for (; front < back && *front != '\n'; ++front)
312 if (putchar(*front) == EOF)
314 if (putchar('\n') == EOF)
320 * Return LESS, GREATER, or EQUAL depending on how the string1 compares with
321 * string2 (s1 ??? s2).
323 * o Matches up to len(s1) are EQUAL.
324 * o Matches up to len(s2) are GREATER.
326 * Compare understands about the -f and -d flags, and treats comparisons
329 * The string "s1" is null terminated. The string s2 is '\n' terminated (or
330 * "back" terminated).
333 compare(wchar_t *s1, unsigned char *s2, unsigned char *back)
338 for (; *s1 && s2 < back && *s2 != '\n'; ++s1, s2 += len2) {
340 len2 = mbrtowc(&ch2, s2, back - s2, NULL);
341 if (len2 == (size_t)-1 || len2 == (size_t)-2) {
347 if (dflag && !iswalnum(ch2)) {
348 /* Ignore character in comparison. */
353 return (ch1 < ch2 ? LESS : GREATER);
355 return (*s1 ? GREATER : EQUAL);
361 (void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n");