Annotation of researchv10no/cmd/dict/namelook.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <ctype.h>
                      3: extern char *ggets(), *fgets(), *strchr();
                      4: 
                      5: #define DICT "/s5/hanks/hanks2"
                      6: 
                      7: char *filename = DICT;
                      8: FILE *dfile;
                      9: 
                     10: int exact;
                     11: int iflag;
                     12: int acomp();
                     13: int (*compare)() = acomp;
                     14: char *strchr();
                     15: int tab = 0374;
                     16: char entry[250];
                     17: char word[250];
                     18: char key[50];
                     19: 
                     20: main(argc,argv)
                     21: char **argv;
                     22: {
                     23:        char *arg;
                     24:        while(argc>=2 && *argv[1]=='-') {
                     25:                for(arg=argv[1];;arg++) {
                     26:                        switch(arg[1]) {
                     27:                        case 'i': 
                     28:                                iflag++;
                     29:                                continue;
                     30:                        case 'x':
                     31:                                exact++;
                     32:                                continue;
                     33:                        case 0:
                     34:                                break;
                     35:                        default:
                     36:                                fprintf(stderr,"namelook: bad option %s\n",arg);
                     37:                                return(2);
                     38:                        }
                     39:                        break;
                     40:                }
                     41:                argc --;
                     42:                argv++;
                     43:        }
                     44:        if(!iflag) {
                     45:                if(argc<2)
                     46:                        iflag++;
                     47:                else {
                     48:                        canon(argv[1],key);
                     49:                        argv++;
                     50:                        argc--;
                     51:                }
                     52:        }
                     53:        if(argc>=2)
                     54:                filename = argv[1];
                     55:        dfile = fopen(filename,"r");
                     56:        if(dfile==NULL) {
                     57:                fprintf(stderr,"namelook: can't open %s\n",filename);
                     58:                return(2);
                     59:        }
                     60:        if(!iflag) {
                     61:                if(locate(key,entry)==0)
                     62:                        return(1);
                     63:        }
                     64:        do {
                     65:                if(iflag) {
                     66:                        if(ggets(entry,sizeof entry,stdin)==0)
                     67:                                return(0);
                     68:                        canon(entry,key);
                     69:                        if(locate(key,entry)==0)
                     70:                                continue;
                     71:                }
                     72:                put(entry,dfile,stdout);
                     73:                while(getword(entry)) {
                     74:                        canon(entry,word);
                     75:                        switch((*compare)(key,word)) {
                     76:                        case -1:
                     77:                                if(exact)
                     78:                                        break;
                     79:                        case 0:
                     80:                                put(entry,dfile,stdout);
                     81:                                continue;
                     82:                        }
                     83:                        break;
                     84:                }
                     85:        } while(iflag);
                     86:        return(0);
                     87: 
                     88: }
                     89: 
                     90: locate(key,entry)
                     91: char *key,*entry;
                     92: {
                     93:        long top,bot,mid;
                     94:        register c;
                     95:        bot = 0;
                     96:        fseek(dfile,0L,2);
                     97:        top = ftell(dfile);
                     98:        for(;;) {
                     99:                mid = (top+bot)/2;
                    100:                fseek(dfile,mid,0);
                    101:                do {
                    102:                        c = getc(dfile);
                    103:                        mid++;
                    104:                } while(c!=EOF && c!='\n');
                    105:                /*printf("%ld %ld %ld\n",bot,mid,top);*/
                    106:                if(!getword(entry))
                    107:                        break;
                    108:                canon(entry,word);
                    109:                switch((*compare)(key,word)) {
                    110:                case -2:
                    111:                case -1:
                    112:                case 0:
                    113:                        if(top<=mid)
                    114:                                break;
                    115:                        top = mid;
                    116:                        continue;
                    117:                case 1:
                    118:                case 2:
                    119:                        bot = mid;
                    120:                        continue;
                    121:                }
                    122:                break;
                    123:        }
                    124:        fseek(dfile,bot,0);
                    125:        while(getword(entry)) {
                    126:                canon(entry,word);
                    127:                switch((*compare)(key,word)) {
                    128:                case -2:
                    129:                        return(0);
                    130:                case -1:
                    131:                        if(exact)
                    132:                                return(0);
                    133:                case 0:
                    134:                        return(1);
                    135:                case 1:
                    136:                case 2:
                    137:                                continue;
                    138:                }
                    139:        }
                    140:        return(0);
                    141: }
                    142: 
                    143: /* acomp(s,t) returns -2 if s strictly precedes t
                    144:                      -1 if s is a prefix of t
                    145:                      0 if s is the same as t
                    146:                      1 if t is a prefix of s
                    147:                      2 if t strictly precedes s
                    148: */
                    149: 
                    150: acomp(s,t)
                    151: register char *s,*t;
                    152: {
                    153:        /*printf("<%s %s\n", s,t);*/
                    154:        for(;*s==*t;s++,t++)
                    155:                if(*s==0)
                    156:                        return(0);
                    157:        return(*s==0? -1:
                    158:                *t==0? 1:
                    159:                *s<*t? -2:
                    160:                2);
                    161: }
                    162: 
                    163: getword(w)
                    164: char *w;
                    165: {
                    166:        register c;
                    167:        for(;;) {
                    168:                c = getc(dfile);
                    169:                if(c==EOF)
                    170:                        return(0);
                    171:                if(c=='\n')
                    172:                        break;
                    173:                *w++ = c;
                    174:        }
                    175:        *w = 0;
                    176:        return(1);
                    177: }
                    178: 
                    179: put(entry, ifile, ofile)
                    180: char *entry;
                    181: FILE *ofile, *ifile;
                    182: {
                    183:        char *p = strchr(entry+1, '%');
                    184:        fprintf(ofile,"%s\n", p? p: entry);
                    185:        fflush(ofile);
                    186: }
                    187: 
                    188: canon(old,new)
                    189: unsigned char *old,*new;
                    190: {
                    191:        register c;
                    192:        unsigned char *savo = old, *savn = new;
                    193:        for(;;) {
                    194:                *new = c = *old++;
                    195:                if(c==0 || c==' ' && *old=='%')
                    196:                        break;
                    197:                if(!isalpha(c))
                    198:                        continue;
                    199:                if(isupper(c))
                    200:                        *new += 'a' - 'A';
                    201:                new++;
                    202:        }
                    203:        *new = 0;
                    204:        /*printf(">%s %s\n",savo,savn);*/
                    205: }
                    206: 
                    207: char *
                    208: ggets(s,n,f)
                    209: char *s;
                    210: FILE *f;
                    211: {
                    212:        char *p = fgets(s,n,f);
                    213:        char *q;
                    214:        if(p && (q=strchr(s,'\n')))
                    215:                *q = 0;
                    216:        return p;
                    217: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.