Annotation of researchv8dc/cmd/look.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <ctype.h>
        !             3: 
        !             4: #define DICT "/usr/dict/words"
        !             5: 
        !             6: char *filename = DICT;
        !             7: FILE *dfile;
        !             8: 
        !             9: int fold;
        !            10: int direc;
        !            11: int exact;
        !            12: int iflag;
        !            13: int rev = 1;   /*-1 for reverse-ordered file, not implemented*/
        !            14: int acomp();
        !            15: int ncomp();
        !            16: int (*compare)() = acomp;
        !            17: int tab;
        !            18: char entry[250];
        !            19: char word[250];
        !            20: char key[50];
        !            21: 
        !            22: main(argc,argv)
        !            23: char **argv;
        !            24: {
        !            25:        char *arg;
        !            26:        while(argc>=2 && *argv[1]=='-') {
        !            27:                for(arg=argv[1];;arg++) {
        !            28:                        switch(arg[1]) {
        !            29:                        case 'd':
        !            30:                                direc++;
        !            31:                                continue;
        !            32:                        case 'f':
        !            33:                                fold++;
        !            34:                                continue;
        !            35:                        case 'i': 
        !            36:                                iflag++;
        !            37:                                continue;
        !            38:                        case 'n':
        !            39:                                compare = ncomp;
        !            40:                                continue;
        !            41:                        case 't':
        !            42:                                tab = arg[2];
        !            43:                                if(tab)
        !            44:                                        ++arg;
        !            45:                                continue;
        !            46:                        case 'x':
        !            47:                                exact++;
        !            48:                                continue;
        !            49:                        case 0:
        !            50:                                break;
        !            51:                        default:
        !            52:                                fprintf(stderr,"look: bad option %s\n",arg);
        !            53:                                return(2);
        !            54:                        }
        !            55:                        break;
        !            56:                }
        !            57:                argc --;
        !            58:                argv++;
        !            59:        }
        !            60:        if(!iflag) {
        !            61:                if(argc<2)
        !            62:                        iflag++;
        !            63:                else {
        !            64:                        canon(argv[1],key);
        !            65:                        argv++;
        !            66:                        argc--;
        !            67:                }
        !            68:        }
        !            69:        if(argc>=2)
        !            70:                filename = argv[1];
        !            71:        else {
        !            72:                direc++;
        !            73:                fold++;
        !            74:        }
        !            75:        dfile = fopen(filename,"r");
        !            76:        if(dfile==NULL) {
        !            77:                fprintf(stderr,"look: can't open %s\n",filename);
        !            78:                return(2);
        !            79:        }
        !            80:        if(!iflag) {
        !            81:                if(locate(key,entry)==0)
        !            82:                        return(1);
        !            83:        }
        !            84:        do {
        !            85:                if(iflag) {
        !            86:                        if(gets(entry)==0)
        !            87:                                return(0);
        !            88:                        canon(entry,key);
        !            89:                        if(locate(key,entry)==0)
        !            90:                                continue;
        !            91:                }
        !            92:                puts(entry,stdout);
        !            93:                while(getword(entry)) {
        !            94:                        canon(entry,word);
        !            95:                        switch((*compare)(key,word)) {
        !            96:                        case -1:
        !            97:                                if(exact)
        !            98:                                        break;
        !            99:                        case 0:
        !           100:                                puts(entry,stdout);
        !           101:                                continue;
        !           102:                        }
        !           103:                        break;
        !           104:                }
        !           105:        } while(iflag);
        !           106:        return(0);
        !           107: 
        !           108: }
        !           109: 
        !           110: locate(key,entry)
        !           111: char *key;
        !           112: {
        !           113:        long top,bot,mid;
        !           114:        register c;
        !           115:        bot = 0;
        !           116:        fseek(dfile,0L,2);
        !           117:        top = ftell(dfile);
        !           118:        for(;;) {
        !           119:                mid = (top+bot)/2;
        !           120:                fseek(dfile,mid,0);
        !           121:                do {
        !           122:                        c = getc(dfile);
        !           123:                        mid++;
        !           124:                } while(c!=EOF && c!='\n');
        !           125:                if(!getword(entry))
        !           126:                        break;
        !           127:                canon(entry,word);
        !           128:                switch((*compare)(key,word)) {
        !           129:                case -2:
        !           130:                case -1:
        !           131:                case 0:
        !           132:                        if(top<=mid)
        !           133:                                break;
        !           134:                        top = mid;
        !           135:                        continue;
        !           136:                case 1:
        !           137:                case 2:
        !           138:                        bot = mid;
        !           139:                        continue;
        !           140:                }
        !           141:                break;
        !           142:        }
        !           143:        fseek(dfile,bot,0);
        !           144:        while(getword(entry)) {
        !           145:                canon(entry,word);
        !           146:                switch((*compare)(key,word)) {
        !           147:                case -2:
        !           148:                        return(0);
        !           149:                case -1:
        !           150:                        if(exact)
        !           151:                                return(0);
        !           152:                case 0:
        !           153:                        return(1);
        !           154:                case 1:
        !           155:                case 2:
        !           156:                        continue;
        !           157:                }
        !           158:        }
        !           159:        return(0);
        !           160: }
        !           161: 
        !           162: /* acomp(s,t) returns -2 if s strictly precedes t
        !           163:                      -1 if s is a prefix of t
        !           164:                      0 if s is the same as t
        !           165:                      1 if t is a prefix of s
        !           166:                      2 if t strictly precedes s
        !           167: */
        !           168: 
        !           169: acomp(s,t)
        !           170: register char *s,*t;
        !           171: {
        !           172:        for(;*s==*t;s++,t++)
        !           173:                if(*s==0)
        !           174:                        return(0);
        !           175:        return(*s==0? -1:
        !           176:                *t==0? 1:
        !           177:                *s<*t? -2:
        !           178:                2);
        !           179: }
        !           180: 
        !           181: getword(w)
        !           182: char *w;
        !           183: {
        !           184:        register c;
        !           185:        for(;;) {
        !           186:                c = getc(dfile);
        !           187:                if(c==EOF)
        !           188:                        return(0);
        !           189:                if(c=='\n')
        !           190:                        break;
        !           191:                *w++ = c;
        !           192:        }
        !           193:        *w = 0;
        !           194:        return(1);
        !           195: }
        !           196: 
        !           197: canon(old,new)
        !           198: char *old,*new;
        !           199: {
        !           200:        register c;
        !           201: /*     printf(">%s\n",old); */
        !           202:        for(;;) {
        !           203:                *new = c = *old++;
        !           204:                if(c==0||c==tab) {
        !           205:                        *new = 0;
        !           206:                        break;
        !           207:                }
        !           208:                if(direc) {
        !           209:                        if(!isalnum(c))
        !           210:                                continue;
        !           211:                }
        !           212:                if(fold) {
        !           213:                        if(isupper(c))
        !           214:                                *new += 'a' - 'A';
        !           215:                }
        !           216:                new++;
        !           217:        }
        !           218: }
        !           219: 
        !           220: ncomp(s,t)
        !           221: register char *s, *t;
        !           222: {
        !           223:        char *is, *it, *js, *jt;
        !           224:        int a, b;
        !           225:        int ssgn, tsgn;
        !           226:        while(isspace(*s))
        !           227:                s++;
        !           228:        while(isspace(*t))
        !           229:                t++;
        !           230:        ssgn = tsgn = -2*rev;
        !           231:        if(*s == '-') {
        !           232:                s++;
        !           233:                ssgn = -ssgn;
        !           234:        }
        !           235:        if(*t == '-') {
        !           236:                t++;
        !           237:                tsgn = -tsgn;
        !           238:        }
        !           239:        for(is = s; isdigit(*is); is++) ;
        !           240:        for(it = t; isdigit(*it); it++) ;
        !           241:        js = is;
        !           242:        jt = it;
        !           243:        a = 0;
        !           244:        if(ssgn==tsgn)
        !           245:                while(it>t && is>s)
        !           246:                        if(b = *--it - *--is)
        !           247:                                a = b;
        !           248:        while(is > s)
        !           249:                if(*--is != '0')
        !           250:                        return(-ssgn);
        !           251:        while(it > t)
        !           252:                if(*--it != '0')
        !           253:                        return(tsgn);
        !           254:        if(a) return(sgn(a)*ssgn);
        !           255:        if(*(s=js) == '.')
        !           256:                s++;
        !           257:        if(*(t=jt) == '.')
        !           258:                t++;
        !           259:        if(ssgn==tsgn)
        !           260:                while(isdigit(*s) && isdigit(*t))
        !           261:                        if(a = *t++ - *s++)
        !           262:                                return(sgn(a)*ssgn);
        !           263:        while(isdigit(*s))
        !           264:                if(*s++ != '0')
        !           265:                        return(-ssgn);
        !           266:        while(isdigit(*t))
        !           267:                if(*t++ != '0')
        !           268:                        return(tsgn);
        !           269:        return(0);
        !           270: }

unix.superglobalmegacorp.com

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