Annotation of researchv8dc/cmd/p/spname.c, revision 1.1.1.1

1.1       root        1: #include <sys/types.h>
                      2: #include <sys/dir.h>
                      3: /*
                      4:  * char *spname(name)
                      5:  *     char name[];
                      6:  *
                      7:  * returns pointer to correctly spelled name,
                      8:  * or 0 if no reasonable name is found;
                      9:  * uses a static buffer to store correct name,
                     10:  * so copy it if you want to call the routine again.
                     11:  */
                     12: char *
                     13: spname(name)
                     14:        register char *name;
                     15: {
                     16:        register char *p, *q, *new;
                     17:        register d, nd, dir;
                     18:        static char newname[80], guess[DIRSIZ+1], best[DIRSIZ+1];
                     19:        static struct{
                     20:                ino_t ino;
                     21:                char name[DIRSIZ+1];
                     22:        } nbuf;
                     23: 
                     24:        new = newname;
                     25:        nbuf.name[DIRSIZ] = '\0';
                     26:        for(;;){
                     27:                while(*name == '/')
                     28:                        *new++ = *name++;
                     29:                *new = '\0';
                     30:                if(*name == '\0')
                     31:                        return(newname);
                     32:                p = guess;
                     33:                while(*name!='/' && *name!='\0'){
                     34:                        if(p != guess+DIRSIZ)
                     35:                                *p++ = *name;
                     36:                        name++;
                     37:                }
                     38:                *p = '\0';
                     39:                if((dir=open(newname,0)) < 0)
                     40:                        return((char *)0);
                     41:                d = 3;
                     42:                while(read(dir, &nbuf, sizeof (struct direct)) == sizeof (struct direct))
                     43:                        if(nbuf.ino){
                     44:                                nd=SPdist(nbuf.name, guess);
                     45:                                if(nd<=d && nd!=3) {    /* <= to avoid "." */
                     46:                                        p = best;
                     47:                                        q = nbuf.name;
                     48:                                        do; while(*p++ = *q++);
                     49:                                        d = nd;
                     50:                                        if(d == 0)
                     51:                                                break;
                     52:                                }
                     53:                        }
                     54:                close(dir);
                     55:                if(d == 3)
                     56:                        return(0);
                     57:                p = best;
                     58:                do; while(*new++ = *p++);
                     59:                --new;
                     60:        }
                     61: }
                     62: /*
                     63:  * very rough spelling metric
                     64:  * 0 if the strings are identical
                     65:  * 1 if two chars are interchanged
                     66:  * 2 if one char wrong, added or deleted
                     67:  * 3 otherwise
                     68:  */
                     69: SPdist(s, t)
                     70:        register char *s, *t;
                     71: {
                     72:        while(*s++ == *t)
                     73:                if(*t++ == '\0')
                     74:                        return(0);
                     75:        if(*--s){
                     76:                if(*t){
                     77:                        if(s[1] && t[1] && *s==t[1] && *t==s[1] && SPeq(s+2,t+2))
                     78:                                return(1);
                     79:                        if(SPeq(s+1, t+1))
                     80:                                return(2);
                     81:                }
                     82:                if(SPeq(s+1, t))
                     83:                        return(2);
                     84:        }
                     85:        if(*t && SPeq(s, t+1))
                     86:                return(2);
                     87:        return(3);
                     88: }
                     89: SPeq(s, t)
                     90:        register char *s, *t;
                     91: {
                     92:        while(*s++ == *t)
                     93:                if(*t++ == '\0')
                     94:                        return(1);
                     95:        return(0);
                     96: }

unix.superglobalmegacorp.com

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