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 389b50d902SRodney W. Grimes static 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 44df3f5d9dSPeter Wemm static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95"; 459b50d902SRodney W. Grimes #endif /* not lint */ 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 599b50d902SRodney W. Grimes #include <limits.h> 60ac7154f5SAndrey A. Chernov #include <locale.h> 61df3f5d9dSPeter Wemm #include <ctype.h> 629b50d902SRodney W. Grimes #include <errno.h> 639b50d902SRodney W. Grimes #include <fcntl.h> 64df3f5d9dSPeter Wemm #include <limits.h> 659b50d902SRodney W. Grimes #include <stdio.h> 669b50d902SRodney W. Grimes #include <stdlib.h> 679b50d902SRodney W. Grimes #include <string.h> 68df3f5d9dSPeter Wemm #include <unistd.h> 69df3f5d9dSPeter Wemm 709b50d902SRodney W. Grimes #include "pathnames.h" 719b50d902SRodney W. Grimes 729b50d902SRodney W. Grimes /* 739b50d902SRodney W. Grimes * FOLD and DICT convert characters to a normal form for comparison, 749b50d902SRodney W. Grimes * according to the user specified flags. 759b50d902SRodney W. Grimes * 769b50d902SRodney W. Grimes * DICT expects integers because it uses a non-character value to 779b50d902SRodney W. Grimes * indicate a character which should not participate in comparisons. 789b50d902SRodney W. Grimes */ 799b50d902SRodney W. Grimes #define EQUAL 0 809b50d902SRodney W. Grimes #define GREATER 1 819b50d902SRodney W. Grimes #define LESS (-1) 829b50d902SRodney W. Grimes #define NO_COMPARE (-2) 839b50d902SRodney W. Grimes 84ac7154f5SAndrey A. Chernov #define FOLD(c) (isupper(c) ? tolower(c) : (unsigned char) (c)) 85ac7154f5SAndrey A. Chernov #define DICT(c) (isalnum(c) ? (c) & 0xFF /* int */ : NO_COMPARE) 869b50d902SRodney W. Grimes 879b50d902SRodney W. Grimes int dflag, fflag; 889b50d902SRodney W. Grimes 89ac7154f5SAndrey A. Chernov char *binary_search __P((unsigned char *, unsigned char *, unsigned char *)); 90ac7154f5SAndrey A. Chernov int compare __P((unsigned char *, unsigned char *, unsigned char *)); 919b50d902SRodney W. Grimes void err __P((const char *fmt, ...)); 92ac7154f5SAndrey A. Chernov char *linear_search __P((unsigned char *, unsigned char *, unsigned char *)); 93ac7154f5SAndrey A. Chernov int look __P((unsigned char *, unsigned char *, unsigned char *)); 94ac7154f5SAndrey A. Chernov void print_from __P((unsigned char *, unsigned char *, unsigned char *)); 959b50d902SRodney W. Grimes 969b50d902SRodney W. Grimes static void usage __P((void)); 979b50d902SRodney W. Grimes 989b50d902SRodney W. Grimes main(argc, argv) 999b50d902SRodney W. Grimes int argc; 1009b50d902SRodney W. Grimes char *argv[]; 1019b50d902SRodney W. Grimes { 1029b50d902SRodney W. Grimes struct stat sb; 1039b50d902SRodney W. Grimes int ch, fd, termchar; 104ac7154f5SAndrey A. Chernov unsigned char *back, *file, *front, *string, *p; 105ac7154f5SAndrey A. Chernov 106ac7154f5SAndrey A. Chernov (void) setlocale(LC_CTYPE, ""); 1079b50d902SRodney W. Grimes 1089b50d902SRodney W. Grimes file = _PATH_WORDS; 1099b50d902SRodney W. Grimes termchar = '\0'; 1109b50d902SRodney W. Grimes while ((ch = getopt(argc, argv, "dft:")) != EOF) 1119b50d902SRodney W. Grimes switch(ch) { 1129b50d902SRodney W. Grimes case 'd': 1139b50d902SRodney W. Grimes dflag = 1; 1149b50d902SRodney W. Grimes break; 1159b50d902SRodney W. Grimes case 'f': 1169b50d902SRodney W. Grimes fflag = 1; 1179b50d902SRodney W. Grimes break; 1189b50d902SRodney W. Grimes case 't': 1199b50d902SRodney W. Grimes termchar = *optarg; 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 1289b50d902SRodney W. Grimes switch (argc) { 1299b50d902SRodney W. Grimes case 2: /* Don't set -df for user. */ 1309b50d902SRodney W. Grimes string = *argv++; 1319b50d902SRodney W. Grimes file = *argv; 1329b50d902SRodney W. Grimes break; 1339b50d902SRodney W. Grimes case 1: /* But set -df by default. */ 1349b50d902SRodney W. Grimes dflag = fflag = 1; 1359b50d902SRodney W. Grimes string = *argv; 1369b50d902SRodney W. Grimes break; 1379b50d902SRodney W. Grimes default: 1389b50d902SRodney W. Grimes usage(); 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes 1419b50d902SRodney W. Grimes if (termchar != '\0' && (p = strchr(string, termchar)) != NULL) 1429b50d902SRodney W. Grimes *++p = '\0'; 1439b50d902SRodney W. Grimes 1449b50d902SRodney W. Grimes if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) 1459b50d902SRodney W. Grimes err("%s: %s", file, strerror(errno)); 1469b50d902SRodney W. Grimes if (sb.st_size > SIZE_T_MAX) 1479b50d902SRodney W. Grimes err("%s: %s", file, strerror(EFBIG)); 1489b50d902SRodney W. Grimes if ((front = mmap(NULL, 1498abdc2ebSAlexander Langer (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) 1509b50d902SRodney W. Grimes err("%s: %s", file, strerror(errno)); 1519b50d902SRodney W. Grimes back = front + sb.st_size; 1529b50d902SRodney W. Grimes exit(look(string, front, back)); 1539b50d902SRodney W. Grimes } 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes look(string, front, back) 156ac7154f5SAndrey A. Chernov unsigned char *string, *front, *back; 1579b50d902SRodney W. Grimes { 1589b50d902SRodney W. Grimes register int ch; 159ac7154f5SAndrey A. Chernov register unsigned char *readp, *writep; 1609b50d902SRodney W. Grimes 1619b50d902SRodney W. Grimes /* Reformat string string to avoid doing it multiple times later. */ 1629b50d902SRodney W. Grimes for (readp = writep = string; ch = *readp++;) { 1639b50d902SRodney W. Grimes if (fflag) 1649b50d902SRodney W. Grimes ch = FOLD(ch); 1659b50d902SRodney W. Grimes if (dflag) 1669b50d902SRodney W. Grimes ch = DICT(ch); 1679b50d902SRodney W. Grimes if (ch != NO_COMPARE) 1689b50d902SRodney W. Grimes *(writep++) = ch; 1699b50d902SRodney W. Grimes } 1709b50d902SRodney W. Grimes *writep = '\0'; 1719b50d902SRodney W. Grimes 1729b50d902SRodney W. Grimes front = binary_search(string, front, back); 1739b50d902SRodney W. Grimes front = linear_search(string, front, back); 1749b50d902SRodney W. Grimes 1759b50d902SRodney W. Grimes if (front) 1769b50d902SRodney W. Grimes print_from(string, front, back); 1779b50d902SRodney W. Grimes return (front ? 0 : 1); 1789b50d902SRodney W. Grimes } 1799b50d902SRodney W. Grimes 1809b50d902SRodney W. Grimes 1819b50d902SRodney W. Grimes /* 1829b50d902SRodney W. Grimes * Binary search for "string" in memory between "front" and "back". 1839b50d902SRodney W. Grimes * 1849b50d902SRodney W. Grimes * This routine is expected to return a pointer to the start of a line at 1859b50d902SRodney W. Grimes * *or before* the first word matching "string". Relaxing the constraint 1869b50d902SRodney W. Grimes * this way simplifies the algorithm. 1879b50d902SRodney W. Grimes * 1889b50d902SRodney W. Grimes * Invariants: 1899b50d902SRodney W. Grimes * front points to the beginning of a line at or before the first 1909b50d902SRodney W. Grimes * matching string. 1919b50d902SRodney W. Grimes * 1929b50d902SRodney W. Grimes * back points to the beginning of a line at or after the first 1939b50d902SRodney W. Grimes * matching line. 1949b50d902SRodney W. Grimes * 1959b50d902SRodney W. Grimes * Base of the Invariants. 1969b50d902SRodney W. Grimes * front = NULL; 1979b50d902SRodney W. Grimes * back = EOF; 1989b50d902SRodney W. Grimes * 1999b50d902SRodney W. Grimes * Advancing the Invariants: 2009b50d902SRodney W. Grimes * 2019b50d902SRodney W. Grimes * p = first newline after halfway point from front to back. 2029b50d902SRodney W. Grimes * 2039b50d902SRodney W. Grimes * If the string at "p" is not greater than the string to match, 2049b50d902SRodney W. Grimes * p is the new front. Otherwise it is the new back. 2059b50d902SRodney W. Grimes * 2069b50d902SRodney W. Grimes * Termination: 2079b50d902SRodney W. Grimes * 2089b50d902SRodney W. Grimes * The definition of the routine allows it return at any point, 2099b50d902SRodney W. Grimes * since front is always at or before the line to print. 2109b50d902SRodney W. Grimes * 2119b50d902SRodney W. Grimes * In fact, it returns when the chosen "p" equals "back". This 2129b50d902SRodney W. Grimes * implies that there exists a string is least half as long as 2139b50d902SRodney W. Grimes * (back - front), which in turn implies that a linear search will 2149b50d902SRodney W. Grimes * be no more expensive than the cost of simply printing a string or two. 2159b50d902SRodney W. Grimes * 2169b50d902SRodney W. Grimes * Trying to continue with binary search at this point would be 2179b50d902SRodney W. Grimes * more trouble than it's worth. 2189b50d902SRodney W. Grimes */ 2199b50d902SRodney W. Grimes #define SKIP_PAST_NEWLINE(p, back) \ 2209b50d902SRodney W. Grimes while (p < back && *p++ != '\n'); 2219b50d902SRodney W. Grimes 2229b50d902SRodney W. Grimes char * 2239b50d902SRodney W. Grimes binary_search(string, front, back) 224ac7154f5SAndrey A. Chernov register unsigned char *string, *front, *back; 2259b50d902SRodney W. Grimes { 226ac7154f5SAndrey A. Chernov register unsigned char *p; 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes p = front + (back - front) / 2; 2299b50d902SRodney W. Grimes SKIP_PAST_NEWLINE(p, back); 2309b50d902SRodney W. Grimes 2319b50d902SRodney W. Grimes /* 2329b50d902SRodney W. Grimes * If the file changes underneath us, make sure we don't 2339b50d902SRodney W. Grimes * infinitely loop. 2349b50d902SRodney W. Grimes */ 2359b50d902SRodney W. Grimes while (p < back && back > front) { 2369b50d902SRodney W. Grimes if (compare(string, p, back) == GREATER) 2379b50d902SRodney W. Grimes front = p; 2389b50d902SRodney W. Grimes else 2399b50d902SRodney W. Grimes back = p; 2409b50d902SRodney W. Grimes p = front + (back - front) / 2; 2419b50d902SRodney W. Grimes SKIP_PAST_NEWLINE(p, back); 2429b50d902SRodney W. Grimes } 2439b50d902SRodney W. Grimes return (front); 2449b50d902SRodney W. Grimes } 2459b50d902SRodney W. Grimes 2469b50d902SRodney W. Grimes /* 2479b50d902SRodney W. Grimes * Find the first line that starts with string, linearly searching from front 2489b50d902SRodney W. Grimes * to back. 2499b50d902SRodney W. Grimes * 2509b50d902SRodney W. Grimes * Return NULL for no such line. 2519b50d902SRodney W. Grimes * 2529b50d902SRodney W. Grimes * This routine assumes: 2539b50d902SRodney W. Grimes * 2549b50d902SRodney W. Grimes * o front points at the first character in a line. 2559b50d902SRodney W. Grimes * o front is before or at the first line to be printed. 2569b50d902SRodney W. Grimes */ 2579b50d902SRodney W. Grimes char * 2589b50d902SRodney W. Grimes linear_search(string, front, back) 259ac7154f5SAndrey A. Chernov unsigned char *string, *front, *back; 2609b50d902SRodney W. Grimes { 2619b50d902SRodney W. Grimes while (front < back) { 2629b50d902SRodney W. Grimes switch (compare(string, front, back)) { 2639b50d902SRodney W. Grimes case EQUAL: /* Found it. */ 2649b50d902SRodney W. Grimes return (front); 2659b50d902SRodney W. Grimes break; 2669b50d902SRodney W. Grimes case LESS: /* No such string. */ 2679b50d902SRodney W. Grimes return (NULL); 2689b50d902SRodney W. Grimes break; 2699b50d902SRodney W. Grimes case GREATER: /* Keep going. */ 2709b50d902SRodney W. Grimes break; 2719b50d902SRodney W. Grimes } 2729b50d902SRodney W. Grimes SKIP_PAST_NEWLINE(front, back); 2739b50d902SRodney W. Grimes } 2749b50d902SRodney W. Grimes return (NULL); 2759b50d902SRodney W. Grimes } 2769b50d902SRodney W. Grimes 2779b50d902SRodney W. Grimes /* 2789b50d902SRodney W. Grimes * Print as many lines as match string, starting at front. 2799b50d902SRodney W. Grimes */ 2809b50d902SRodney W. Grimes void 2819b50d902SRodney W. Grimes print_from(string, front, back) 282ac7154f5SAndrey A. Chernov register unsigned char *string, *front, *back; 2839b50d902SRodney W. Grimes { 2849b50d902SRodney W. Grimes for (; front < back && compare(string, front, back) == EQUAL; ++front) { 2859b50d902SRodney W. Grimes for (; front < back && *front != '\n'; ++front) 2869b50d902SRodney W. Grimes if (putchar(*front) == EOF) 2879b50d902SRodney W. Grimes err("stdout: %s", strerror(errno)); 2889b50d902SRodney W. Grimes if (putchar('\n') == EOF) 2899b50d902SRodney W. Grimes err("stdout: %s", strerror(errno)); 2909b50d902SRodney W. Grimes } 2919b50d902SRodney W. Grimes } 2929b50d902SRodney W. Grimes 2939b50d902SRodney W. Grimes /* 2949b50d902SRodney W. Grimes * Return LESS, GREATER, or EQUAL depending on how the string1 compares with 2959b50d902SRodney W. Grimes * string2 (s1 ??? s2). 2969b50d902SRodney W. Grimes * 2979b50d902SRodney W. Grimes * o Matches up to len(s1) are EQUAL. 2989b50d902SRodney W. Grimes * o Matches up to len(s2) are GREATER. 2999b50d902SRodney W. Grimes * 3009b50d902SRodney W. Grimes * Compare understands about the -f and -d flags, and treats comparisons 3019b50d902SRodney W. Grimes * appropriately. 3029b50d902SRodney W. Grimes * 3039b50d902SRodney W. Grimes * The string "s1" is null terminated. The string s2 is '\n' terminated (or 3049b50d902SRodney W. Grimes * "back" terminated). 3059b50d902SRodney W. Grimes */ 3069b50d902SRodney W. Grimes int 3079b50d902SRodney W. Grimes compare(s1, s2, back) 308ac7154f5SAndrey A. Chernov register unsigned char *s1, *s2, *back; 3099b50d902SRodney W. Grimes { 3109b50d902SRodney W. Grimes register int ch; 3119b50d902SRodney W. Grimes 3129b50d902SRodney W. Grimes for (; *s1 && s2 < back && *s2 != '\n'; ++s1, ++s2) { 3139b50d902SRodney W. Grimes ch = *s2; 3149b50d902SRodney W. Grimes if (fflag) 3159b50d902SRodney W. Grimes ch = FOLD(ch); 3169b50d902SRodney W. Grimes if (dflag) 3179b50d902SRodney W. Grimes ch = DICT(ch); 3189b50d902SRodney W. Grimes 3199b50d902SRodney W. Grimes if (ch == NO_COMPARE) { 3209b50d902SRodney W. Grimes ++s2; /* Ignore character in comparison. */ 3219b50d902SRodney W. Grimes continue; 3229b50d902SRodney W. Grimes } 3239b50d902SRodney W. Grimes if (*s1 != ch) 3249b50d902SRodney W. Grimes return (*s1 < ch ? LESS : GREATER); 3259b50d902SRodney W. Grimes } 3269b50d902SRodney W. Grimes return (*s1 ? GREATER : EQUAL); 3279b50d902SRodney W. Grimes } 3289b50d902SRodney W. Grimes 3299b50d902SRodney W. Grimes static void 3309b50d902SRodney W. Grimes usage() 3319b50d902SRodney W. Grimes { 3329b50d902SRodney W. Grimes (void)fprintf(stderr, "usage: look [-df] [-t char] string [file]\n"); 3339b50d902SRodney W. Grimes exit(2); 3349b50d902SRodney W. Grimes } 3359b50d902SRodney W. Grimes 3369b50d902SRodney W. Grimes #if __STDC__ 3379b50d902SRodney W. Grimes #include <stdarg.h> 3389b50d902SRodney W. Grimes #else 3399b50d902SRodney W. Grimes #include <varargs.h> 3409b50d902SRodney W. Grimes #endif 3419b50d902SRodney W. Grimes 3429b50d902SRodney W. Grimes void 3439b50d902SRodney W. Grimes #if __STDC__ 3449b50d902SRodney W. Grimes err(const char *fmt, ...) 3459b50d902SRodney W. Grimes #else 3469b50d902SRodney W. Grimes err(fmt, va_alist) 3479b50d902SRodney W. Grimes char *fmt; 3489b50d902SRodney W. Grimes va_dcl 3499b50d902SRodney W. Grimes #endif 3509b50d902SRodney W. Grimes { 3519b50d902SRodney W. Grimes va_list ap; 3529b50d902SRodney W. Grimes #if __STDC__ 3539b50d902SRodney W. Grimes va_start(ap, fmt); 3549b50d902SRodney W. Grimes #else 3559b50d902SRodney W. Grimes va_start(ap); 3569b50d902SRodney W. Grimes #endif 3579b50d902SRodney W. Grimes (void)fprintf(stderr, "look: "); 3589b50d902SRodney W. Grimes (void)vfprintf(stderr, fmt, ap); 3599b50d902SRodney W. Grimes va_end(ap); 3609b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 3619b50d902SRodney W. Grimes exit(2); 3629b50d902SRodney W. Grimes /* NOTREACHED */ 3639b50d902SRodney W. Grimes } 364