Категории
Самые читаемые
💎Читать книги // БЕСПЛАТНО // 📱Online » Компьютеры и Интернет » Программное обеспечение » UNIX — универсальная среда программирования - Брайан Керниган

Читаем без скачивания UNIX — универсальная среда программирования - Брайан Керниган

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

Шрифт:

-
+

Интервал:

-
+

Закладка:

Сделать
1 ... 94 95 96 97 98 99 100 101 102 103
Перейти на страницу:

 int t;

 double d;

{

 Symbol *sp;

 char *emalloc();

 sp = (Symbol*)emalloc(sizeof(Symbol));

 sp->name = emalloc(strlen(s)+1); /* +1 for '' */

 strcpy(sp->name, s);

 sp->type = t;

 sp->u.val = d;

 sp->next = symlist; /* put at front of list */

 symlist = sp;

 return sp;

}

char *emalloc(n) /* check return from malloc */

 unsigned n;

{

 char *p, *malloc();

 p = malloc(n);

 if (p == 0)

  execerror("out of memory", (char*)0);

 return p;

}

3.8 Всякая всячина

3.8.1 addup1

awk '{ s += $'$1' }

 END { print s }'

3.8.2. addup2

awk '

 BEGIN { n = '$1' }

 { for (i = 1; i <= n; i++)

  sum[i] += $i

 }

 END { for (i = 1; i <= n; i++) {

  printf "%6g ", sum[i]

  total += sum[i]

  }

 printf "; total = %6gn", total

}'

3.8.3 backup

push -v panther $* /usr/bwk/eff/Code

3.8.4 backwards

# backwards: print input in backward line order

awk ' { line[NR] = $0 }

END { for (i = NR; i > 0; i--) print line[i] } ' $*

3.8.5 badpick.c

pick(s) /* offer choice of s */

 char *s;

{

 fprintf("%s? ", s);

 if (ttyin() == 'y')

  printf("%sn", s);

}

3.8.6 bundle

# bundle: group files into distribution package

echo '# To unbundle, sh this file'

for i

do

 echo "echo $i 1>&2"

 echo "cat >$i <<'End of $i'"

 cat $i

 echo "End of $i"

done

3.8.7 cal

# cal: nicer interface to /usr/bin/cal

case $# in

0) set `date`; m=$2; y=$6 ;; # no args: use today

1) m=$1; set `date`; y=$6 ;; #1 arg: use this year

*) m=$1; y=$2 ;; #2 args: month and year

esac

case $m in

jan*|Jan*) m=1 ;;

feb*|Feb*) m=2 ;;

mar*|Mar*) m=3 ;;

apr*|Apr*) m=4 ;;

may*|May*) m=5 ;;

jun*|Jun*) m=6 ;;

jul*|Jul*) m=7 ;;

aug*|Aug*) m=8 ;;

sep*|Sep*) m=9 ;;

oct*|Oct*) m=10 ;;

nov*|Nov*) m=11 ;;

dec*|Dec*) m=12 ;;

[1-9]|10|11|12) ;; # numeric month

*) y=$m; m="" ;; # plain year

esac

/usr/bin/cal $m $y # run the real one

3.8.8 calendar1

# calendar: version 1 -- today only

awk <$HOME/calendar '

 BEGIN { split("'"`date`"'", date) }

 $1 == date[2] && $2 == date[3]

' | mail $NAME

3.8.9 calendar2

# calendar: version 2 -- today only, no quotes

(date; cat $HOME/calendar) |

awk '

 NR == 1 { mon = $2; day = $3 } # set the date

 NR > 1 && $1 == mon && $2 == day # print calendar lines

' | mail $NAME

3.8.10 calendar3

# calendar: version 3 -- today and tomorrow

awk <$HOME/calendar '

 BEGIN {

  x = "Jan 31 Feb 28 Mar 31 Apr 30 May 31 Jun 30 "

      "Jul 31 Aug 31 Sep 30 Oct 31 Nov 30 Dec 31 Jan 31"

  split(x, data)

  for (i = 1; i < 24; i += 2) {

   days[data[i]] = data[i+1]

   nextmon[data[i]] = data[i+2]

  }

  split("'"`date`"'", date)

  mon1 = date[2]; day1 = date[3]

  mon2 = mon1; day2 = day1 + 1

  if (day1 >= days[mon1]) {

   day2 = 1

   mon2 = nextmon[mon1]

  }

 }

 $1 == mon1 && $2 == day1 || $1 == mon2 && $2 == day2

' | mail $NAME

3.8.11 cat0.c

/* cat: minimal version */

#define SIZE 512 /* arbitrary */

main() {

 char buf[SIZE];

 int n;

 while ((n = read(0, buf, sizeof buf)) > 0)

  write(1, buf, n);

 exit(0);

}

3.8.12 checkmail.c

/* checkmail: watch user's mailbox */

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

char *progname;

char *maildir = "/usr/spool/mail"; /* system dependent */

main(argc, argv)

 int argc;

 char *argv[];

{

 struct stat buf;

 char *name, *getlogin();

 int lastsize = 0;

 progname = argv[0];

 if ((name = getlogin()) == NULL)

  error("can't get login name", (char*)0);

 if (chdir(maildir) == -1)

  error("can't cd to %s", maildir);

 for (;;) {

  if (stat(name, &buf) == -1) /* no mailbox */

   buf.st_size = 0;

  if (buf.st_size > lastsize)

   fprintf(stderr, "nYou have mail07n");

  lastsize = buf.st_size;

  sleep(60);

 }

}

#include "error.c"

3.8.13 checkmail.sh

# checkmail: watch mailbox for growth

PATH=/bin:/usr/bin

MAIL=/usr/spool/mail/`getname` # system dependent

t=${1-60}

x="`ls -l $MAIL`"

while :

do

 y="`ls -l $MAIL`"

 echo $x $y x="$y"

 sleep $t

done | awk '$4 < $12 { print "You have mail" }'

3.8.14 cp.c

/* cp: minimal version */

#include <stdio.h>

#define PERMS 0644 /* RW for owner, R for group, others */

char *progname;

main(argc, argv) /* cp: copy f1 to f2 */

 int argc;

 char *argv[];

{

 char buf[BUFSIZ];

 progname = argv[0];

 if (argc != 3)

  error("Usage: %s from to", progname);

 if ((f1 = open(argv[1], 0)) == -1)

  error("can't open %s", argv[1]);

 if ((f2 = creat(argv[2] , PERMS)) == -1)

  error("can't create %s", argv[2]);

 while ((n = read(f1, buf, BUFSIZ)) > 0)

  if (write(f2, buf, n) != n)

   error("write error", (char*)0);

 exit(0);

}

#include "error.c"

3.8.15 doctype

# doctype: synthesize proper command line for troff

echo -n "cat $* | "

egrep -h '^.(EQ|TS|[|PS|IS|PP)' $* |

sort -u | awk '

/^.PP/ { ms++ }

/^.EQ/ { eqn++ }

/^.TS/ { tbl++ }

/^.PS/ { pic++ }

/^.IS/ { ideal++ }

/^.[/ { refer++ }

END {

 if (refer > 0) printf "refer | "

 if (pic > 0) printf "pic | "

 if (ideal > 0) printf "ideal | "

 if (tbl > 0) printf "tbl | "

 if (eqn > 0) printf "eqn | "

 printf "troff "

 if (ms > 0) printf "-ms"

 printf "n"

}'

3.8.16 double

awk '

FILENAME != prevfile { # new file

 NR = 1                # reset line number

 prevfile = FILENAME

}

NF > 0 {

 if ($1 == lastword)

  printf "double %s, file %s, line %dn" ,$1,FILENAME,NR

 for (i = 2; i <= NF; i++)

  if ($i == $(i-1))

   printf "double %s, file %s, line %dn" ,$i, FILENAME ,NR

 if (NF > 0)

  lastword = $NF

}' $*

3.8.17 efopen.c

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 %sn",

  progname, file, mode);

 exit(1);

}

3.8.18 error.c

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

}

3.8.19 field1

awk '{ print $'$1' }'

3.8.20 field2

awk "{ print $$1 }"

3.8.21 fold

# 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%sn", substr(blanks, 1, N-n), substr($0, i)

  }

 } '

3.8.22 frequent

cat $* |

tr -sc A-Za-z '12' |

sort |

uniq -с |

sort -n |

tail |

5

3.8.23 frequent2

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

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

3.8.24 get

# get: extract file from history

1 ... 94 95 96 97 98 99 100 101 102 103
Перейти на страницу:
На этой странице вы можете бесплатно скачать UNIX — универсальная среда программирования - Брайан Керниган торрент бесплатно.
Комментарии