|
|
1.1 ! root 1: #include <sys/types.h> ! 2: #include <ndir.h> ! 3: ! 4: /* ! 5: * char *spname(name, score) ! 6: * char name[]; ! 7: * int *score; ! 8: * ! 9: * returns pointer to correctly spelled name, ! 10: * or 0 if no reasonable name is found; ! 11: * uses a static buffer to store correct name, ! 12: * so copy it if you want to call the routine again. ! 13: * score records how good the match was; ignore if NULL return. ! 14: */ ! 15: char * ! 16: spname(name, score) ! 17: register char *name; ! 18: int *score; ! 19: { ! 20: #undef DIRSIZ ! 21: #define DIRSIZ 14 ! 22: register char *p, *q, *new; ! 23: register d, nd; ! 24: register DIR *dirf; ! 25: register struct direct *ep; ! 26: static char newname[128], guess[DIRSIZ+1], best[DIRSIZ+1]; ! 27: ! 28: new = newname; ! 29: *score = 0; ! 30: for(;;){ ! 31: if (new >= &newname[128-DIRSIZ-2]) ! 32: return((char *)0); ! 33: while(*name == '/') ! 34: *new++ = *name++; ! 35: *new = '\0'; ! 36: if(*name == '\0') ! 37: return(newname); ! 38: p = guess; ! 39: while(*name!='/' && *name!='\0'){ ! 40: if(p != guess+DIRSIZ) ! 41: *p++ = *name; ! 42: name++; ! 43: } ! 44: *p = '\0'; ! 45: if((dirf=opendir(newname,0)) == NULL) ! 46: return((char *)0); ! 47: d = 3; ! 48: while(ep = readdir(dirf)) { ! 49: nd = SPdist(ep->d_name, guess); ! 50: if (nd>0 ! 51: && (SPeq(".", ep->d_name) || SPeq("..", ep->d_name))) ! 52: continue; ! 53: if(nd<d) { ! 54: p = best; ! 55: q = ep->d_name; ! 56: do; while(*p++ = *q++); ! 57: d = nd; ! 58: if(d == 0) ! 59: break; ! 60: } ! 61: } ! 62: closedir(dirf); ! 63: if(d == 3) ! 64: return((char *)0); ! 65: p = best; ! 66: *score += d; ! 67: do; while(*new++ = *p++); ! 68: --new; ! 69: } ! 70: } ! 71: /* ! 72: * very rough spelling metric ! 73: * 0 if the strings are identical ! 74: * 1 if two chars are interchanged ! 75: * 2 if one char wrong, added or deleted ! 76: * 3 otherwise ! 77: */ ! 78: SPdist(s, t) ! 79: register char *s, *t; ! 80: { ! 81: while(*s++ == *t) ! 82: if(*t++ == '\0') ! 83: return(0); ! 84: if(*--s){ ! 85: if(*t){ ! 86: if(s[1] && t[1] && *s==t[1] && *t==s[1] && SPeq(s+2,t+2)) ! 87: return(1); ! 88: if(SPeq(s+1, t+1)) ! 89: return(2); ! 90: } ! 91: if(SPeq(s+1, t)) ! 92: return(2); ! 93: } ! 94: if(*t && SPeq(s, t+1)) ! 95: return(2); ! 96: return(3); ! 97: } ! 98: SPeq(s, t) ! 99: register char *s, *t; ! 100: { ! 101: while(*s++ == *t) ! 102: if(*t++ == '\0') ! 103: return(1); ! 104: return(0); ! 105: } ! 106:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.