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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. 33 * @(#)look.c 8.2 (Berkeley) 5/4/95 34 * $FreeBSD: src/usr.bin/look/look.c,v 1.11 1999/08/28 01:03:14 peter Exp $ 35 * $DragonFly: src/usr.bin/look/look.c,v 1.4 2005/01/07 02:43:41 cpressey Exp $ 36 */ 37 38 /* 39 * look -- find lines in a sorted list. 40 * 41 * The man page said that TABs and SPACEs participate in -d comparisons. 42 * In fact, they were ignored. This implements historic practice, not 43 * the manual page. 44 */ 45 46 #include <sys/types.h> 47 #include <sys/mman.h> 48 #include <sys/stat.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <fcntl.h> 54 #include <limits.h> 55 #include <locale.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include "pathnames.h" 62 63 /* 64 * FOLD and DICT convert characters to a normal form for comparison, 65 * according to the user specified flags. 66 * 67 * DICT expects integers because it uses a non-character value to 68 * indicate a character which should not participate in comparisons. 69 */ 70 #define EQUAL 0 71 #define GREATER 1 72 #define LESS (-1) 73 #define NO_COMPARE (-2) 74 75 #define FOLD(c) (isupper(c) ? tolower(c) : (unsigned char) (c)) 76 #define DICT(c) (isalnum(c) ? (c) & 0xFF /* int */ : NO_COMPARE) 77 78 int dflag, fflag; 79 80 char *binary_search(unsigned char *, unsigned char *, unsigned char *); 81 int compare(unsigned char *, unsigned char *, unsigned char *); 82 char *linear_search(unsigned char *, unsigned char *, unsigned char *); 83 int look(unsigned char *, unsigned char *, unsigned char *); 84 void print_from(unsigned char *, unsigned char *, unsigned char *); 85 86 static void usage(void); 87 88 int 89 main(int argc, char **argv) 90 { 91 struct stat sb; 92 int ch, fd, termchar, match; 93 unsigned char *back, *front, *string, *p; 94 const unsigned char *file; 95 size_t len; 96 97 setlocale(LC_CTYPE, ""); 98 99 file = _PATH_WORDS; 100 termchar = '\0'; 101 while ((ch = getopt(argc, argv, "dft:")) != -1) 102 switch(ch) { 103 case 'd': 104 dflag = 1; 105 break; 106 case 'f': 107 fflag = 1; 108 break; 109 case 't': 110 termchar = *optarg; 111 break; 112 case '?': 113 default: 114 usage(); 115 } 116 argc -= optind; 117 argv += optind; 118 119 if (argc == 0) 120 usage(); 121 if (argc == 1) /* But set -df by default. */ 122 dflag = fflag = 1; 123 string = *argv++; 124 if (argc >= 2) 125 file = *argv++; 126 127 if (termchar != '\0' && (p = strchr(string, termchar)) != NULL) 128 *++p = '\0'; 129 match = 1; 130 131 do { 132 if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) 133 err(2, "%s", file); 134 len = (size_t)sb.st_size; 135 if ((off_t)len != sb.st_size) { 136 errno = EFBIG; 137 err(2, "%s", file); 138 } 139 if ((front = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) 140 err(2, "%s", file); 141 back = front + sb.st_size; 142 match *= (look(string, front, back)); 143 close(fd); 144 } while (argc-- > 2 && (file = *argv++)); 145 146 exit(match); 147 } 148 149 int 150 look(unsigned char *string, unsigned char *front, unsigned char *back) 151 { 152 int ch; 153 unsigned char *readp, *writep; 154 155 /* Reformat string string to avoid doing it multiple times later. */ 156 for (readp = writep = string; (ch = *readp++) != '\0';) { 157 if (fflag) 158 ch = FOLD(ch); 159 if (dflag) 160 ch = DICT(ch); 161 if (ch != NO_COMPARE) 162 *(writep++) = ch; 163 } 164 *writep = '\0'; 165 166 front = binary_search(string, front, back); 167 front = linear_search(string, front, back); 168 169 if (front) 170 print_from(string, front, back); 171 return (front ? 0 : 1); 172 } 173 174 175 /* 176 * Binary search for "string" in memory between "front" and "back". 177 * 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. 181 * 182 * Invariants: 183 * front points to the beginning of a line at or before the first 184 * matching string. 185 * 186 * back points to the beginning of a line at or after the first 187 * matching line. 188 * 189 * Base of the Invariants. 190 * front = NULL; 191 * back = EOF; 192 * 193 * Advancing the Invariants: 194 * 195 * p = first newline after halfway point from front to back. 196 * 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. 199 * 200 * Termination: 201 * 202 * The definition of the routine allows it return at any point, 203 * since front is always at or before the line to print. 204 * 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. 209 * 210 * Trying to continue with binary search at this point would be 211 * more trouble than it's worth. 212 */ 213 #define SKIP_PAST_NEWLINE(p, back) \ 214 while (p < back && *p++ != '\n'); 215 216 char * 217 binary_search(unsigned char *string, unsigned char *front, 218 unsigned char *back) 219 { 220 unsigned char *p; 221 222 p = front + (back - front) / 2; 223 SKIP_PAST_NEWLINE(p, back); 224 225 /* 226 * If the file changes underneath us, make sure we don't 227 * infinitely loop. 228 */ 229 while (p < back && back > front) { 230 if (compare(string, p, back) == GREATER) 231 front = p; 232 else 233 back = p; 234 p = front + (back - front) / 2; 235 SKIP_PAST_NEWLINE(p, back); 236 } 237 return (front); 238 } 239 240 /* 241 * Find the first line that starts with string, linearly searching from front 242 * to back. 243 * 244 * Return NULL for no such line. 245 * 246 * This routine assumes: 247 * 248 * o front points at the first character in a line. 249 * o front is before or at the first line to be printed. 250 */ 251 char * 252 linear_search(unsigned char *string, unsigned char *front, unsigned char *back) 253 { 254 while (front < back) { 255 switch (compare(string, front, back)) { 256 case EQUAL: /* Found it. */ 257 return (front); 258 break; 259 case LESS: /* No such string. */ 260 return (NULL); 261 break; 262 case GREATER: /* Keep going. */ 263 break; 264 } 265 SKIP_PAST_NEWLINE(front, back); 266 } 267 return (NULL); 268 } 269 270 /* 271 * Print as many lines as match string, starting at front. 272 */ 273 void 274 print_from(unsigned char *string, unsigned char *front, 275 unsigned char *back) 276 { 277 for (; front < back && compare(string, front, back) == EQUAL; ++front) { 278 for (; front < back && *front != '\n'; ++front) 279 if (putchar(*front) == EOF) 280 err(2, "stdout"); 281 if (putchar('\n') == EOF) 282 err(2, "stdout"); 283 } 284 } 285 286 /* 287 * Return LESS, GREATER, or EQUAL depending on how the string1 compares with 288 * string2 (s1 ??? s2). 289 * 290 * o Matches up to len(s1) are EQUAL. 291 * o Matches up to len(s2) are GREATER. 292 * 293 * Compare understands about the -f and -d flags, and treats comparisons 294 * appropriately. 295 * 296 * The string "s1" is null terminated. The string s2 is '\n' terminated (or 297 * "back" terminated). 298 */ 299 int 300 compare(unsigned char *s1, unsigned char *s2, 301 unsigned char *back) 302 { 303 int ch; 304 305 for (; *s1 && s2 < back && *s2 != '\n'; ++s1, ++s2) { 306 ch = *s2; 307 if (fflag) 308 ch = FOLD(ch); 309 if (dflag) 310 ch = DICT(ch); 311 312 if (ch == NO_COMPARE) { 313 ++s2; /* Ignore character in comparison. */ 314 continue; 315 } 316 if (*s1 != ch) 317 return (*s1 < ch ? LESS : GREATER); 318 } 319 return (*s1 ? GREATER : EQUAL); 320 } 321 322 static void 323 usage(void) 324 { 325 fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n"); 326 exit(2); 327 } 328