Читаем UNIX полностью

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

}

<p>3.8.18 <code>error.c</code></p>

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

}

<p>3.8.19 <code>field1</code></p>

awk '{ print $'$1' }'

<p>3.8.20 <code>field2</code></p>

awk "{ print \$$1 }"

<p>3.8.21 <code>fold</code></p>

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

  }

 } '

<p>3.8.22 <code>frequent</code></p>

cat $* |

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

sort |

uniq -с |

sort -n |

tail |

5

<p>3.8.23 <code>frequent2</code></p>

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

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

<p>3.8.24 <code>get</code></p>

# 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]$$

<p>3.8.25 <code>get.с</code></p>

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

}

<p>3.8.26 <code>getname</code></p>

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

<p>3.8.27 <code>idiff.c</code></p>

/* 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);

 }

Перейти на страницу:

Похожие книги