1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * David Hitz of Auspex Systems, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 * 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.3 2003/10/04 20:36:48 hmp Exp $ 40 */ 41 42 /* 43 * look -- find lines in a sorted list. 44 * 45 * The man page said that TABs and SPACEs participate in -d comparisons. 46 * In fact, they were ignored. This implements historic practice, not 47 * the manual page. 48 */ 49 50 #include <sys/types.h> 51 #include <sys/mman.h> 52 #include <sys/stat.h> 53 54 #include <ctype.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <fcntl.h> 58 #include <limits.h> 59 #include <locale.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "pathnames.h" 66 67 /* 68 * FOLD and DICT convert characters to a normal form for comparison, 69 * according to the user specified flags. 70 * 71 * DICT expects integers because it uses a non-character value to 72 * indicate a character which should not participate in comparisons. 73 */ 74 #define EQUAL 0 75 #define GREATER 1 76 #define LESS (-1) 77 #define NO_COMPARE (-2) 78 79 #define FOLD(c) (isupper(c) ? tolower(c) : (unsigned char) (c)) 80 #define DICT(c) (isalnum(c) ? (c) & 0xFF /* int */ : NO_COMPARE) 81 82 int dflag, fflag; 83 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 *); 89 90 static void usage(void); 91 92 int 93 main(int argc, char **argv) 94 { 95 struct stat sb; 96 int ch, fd, termchar, match; 97 unsigned char *back, *file, *front, *string, *p; 98 99 (void) setlocale(LC_CTYPE, ""); 100 101 file = _PATH_WORDS; 102 termchar = '\0'; 103 while ((ch = getopt(argc, argv, "dft:")) != -1) 104 switch(ch) { 105 case 'd': 106 dflag = 1; 107 break; 108 case 'f': 109 fflag = 1; 110 break; 111 case 't': 112 termchar = *optarg; 113 break; 114 case '?': 115 default: 116 usage(); 117 } 118 argc -= optind; 119 argv += optind; 120 121 if (argc == 0) 122 usage(); 123 if (argc == 1) /* But set -df by default. */ 124 dflag = fflag = 1; 125 string = *argv++; 126 if (argc >= 2) 127 file = *argv++; 128 129 if (termchar != '\0' && (p = strchr(string, termchar)) != NULL) 130 *++p = '\0'; 131 match = 1; 132 133 do { 134 if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) 135 err(2, "%s", file); 136 if (sb.st_size > SIZE_T_MAX) 137 errx(2, "%s: %s", file, strerror(EFBIG)); 138 if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) 139 err(2, "%s", file); 140 back = front + sb.st_size; 141 match *= (look(string, front, back)); 142 close(fd); 143 } while (argc-- > 2 && (file = *argv++)); 144 145 exit(match); 146 } 147 148 int 149 look(unsigned char *string, unsigned char *front, unsigned char *back) 150 { 151 register int ch; 152 register unsigned char *readp, *writep; 153 154 /* Reformat string string to avoid doing it multiple times later. */ 155 for (readp = writep = string; ch = *readp++;) { 156 if (fflag) 157 ch = FOLD(ch); 158 if (dflag) 159 ch = DICT(ch); 160 if (ch != NO_COMPARE) 161 *(writep++) = ch; 162 } 163 *writep = '\0'; 164 165 front = binary_search(string, front, back); 166 front = linear_search(string, front, back); 167 168 if (front) 169 print_from(string, front, back); 170 return (front ? 0 : 1); 171 } 172 173 174 /* 175 * Binary search for "string" in memory between "front" and "back". 176 * 177 * This routine is expected to return a pointer to the start of a line at 178 * *or before* the first word matching "string". Relaxing the constraint 179 * this way simplifies the algorithm. 180 * 181 * Invariants: 182 * front points to the beginning of a line at or before the first 183 * matching string. 184 * 185 * back points to the beginning of a line at or after the first 186 * matching line. 187 * 188 * Base of the Invariants. 189 * front = NULL; 190 * back = EOF; 191 * 192 * Advancing the Invariants: 193 * 194 * p = first newline after halfway point from front to back. 195 * 196 * If the string at "p" is not greater than the string to match, 197 * p is the new front. Otherwise it is the new back. 198 * 199 * Termination: 200 * 201 * The definition of the routine allows it return at any point, 202 * since front is always at or before the line to print. 203 * 204 * In fact, it returns when the chosen "p" equals "back". This 205 * implies that there exists a string is least half as long as 206 * (back - front), which in turn implies that a linear search will 207 * be no more expensive than the cost of simply printing a string or two. 208 * 209 * Trying to continue with binary search at this point would be 210 * more trouble than it's worth. 211 */ 212 #define SKIP_PAST_NEWLINE(p, back) \ 213 while (p < back && *p++ != '\n'); 214 215 char * 216 binary_search(register unsigned char *string, register unsigned char *front, 217 register unsigned char *back) 218 { 219 register unsigned char *p; 220 221 p = front + (back - front) / 2; 222 SKIP_PAST_NEWLINE(p, back); 223 224 /* 225 * If the file changes underneath us, make sure we don't 226 * infinitely loop. 227 */ 228 while (p < back && back > front) { 229 if (compare(string, p, back) == GREATER) 230 front = p; 231 else 232 back = p; 233 p = front + (back - front) / 2; 234 SKIP_PAST_NEWLINE(p, back); 235 } 236 return (front); 237 } 238 239 /* 240 * Find the first line that starts with string, linearly searching from front 241 * to back. 242 * 243 * Return NULL for no such line. 244 * 245 * This routine assumes: 246 * 247 * o front points at the first character in a line. 248 * o front is before or at the first line to be printed. 249 */ 250 char * 251 linear_search(unsigned char *string, unsigned char *front, unsigned char *back) 252 { 253 while (front < back) { 254 switch (compare(string, front, back)) { 255 case EQUAL: /* Found it. */ 256 return (front); 257 break; 258 case LESS: /* No such string. */ 259 return (NULL); 260 break; 261 case GREATER: /* Keep going. */ 262 break; 263 } 264 SKIP_PAST_NEWLINE(front, back); 265 } 266 return (NULL); 267 } 268 269 /* 270 * Print as many lines as match string, starting at front. 271 */ 272 void 273 print_from(register unsigned char *string, register unsigned char *front, 274 register unsigned char *back) 275 { 276 for (; front < back && compare(string, front, back) == EQUAL; ++front) { 277 for (; front < back && *front != '\n'; ++front) 278 if (putchar(*front) == EOF) 279 err(2, "stdout"); 280 if (putchar('\n') == EOF) 281 err(2, "stdout"); 282 } 283 } 284 285 /* 286 * Return LESS, GREATER, or EQUAL depending on how the string1 compares with 287 * string2 (s1 ??? s2). 288 * 289 * o Matches up to len(s1) are EQUAL. 290 * o Matches up to len(s2) are GREATER. 291 * 292 * Compare understands about the -f and -d flags, and treats comparisons 293 * appropriately. 294 * 295 * The string "s1" is null terminated. The string s2 is '\n' terminated (or 296 * "back" terminated). 297 */ 298 int 299 compare(register unsigned char *s1, register unsigned char *s2, 300 register unsigned char *back) 301 { 302 register int ch; 303 304 for (; *s1 && s2 < back && *s2 != '\n'; ++s1, ++s2) { 305 ch = *s2; 306 if (fflag) 307 ch = FOLD(ch); 308 if (dflag) 309 ch = DICT(ch); 310 311 if (ch == NO_COMPARE) { 312 ++s2; /* Ignore character in comparison. */ 313 continue; 314 } 315 if (*s1 != ch) 316 return (*s1 < ch ? LESS : GREATER); 317 } 318 return (*s1 ? GREATER : EQUAL); 319 } 320 321 static void 322 usage(void) 323 { 324 (void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n"); 325 exit(2); 326 } 327