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