Annotation of researchv10no/cmd/dict/namelook1.c, revision 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,"namelook1: 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,"namelook1: 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:                for(;;) {
        !           102:                        mid++;
        !           103:                        if(getc(dfile)!='%')
        !           104:                                continue;
        !           105:                        mid++;
        !           106:                        if(getc(dfile)!='8')
        !           107:                                continue;
        !           108:                        break;
        !           109:                }
        !           110:                /*printf("%ld %ld %ld\n",bot,mid,top);*/
        !           111:                if(!getword(entry))
        !           112:                        break;
        !           113:                canon(entry,word);
        !           114:                switch((*compare)(key,word)) {
        !           115:                case -2:
        !           116:                case -1:
        !           117:                case 0:
        !           118:                        if(top<=mid)
        !           119:                                break;
        !           120:                        top = mid;
        !           121:                        continue;
        !           122:                case 1:
        !           123:                case 2:
        !           124:                        bot = mid;
        !           125:                        continue;
        !           126:                }
        !           127:                break;
        !           128:        }
        !           129:        fseek(dfile,bot,0);
        !           130:        while(getword(entry)) {
        !           131:                canon(entry,word);
        !           132:                switch((*compare)(key,word)) {
        !           133:                case -2:
        !           134:                        return(0);
        !           135:                case -1:
        !           136:                        if(exact)
        !           137:                                return(0);
        !           138:                case 0:
        !           139:                        return(1);
        !           140:                case 1:
        !           141:                case 2:
        !           142:                        for(;;) {
        !           143:                                c = getc(dfile);
        !           144:                                if(c==EOF)
        !           145:                                        return 0;
        !           146:                                if(c!='%')
        !           147:                                        continue;
        !           148:                                if(getc(dfile)!='8')
        !           149:                                        continue;
        !           150:                                getc(dfile);
        !           151:                                break;
        !           152:                        }
        !           153:                        continue;
        !           154:                }
        !           155:        }
        !           156:        return(0);
        !           157: }
        !           158: 
        !           159: /* acomp(s,t) returns -2 if s strictly precedes t
        !           160:                      -1 if s is a prefix of t
        !           161:                      0 if s is the same as t
        !           162:                      1 if t is a prefix of s
        !           163:                      2 if t strictly precedes s
        !           164: */
        !           165: 
        !           166: acomp(s,t)
        !           167: register char *s,*t;
        !           168: {
        !           169:        /*printf("<%s %s\n", s,t);*/
        !           170:        for(;*s==*t;s++,t++)
        !           171:                if(*s==0)
        !           172:                        return(0);
        !           173:        return(*s==0? -1:
        !           174:                *t==0? 1:
        !           175:                *s<*t? -2:
        !           176:                2);
        !           177: }
        !           178: 
        !           179: getword(w)
        !           180: char *w;
        !           181: {
        !           182:        register c;
        !           183:        for(;;) {
        !           184:                c = getc(dfile);
        !           185:                if(c==EOF)
        !           186:                        return(0);
        !           187:                if(c=='\n')
        !           188:                        break;
        !           189:                *w++ = c;
        !           190:        }
        !           191:        *w = 0;
        !           192:        return(1);
        !           193: }
        !           194: 
        !           195: put(entry, ifile, ofile)
        !           196: char *entry;
        !           197: FILE *ofile, *ifile;
        !           198: {
        !           199:        char buffer[200];
        !           200:        int i;
        !           201:        fprintf(ofile, "%s\n", entry);
        !           202:        for(i=0; i<2; ) {
        !           203:                fgets(buffer,sizeof(buffer)-1,ifile);
        !           204:                fputs(buffer, ofile);
        !           205:                i = *buffer=='\n'? i+1: 0;
        !           206:        }
        !           207:        fflush(ofile);
        !           208: }
        !           209: 
        !           210: canon(old,new)
        !           211: char *old,*new;
        !           212: {
        !           213:        register c;
        !           214:        char *savo = old, *savn = new;
        !           215:        for(;;) {
        !           216:                *new = c = *old++;
        !           217:                if(c==0 || c==' ' && *old=='%')
        !           218:                        break;
        !           219:                if(!isalpha(c))
        !           220:                        continue;
        !           221:                if(isupper(c))
        !           222:                        *new += 'a' - 'A';
        !           223:                new++;
        !           224:        }
        !           225:        *new = 0;
        !           226:        /*printf(">%s %s\n",savo,savn);*/
        !           227: }
        !           228: 
        !           229: char *
        !           230: ggets(s,n,f)
        !           231: char *s;
        !           232: FILE *f;
        !           233: {
        !           234:        char *p = fgets(s,n,f);
        !           235:        char *q;
        !           236:        if(p && (q=strchr(s,'\n')))
        !           237:                *q = 0;
        !           238:        return p;
        !           239: }

unix.superglobalmegacorp.com

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