Книга: UNIX — универсальная среда программирования
3.8.49 spname.c
3.8.49 spname.c
/* spname: return correctly spelled filename */
/*
* spname(oldname, newname) char *oldname, *newname;
* returns -1 if no reasonable match to oldname,
* 0 if exact match,
* 1 if corrected.
* stores corrected name in newname.
*/
#include <sys/types.h>
#include <sys/dir.h>
spname(oldname, newname)
char *oldname, *newname;
{
char *p, guess[DIRSIZ+1], best[DIRSIZ+1];
char *new = newname, *old = oldname;
for (;;) {
while (*old == '/') /* skip slashes */
*new++ = *old++;
*new = '';
if (*old == '') /* exact or corrected */
return strcmp(oldname, newname) != 0;
p = guess; /* copy next component into guess */
for ( ; *old != '/' && *old != ''; old++)
if (p < guess+DIRSIZ)
*p++ = *old;
*p = '';
if (mindist(newname, guess, best) >= 3)
return -1; /* hopeless */
for (p = best; *new = *p++; ) /* add to end */
new++; /* of newname */
}
}
mindist(dir, guess, best) /* search dir for guess */
char *dir, *guess, *best;
{
/* set best, return distance 0..3 */
int d, nd, fd;
struct {
ino_t ino;
char name[DIRSIZ+1]; /* 1 more than in dir.h */
} nbuf;
nbuf.name[DIRSIZ] = ''; /* +1 for terminal '' */
if (dir[0] == '') /* current directory */
dir = ".";
d = 3; /* minimum distance */
if ((fd=open(dir, 0)) == -1)
return d;
while (read(fd, (char*)&nbuf, sizeof(struct direct)) > 0)
if (nbuf.ino) {
nd = spdist(nbuf.name, guess);
if (nd <= d && nd != 3) {
strcpy(best, nbuf.name);
d = nd;
if (d == 0) /* exact match */
break;
}
}
close(fd);
return d;
}
/* spdist: return distance between two names */
/*
* very rough spelling metric:
* 0 if the strings are identical
* 1 if two chars are transposed
* 2 if one char wrong, added or deleted
* 3 otherwise
*/
#define EQ(s,t) (strcmp(s,t) == 0)
spdist(s, t)
char *s, *t;
{
while (*s++ == *t)
if (*t++ == '')
return 0; /* exact match */
if (*--s) {
if (*t) {
if (s[1] && t[1] && *s == t[1] && *t == s[1] && EQ(s+2, t+2))
return 1; /* transposition */
if (EQ(s+1, t+1))
return 2; /* 1 char mismatch */
}
if (EQ(s+1, t))
return 2; /* extra character */
}
if (*t && EQ(s, t+1))
return 2; /* missing character */
return 3;
}
- 3.8.1 addup1
- 3.8.2. addup2
- 3.8.3 backup
- 3.8.4 backwards
- 3.8.5 badpick.c
- 3.8.6 bundle
- 3.8.7 cal
- 3.8.8 calendar1
- 3.8.9 calendar2
- 3.8.10 calendar3
- 3.8.11 cat0.c
- 3.8.12 checkmail.c
- 3.8.13 checkmail.sh
- 3.8.14 cp.c
- 3.8.15 doctype
- 3.8.16 double
- 3.8.17 efopen.c
- 3.8.18 error.c
- 3.8.19 field1
- 3.8.20 field2
- 3.8.21 fold
- 3.8.22 frequent
- 3.8.23 frequent2
- 3.8.24 get
- 3.8.25 get.с
- 3.8.26 getname
- 3.8.27 idiff.c
- 3.8.28 makefile
- 3.8.29 newer
- 3.8.30 news1
- 3.8.31 news2
- 3.8.32 news3
- 3.8.33 nohup
- 3.8.34 older
- 3.8.35 overwrite1
- 3.8.36 overwrite2
- 3.8.37 overwrite3
- 3.8.38 p1.c
- 3.8.39 p2.c
- 3.8.40 p3.c
- 3.8.41 p4.c
- 3.8.42 pick1
- 3.8.43 pick.c
- 3.8.44 prpages
- 3.8.45 put
- 3.8.46 readslow.c
- 3.8.47 replace
- 3.8.48 signaltest.c
- 3.8.49 spname.c
- 3.8.50 strindex.c
- 3.8.51 sv.c
- 3.8.52 system1.c
- 3.8.53 system.c
- 3.8.54 timeout.c
- 3.8.55 toolong
- 3.8.56 ttyin1.c
- 3.8.57 ttyin2.c
- 3.5.58 vis1.c
- 3.5.59 vis2.c
- 3.8.60 vis3.c
- 3.8.61 waitfile.c
- 3.8.62 watchfor
- 3.8.63 watchwho
- 3.8.64 which1
- 3.8.65 which1.H
- 3.8.66 which2
- 3.8.67 wordfreq
- 3.8.68 zap1
- 3.8.69 zap2
- 3.8.70 zap.c