Читать книги » Книги » Компьютеры и Интернет » Программное обеспечение » Брайан Керниган - UNIX — универсальная среда программирования

Брайан Керниган - UNIX — универсальная среда программирования

Читать книгу Брайан Керниган - UNIX — универсальная среда программирования, Брайан Керниган . Жанр: Программное обеспечение.
Брайан Керниган - UNIX — универсальная среда программирования
Название: UNIX — универсальная среда программирования
ISBN: -
Год: -
Дата добавления: 3 июль 2019
Количество просмотров: 235
(18+) Внимание! Книга может содержать контент только для совершеннолетних. Для несовершеннолетних просмотр данного контента СТРОГО ЗАПРЕЩЕН! Если в книге присутствует наличие пропаганды ЛГБТ и другого, запрещенного контента - просьба написать на почту для удаления материала.
Читать онлайн

UNIX — универсальная среда программирования читать книгу онлайн

UNIX — универсальная среда программирования - читать онлайн , автор Брайан Керниган
В книге американских авторов — разработчиков операционной системы UNIX — блестяще решена проблема автоматизации деятельности программиста, системной поддержки его творчества, выходящей за рамки языков программирования. Профессионалам открыт богатый "встроенный" арсенал системы UNIX. Многочисленными примерами иллюстрировано использование языка управления заданиями shell.Для программистов-пользователей операционной системы UNIX.
Перейти на страницу:

  else {

   if (errno == EINTR) {

    errno = 0;

    printf("interrupt side %dn", n);

   } else {

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

   }

  }

 }

}

onintr() {

 signal(SIGINT, onintr);

 printf("interruptn");

}

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.50 strindex.c

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] != ''; i++)

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

   return i;

 return -1;

}

3.8.51 sv.c

/* 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;

{

 struct stat sti, sto;

 int fin, fout, n;

 char target[BUFSIZ], buf[BUFSIZ], *index();

 sprintf(target, "%s/%s", dir, file);

 if (index(file, '/') != NULL) /* strchr() in some systems */

  error("won't handle '/'s in %s", file);

 if (stat(file, &sti) == -1)

  error("can't stat %s", file);

 if (stat(target, &sto) == -1) /* target not present */

  sto.st_mtime = 0; /* so make it look old */

 if (sti.st_mtime < sto.st_mtime) /* target is newer */

  fprintf(stderr, "%s: %s not copiedn", progname, file);

 else if ((fin = open(file, 0)) == -1)

  error("can't open file %s", file);

 else if ((fout = creat(target, sti.st_mode)) == -1)

  error("can't create %s", target);

 while ((n = read(fin, buf, sizeof buf)) > 0)

  if (write(fout, buf, n) != n)

   error("error writing %s", target);

 close(fin);

 close(fout);

}

#include "error.c"

3.8.52 system1.c

#include <signal.h>

system(s) /* run command line s */

 char *s;

{

 int status, pid, w, tty;

 int (*istat)(), (*qstat)();

 ...

 if ((pid = fork()) == 0) {

  ...

  execlp("sh", "sh", "-c", s, (char*)0);

  exit(127);

 }

 ...

 istat = signal(SIGINT, SIG_IGN);

 qstat = signal(SIGQUIT, SIG_IGN);

 while ((w = wait(&status)) != pid && w != -1)

  ;

 if (w == -1)

  status = -1;

 signal(SIGINT, istat);

 signal(SIGQUIT, qstat);

 return status;

}

3.8.53 system.c

/*

 * Safer version of system for interactive programs

 */

#include <signal.h>

#include <stdio.h>

system(s) /* run command line s */

 char *s;

{

 int status, pid, w, tty;

 int (*istat)(), (*qstat)();

 extern char *progname;

 fflush(stdout);

 tty = open("/dev/tty", 2);

 if (tty == -1) {

  fprintf (stderr, "%s: can't open /dev/ttyn", progname);

  return -1;

 }

 if ((pid = fork()) == 0) {

  close(0);

  dup(tty);

  close(1);

  dup(tty);

  close(2);

  dup(tty);

  close(tty);

  execlp("sh", "sh", "-c", s, (char*)0);

  exit(127);

 }

 close(tty);

 istat = signal(SIGINT, SIG_IGN);

 qstat = signal(SIGQUIT, SIG_IGN);

 while ((w = wait(&status)) != pid && w != -1)

  ;

 if (w == -1)

  status = -1;

 signal(SIGINT, istat);

 signal(SIGQUIT, qstat);

 return status;

}

3.8.54 timeout.c

/* timeout: set time limit on a process */

#include <stdio.h>

#include <signal.h>

int pid; /* child process id */

char *progname;

main(argc, argv)

 int argc;

 char *argv[];

{

 int sec = 10, status, onalarm();

 progname = argv[0];

 if (argc > 1 && argv[1][0] == '-') {

  sec = atoi(&argv[1][1]);

  argc--;

  argv++;

 }

 if (argc < 2)

  error("Usage: %s [-10] command", progname);

 if ((pid=fork()) == 0) {

  execvp(argv[1], &argv[1]);

  error("couldn't start %s", argv[1]);

 }

 signal(SIGALRM, onalarm);

 alarm(sec);

 if (wait(&status) == -1 || (status & 0177) != 0)

  error("%s killed", argv[1]);

 exit((status >> 8) & 0377);

}

onalarm() /* kill child when alarm arrives */

{

 kill(pid, SIGKILL);

}

#include "error.c"

3.8.55 toolong

length($0) > 72 { print "Line", NR, "too long:", substr($0,1,60) }

3.8.56 ttyin1.c

ttyin() /* process response from /dev/tty (version 1) */

{

 char buf[BUFSIZ];

 FILE *efopen();

 static FILE *tty = NULL;

 if (tty == NULL)

  tty = efopen("/dev/tty", "r");

 if (fgets(buf, BUFSIZ, tty) == NULL || buf[0] == 'q')

  exit(0);

 else /* ordinary line */

  return buf[0];

}

3.8.57 ttyin2.c

ttyin() /* process response from /dev/tty (version 2) */

{

 char buf[BUFSIZ];

 FILE *efopen();

 static FILE *tty = NULL;

 if (tty == NULL)

  tty = efopen("/dev/tty", "r");

 for (;;) {

  if (fgets(buf,BUFSIZ,tty) == NULL || buf[0] == 'q')

  exit(0);

  else if (buf[0] == '!') {

   system(buf+1); /* BUG here */

   printf("!n");

  }

  else /* ordinary line */

   return buf[0];

 }

}

#include "system.c"

Перейти на страницу:
Комментарии (0)