Вход/Регистрация
UNIX — универсальная среда программирования
вернуться

Керниган Брайан Уилсон

Шрифт:

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

  • Читать дальше
  • 1
  • ...
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • ...

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: