pf , os.path.walk . , 3 , . Kernighan Pike, , :
#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 == '/')
*new++ = *old++;
*new = '\0';
if (*old == '\0')
return strcmp(oldname,newname) != 0;
p = guess;
for ( ; *old != '/' && *old != '\0'; old++)
if (p < guess+DIRSIZ)
*p++ = *old;
*p = '\0';
if (mindist(newname, guess, best) >= 3)
return -1;
for (p = best; *new = *p++; )
new++;
}
}
mindist(dir, guess, best)
char *dir, *guess, *best;
{
int d, nd, fd;
struct {
ino_t ino;
char name[DIRSIZ+1];
} nbuf;
nbuf.name[DIRSIZ] = '\0';
if (dir[0] == '\0')
dir = ".";
d = 3;
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)
break;
}
}
close(fd);
return d;
}
#define EQ(s,t) (strcmp(s,t) == 0)
spdist(s, t)
char *s, *t;
{
while (*s++ == *t)
if (*t++ == '\0')
return 0;
if (*--s) {
if (*t) {
if (s[1] && t[1] && *s == t[1]
&& *t == s[1] && EQ(s+2, t+2))
return 1;
if (EQ(s+1, t+1))
return 2;
}
if (EQ(s+1, t))
return 2;
}
if (*t && EQ(s, t+1))
return 2;
return 3;
}
: , ANSI C, ISO C POSIX , . , .