1 /* $NetBSD: look.c,v 1.11 2003/08/07 11:14:28 agc Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * David Hitz of Auspex Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"); 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95"; 44 #endif 45 __RCSID("$NetBSD: look.c,v 1.11 2003/08/07 11:14:28 agc Exp $"); 46 #endif /* not lint */ 47 48 /* 49 * look -- find lines in a sorted list. 50 * 51 * The man page said that TABs and SPACEs participate in -d comparisons. 52 * In fact, they were ignored. This implements historic practice, not 53 * the manual page. 54 */ 55 56 #include <sys/types.h> 57 #include <sys/mman.h> 58 #include <sys/stat.h> 59 60 #include <ctype.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <limits.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 #include <err.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) (isascii(c) && isupper(c) ? tolower(c) : (c)) 85 #define DICT(c) (isascii(c) && isalnum(c) ? (c) : NO_COMPARE) 86 87 int dflag, fflag; 88 89 char *binary_search __P((char *, char *, char *)); 90 int compare __P((char *, char *, char *)); 91 char *linear_search __P((char *, char *, char *)); 92 int look __P((char *, char *, char *)); 93 int main __P((int, char **)); 94 void print_from __P((char *, char *, char *)); 95 void usage __P((void)); 96 97 int 98 main(argc, argv) 99 int argc; 100 char *argv[]; 101 { 102 struct stat sb; 103 int ch, fd, termchar; 104 char *back, *file, *front, *string, *p; 105 106 string = NULL; 107 file = _PATH_WORDS; 108 termchar = '\0'; 109 while ((ch = getopt(argc, argv, "dft:")) != -1) 110 switch(ch) { 111 case 'd': 112 dflag = 1; 113 break; 114 case 'f': 115 fflag = 1; 116 break; 117 case 't': 118 termchar = *optarg; 119 break; 120 case '?': 121 default: 122 usage(); 123 } 124 argc -= optind; 125 argv += optind; 126 127 switch (argc) { 128 case 2: /* Don't set -df for user. */ 129 string = *argv++; 130 file = *argv; 131 break; 132 case 1: /* But set -df by default. */ 133 dflag = fflag = 1; 134 string = *argv; 135 break; 136 default: 137 usage(); 138 } 139 140 if (termchar != '\0' && (p = strchr(string, termchar)) != NULL) 141 *++p = '\0'; 142 143 if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb)) 144 err(2, "%s", file); 145 if (sb.st_size > SIZE_T_MAX) 146 err(2, "%s: %s", file, strerror(EFBIG)); 147 if ((front = mmap(NULL, (size_t)sb.st_size, 148 PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0)) == NULL) 149 err(2, "%s", file); 150 back = front + sb.st_size; 151 exit(look(string, front, back)); 152 } 153 154 int 155 look(string, front, back) 156 char *string, *front, *back; 157 { 158 int ch; 159 char *readp, *writep; 160 161 /* Reformat string string to avoid doing it multiple times later. */ 162 for (readp = writep = string; (ch = *readp++) != 0; ) { 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 222 char * 223 binary_search(string, front, back) 224 char *string, *front, *back; 225 { 226 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 */ 257 char * 258 linear_search(string, front, back) 259 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 */ 280 void 281 print_from(string, front, back) 282 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(2, "stdout"); 288 if (putchar('\n') == EOF) 289 err(2, "stdout"); 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 */ 306 int 307 compare(s1, s2, back) 308 char *s1, *s2, *back; 309 { 310 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 329 void 330 usage() 331 { 332 (void)fprintf(stderr, "usage: look [-df] [-t char] string [file]\n"); 333 exit(2); 334 } 335