Читаем UNIX — универсальная среда программирования полностью

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 /dev/tty

3.8.43 pick.c

/* pick: offer choice on each argument */


#include stdio.h

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' 12; exit 1 ;;

esac

if test ! -r $1

then

 echo "put: can't open $1" 12

 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' 12; 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 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 %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.

Перейти на страницу:
Нет соединения с сервером, попробуйте зайти чуть позже