Читаем UNIX полностью

  } else {

   vis(fp);

   fclose(fp);

  }

 exit(0);

}

vis(fp) /* make chars visible in FILE *fp */

 FILE *fp;

{

 int c;

 while ((c = getc(fp)) != EOF)

  if (isascii(c) &&

   (isprint(c) || c=='\n' || c=='\t' || c==' '))

   putchar(c);

  else if (!strip)

   printf("\\%03o", с);

}

<p>3.8.61 <code>waitfile.c</code></p>

/* waitfile: wait until file stops changing */

#include

#include

#include

char *progname;

main(argc, argv)

 int argc;

 char *argv[];

{

 int fd;

 struct stat stbuf;

 time_t old_time = 0;

 progname = argv[0];

 if (argc < 2)

  error("Usage: %s filename [cmd]", progname);

 if ((fd = open(argv[1], 0)) == -1)

  error("can't open %s", argv[1]);

 fstat(fd, &stbuf);

 while (stbuf.st_mtime != old_time) {

  old_time = stbuf.st_mtime;

  sleep(60);

  fstat(fd, &stbuf);

 }

 if (argc == 2) { /* copy file */

  execlp("cat", "cat", argv[1], (char*)0);

  error("can't execute cat %s", argv[1]);

 } else { /* run process */

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

  error("can't execute %s", argv[2]);

 }

 exit(0);

}

#include "error.c"

<p>3.8.62 <code>watchfor</code></p>

# watchfor: watch for someone to log in

PATH=/bin:/usr/bin

case $# in

0) echo 'Usage: watchfor person' 1>&2; exit 1

esac

until who | egrep "$1"

do

 sleep 60

done

<p>3.8.63 <code>watchwho</code></p>

# watchwho: watch who logs in and out

PATH=/bin:/usr/bin

new=/tmp/wwho1.$$

old=/tmp/wwho2.$$

> $old # create an empty file

while : # loop forever

do

 who >$new

 diff $old $new

 mv $new $old

 sleep 60

done | awk '/>/ { $1 = "in: "; print }

            /

<p>3.8.64 <code>which1</code></p>

# which cmd: which cmd in PATH is executed, version 1

case $# in

0) echo 'Usage: which command' 1>&2; exit 2

esac

for i in `echo $PATH | sed 's/^:/.:/

                            s/::/:.:/g

                            s/:$/:./

                            s/:/ /g'`

do

 if test -f $i/$1 # use test -x if you can

 then

  echo $i/$1

  exit 0 # found it

 fi

done

exit 1 # not found

<p>3.8.65 <code>which1.H</code></p>

# which cmd: which cmd in PATH is executed, version 1

case $# in

0) echo 'Usage: which command' 1>&2; exit 2

esac

for i in `echo $PATH | sed 's/^:/.:/

                            s/::/:.:/g

                            s/:$/:./

                            s/:/ /g'`

do

 if test -f $i/$1 # use test -x if you can

 then

  echo $i/$1

  exit 0 # found it

 fi

done

exit 1 # not found

@@@ Fri Oct 14 14:21:11 EDT 1983 original version

<p>3.8.66 <code>which2</code></p>

# which cmd: which cmd in PATH is executed, final version

opath=$PATH PATH=/bin:/usr/bin

case $# in

0) echo 'Usage: which command' 1>&2; exit 2

esac

for i in `echo $opath | sed 's/^:/.:/

                             s/::/:.:/g

                             s/:$/:./

                             s/:/ /g'`

do

 if test -f $i/$1 # this is /bin/test

 then # or /usr/bin/test only

  echo $i/$1

  exit 0 # found it

 fi

done

exit 1 # not found

<p>3.8.67 <code>wordfreq</code></p>

awk ' { for (i = 1; i <= NF; i++) num[$i]++ }

END {for (word in num) print word, num[word] }

' $*

<p>3.8.68 <code>zap1</code></p>

# zap pattern: kill all processes matching pattern

# BUG in this version

PATH=/bin:/usr/bin

case $# in

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

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