1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 2 /* All Rights Reserved */ 3 4 5 /* 6 * Copyright (c) 1980 Regents of the University of California. 7 * All rights reserved. The Berkeley software License Agreement 8 * specifies the terms and conditions for redistribution. 9 */ 10 11 /* 12 * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc. 13 * All Rights Reserved. 14 */ 15 16 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */ 17 18 #include <stdio.h> 19 #include <ctype.h> 20 #include <string.h> 21 22 FILE *dfile; 23 char *filenam = "/usr/share/lib/dict/words"; 24 25 int fold; 26 int dict; 27 int tab; 28 #define WORDSIZE 257 29 char entry[WORDSIZE]; 30 char word[WORDSIZE]; 31 char key[WORDSIZE]; 32 33 main(argc,argv) 34 char **argv; 35 { 36 register c; 37 long top,bot,mid; 38 char *wstring, *ptr; 39 40 while(argc>=2 && *argv[1]=='-') { 41 for(;;) { 42 switch(*++argv[1]) { 43 case 'd': 44 dict++; 45 continue; 46 case 'f': 47 fold++; 48 continue; 49 case 't': 50 tab = argv[1][1]; 51 if(tab) 52 ++argv[1]; 53 continue; 54 case 0: 55 break; 56 default: 57 continue; 58 } 59 break; 60 } 61 argc --; 62 argv++; 63 } 64 if(argc<=1) 65 return; 66 if(argc==2) { 67 fold++; 68 dict++; 69 } else 70 filenam = argv[2]; 71 dfile = fopen(filenam,"r"); 72 if(dfile==NULL) { 73 fprintf(stderr,"look: can't open %s\n",filenam); 74 exit(2); 75 } 76 wstring = strdup(argv[1]); 77 if (tab != NULL) { 78 if ((ptr = strchr(wstring, tab)) != NULL) { 79 *++ptr = '\0'; 80 } 81 } 82 canon(wstring,key); 83 bot = 0; 84 fseek(dfile,0L,2); 85 top = ftell(dfile); 86 for(;;) { 87 mid = (top+bot)/2; 88 fseek(dfile,mid,0); 89 do { 90 c = getc(dfile); 91 mid++; 92 } while(c!=EOF && c!='\n'); 93 if(!getword(entry)) 94 break; 95 canon(entry,word); 96 switch(compare(key,word)) { 97 case -2: 98 case -1: 99 case 0: 100 if(top<=mid) 101 break; 102 top = mid; 103 continue; 104 case 1: 105 case 2: 106 bot = mid; 107 continue; 108 } 109 break; 110 } 111 fseek(dfile,bot,0); 112 while(ftell(dfile)<top) { 113 if(!getword(entry)) 114 return; 115 canon(entry,word); 116 switch(compare(key,word)) { 117 case -2: 118 return; 119 case -1: 120 case 0: 121 puts(entry); 122 break; 123 case 1: 124 case 2: 125 continue; 126 } 127 break; 128 } 129 while(getword(entry)) { 130 canon(entry,word); 131 switch(compare(key,word)) { 132 case -1: 133 case 0: 134 puts(entry); 135 continue; 136 } 137 break; 138 } 139 exit(0); 140 } 141 142 compare(s,t) 143 register char *s,*t; 144 { 145 for(;*s==*t;s++,t++) 146 if(*s==0) 147 return(0); 148 return(*s==0? -1: 149 *t==0? 1: 150 *s<*t? -2: 151 2); 152 } 153 154 getword(w) 155 register char *w; 156 { 157 register c; 158 register avail = WORDSIZE - 1; 159 160 while(avail--) { 161 c = getc(dfile); 162 if(c==EOF) 163 return(0); 164 if(c=='\n') 165 break; 166 *w++ = c; 167 } 168 while (c != '\n') 169 c = getc(dfile); 170 *w = 0; 171 return(1); 172 } 173 174 canon(old,new) 175 char *old,*new; 176 { 177 register c; 178 register avail = WORDSIZE - 1; 179 180 for(;;) { 181 *new = c = *old++; 182 if(c==0) { 183 *new = 0; 184 break; 185 } 186 if(dict) { 187 if(!isalnum(c)) 188 continue; 189 } 190 if(fold) { 191 if(isupper(c)) 192 *new += 'a' - 'A'; 193 } 194 new++; 195 avail--; 196 if (avail <= 0) { 197 *new = 0; 198 break; 199 } 200 } 201 } 202