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