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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)look.c 8.2 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/look/look.c,v 1.11 1999/08/28 01:03:14 peter Exp $
39 * $DragonFly: src/usr.bin/look/look.c,v 1.4 2005/01/07 02:43:41 cpressey Exp $
43 * look -- find lines in a sorted list.
45 * The man page said that TABs and SPACEs participate in -d comparisons.
46 * In fact, they were ignored. This implements historic practice, not
50 #include <sys/types.h>
65 #include "pathnames.h"
68 * FOLD and DICT convert characters to a normal form for comparison,
69 * according to the user specified flags.
71 * DICT expects integers because it uses a non-character value to
72 * indicate a character which should not participate in comparisons.
77 #define NO_COMPARE (-2)
79 #define FOLD(c) (isupper(c) ? tolower(c) : (unsigned char) (c))
80 #define DICT(c) (isalnum(c) ? (c) & 0xFF /* int */ : NO_COMPARE)
84 char *binary_search(unsigned char *, unsigned char *, unsigned char *);
85 int compare(unsigned char *, unsigned char *, unsigned char *);
86 char *linear_search(unsigned char *, unsigned char *, unsigned char *);
87 int look(unsigned char *, unsigned char *, unsigned char *);
88 void print_from(unsigned char *, unsigned char *, unsigned char *);
90 static void usage(void);
93 main(int argc
, char **argv
)
96 int ch
, fd
, termchar
, match
;
97 unsigned char *back
, *front
, *string
, *p
;
98 const unsigned char *file
;
100 setlocale(LC_CTYPE
, "");
104 while ((ch
= getopt(argc
, argv
, "dft:")) != -1)
124 if (argc
== 1) /* But set -df by default. */
130 if (termchar
!= '\0' && (p
= strchr(string
, termchar
)) != NULL
)
135 if ((fd
= open(file
, O_RDONLY
, 0)) < 0 || fstat(fd
, &sb
))
137 if (sb
.st_size
> (off_t
)SIZE_T_MAX
)
138 errx(2, "%s: %s", file
, strerror(EFBIG
));
139 if ((front
= mmap(NULL
, (size_t)sb
.st_size
, PROT_READ
, MAP_SHARED
, fd
, (off_t
)0)) == MAP_FAILED
)
141 back
= front
+ sb
.st_size
;
142 match
*= (look(string
, front
, back
));
144 } while (argc
-- > 2 && (file
= *argv
++));
150 look(unsigned char *string
, unsigned char *front
, unsigned char *back
)
153 unsigned char *readp
, *writep
;
155 /* Reformat string string to avoid doing it multiple times later. */
156 for (readp
= writep
= string
; (ch
= *readp
++) != '\0';) {
161 if (ch
!= NO_COMPARE
)
166 front
= binary_search(string
, front
, back
);
167 front
= linear_search(string
, front
, back
);
170 print_from(string
, front
, back
);
171 return (front
? 0 : 1);
176 * Binary search for "string" in memory between "front" and "back".
178 * This routine is expected to return a pointer to the start of a line at
179 * *or before* the first word matching "string". Relaxing the constraint
180 * this way simplifies the algorithm.
183 * front points to the beginning of a line at or before the first
186 * back points to the beginning of a line at or after the first
189 * Base of the Invariants.
193 * Advancing the Invariants:
195 * p = first newline after halfway point from front to back.
197 * If the string at "p" is not greater than the string to match,
198 * p is the new front. Otherwise it is the new back.
202 * The definition of the routine allows it return at any point,
203 * since front is always at or before the line to print.
205 * In fact, it returns when the chosen "p" equals "back". This
206 * implies that there exists a string is least half as long as
207 * (back - front), which in turn implies that a linear search will
208 * be no more expensive than the cost of simply printing a string or two.
210 * Trying to continue with binary search at this point would be
211 * more trouble than it's worth.
213 #define SKIP_PAST_NEWLINE(p, back) \
214 while (p < back && *p++ != '\n');
217 binary_search(unsigned char *string
, unsigned char *front
,
222 p
= front
+ (back
- front
) / 2;
223 SKIP_PAST_NEWLINE(p
, back
);
226 * If the file changes underneath us, make sure we don't
229 while (p
< back
&& back
> front
) {
230 if (compare(string
, p
, back
) == GREATER
)
234 p
= front
+ (back
- front
) / 2;
235 SKIP_PAST_NEWLINE(p
, back
);
241 * Find the first line that starts with string, linearly searching from front
244 * Return NULL for no such line.
246 * This routine assumes:
248 * o front points at the first character in a line.
249 * o front is before or at the first line to be printed.
252 linear_search(unsigned char *string
, unsigned char *front
, unsigned char *back
)
254 while (front
< back
) {
255 switch (compare(string
, front
, back
)) {
256 case EQUAL
: /* Found it. */
259 case LESS
: /* No such string. */
262 case GREATER
: /* Keep going. */
265 SKIP_PAST_NEWLINE(front
, back
);
271 * Print as many lines as match string, starting at front.
274 print_from(unsigned char *string
, unsigned char *front
,
277 for (; front
< back
&& compare(string
, front
, back
) == EQUAL
; ++front
) {
278 for (; front
< back
&& *front
!= '\n'; ++front
)
279 if (putchar(*front
) == EOF
)
281 if (putchar('\n') == EOF
)
287 * Return LESS, GREATER, or EQUAL depending on how the string1 compares with
288 * string2 (s1 ??? s2).
290 * o Matches up to len(s1) are EQUAL.
291 * o Matches up to len(s2) are GREATER.
293 * Compare understands about the -f and -d flags, and treats comparisons
296 * The string "s1" is null terminated. The string s2 is '\n' terminated (or
297 * "back" terminated).
300 compare(unsigned char *s1
, unsigned char *s2
,
305 for (; *s1
&& s2
< back
&& *s2
!= '\n'; ++s1
, ++s2
) {
312 if (ch
== NO_COMPARE
) {
313 ++s2
; /* Ignore character in comparison. */
317 return (*s1
< ch
? LESS
: GREATER
);
319 return (*s1
? GREATER
: EQUAL
);
325 fprintf(stderr
, "usage: look [-df] [-t char] string [file ...]\n");