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