xref: /freebsd/usr.bin/look/look.c (revision f9f231846a10c0ce03c2f132bf28f0465316b71b)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1991, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * David Hitz of Auspex Systems, Inc.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
389baefe4aSPhilippe Charnier static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
419b50d902SRodney W. Grimes #endif /* not lint */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
449baefe4aSPhilippe Charnier #if 0
45df3f5d9dSPeter Wemm static char sccsid[] = "@(#)look.c	8.2 (Berkeley) 5/4/95";
469baefe4aSPhilippe Charnier #endif
479b50d902SRodney W. Grimes #endif /* not lint */
48e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
49e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes /*
529b50d902SRodney W. Grimes  * look -- find lines in a sorted list.
539b50d902SRodney W. Grimes  *
549b50d902SRodney W. Grimes  * The man page said that TABs and SPACEs participate in -d comparisons.
559b50d902SRodney W. Grimes  * In fact, they were ignored.  This implements historic practice, not
569b50d902SRodney W. Grimes  * the manual page.
579b50d902SRodney W. Grimes  */
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes #include <sys/types.h>
609b50d902SRodney W. Grimes #include <sys/mman.h>
619b50d902SRodney W. Grimes #include <sys/stat.h>
629b50d902SRodney W. Grimes 
639baefe4aSPhilippe Charnier #include <err.h>
649b50d902SRodney W. Grimes #include <errno.h>
659b50d902SRodney W. Grimes #include <fcntl.h>
66df3f5d9dSPeter Wemm #include <limits.h>
679baefe4aSPhilippe Charnier #include <locale.h>
689b50d902SRodney W. Grimes #include <stdio.h>
699b50d902SRodney W. Grimes #include <stdlib.h>
709b50d902SRodney W. Grimes #include <string.h>
71df3f5d9dSPeter Wemm #include <unistd.h>
72d67148e4STim J. Robbins #include <wchar.h>
73d67148e4STim J. Robbins #include <wctype.h>
74df3f5d9dSPeter Wemm 
759b50d902SRodney W. Grimes #include "pathnames.h"
769b50d902SRodney W. Grimes 
779e5ff032SMark Murray static char _path_words[] = _PATH_WORDS;
789e5ff032SMark Murray 
799b50d902SRodney W. Grimes #define	EQUAL		0
809b50d902SRodney W. Grimes #define	GREATER		1
819b50d902SRodney W. Grimes #define	LESS		(-1)
829b50d902SRodney W. Grimes 
839b50d902SRodney W. Grimes int dflag, fflag;
849b50d902SRodney W. Grimes 
85d67148e4STim J. Robbins char    *binary_search(wchar_t *, unsigned char *, unsigned char *);
86d67148e4STim J. Robbins int      compare(wchar_t *, unsigned char *, unsigned char *);
87d67148e4STim J. Robbins char    *linear_search(wchar_t *, unsigned char *, unsigned char *);
88d67148e4STim J. Robbins int      look(wchar_t *, unsigned char *, unsigned char *);
89d67148e4STim J. Robbins wchar_t	*prepkey(const char *, wchar_t);
90d67148e4STim J. Robbins void     print_from(wchar_t *, unsigned char *, unsigned char *);
919b50d902SRodney W. Grimes 
92f1bb2cd2SWarner Losh static void usage(void);
939b50d902SRodney W. Grimes 
949baefe4aSPhilippe Charnier int
95f4ac32deSDavid Malone main(int argc, char *argv[])
969b50d902SRodney W. Grimes {
979b50d902SRodney W. Grimes 	struct stat sb;
98d67148e4STim J. Robbins 	int ch, fd, match;
99d67148e4STim J. Robbins 	wchar_t termchar;
100d67148e4STim J. Robbins 	unsigned char *back, *front;
101106b1a8cSDavid Malone 	unsigned const char *file;
102d67148e4STim J. Robbins 	wchar_t *key;
103ac7154f5SAndrey A. Chernov 
104ac7154f5SAndrey A. Chernov 	(void) setlocale(LC_CTYPE, "");
1059b50d902SRodney W. Grimes 
1069e5ff032SMark Murray 	file = _path_words;
107d67148e4STim J. Robbins 	termchar = L'\0';
1081c8af878SWarner Losh 	while ((ch = getopt(argc, argv, "dft:")) != -1)
1099b50d902SRodney W. Grimes 		switch(ch) {
1109b50d902SRodney W. Grimes 		case 'd':
1119b50d902SRodney W. Grimes 			dflag = 1;
1129b50d902SRodney W. Grimes 			break;
1139b50d902SRodney W. Grimes 		case 'f':
1149b50d902SRodney W. Grimes 			fflag = 1;
1159b50d902SRodney W. Grimes 			break;
1169b50d902SRodney W. Grimes 		case 't':
117d67148e4STim J. Robbins 			if (mbrtowc(&termchar, optarg, MB_LEN_MAX, NULL) !=
118d67148e4STim J. Robbins 			    strlen(optarg))
119d67148e4STim J. Robbins 				errx(2, "invalid termination character");
1209b50d902SRodney W. Grimes 			break;
1219b50d902SRodney W. Grimes 		case '?':
1229b50d902SRodney W. Grimes 		default:
1239b50d902SRodney W. Grimes 			usage();
1249b50d902SRodney W. Grimes 		}
1259b50d902SRodney W. Grimes 	argc -= optind;
1269b50d902SRodney W. Grimes 	argv += optind;
1279b50d902SRodney W. Grimes 
1284d3ee609SWolfram Schneider 	if (argc == 0)
1299b50d902SRodney W. Grimes 		usage();
1304d3ee609SWolfram Schneider 	if (argc == 1) 			/* But set -df by default. */
1314d3ee609SWolfram Schneider 		dflag = fflag = 1;
132d67148e4STim J. Robbins 	key = prepkey(*argv++, termchar);
1334d3ee609SWolfram Schneider 	if (argc >= 2)
1344d3ee609SWolfram Schneider 		file = *argv++;
1359b50d902SRodney W. Grimes 
1364d3ee609SWolfram Schneider 	match = 1;
1379b50d902SRodney W. Grimes 
1384d3ee609SWolfram Schneider 	do {
1399b50d902SRodney W. Grimes 		if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
1409baefe4aSPhilippe Charnier 			err(2, "%s", file);
1419b50d902SRodney W. Grimes 		if (sb.st_size > SIZE_T_MAX)
1429baefe4aSPhilippe Charnier 			errx(2, "%s: %s", file, strerror(EFBIG));
143f9f23184SColin Percival 		if (sb.st_size == 0) {
144f9f23184SColin Percival 			close(fd);
145f9f23184SColin Percival 			continue;
146f9f23184SColin Percival 		}
1474d3ee609SWolfram Schneider 		if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED)
1489baefe4aSPhilippe Charnier 			err(2, "%s", file);
1499b50d902SRodney W. Grimes 		back = front + sb.st_size;
150d67148e4STim J. Robbins 		match *= (look(key, front, back));
1514d3ee609SWolfram Schneider 		close(fd);
1524d3ee609SWolfram Schneider 	} while (argc-- > 2 && (file = *argv++));
1534d3ee609SWolfram Schneider 
1544d3ee609SWolfram Schneider 	exit(match);
1559b50d902SRodney W. Grimes }
1569b50d902SRodney W. Grimes 
157d67148e4STim J. Robbins wchar_t *
158d67148e4STim J. Robbins prepkey(const char *string, wchar_t termchar)
1599b50d902SRodney W. Grimes {
160d67148e4STim J. Robbins 	const char *readp;
161d67148e4STim J. Robbins 	wchar_t *key, *writep;
162d67148e4STim J. Robbins 	wchar_t ch;
163d67148e4STim J. Robbins 	size_t clen;
1649b50d902SRodney W. Grimes 
165d67148e4STim J. Robbins 	/*
166d67148e4STim J. Robbins 	 * Reformat search string and convert to wide character representation
167d67148e4STim J. Robbins 	 * to avoid doing it multiple times later.
168d67148e4STim J. Robbins 	 */
169d67148e4STim J. Robbins 	if ((key = malloc(sizeof(wchar_t) * (strlen(string) + 1))) == NULL)
170d67148e4STim J. Robbins 		err(2, NULL);
171d67148e4STim J. Robbins 	readp = string;
172d67148e4STim J. Robbins 	writep = key;
173d67148e4STim J. Robbins 	while ((clen = mbrtowc(&ch, readp, MB_LEN_MAX, NULL)) != 0) {
174d67148e4STim J. Robbins 		if (clen == (size_t)-1 || clen == (size_t)-2)
175d67148e4STim J. Robbins 			errc(2, EILSEQ, NULL);
1769b50d902SRodney W. Grimes 		if (fflag)
177d67148e4STim J. Robbins 			ch = towlower(ch);
178d67148e4STim J. Robbins 		if (!dflag || iswalnum(ch))
179d67148e4STim J. Robbins 			*writep++ = ch;
180d67148e4STim J. Robbins 		readp += clen;
1819b50d902SRodney W. Grimes 	}
182d67148e4STim J. Robbins 	*writep = L'\0';
183d67148e4STim J. Robbins 	if (termchar != L'\0' && (writep = wcschr(key, termchar)) != NULL)
184d67148e4STim J. Robbins 		*++writep = L'\0';
185d67148e4STim J. Robbins 	return (key);
186d67148e4STim J. Robbins }
187d67148e4STim J. Robbins 
188d67148e4STim J. Robbins int
189d67148e4STim J. Robbins look(wchar_t *string, unsigned char *front, unsigned char *back)
190d67148e4STim J. Robbins {
1919b50d902SRodney W. Grimes 
1929b50d902SRodney W. Grimes 	front = binary_search(string, front, back);
1939b50d902SRodney W. Grimes 	front = linear_search(string, front, back);
1949b50d902SRodney W. Grimes 
1959b50d902SRodney W. Grimes 	if (front)
1969b50d902SRodney W. Grimes 		print_from(string, front, back);
1979b50d902SRodney W. Grimes 	return (front ? 0 : 1);
1989b50d902SRodney W. Grimes }
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes 
2019b50d902SRodney W. Grimes /*
2029b50d902SRodney W. Grimes  * Binary search for "string" in memory between "front" and "back".
2039b50d902SRodney W. Grimes  *
2049b50d902SRodney W. Grimes  * This routine is expected to return a pointer to the start of a line at
2059b50d902SRodney W. Grimes  * *or before* the first word matching "string".  Relaxing the constraint
2069b50d902SRodney W. Grimes  * this way simplifies the algorithm.
2079b50d902SRodney W. Grimes  *
2089b50d902SRodney W. Grimes  * Invariants:
2099b50d902SRodney W. Grimes  * 	front points to the beginning of a line at or before the first
2109b50d902SRodney W. Grimes  *	matching string.
2119b50d902SRodney W. Grimes  *
2129b50d902SRodney W. Grimes  * 	back points to the beginning of a line at or after the first
2139b50d902SRodney W. Grimes  *	matching line.
2149b50d902SRodney W. Grimes  *
2159b50d902SRodney W. Grimes  * Base of the Invariants.
2169b50d902SRodney W. Grimes  * 	front = NULL;
2179b50d902SRodney W. Grimes  *	back = EOF;
2189b50d902SRodney W. Grimes  *
2199b50d902SRodney W. Grimes  * Advancing the Invariants:
2209b50d902SRodney W. Grimes  *
2219b50d902SRodney W. Grimes  * 	p = first newline after halfway point from front to back.
2229b50d902SRodney W. Grimes  *
2239b50d902SRodney W. Grimes  * 	If the string at "p" is not greater than the string to match,
2249b50d902SRodney W. Grimes  *	p is the new front.  Otherwise it is the new back.
2259b50d902SRodney W. Grimes  *
2269b50d902SRodney W. Grimes  * Termination:
2279b50d902SRodney W. Grimes  *
2289b50d902SRodney W. Grimes  * 	The definition of the routine allows it return at any point,
2299b50d902SRodney W. Grimes  *	since front is always at or before the line to print.
2309b50d902SRodney W. Grimes  *
2319b50d902SRodney W. Grimes  * 	In fact, it returns when the chosen "p" equals "back".  This
2329b50d902SRodney W. Grimes  *	implies that there exists a string is least half as long as
2339b50d902SRodney W. Grimes  *	(back - front), which in turn implies that a linear search will
2349b50d902SRodney W. Grimes  *	be no more expensive than the cost of simply printing a string or two.
2359b50d902SRodney W. Grimes  *
2369b50d902SRodney W. Grimes  * 	Trying to continue with binary search at this point would be
2379b50d902SRodney W. Grimes  *	more trouble than it's worth.
2389b50d902SRodney W. Grimes  */
2399b50d902SRodney W. Grimes #define	SKIP_PAST_NEWLINE(p, back) \
2409b50d902SRodney W. Grimes 	while (p < back && *p++ != '\n');
2419b50d902SRodney W. Grimes 
2429b50d902SRodney W. Grimes char *
243d67148e4STim J. Robbins binary_search(wchar_t *string, unsigned char *front, unsigned char *back)
2449b50d902SRodney W. Grimes {
245f4ac32deSDavid Malone 	unsigned char *p;
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes 	p = front + (back - front) / 2;
2489b50d902SRodney W. Grimes 	SKIP_PAST_NEWLINE(p, back);
2499b50d902SRodney W. Grimes 
2509b50d902SRodney W. Grimes 	/*
2519b50d902SRodney W. Grimes 	 * If the file changes underneath us, make sure we don't
2529b50d902SRodney W. Grimes 	 * infinitely loop.
2539b50d902SRodney W. Grimes 	 */
2549b50d902SRodney W. Grimes 	while (p < back && back > front) {
2559b50d902SRodney W. Grimes 		if (compare(string, p, back) == GREATER)
2569b50d902SRodney W. Grimes 			front = p;
2579b50d902SRodney W. Grimes 		else
2589b50d902SRodney W. Grimes 			back = p;
2599b50d902SRodney W. Grimes 		p = front + (back - front) / 2;
2609b50d902SRodney W. Grimes 		SKIP_PAST_NEWLINE(p, back);
2619b50d902SRodney W. Grimes 	}
2629b50d902SRodney W. Grimes 	return (front);
2639b50d902SRodney W. Grimes }
2649b50d902SRodney W. Grimes 
2659b50d902SRodney W. Grimes /*
2669b50d902SRodney W. Grimes  * Find the first line that starts with string, linearly searching from front
2679b50d902SRodney W. Grimes  * to back.
2689b50d902SRodney W. Grimes  *
2699b50d902SRodney W. Grimes  * Return NULL for no such line.
2709b50d902SRodney W. Grimes  *
2719b50d902SRodney W. Grimes  * This routine assumes:
2729b50d902SRodney W. Grimes  *
2739b50d902SRodney W. Grimes  * 	o front points at the first character in a line.
2749b50d902SRodney W. Grimes  *	o front is before or at the first line to be printed.
2759b50d902SRodney W. Grimes  */
2769b50d902SRodney W. Grimes char *
277d67148e4STim J. Robbins linear_search(wchar_t *string, unsigned char *front, unsigned char *back)
2789b50d902SRodney W. Grimes {
2799b50d902SRodney W. Grimes 	while (front < back) {
2809b50d902SRodney W. Grimes 		switch (compare(string, front, back)) {
2819b50d902SRodney W. Grimes 		case EQUAL:		/* Found it. */
2829b50d902SRodney W. Grimes 			return (front);
2839b50d902SRodney W. Grimes 		case LESS:		/* No such string. */
2849b50d902SRodney W. Grimes 			return (NULL);
2859b50d902SRodney W. Grimes 		case GREATER:		/* Keep going. */
2869b50d902SRodney W. Grimes 			break;
2879b50d902SRodney W. Grimes 		}
2889b50d902SRodney W. Grimes 		SKIP_PAST_NEWLINE(front, back);
2899b50d902SRodney W. Grimes 	}
2909b50d902SRodney W. Grimes 	return (NULL);
2919b50d902SRodney W. Grimes }
2929b50d902SRodney W. Grimes 
2939b50d902SRodney W. Grimes /*
2949b50d902SRodney W. Grimes  * Print as many lines as match string, starting at front.
2959b50d902SRodney W. Grimes  */
2969b50d902SRodney W. Grimes void
297d67148e4STim J. Robbins print_from(wchar_t *string, unsigned char *front, unsigned char *back)
2989b50d902SRodney W. Grimes {
2999b50d902SRodney W. Grimes 	for (; front < back && compare(string, front, back) == EQUAL; ++front) {
3009b50d902SRodney W. Grimes 		for (; front < back && *front != '\n'; ++front)
3019b50d902SRodney W. Grimes 			if (putchar(*front) == EOF)
3029baefe4aSPhilippe Charnier 				err(2, "stdout");
3039b50d902SRodney W. Grimes 		if (putchar('\n') == EOF)
3049baefe4aSPhilippe Charnier 			err(2, "stdout");
3059b50d902SRodney W. Grimes 	}
3069b50d902SRodney W. Grimes }
3079b50d902SRodney W. Grimes 
3089b50d902SRodney W. Grimes /*
3099b50d902SRodney W. Grimes  * Return LESS, GREATER, or EQUAL depending on how the string1 compares with
3109b50d902SRodney W. Grimes  * string2 (s1 ??? s2).
3119b50d902SRodney W. Grimes  *
3129b50d902SRodney W. Grimes  * 	o Matches up to len(s1) are EQUAL.
3139b50d902SRodney W. Grimes  *	o Matches up to len(s2) are GREATER.
3149b50d902SRodney W. Grimes  *
3159b50d902SRodney W. Grimes  * Compare understands about the -f and -d flags, and treats comparisons
3169b50d902SRodney W. Grimes  * appropriately.
3179b50d902SRodney W. Grimes  *
3189b50d902SRodney W. Grimes  * The string "s1" is null terminated.  The string s2 is '\n' terminated (or
3199b50d902SRodney W. Grimes  * "back" terminated).
3209b50d902SRodney W. Grimes  */
3219b50d902SRodney W. Grimes int
322d67148e4STim J. Robbins compare(wchar_t *s1, unsigned char *s2, unsigned char *back)
3239b50d902SRodney W. Grimes {
324d67148e4STim J. Robbins 	wchar_t ch1, ch2;
325d67148e4STim J. Robbins 	size_t len2;
3269b50d902SRodney W. Grimes 
327d67148e4STim J. Robbins 	for (; *s1 && s2 < back && *s2 != '\n'; ++s1, s2 += len2) {
328d67148e4STim J. Robbins 		ch1 = *s1;
329d67148e4STim J. Robbins 		len2 = mbrtowc(&ch2, s2, back - s2, NULL);
330d67148e4STim J. Robbins 		if (len2 == (size_t)-1 || len2 == (size_t)-2) {
331d67148e4STim J. Robbins 			ch2 = *s2;
332d67148e4STim J. Robbins 			len2 = 1;
333d67148e4STim J. Robbins 		}
3349b50d902SRodney W. Grimes 		if (fflag)
335d67148e4STim J. Robbins 			ch2 = towlower(ch2);
336d67148e4STim J. Robbins 		if (dflag && !iswalnum(ch2)) {
337d67148e4STim J. Robbins 			/* Ignore character in comparison. */
338d67148e4STim J. Robbins 			--s1;
3399b50d902SRodney W. Grimes 			continue;
3409b50d902SRodney W. Grimes 		}
341d67148e4STim J. Robbins 		if (ch1 != ch2)
342d67148e4STim J. Robbins 			return (ch1 < ch2 ? LESS : GREATER);
3439b50d902SRodney W. Grimes 	}
3449b50d902SRodney W. Grimes 	return (*s1 ? GREATER : EQUAL);
3459b50d902SRodney W. Grimes }
3469b50d902SRodney W. Grimes 
3479b50d902SRodney W. Grimes static void
348f4ac32deSDavid Malone usage(void)
3499b50d902SRodney W. Grimes {
3504d3ee609SWolfram Schneider 	(void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n");
3519b50d902SRodney W. Grimes 	exit(2);
3529b50d902SRodney W. Grimes }
353