Выбрать главу

int strip = 0; /* 1 => discard special characters */

main(argc, argv)

 int argc;

 char *argv[];

{

 int i;

 FILE *fp;

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

  switch (argv[1][1]) {

  case 's': /* -s: strip funny chars */

   strip = 1;

   break;

  default:

   fprintf(stderr, "%s: unknown arg %s\n",

    argv[0], argv[1]);

   exit(1);

  }

  argc--;

  argv++;

 }

 if (argc == 1)

  vis(stdin);

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

  if ((fp=fopen(argv[i], "r")) == NULL) {

   fprintf(stderr, "%s: can't open %s\n",

    argv[0], argv[i]);

   exit(1);

  } 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", с);

}

3.8.61 waitfile.c

/* waitfile: wait until file stops changing */

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

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"

3.8.62 watchfor

# 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

3.8.63 watchwho

# 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 }

            /</ { $1 = "out: "; print }'

3.8.64 which1

# 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

3.8.65 which1.H

# 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

3.8.66 which2

# 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

3.8.67 wordfreq

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