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. 16*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 189b50d902SRodney W. Grimes * without specific prior written permission. 199b50d902SRodney W. Grimes * 209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309b50d902SRodney W. Grimes * SUCH DAMAGE. 319b50d902SRodney W. Grimes */ 329b50d902SRodney W. Grimes 339b50d902SRodney W. Grimes #ifndef lint 349baefe4aSPhilippe Charnier static const char copyright[] = 359b50d902SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\ 369b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 379b50d902SRodney W. Grimes #endif /* not lint */ 389b50d902SRodney W. Grimes 399b50d902SRodney W. Grimes #ifndef lint 409baefe4aSPhilippe Charnier #if 0 41df3f5d9dSPeter Wemm static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95"; 429baefe4aSPhilippe Charnier #endif 439b50d902SRodney W. Grimes #endif /* not lint */ 44e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 45e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes /* 489b50d902SRodney W. Grimes * look -- find lines in a sorted list. 499b50d902SRodney W. Grimes * 509b50d902SRodney W. Grimes * The man page said that TABs and SPACEs participate in -d comparisons. 519b50d902SRodney W. Grimes * In fact, they were ignored. This implements historic practice, not 529b50d902SRodney W. Grimes * the manual page. 539b50d902SRodney W. Grimes */ 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes #include <sys/types.h> 569b50d902SRodney W. Grimes #include <sys/mman.h> 579b50d902SRodney W. Grimes #include <sys/stat.h> 589b50d902SRodney W. Grimes 599baefe4aSPhilippe Charnier #include <err.h> 609b50d902SRodney W. Grimes #include <errno.h> 619b50d902SRodney W. Grimes #include <fcntl.h> 62b5bb9538SEitan Adler #include <getopt.h> 63df3f5d9dSPeter Wemm #include <limits.h> 649baefe4aSPhilippe Charnier #include <locale.h> 65d13b008dSEd Schouten #include <stdint.h> 662a5e51faSEd Schouten #include <stdio.h> 679b50d902SRodney W. Grimes #include <stdlib.h> 689b50d902SRodney W. Grimes #include <string.h> 69df3f5d9dSPeter Wemm #include <unistd.h> 70d67148e4STim J. Robbins #include <wchar.h> 71d67148e4STim J. Robbins #include <wctype.h> 72df3f5d9dSPeter Wemm 739b50d902SRodney W. Grimes #include "pathnames.h" 749b50d902SRodney W. Grimes 759e5ff032SMark Murray static char _path_words[] = _PATH_WORDS; 769e5ff032SMark Murray 779b50d902SRodney W. Grimes #define EQUAL 0 789b50d902SRodney W. Grimes #define GREATER 1 799b50d902SRodney W. Grimes #define LESS (-1) 809b50d902SRodney W. Grimes 8176de4396SEd Schouten static int dflag, fflag; 829b50d902SRodney W. Grimes 8376de4396SEd Schouten static char *binary_search(wchar_t *, unsigned char *, unsigned char *); 8476de4396SEd Schouten static int compare(wchar_t *, unsigned char *, unsigned char *); 8576de4396SEd Schouten static char *linear_search(wchar_t *, unsigned char *, unsigned char *); 8676de4396SEd Schouten static int look(wchar_t *, unsigned char *, unsigned char *); 8776de4396SEd Schouten static wchar_t *prepkey(const char *, wchar_t); 8876de4396SEd Schouten static void print_from(wchar_t *, unsigned char *, unsigned char *); 899b50d902SRodney W. Grimes 90f1bb2cd2SWarner Losh static void usage(void); 919b50d902SRodney W. Grimes 92b5bb9538SEitan Adler static struct option longopts[] = { 93b5bb9538SEitan Adler { "alternative",no_argument, NULL, 'a' }, 94b5bb9538SEitan Adler { "alphanum", no_argument, NULL, 'd' }, 95b5bb9538SEitan Adler { "ignore-case",no_argument, NULL, 'i' }, 96b5bb9538SEitan Adler { "terminate", required_argument, NULL, 't'}, 97b5bb9538SEitan Adler { NULL, 0, NULL, 0 }, 98b5bb9538SEitan Adler }; 99b5bb9538SEitan Adler 1009baefe4aSPhilippe Charnier int 101f4ac32deSDavid Malone main(int argc, char *argv[]) 1029b50d902SRodney W. Grimes { 1039b50d902SRodney W. Grimes struct stat sb; 104d67148e4STim J. Robbins int ch, fd, match; 105d67148e4STim J. Robbins wchar_t termchar; 106d67148e4STim J. Robbins unsigned char *back, *front; 107106b1a8cSDavid Malone unsigned const char *file; 108d67148e4STim J. Robbins wchar_t *key; 109ac7154f5SAndrey A. Chernov 110ac7154f5SAndrey A. Chernov (void) setlocale(LC_CTYPE, ""); 1119b50d902SRodney W. Grimes 1129e5ff032SMark Murray file = _path_words; 113d67148e4STim J. Robbins termchar = L'\0'; 114b5bb9538SEitan Adler while ((ch = getopt_long(argc, argv, "+adft:", longopts, NULL)) != -1) 1159b50d902SRodney W. Grimes switch(ch) { 116b245fc98SEitan Adler case 'a': 117b245fc98SEitan Adler /* COMPATIBILITY */ 118b245fc98SEitan Adler break; 1199b50d902SRodney W. Grimes case 'd': 1209b50d902SRodney W. Grimes dflag = 1; 1219b50d902SRodney W. Grimes break; 1229b50d902SRodney W. Grimes case 'f': 1239b50d902SRodney W. Grimes fflag = 1; 1249b50d902SRodney W. Grimes break; 1259b50d902SRodney W. Grimes case 't': 126d67148e4STim J. Robbins if (mbrtowc(&termchar, optarg, MB_LEN_MAX, NULL) != 127d67148e4STim J. Robbins strlen(optarg)) 128d67148e4STim J. Robbins errx(2, "invalid termination character"); 1299b50d902SRodney W. Grimes break; 1309b50d902SRodney W. Grimes case '?': 1319b50d902SRodney W. Grimes default: 1329b50d902SRodney W. Grimes usage(); 1339b50d902SRodney W. Grimes } 1349b50d902SRodney W. Grimes argc -= optind; 1359b50d902SRodney W. Grimes argv += optind; 1369b50d902SRodney W. Grimes 1374d3ee609SWolfram Schneider if (argc == 0) 1389b50d902SRodney W. Grimes usage(); 1394d3ee609SWolfram Schneider if (argc == 1) /* But set -df by default. */ 1404d3ee609SWolfram Schneider dflag = fflag = 1; 141d67148e4STim J. Robbins key = prepkey(*argv++, termchar); 1424d3ee609SWolfram Schneider if (argc >= 2) 1434d3ee609SWolfram Schneider file = *argv++; 1449b50d902SRodney W. Grimes 1454d3ee609SWolfram Schneider match = 1; 1469b50d902SRodney W. Grimes 1474d3ee609SWolfram Schneider do { 1489b50d902SRodney W. Grimes if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) 1499baefe4aSPhilippe Charnier err(2, "%s", file); 150d2ea3bedSEd Schouten if ((uintmax_t)sb.st_size > (uintmax_t)SIZE_T_MAX) 1519baefe4aSPhilippe Charnier errx(2, "%s: %s", file, strerror(EFBIG)); 152f9f23184SColin Percival if (sb.st_size == 0) { 153f9f23184SColin Percival close(fd); 154f9f23184SColin Percival continue; 155f9f23184SColin Percival } 1564d3ee609SWolfram Schneider if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) 1579baefe4aSPhilippe Charnier err(2, "%s", file); 1589b50d902SRodney W. Grimes back = front + sb.st_size; 159d67148e4STim J. Robbins match *= (look(key, front, back)); 1604d3ee609SWolfram Schneider close(fd); 1614d3ee609SWolfram Schneider } while (argc-- > 2 && (file = *argv++)); 1624d3ee609SWolfram Schneider 1634d3ee609SWolfram Schneider exit(match); 1649b50d902SRodney W. Grimes } 1659b50d902SRodney W. Grimes 16676de4396SEd Schouten static wchar_t * 167d67148e4STim J. Robbins prepkey(const char *string, wchar_t termchar) 1689b50d902SRodney W. Grimes { 169d67148e4STim J. Robbins const char *readp; 170d67148e4STim J. Robbins wchar_t *key, *writep; 171d67148e4STim J. Robbins wchar_t ch; 172d67148e4STim J. Robbins size_t clen; 1739b50d902SRodney W. Grimes 174d67148e4STim J. Robbins /* 175d67148e4STim J. Robbins * Reformat search string and convert to wide character representation 176d67148e4STim J. Robbins * to avoid doing it multiple times later. 177d67148e4STim J. Robbins */ 178d67148e4STim J. Robbins if ((key = malloc(sizeof(wchar_t) * (strlen(string) + 1))) == NULL) 179d67148e4STim J. Robbins err(2, NULL); 180d67148e4STim J. Robbins readp = string; 181d67148e4STim J. Robbins writep = key; 182d67148e4STim J. Robbins while ((clen = mbrtowc(&ch, readp, MB_LEN_MAX, NULL)) != 0) { 183d67148e4STim J. Robbins if (clen == (size_t)-1 || clen == (size_t)-2) 184d67148e4STim J. Robbins errc(2, EILSEQ, NULL); 1859b50d902SRodney W. Grimes if (fflag) 186d67148e4STim J. Robbins ch = towlower(ch); 187d67148e4STim J. Robbins if (!dflag || iswalnum(ch)) 188d67148e4STim J. Robbins *writep++ = ch; 189d67148e4STim J. Robbins readp += clen; 1909b50d902SRodney W. Grimes } 191d67148e4STim J. Robbins *writep = L'\0'; 192d67148e4STim J. Robbins if (termchar != L'\0' && (writep = wcschr(key, termchar)) != NULL) 193d67148e4STim J. Robbins *++writep = L'\0'; 194d67148e4STim J. Robbins return (key); 195d67148e4STim J. Robbins } 196d67148e4STim J. Robbins 19776de4396SEd Schouten static int 198d67148e4STim J. Robbins look(wchar_t *string, unsigned char *front, unsigned char *back) 199d67148e4STim J. Robbins { 2009b50d902SRodney W. Grimes 2019b50d902SRodney W. Grimes front = binary_search(string, front, back); 2029b50d902SRodney W. Grimes front = linear_search(string, front, back); 2039b50d902SRodney W. Grimes 2049b50d902SRodney W. Grimes if (front) 2059b50d902SRodney W. Grimes print_from(string, front, back); 2069b50d902SRodney W. Grimes return (front ? 0 : 1); 2079b50d902SRodney W. Grimes } 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes 2109b50d902SRodney W. Grimes /* 2119b50d902SRodney W. Grimes * Binary search for "string" in memory between "front" and "back". 2129b50d902SRodney W. Grimes * 2139b50d902SRodney W. Grimes * This routine is expected to return a pointer to the start of a line at 2149b50d902SRodney W. Grimes * *or before* the first word matching "string". Relaxing the constraint 2159b50d902SRodney W. Grimes * this way simplifies the algorithm. 2169b50d902SRodney W. Grimes * 2179b50d902SRodney W. Grimes * Invariants: 2189b50d902SRodney W. Grimes * front points to the beginning of a line at or before the first 2199b50d902SRodney W. Grimes * matching string. 2209b50d902SRodney W. Grimes * 2219b50d902SRodney W. Grimes * back points to the beginning of a line at or after the first 2229b50d902SRodney W. Grimes * matching line. 2239b50d902SRodney W. Grimes * 2249b50d902SRodney W. Grimes * Base of the Invariants. 2259b50d902SRodney W. Grimes * front = NULL; 2269b50d902SRodney W. Grimes * back = EOF; 2279b50d902SRodney W. Grimes * 2289b50d902SRodney W. Grimes * Advancing the Invariants: 2299b50d902SRodney W. Grimes * 2309b50d902SRodney W. Grimes * p = first newline after halfway point from front to back. 2319b50d902SRodney W. Grimes * 2329b50d902SRodney W. Grimes * If the string at "p" is not greater than the string to match, 2339b50d902SRodney W. Grimes * p is the new front. Otherwise it is the new back. 2349b50d902SRodney W. Grimes * 2359b50d902SRodney W. Grimes * Termination: 2369b50d902SRodney W. Grimes * 2379b50d902SRodney W. Grimes * The definition of the routine allows it return at any point, 2389b50d902SRodney W. Grimes * since front is always at or before the line to print. 2399b50d902SRodney W. Grimes * 2409b50d902SRodney W. Grimes * In fact, it returns when the chosen "p" equals "back". This 2419b50d902SRodney W. Grimes * implies that there exists a string is least half as long as 2429b50d902SRodney W. Grimes * (back - front), which in turn implies that a linear search will 2439b50d902SRodney W. Grimes * be no more expensive than the cost of simply printing a string or two. 2449b50d902SRodney W. Grimes * 2459b50d902SRodney W. Grimes * Trying to continue with binary search at this point would be 2469b50d902SRodney W. Grimes * more trouble than it's worth. 2479b50d902SRodney W. Grimes */ 2489b50d902SRodney W. Grimes #define SKIP_PAST_NEWLINE(p, back) \ 2499b50d902SRodney W. Grimes while (p < back && *p++ != '\n'); 2509b50d902SRodney W. Grimes 25176de4396SEd Schouten static char * 252d67148e4STim J. Robbins binary_search(wchar_t *string, unsigned char *front, unsigned char *back) 2539b50d902SRodney W. Grimes { 254f4ac32deSDavid Malone unsigned char *p; 2559b50d902SRodney W. Grimes 2569b50d902SRodney W. Grimes p = front + (back - front) / 2; 2579b50d902SRodney W. Grimes SKIP_PAST_NEWLINE(p, back); 2589b50d902SRodney W. Grimes 2599b50d902SRodney W. Grimes /* 2609b50d902SRodney W. Grimes * If the file changes underneath us, make sure we don't 2619b50d902SRodney W. Grimes * infinitely loop. 2629b50d902SRodney W. Grimes */ 2639b50d902SRodney W. Grimes while (p < back && back > front) { 2649b50d902SRodney W. Grimes if (compare(string, p, back) == GREATER) 2659b50d902SRodney W. Grimes front = p; 2669b50d902SRodney W. Grimes else 2679b50d902SRodney W. Grimes back = p; 2689b50d902SRodney W. Grimes p = front + (back - front) / 2; 2699b50d902SRodney W. Grimes SKIP_PAST_NEWLINE(p, back); 2709b50d902SRodney W. Grimes } 2719b50d902SRodney W. Grimes return (front); 2729b50d902SRodney W. Grimes } 2739b50d902SRodney W. Grimes 2749b50d902SRodney W. Grimes /* 2759b50d902SRodney W. Grimes * Find the first line that starts with string, linearly searching from front 2769b50d902SRodney W. Grimes * to back. 2779b50d902SRodney W. Grimes * 2789b50d902SRodney W. Grimes * Return NULL for no such line. 2799b50d902SRodney W. Grimes * 2809b50d902SRodney W. Grimes * This routine assumes: 2819b50d902SRodney W. Grimes * 2829b50d902SRodney W. Grimes * o front points at the first character in a line. 2839b50d902SRodney W. Grimes * o front is before or at the first line to be printed. 2849b50d902SRodney W. Grimes */ 28576de4396SEd Schouten static char * 286d67148e4STim J. Robbins linear_search(wchar_t *string, unsigned char *front, unsigned char *back) 2879b50d902SRodney W. Grimes { 2889b50d902SRodney W. Grimes while (front < back) { 2899b50d902SRodney W. Grimes switch (compare(string, front, back)) { 2909b50d902SRodney W. Grimes case EQUAL: /* Found it. */ 2919b50d902SRodney W. Grimes return (front); 2929b50d902SRodney W. Grimes case LESS: /* No such string. */ 2939b50d902SRodney W. Grimes return (NULL); 2949b50d902SRodney W. Grimes case GREATER: /* Keep going. */ 2959b50d902SRodney W. Grimes break; 2969b50d902SRodney W. Grimes } 2979b50d902SRodney W. Grimes SKIP_PAST_NEWLINE(front, back); 2989b50d902SRodney W. Grimes } 2999b50d902SRodney W. Grimes return (NULL); 3009b50d902SRodney W. Grimes } 3019b50d902SRodney W. Grimes 3029b50d902SRodney W. Grimes /* 3039b50d902SRodney W. Grimes * Print as many lines as match string, starting at front. 3049b50d902SRodney W. Grimes */ 30576de4396SEd Schouten static void 306d67148e4STim J. Robbins print_from(wchar_t *string, unsigned char *front, unsigned char *back) 3079b50d902SRodney W. Grimes { 3089b50d902SRodney W. Grimes for (; front < back && compare(string, front, back) == EQUAL; ++front) { 3099b50d902SRodney W. Grimes for (; front < back && *front != '\n'; ++front) 3109b50d902SRodney W. Grimes if (putchar(*front) == EOF) 3119baefe4aSPhilippe Charnier err(2, "stdout"); 3129b50d902SRodney W. Grimes if (putchar('\n') == EOF) 3139baefe4aSPhilippe Charnier err(2, "stdout"); 3149b50d902SRodney W. Grimes } 3159b50d902SRodney W. Grimes } 3169b50d902SRodney W. Grimes 3179b50d902SRodney W. Grimes /* 3189b50d902SRodney W. Grimes * Return LESS, GREATER, or EQUAL depending on how the string1 compares with 3199b50d902SRodney W. Grimes * string2 (s1 ??? s2). 3209b50d902SRodney W. Grimes * 3219b50d902SRodney W. Grimes * o Matches up to len(s1) are EQUAL. 3229b50d902SRodney W. Grimes * o Matches up to len(s2) are GREATER. 3239b50d902SRodney W. Grimes * 3249b50d902SRodney W. Grimes * Compare understands about the -f and -d flags, and treats comparisons 3259b50d902SRodney W. Grimes * appropriately. 3269b50d902SRodney W. Grimes * 3279b50d902SRodney W. Grimes * The string "s1" is null terminated. The string s2 is '\n' terminated (or 3289b50d902SRodney W. Grimes * "back" terminated). 3299b50d902SRodney W. Grimes */ 33076de4396SEd Schouten static int 331d67148e4STim J. Robbins compare(wchar_t *s1, unsigned char *s2, unsigned char *back) 3329b50d902SRodney W. Grimes { 333d67148e4STim J. Robbins wchar_t ch1, ch2; 334d67148e4STim J. Robbins size_t len2; 3359b50d902SRodney W. Grimes 336d67148e4STim J. Robbins for (; *s1 && s2 < back && *s2 != '\n'; ++s1, s2 += len2) { 337d67148e4STim J. Robbins ch1 = *s1; 338d67148e4STim J. Robbins len2 = mbrtowc(&ch2, s2, back - s2, NULL); 339d67148e4STim J. Robbins if (len2 == (size_t)-1 || len2 == (size_t)-2) { 340d67148e4STim J. Robbins ch2 = *s2; 341d67148e4STim J. Robbins len2 = 1; 342d67148e4STim J. Robbins } 3439b50d902SRodney W. Grimes if (fflag) 344d67148e4STim J. Robbins ch2 = towlower(ch2); 345d67148e4STim J. Robbins if (dflag && !iswalnum(ch2)) { 346d67148e4STim J. Robbins /* Ignore character in comparison. */ 347d67148e4STim J. Robbins --s1; 3489b50d902SRodney W. Grimes continue; 3499b50d902SRodney W. Grimes } 350d67148e4STim J. Robbins if (ch1 != ch2) 351d67148e4STim J. Robbins return (ch1 < ch2 ? LESS : GREATER); 3529b50d902SRodney W. Grimes } 3539b50d902SRodney W. Grimes return (*s1 ? GREATER : EQUAL); 3549b50d902SRodney W. Grimes } 3559b50d902SRodney W. Grimes 3569b50d902SRodney W. Grimes static void 357f4ac32deSDavid Malone usage(void) 3589b50d902SRodney W. Grimes { 3594d3ee609SWolfram Schneider (void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n"); 3609b50d902SRodney W. Grimes exit(2); 3619b50d902SRodney W. Grimes } 362