`
Читать книги » Книги » Компьютеры и Интернет » Программное обеспечение » Брайан Керниган - UNIX — универсальная среда программирования

Брайан Керниган - UNIX — универсальная среда программирования

1 ... 97 98 99 100 101 ... 103 ВПЕРЕД
Перейти на страницу:

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 </tmp/get.b$$ '

 /^@@@/ { 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 <stdio.h>

#include <ctype.h>

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 file2n");

  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.outn", 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 <stdio.h>

1 ... 97 98 99 100 101 ... 103 ВПЕРЕД
Перейти на страницу:

Откройте для себя мир чтения на siteknig.com - месте, где каждая книга оживает прямо в браузере. Здесь вас уже ждёт произведение Брайан Керниган - UNIX — универсальная среда программирования, относящееся к жанру Программное обеспечение. Никаких регистраций, никаких преград - только вы и история, доступная в полном формате. Наш литературный портал создан для тех, кто любит комфорт: хотите читать с телефона - пожалуйста; предпочитаете ноутбук - идеально! Все книги открываются моментально и представлены полностью, без сокращений и скрытых страниц. Каталог жанров поможет вам быстро найти что-то по настроению: увлекательный роман, динамичное фэнтези, глубокую классику или лёгкое чтение перед сном. Мы ежедневно расширяем библиотеку, добавляя новые произведения, чтобы вам всегда было что открыть "на потом". Сегодня на siteknig.com доступно более 200000 книг - и каждая готова стать вашей новой любимой. Просто выбирайте, открывайте и наслаждайтесь чтением там, где вам удобно.

Комментарии (0)