Читаем UNIX — универсальная среда программирования полностью

 * 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 = '\0';

  if (*old == '\0') /* exact or corrected */

   return strcmp(oldname, newname) != 0;

  p = guess; /* copy next component into guess */

  for ( ; *old != '/' *old != '\0'; old++)

   if (p guess+DIRSIZ)

  *p++ = *old;

  *p = '\0';

  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] = '\0'; /* +1 for terminal '\0' */

 if (dir[0] == '\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++ == '\0')

   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;

}

<p>3.8.50 <code>strindex.c</code></p>

strindex(s, t) /* return index of t in s, -1 if none */

 char *s, *t;

{

 int i, n;

 n = strlen(t);

 for (i = 0; s[i] != '\0'; i++)

  if (strncmp(s+i, t, n) == 0)

   return i;

 return -1;

}

<p>3.8.51 <code>sv.c</code></p>

/* sv: save new files */

#include stdio.h

#include sys/types.h

#include sys/dir.h

#include sys/stat.h

char *progname;

main(argc, argv)

 int argc;

 char *argv[];

{

 int i;

 struct stat stbuf;

 char *dir = argv[argc-1];

 progname = argv[0];

 if (argc = 2)

  error ("Usage: %s files... dir", progname);

 if (stat(dir, festbuf) == -1)

  error("can't access directory %s", dir);

 if ((stbuf.st_mode S_IFMT) != S_IFDIR)

  error("%s is not a directory", dir);

 for (i = 1; i argc-1; i++)

  sv(argv[i], dir);

 exit(0);

}

sv(file, dir) /* save file in dir */

 char *file, *dir;

{

Перейти на страницу:

Похожие книги

1001 совет по обустройству компьютера
1001 совет по обустройству компьютера

В книге собраны и обобщены советы по решению различных проблем, которые рано или поздно возникают при эксплуатации как экономичных нетбуков, так и современных настольных моделей. Все приведенные рецепты опробованы на практике и разбиты по темам: аппаратные средства персональных компьютеров, компьютерные сети и подключение к Интернету, установка, настройка и ремонт ОС Windows, работа в Интернете, защита от вирусов. Рассмотрены не только готовые решения внезапно возникающих проблем, но и ответы на многие вопросы, которые возникают еще до покупки компьютера. Приведен необходимый минимум технических сведений, позволяющий принять осознанное решение.Компакт-диск прилагается только к печатному изданию книги.

Юрий Всеволодович Ревич

Программирование, программы, базы данных / Интернет / Компьютерное «железо» / ОС и Сети / Программное обеспечение / Книги по IT