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