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

 char *s;

{

 Symbol *sp;

 for (sp = symlist; sp != (Symbol*)0; sp = sp->next)

  if (strcmp(sp->name, s) == 0)

   return sp;

 return 0; /* 0 ==> not found */

}

Symbol *install(s, t, d) /* install s in symbol table */

 char *s;

 int t;

 double d;

{

 Symbol *sp;

 char *emalloc();

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

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

 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;

}

<p>3.8 Всякая всячина</p><p>3.8.1 <code>addup1</code></p>

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

 END { print s }'

<p>3.8.2. <code>addup2</code></p>

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 = %6g\n", total

}'

<p>3.8.3 <code>backup</code></p>

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

<p>3.8.4 <code>backwards</code></p>

# backwards: print input in backward line order

awk ' { line[NR] = $0 }

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

<p>3.8.5 <code>badpick.c</code></p>

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

 char *s;

{

 fprintf("%s? ", s);

 if (ttyin() == 'y')

  printf("%s\n", s);

}

<p>3.8.6 <code>bundle</code></p>

# 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

<p>3.8.7 <code>cal</code></p>

# 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

<p>3.8.8 <code>calendar1</code></p>

# calendar: version 1 -- today only

awk <$HOME/calendar '

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

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

' | mail $NAME

<p>3.8.9 <code>calendar2</code></p>

# 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

<p>3.8.10 <code>calendar3</code></p>

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

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

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