UNIX — страница 115 из 115

3.8.1
addup1

awk '{ s += $'$1' }

 END { print s }'

3.8.2.
addup2

awk '

 BEGIN { n = '$1' }

 { for (i = 1; i <= n; i++)

  sum[i] += $i

 }

 END { for (i = 1; i <= n; i++) {

  printf "%6g ", sum[i]

  total += sum[i]

  }

 printf "; total = %6g\n", total

}'

3.8.3
backup

push -v panther $* /usr/bwk/eff/Code

3.8.4
backwards

# backwards: print input in backward line order

awk ' { line[NR] = $0 }

END { for (i = NR; i > 0; i--) print line[i] } ' $*

3.8.5
badpick.c

pick(s) /* offer choice of s */

 char *s;

{

 fprintf("%s? ", s);

 if (ttyin() == 'y')

  printf("%s\n", s);

}

3.8.6
bundle

# bundle: group files into distribution package


echo '# To unbundle, sh this file'

for i

do

 echo "echo $i 1>&2"

 echo "cat >$i <<'End of $i'"

 cat $i

 echo "End of $i"

done

3.8.7
cal

# cal: nicer interface to /usr/bin/cal


case $# in

0) set `date`; m=$2; y=$6 ;; # no args: use today

1) m=$1; set `date`; y=$6 ;; #1 arg: use this year

*) m=$1; y=$2 ;; #2 args: month and year

esac


case $m in

jan*|Jan*) m=1 ;;

feb*|Feb*) m=2 ;;

mar*|Mar*) m=3 ;;

apr*|Apr*) m=4 ;;

may*|May*) m=5 ;;

jun*|Jun*) m=6 ;;

jul*|Jul*) m=7 ;;

aug*|Aug*) m=8 ;;

sep*|Sep*) m=9 ;;

oct*|Oct*) m=10 ;;

nov*|Nov*) m=11 ;;

dec*|Dec*) m=12 ;;

[1-9]|10|11|12) ;; # numeric month

*) y=$m; m="" ;; # plain year

esac


/usr/bin/cal $m $y # run the real one

3.8.8
calendar1

# calendar: version 1 -- today only

awk <$HOME/calendar '

 BEGIN { split("'"`date`"'", date) }

 $1 == date[2] && $2 == date[3]

' | mail $NAME

3.8.9
calendar2

# calendar: version 2 -- today only, no quotes

(date; cat $HOME/calendar) |

awk '

 NR == 1 { mon = $2; day = $3 } # set the date

 NR > 1 && $1 == mon && $2 == day # print calendar lines

' | mail $NAME

3.8.10
calendar3

# calendar: version 3 -- today and tomorrow

awk <$HOME/calendar '

 BEGIN {

  x = "Jan 31 Feb 28 Mar 31 Apr 30 May 31 Jun 30 " \

      "Jul 31 Aug 31 Sep 30 Oct 31 Nov 30 Dec 31 Jan 31"

  split(x, data)

  for (i = 1; i < 24; i += 2) {

   days[data[i]] = data[i+1]

   nextmon[data[i]] = data[i+2]

  }

  split("'"`date`"'", date)

  mon1 = date[2]; day1 = date[3]

  mon2 = mon1; day2 = day1 + 1

  if (day1 >= days[mon1]) {

   day2 = 1

   mon2 = nextmon[mon1]

  }

 }

 $1 == mon1 && $2 == day1 || $1 == mon2 && $2 == day2

' | mail $NAME

3.8.11
cat0.c

/* cat: minimal version */

#define SIZE 512 /* arbitrary */


main() {

 char buf[SIZE];

 int n;


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

  write(1, buf, n);

 exit(0);

}

3.8.12
checkmail.c

/* checkmail: watch user's mailbox */

#include 

#include 

#include 


char *progname;

char *maildir = "/usr/spool/mail"; /* system dependent */


main(argc, argv)

 int argc;

 char *argv[];

{

 struct stat buf;

 char *name, *getlogin();

 int lastsize = 0;


 progname = argv[0];

 if ((name = getlogin()) == NULL)

  error("can't get login name", (char*)0);

 if (chdir(maildir) == -1)

  error("can't cd to %s", maildir);

 for (;;) {

  if (stat(name, &buf) == -1) /* no mailbox */

   buf.st_size = 0;

  if (buf.st_size > lastsize)

   fprintf(stderr, "\nYou have mail\007\n");

  lastsize = buf.st_size;

  sleep(60);

 }

}

#include "error.c"

3.8.13
checkmail.sh

# checkmail: watch mailbox for growth


PATH=/bin:/usr/bin

MAIL=/usr/spool/mail/`getname` # system dependent


t=${1-60}


x="`ls -l $MAIL`"

while :

do

 y="`ls -l $MAIL`"

 echo $x $y x="$y"

 sleep $t

done | awk '$4 < $12 { print "You have mail" }'

3.8.14
cp.c

/* cp: minimal version */

#include 

#define PERMS 0644 /* RW for owner, R for group, others */


char *progname;


main(argc, argv) /* cp: copy f1 to f2 */

 int argc;

 char *argv[];

{

 char buf[BUFSIZ];


 progname = argv[0];

 if (argc != 3)

  error("Usage: %s from to", progname);

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

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

 if ((f2 = creat(argv[2] , PERMS)) == -1)

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

 while ((n = read(f1, buf, BUFSIZ)) > 0)

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

   error("write error", (char*)0);

 exit(0);

}

#include "error.c"

3.8.15
doctype

# doctype: synthesize proper command line for troff

echo -n "cat $* | "

egrep -h '^\.(EQ|TS|\[|PS|IS|PP)' $* |

sort -u | awk '

/^\.PP/ { ms++ }

/^\.EQ/ { eqn++ }

/^\.TS/ { tbl++ }

/^\.PS/ { pic++ }

/^\.IS/ { ideal++ }

/^\.\[/ { refer++ }

END {

 if (refer > 0) printf "refer | "

 if (pic > 0) printf "pic | "

 if (ideal > 0) printf "ideal | "

 if (tbl > 0) printf "tbl | "

 if (eqn > 0) printf "eqn | "

 printf "troff "

 if (ms > 0) printf "-ms"

 printf "\n"

}'

3.8.16
double

awk '

FILENAME != prevfile { # new file

 NR = 1                # reset line number

 prevfile = FILENAME

}

NF > 0 {

 if ($1 == lastword)

  printf "double %s, file %s, line %d\n" ,$1,FILENAME,NR

 for (i = 2; i <= NF; i++)

  if ($i == $(i-1))

   printf "double %s, file %s, line %d\n" ,$i, FILENAME ,NR

 if (NF > 0)

  lastword = $NF

}' $*

3.8.17
efopen.c

FILE *efopen(file, mode) /* fopen file, die if can't */

 char *file, *mode;

{

 FILE *fp, *fopen();

 extern char *progname;


 if ((fp = fopen(file, mode)) != NULL)

  return fp;

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

  progname, file, mode);

 exit(1);

}

3.8.18
error.c

error(s1, s2) /* print error message and die */

 char *s1, *s2;

{

 extern int errno, sys_nerr;

 extern char *sys_errlist[], *progname;


 if (progname)

  fprintf(stderr, "%s: ", progname);

 fprintf(stderr, s1, s2);

 if (errno > 0 && errno < sys_nerr)

  fprintf (stderr, " (%s)", sys_errlist[errno]);

 fprintf(stderr, "\n");

 exit(1);

}

3.8.19
field1

awk '{ print $'$1' }'

3.8.20
field2

awk "{ print \$$1 }"

3.8.21
fold

# fold: fold long lines

sed 's/\(->/ /g' $* | # convert tabs to spaces

awk '

 BEGIN {

  N = 80                   # folds at column 80

  for (i = 1; i <= N; i++) # make a string of blanks

   blanks = blanks " "

 }

 { if ((n = length($0)) <= N)

  print

  else {

   for (i = 1; n > N; n -= N) {

    printf "%s\\\n", substr($0, i ,N)

    i += N;

   }

   printf "%s%s\n", substr(blanks, 1, N-n), substr($0, i)

  }

 } '

3.8.22
frequent

cat $* |

tr -sc A-Za-z '\012' |

sort |

uniq -с |

sort -n |

tail |

5

3.8.23
frequent2

sed 's/[ \(->][ \(->]*/\

/g' $* | sort | uniq -с | sort -nr | sed 10q

3.8.24
get

# get: extract file from history


PATH=/bin:/usr/bin


VERSION=0

while test "$1" != ""

do

 case "$1" in

 -i) INPUT=$2; shift ;;

 -o) OUTPUT=$2; shift ;;

 -[0-9]) VERSION=$1 ;;

 -*) echo "get: Unknown argument $i" 1>&2; exit 1 ;;

 *) case "$OUTPUT" in

  "") OUTPUT=$1 ;;

  *) INPUT=$1.H ;;

  esac

 esac

 shift

done

OUTPUT=${OUTPUT?"Usage: get [-o outfile] [-i file.H] file"}

INPUT=${INPUT-$OUTPUT.H}

test -r $INPUT || { echo "get: no file $INPUT" 1>&2; exit 1; }

trap 'rm -f /tmp/get.[ab]$$; exit 1' 1 2 15

# split into current version and editing commands

sed <$INPUT -n '1,/^@@@/w /tmp/get.a'$$'

/^@@@/,$w /tmp/get.b'$$


# perform the edits

awk 

 /^@@@/ { count++ }

 !/^@@@/ && count > 0 && count <= - '$VERSION'

 END { print "$d"; print "w", "'$OUTPUT'" }

' | ed - /tmp/get.a$$

rm -f /tmp/get.[ab]$$

3.8.25
get.с

get(fd, pos, buf, n) /* read n bytes from position pos */

 int fd, n;

 long pos;

 char *buf;

{

 if (lseek(fd, pos, 0) == -1) /* get to pos */

  return -1;

 return read(fd, buf, n);

}

3.8.26
getname

who am i | sed 's/ .*//'

3.8.27
idiff.c

/* idiff: interactive diff */


#include 

#include 


char *progname;

#define HUGE 10000 /* large number of lines */


main(argc, argv)

 int argc;

 char *argv[];

{

 FILE *fin, *fout, *f1, *f2, *efopen();

 char buf[BUFSIZ], *mktemp();

 char *diffout = "idiff.XXXXXX";

 progname = argv[0];


 if (argc != 3) {

  fprintf(stderr, "Usage: idiff file1 file2\n");

  exit(1);

 }

 f1 = efopen(argv[1], "r");

 f2 = efopen(argv[2], "r");

 fout = efopen("idiff.out", "w");

 mktemp(diffout);

 sprintf(buf, "diff %s %s >%s", argv[1], argv[2], diffout);

 system(buf);

 fin = efopen(diffout, "r");

 idiff(f1, f2, fin, fout); unlink(diffout);

 printf("%s output in file idiff.out\n", progname);

 exit(0);

}


idiff(f1, f2, fin, fout) /* process diffs */

 FILE *f1, *f2, *fin, *fout;

{

 char *tempfile = "idiff.XXXXXX";

 char buf[BUFSIZ], buf2[BUFSIZ], *mktemp();

 FILE *ft, *efopen();

 int cmd, n, from1, to1, from2, to2, nf1, nf2;


 mktemp(tempfile);

 nf1 = nf2 = 0;

 while (fgets(buf, sizeof buf, fin) != NULL) {

  parse(buf, &from1, &to1, &cmd, &from2, &to2);

  n = to1-from1 + to2-from2 + 1; /* #lines from diff */

  if (cmd == 'c')

   n += 2;

  else if (cmd == 'a')

   from1++;

  else if (cmd == 'd')

   from2++;

  printf("%s", buf);

  while (n-- > 0) {

   fgets(buf, sizeof buf, fin);

   printf("%s", buf);

  }

  do {

   printf("? ");

   fflush(stdout);

   fgets(buf, sizeof buf, stdin);

   switch (buf[0]) {

   case '>':

    nskip(f1, to1-nf1);

    ncopy(f2, to2-nf2, fout);

    break;

   case '<':

    nskip(f2, to2-nf2);

    ncopy(f1, to1-nf1, fout);

    break;

   case 'e':

    ncopy(f1, from1-1-nf1, fout);

    nskip(f2, from2-1-nf2);

    ft = efopen(tempfile, "w");

    ncopy(f1, to1+1-from1, ft);

    fprintf(ft, "--- \n");

    ncopy(f2, to2+1-from2, ft);

    fclose(ft);

    sprintf(buf2, "ed %s", tempfile);

    system(buf2);

    ft = efopen(tempfile, "r");

    ncopy(ft, HUGE, fout);

    fclose(ft);

    break;

   case '!':

    system(buf+1);

    printf("!\n");

    break;

   default:

    printf("< or > or e or !\n");

    break;

   }

  } while (buf[0]!= '<' && buf[0]!= '>' && buf[0]! = 'e');

  nf1 = to1;

  nf2 = to2;

 }

 ncopy(f1, HUGE, fout); /* can fail on very long files */

 unlink(tempfile);

}


parse(s, pfrom1, pto1, pcmd, pfrom2, pto2)

 char *s;

 int *pcmd, *pfrom1, *pto1, *pfrom2, *pto2;

{

#define a2i(p) while (isdigit(*s)) \

 p = 10*(p) + *s++ - '0'


 *pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;

 a2i(*pfrom1);

 if (*s == ',') {

  s++;

  a2i(*pto1);

 } else

  *pto1 = *pfrom1;

 *pcmd = *s++;

 a2i(*pfrom2);

 if (*s == ',') {

  s++;

  a2i(*pto2);

 } else

  *pto2 = *pfrom2;

}


nskip(fin, n) /* skip n lines of file fin */

 FILE *fin;

{

 char buf[BUFSIZ];


 while (n-- > 0)

  fgets(buf, sizeof buf, fin);

}


ncopy(fin, n, fout) /* copy n lines from fin to fout */

 FILE *fin, *fout;

{

 char buf[BUFSIZ];

 while (n-- > 0) {

  if (fgets(buf, sizeof buf, fin) == NULL)

   return;

  fputs(buf, fout);

 }

}


#include "efopen.c"

3.8.28
makefile

files: files.o files1.o directory.o

      cc files.o files1.o directory.o -o files


p0: p0.c ttyin0.c

      cc p0.c ttyin0.c


clean:

      rm -f *.o a.out

3.8.29
newer

# newer f: list files newer than f

ls -t | sed '/^'$1'$/q'

3.8.30
news1

# news: print news files, version 1


HOME=. # debugging only

cd . # place holder for /usr/news

for i in `ls -t * $HOME/.news_time`

do

 case $i in

 */.news_time) break ;;

 *) echo news: $i

 esac

done

touch $HOME/.news_time

3.8.31
news2

# news: print news files, version 2


HOME=. # debugging only

cd . # place holder for /usr/news

IFS='

' # just a newline

for i in `ls -t * $HOME/.news_time 2>&1`

do

case $i in

*' not found') ;;

*/.news_time) break ;;

*) echo news: $i ;;

esac

done

touch $HOME/.news_time

3.8.32
news3

# news: print news files, final version


PATH=/bin:/usr/bin

IFS='

' # just a newline

cd /usr/news


for i in `ls -t * $HOME/.news_time 2>&1`

do

 IFS=' '

 case $i in

 *' not found') ;;

 */.news_time) break ;;

 *) set X`ls -l $i`

  echo "

$i: ($3) $5 $6 $7

"

  cat $i

 esac

done

touch $HOME/.news_time

3.8.33
nohup

trap "" 1 15

if test -t 2>&1

then

 echo "Sending output to 'nohup.out'"

 exec nice -5 $* >>nohup.out 2>&1

else

 exec nice -5 $* 2>&1

fi

3.8.34
older

# older f: list files older than f

ls -tr | sed '/^'$!'$/q'

3.8.35
overwrite1

# overwrite: copy standard input to output after EOF

# version 1. BUG here


PATH=/bin:/usr/bin


case $# in

1) ;;

*) echo 'Usage: overwrite file' 1>&2; exit 2

esac


new=/tmp/overwr.$$

trap 'rm -f $new; exit 1' 1 2 15


cat >$new # collect the input

cp $new $1 # overwrite the input file

rm -f $new

3.8.36
overwrite2

# overwrite: copy standard input to output after EOF

# version 2. BUG here too


PATH=/bin:/usr/bin


case $# in

1) ;;

*) echo 'Usage: overwrite file' 1>&2; exit 2

esac


new=/tmp/overwr1.$$

old=/tmp/overwr2.$$

trap 'rm -f $new $old; exit 1' 1 2 15


cat >$new # collect the input

cp $1 $old # save original file


trap '' 1 2 15 # we are committed; ignore signals

cp $new $1 # overwrite the input file


rm -f $new $old

3.8.37
overwrite3

# overwrite: copy standard input to output after EOF

# final version


opath=$PATH

PATH=/bin:/usr/bin


case $# in

0|1) echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2

esac


file=$1; shift

new=/tmp/overwr1.$$; old=/tmp/overwr2.$$

trap 'rm -f $new $old; exit 1' 1 2 15 # clean up files


if PATH=$opath >$new # collect input

then

 cp $file $old # save original file

 trap '' 1 2 15 # we are committed; ignore signals

 cp $new $file

else

 echo "overwrite: $1 failed, $file unchanged" 1>&2

 exit 1

fi

rm -f $new $old

3.8.38
p1.c

/* p: print input in chunks (version 1) */


#include 

#define PAGESIZE 22

char *progname; /* program name for error message */


main(argc, argv)

 int argc;

 char *argv[];

{

 int i;

 FILE *fp, *efopen();


 progname = argv[0];

 if (argc == 1)

  print(stdin, PAGESIZE);

 else

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

   fp = efopen(argv[i], "r");

   print(fp, PAGESIZE);

   fclose(fp);

  }

  exit(0);

}


print(fp, pagesize) /* print fp in pagesize chunks */

 FILE *fp;

 int pagesize;

{

 static int lines = 0; /* number of lines so far */

 char buf[BUFSIZ];


 while (fgets(buf, sizeof buf, fp) != NULL)

  if (++lines < pagesize)

   fputs(buf, stdout);

  else {

   buf[strlen(buf)-1] = '\0';

   fputs(buf, stdout);

   fflush(stdout);

   ttyin();

   lines = 0;

  }

}


#include "ttyin1.c"

#include "efopen.c"

3.8.39
p2.c

/* p: print input in chunks (version 2) */


#include 

#define PAGESIZE 22


char *progname; /* program name for error message */


main(argc, argv)

 int argc;

 char *argv[];

{

 FILE *fp, *efopen();

 int i, pagesize = PAGESIZE;


 progname = argv[0];

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

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

  argc--;

  argv++;

 }

 if (argc == 1)

  print(stdin, pagesize);

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

  fp = efopen(argv[i], "r");

  print(fp, pagesize);

  fclose(fp);

 }

 exit(0);

}


print(fp, pagesize) /* print fp in pagesize chunks */

 FILE *fp;

 int pagesize;

{

 static int lines = 0; /* number of lines so far */

 char buf[BUFSIZ];


 while (fgets(buf, sizeof buf, fp) != NULL)

  if (++lines < pagesize)

   fputs(buf, stdout);

  else {

   buf[strlen(buf)-1] = '\0';

   fputs(buf, stdout);

   fflush(stdout);

   ttyin();

   lines = 0;

  }

}


#include "ttyin2.c"

#include "efopen.c"

3.8.40
p3.c

/* p: print input in chunks (version 3) */


#include 

#define PAGESIZE 22

char *progname; /* program name for error message */


main(argc, argv)

 int argc;

 char *argv[];

{

 FILE *fp, *efopen();

 int i, pagesize = PAGESIZE;

 char *p, *getenv();


 progname = argv[0];

 if ((p=getenv("PAGESIZE")) != NULL)

  pagesize = atoi(p);

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

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

  argc--;

  argv++;

 }

 if (argc == 1)

  print(stdin, pagesize);

 else

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

   fp = efopen(argv[i], "r");

   print(fp, pagesize);

   fclose(fp);

  }

 exit(0);

}


print(fp, pagesize) /* print fp in pagesize chunks */

 FILE *fp;

 int pagesize;

{

 static int lines = 0; /* number of lines so far */

 char buf[BUFSIZ];


 while (fgets(buf, sizeof buf, fp) != NULL)

  if (++lines < pagesize)

   fputs(buf, stdout);

  else {

   buf[strlen(buf)-1] = '\0';

   fputs(buf, stdout);

   fflush(stdout);

   ttyin();

   lines = 0;

  }

}


#include "ttyin2.c"

#include "efopen.c"

3.8.41
p4.c

/* p: print input in chunks (version 4) */


#include 

#define PAGESIZE 22

char *progname; /* program name for error message */


main(argc, argv)

 int argc;

 char *argv[];

{

 FILE *fp, *efopen();

 int i, pagesize = PAGESIZE;

 char *p, *getenv(), buf[BUFSIZ];


 progname = argv[0];

 if ((p=getenv("PAGESIZE")) != NULL)

  pagesize = atoi(p);

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

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

  argc--;

  argv++;

 }

 if (argc == 1)

  print(stdin, pagesize);

 else

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

   switch (spname(argv[i], buf)) {

   case -1: /* no match possible */

    fp = efopen(argv[i], "r");

    break;

   case 1: /* corrected */

    fprintf (stderr, "\"%s\"? ", buf);

    if (ttyin() == 'n')

     break;

    argv[i] = buf;

    /* fall through... */

   case 0: /* exact match */

    fp = efopen(argv[i], "r");

    print(fp, pagesize);

    fclose(fp);

   }

 exit(0);

}


print(fp, pagesize) /* print fp in pagesize chunks */

 FILE *fp;

 int pagesize;

{

 static int lines = 0; /* number of lines so far */

 char buf[BUFSIZ];


 while (fgets(buf, sizeof buf, fp) != NULL)

  if (++lines < pagesize) fputs(buf, stdout);

  else {

   buf[strlen(buf)-1] = '\0';

   fputs(buf, stdout);

   fflush(stdout);

   ttyin();

   lines = 0;

  }

}


#include "ttyin2.c"

#include "efopen.c"

#include "spname.c"

3.8.42
pick1

# pick: select arguments


PATH=/bin:/usr/bin


for i # for each argument

do

 echo -n "$i? " >/dev/tty

 read response

 case $response in

 y*) echo $i ;;

 q*) break

 esac

done 

3.8.43
pick.c

/* pick: offer choice on each argument */


#include 

char *progname; /* program name for error message */


main(argc, argv)

 int argc;

 char *argv[];

{

 int i;

 char buf[BUFSIZ];


 progname = argv[0];

 if (argc == 2 && strcmp(argv[1], "-") == 0) /* pick - */

  while (fgets(buf, sizeof buf, stdin) != NULL) {

   buf[strlen(buf)-1] = '\0'; /* drop newline */

   pick(buf);

  }

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

  pick(argv[i]);

 exit(0);

}


pick(s) /* offer choice of s */

 char *s;

{

 fprintf(stderr, "%s? ", s);

 if (ttyin() == 'y')

  printf("%s\n", s);

}


#include "ttyin2.c"

#include "efopen.c"

3.8.44
prpages

# prpages: compute number of pages that pr will print

wc $* |

awk '!/ total$/ { n += int(($1+55) / 56) }

 END { print n }'

3.8.45
put

# put: install file into history


PATH=/bin:/usr/bin


case $# in

1) HIST=$1.H ;;

*) echo 'Usage: put file' 1>&2; exit 1 ;;

esac

if test ! -r $1

then

 echo "put: can't open $1" 1>&2

 exit 1

fi

trap 'rm -f /tmp/put.[ab]$$; exit 1 12 15

echo -n 'Summary: '

read Summary


if get -o /tmp/put.a$$ $1 # previous version

then # merge pieces

 cp $1 /tmp/put.b$$ # current version

 echo `getname` `date` $Summary" >>/tmp/put.b$$

 diff -e $1 /tmp/put.a$$ >>/tmp/put.b$$ # latest diffs

 sed -n '/^@@@/,$р' <$HIST >>/tmp/put.b$$ # old diffs

 overwrite $HIST cat /tmp/put.b$$ # put it back

else # make a new one

 echo "put: creating $HIST"

 cp $1 $HIST

 echo "@@@ `getname` `date` $Summary" >>$HIST

fi

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 "s@$left@$right@g" $i

done

3.8.48
signaltest.c

#include 

#include 

#include 


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 

#include 


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:

 * 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++ == '\0')

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

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

   return i;

 return -1;

}

3.8.51
sv.c

/* sv: save new files */


#include 

#include 

#include 

#include 


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 copied\n", 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 


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 

#include 


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/tty\n", 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 

#include 


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"

3.5.58
vis1.c

/* vis: make funny characters visible (version 1) */

#include 

#include 


main() {

 int c;


 while ((c = getchar()) != EOF)

  if (isascii(c) &&

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

   putchar(c);

  else

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

 exit(0);

}

3.5.59
vis2.c

/* vis: make funny characters visible (version 2) */

#include 

#include 


main(argc, argv)

 int argc;

 char *argv[];

{

 int с, strip = 0;


 if (argc > 1 && strcmp(argv[1] , "-s") == 0)

  strip = 1;

 while ((c = getchar()) != EOF) if (isascii(c) &&

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

  putchar(c);

 else if (!strip)

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

 exit(0);

}

3.8.60
vis3.c

/* vis: make funny characters visible (version 3) */

#include 

#include 


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 

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

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 }

            /

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]++ }

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

' $*

3.8.68
zap1

# zap pattern: kill all processes matching pattern

# BUG in this version


PATH=/bin:/usr/bin


case $# in

0) echo 'Usage: zap pattern' 1>&2; exit 1

esac


kill `pick \`ps -ag | grep "$*"\` | awk '{print $1}'`

3.8.69
zap2

# zap pat: kill all processes matching pat

# final version


PATH=/bin:/usr/bin


IFS='

' # just a newline

case $1 in

"") echo 'Usage: zap [-2] pattern' 1>&2; exit 1 ;;

-*) SIG=$1; shift

esac


echo ' PID TTY TIME CMD'

kill $SIG `pick \`ps -ag | egrep "$*"|` | awk '{print $1}'`

3.8.70
zap.c

/* zap: interactive process killer */


#include 

#include 

char *progname; /* program name for error message */

char *ps = "ps -ag"; /* system dependent */


main(argc, argv)

 int argc;

 char *argv[];

{

 FILE *fin, *popen();

 char buf[BUFSIZ];

 int pid;


 progname = argv[0];

 if ((fin = popen(ps, "r")) == NULL) {

  fprintf (stderr, "%s: can't run %s\n", progname, ps);

  exit(1);

 }

 fgets(buf, sizeof buf, fin); /* get header line */

 fprintf (stderr, "%s", buf);

 while (fgets(buf, sizeof buf, fin) != NULL)

  if (argc == 1 || strindex(buf, argv[1]) >= 0) {

   buf[strlen(buf)-1] = '\0'; /* suppress \n */

   fprintf (stderr, "%s? ", buf);

   if (ttyin() == 'y') {

    sscanf(buf, "%d", &pid);

    kill(pid, SIGKILL);

   }

  }

 exit(0);

}

#include "ttyin2.c"

#include "strindex.c"

#include "efopen.c"