Вход/Регистрация
UNIX — универсальная среда программирования
вернуться

Керниган Брайан Уилсон

Шрифт:

#include <errno.h>

extern int errno;

main {

 int с, n;

 char buf[100];

 int onintr;

 signal(SIGINT, onintr);

 for (;;) {

n = read(0, buf, 100);

if (n > 0)

printf(buf);

else {

if (errno == EINTR) {

errno = 0;

printf("interrupt side %d\n", n);

} else {

printf("true end of file %d\n", n);

}

}

 }

}

onintr {

 signal(SIGINT, onintr);

 printf("interrupt\n");

}

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 = '\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:

  • Читать дальше
  • 1
  • ...
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: