Литмир - Электронная Библиотека
Содержание  
A
A

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 <stdio.h>

#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 <stdio.h>

#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);

179
{"b":"248117","o":1}