UNIX — универсальная среда программирования - Керниган Брайан Уилсон
rm -f /tmp/put.[ab]$$
3.8.46 readslow.c
/* readslow: keep reading, waiting for more */
#define SIZE 512 /* arbitrary */
main() {
char buf[SIZE];
int n;
for (;;) {
while ((n = read(0, buf, sizeof buf)) > 0)
write(1, buf, n);
sleep(10);
}
}
3.8.47 replace
# replace: replace str1 in files with str2, in place
PATH=/bin:/usr/bin
case $# in
0|1|2) echo 'Usage: replace str1 str2 files' 1>&2; exit 1
esac
left="$1"; right="$2"; shift; shift
for i
do
overwrite $i sed "[email protected][email protected][email protected]" $i
done
3.8.48 signaltest.c
#include <stdio.h>
#include <signal.h>
#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 %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)