###asst.c
/* EXAMPLES OF ASSIGNMENT
 * (No output is produced)
  */
 main()
 	{
 	char c;
 	short i;

 	c = 'A';
	i = 65;
	c = 'X';
	i = -4;
	}
###asst2.c
/* asst2 - print assigned values
 */
main()
	{
	char c;
	short i;

	c = 'A';
	i = 65;
	printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
		 c, c, c, c);
	printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
		 i, i, i, i);
	c = 'X';
	i = -4;
	printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
		 c, c, c, c);
	printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
		 i, i, i, i);
	}
###badpow.c
/* badpow - demonstrate power function (argument mismatch error)
 */
#include "local.h"
#include <math.h>
main()
	{
	short i;

	for (i = 0; i < 10; ++i)
		printf("2 to the power %d equals %.0f\n",
			i, pow(2, i));
	exit(SUCCEED);
	}
###bdrill.c
/* bdrill - binary arithmetic practice
 */
#include "local.h"
#include <ctype.h>
main()
	{
	short a, b;
	short getbin();
	void putbin();

	while ((a = getbin()) != 0 && (b = getbin()) != 0)
		{
		printf("\n    a = ");
		putbin(a);
		printf("\n    b = ");
		putbin(b);
		printf("\na + b = ");
		putbin(a + b);
		printf("\na & b = ");
		putbin(a & b);
		printf("\na | b = ");
		putbin(a | b);
		printf("\na ^ b = ");
		putbin(a ^ b);
		printf("\n");
		}
	}
<:IT:>(<:fC:>getbin<:IT:> function goes here)<:fC:>
<:IT:>(<:fC:>putbin<:IT:> function goes here)<:fC:>
###bits.c
/* bits - examples of bitwise operations
 */
#include "local.h"
main()
	{
	bits b1, b2;

	b1 = 0xF0F0 & 0x1234;
	b2 = b1 | 0x60;
	printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
	b1 = ~1 & 0307;
	b2 = (bits)b1 >> 2;
	printf("b1=0%03o, b2=0%03o\n", b1, b2);
	b1 = 0xF001 | 0x8801;
	b2 = b1 & 0xB800;
	printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
	}
###blast.c
/* blast - print countdown
 */
main()
	{
	short n;

	for (n = 10; n >= 0; n = n - 1)
		printf("%d\n", n);
	printf("Blast off!\n");
	}
###blast2.c
/* blast2 - print countdown
 */
main()
	{
	short n;

	for (n = 10; n >= 0; n = n - 1)
		{
		printf("%d\n", n);
		if (n == 3)
			printf("We have ignition!\n");
		}
	printf("Blast off!\n");
	}
###cadd.c
/* cadd - add two COMPLEX numbers
 */
#include "complex.h"
COMPLEX cadd(x, y)
	COMPLEX x, y;
	{
	COMPLEX z;

	z.real = x.real + y.real;
	z.imag = x.imag + y.imag;
	return (z);
	}
###cap.c
/* cap - capitalize initial letters
 */
#include "local.h"
#include <ctype.h>
main()
	{
	metachar c;

	while (isspace(c = getchar()))
		putchar(c);
	while (c != EOF)
		{
		putchar(toupper(c));
		while (!isspace(c = getchar()))
			putchar(c);
		while (isspace(c))
			{
			putchar(c);
			c = getchar();
			}
		}
	}
/* toupper - convert lower-case letter to lower case
 */
metachar toupper(c)
metachar c;
	{
	return (islower(c) ? c + 'A' - 'a' : c);
	}
###cap2.c
/* cap - capitalize initial letters
 */
#include "local.h"
#include <ctype.h>
main()
	{
	tbool waswhite;	/* was preceding character a whitespace? */
	metachar c;

	for (waswhite = YES; (c = getchar()) != EOF; waswhite = isspace(c))
		{
		if (!isspace(c) && waswhite)
			putchar(toupper(c));
		else
			putchar(c);
		}
	}
/* toupper - convert lower-case letter to lower case
 */
metachar toupper(c)
metachar c;
	{
	return (islower(c) ? c + 'A' - 'a' : c);
	}
###codes1.c
/* codes1 - print ASCII codes
 */
main()
	{
	short c;

	for (c = 0; c <= 127; c = c + 1)
		{
		printf("%3d 0x%02x 0%03o", c, c, c);
		if (' ' <= c && c <= '~')
			printf(" '%c'", c);
		if ('0' <= c && c <= '9')
			printf(" digit");
		if ('A' <= c && c <= 'Z')
			printf(" uppercase");
		if ('a' <= c && c <= 'z')
			printf(" lowercase");
		printf("\n");
		}
	}
###codes2.c
/* codes2 - print ASCII codes
 */
#include "local.h"
main()
	{
	short c;

	while ((c = getchar()) != EOF)
		{
		printf("%3d 0x%02x 0%03o", c, c, c);
		if (' ' <= c && c <= '~')
			printf(" '%c'", c);
		if ('0' <= c && c <= '9')
			printf(" digit");
		if ('A' <= c && c <= 'Z')
			printf(" uppercase");
		if ('a' <= c && c <= 'z')
			printf(" lowercase");
		printf("\n");
		}
	}
###codes3.c
/* codes3 - print ASCII codes
 */
#include "local.h"
main()
	{
	metachar c;	/* return from getchar: char or EOF */

	while ((c = getchar()) != EOF)
		{
		printf("%3d 0x%02x 0%03o", c, c, c);
		if (' ' <= c && c <= '~')
			printf(" '%c'", c);
		if ('0' <= c && c <= '9')
			printf(" digit");
		if ('A' <= c && c <= 'Z')
			printf(" uppercase");
		if ('a' <= c && c <= 'z')
			printf(" lowercase");
		printf("\n");
		}
	}
###codes4.c
/* codes1 - print ASCII codes
 */
#include "local.h"
#include <ctype.h>
main()
	{
	metachar c;

	for (c = 0; c <= 127; c = c + 1)
		{
		printf("%3d 0x%02x 0%03o", c, c, c);
		if (isprint(c))
			printf(" '%c'", c);
		if (isdigit(c))
			printf(" D");
		if (isupper(c))
			printf(" UC");
		if (islower(c))
			printf(" LC");
		if (isalpha(c))
			printf(" L");
		if (isalnum(c))
			printf(" AN");
		if (isspace(c))
			printf(" S");
		if (ispunct(c))
			printf(" P");
		if (iscntrl(c))
			printf(" C");
		printf("\n");
		}
	}
###collect.c
#include <std.h>
#define MAXL 512

/* collect line numbers on common index entries
 */
main(ac, av)
	COUNT ac;
	TEXT **av;
		{
	TEXT c, *s;
	TEXT this[MAXL], next[MAXL];

	c = (ac <= 1) ? '\t' : av[1][0];
	s = (ac <= 2) ? "\n\t" : av[2];
	if (getfmt("%512p", this) <= 0)
		this[0] = '\0';
	putfmt("%p", this);
	while (this[0])
			{
		if (getfmt("%512p", next) <= 0)
			next[0] = '\0';
		if (mthru(this, next, c))
				{
			putfmt("%p", s);
			putfmt("%p", suf(next, c));
				}
		else
				{
			putfmt("\n");
			cpystr(this, next, NULL);
			putfmt("%p", this);
				}
			}
		}

/* match strings thru null or c
 */
mthru(pa, pb, c)
	TEXT *pa, *pb, c;
		{
	for ( ; *pa == *pb && *pa != c; pa++)
		if (*pb == '\0')
			return(1);
		else
			pb++;
	return (*pa == *pb || *pa == '\0' && *pb == c);
		}

/* locate suffix of s following c
 */
suf(s, c)
	TEXT *s, c;
		{
	while (*s && *s++ != c)
		;
	return (s);
		}
###copy.c
/* copy - copy input to output
 */
#include "local.h"
main()
	{
	metachar c;

	while ((c = getchar()) != EOF)
		putchar(c);
	exit(SUCCEED);
	}
###copy2.c
/* copy2 - copy input to output 
 */
#include "local.h"
main()
	{
	char s[BUFSIZ];

	while (getln(s, BUFSIZ) != EOF)
		printf("%s", s);
	}
###copy3.c
/* copy3 - most efficient file copy
 */
#include "local.h"
main()
	{
	char s[BUFSIZ];	/* array for characters */
	short i;		/* number of characters read */

	while (0 != (i = read(STDIN, s, BUFSIZ)))
		{
		if (i < 0)
			error("I/O error on read\n", "");
		else if (i != write(STDOUT, s, i))
			error("I/O error on write\n", "");
		}
	exit(SUCCEED);
	}
/* error - print fatal error message
 */
void error(s1, s2)
	char s1[], s2[];
	{
	write(STDERR, s1, strlen(s1));
	write(STDERR, " ", 1);
	write(STDERR, s2, strlen(s2));
	write(STDERR, "\n", 1);
	exit(FAIL);
	}
###dmpdem.c
/* dump - print memory bytes
 *		(Warning - some usages may be non-portable)
 */
#include "local.h"
#define LINESIZE 16
#define BYTEMASK 0xFF
void dump(s, n)
	char s[];		/* byte address to be dumped */
	unsigned n;		/* number of bytes to dump */
	{
	unsigned i;		/* byte counter */

	for (i = 0; i < n; ++i)
		{
		if (i % LINESIZE == 0)
			printf("\n%08x: ", &s[i]);
		printf(" %02x", s[i] & BYTEMASK);
		}
	printf("\n");
	}
/* dmpdem - demonstrate dump function
 */
main()
	{
	char msg[16];
	double d = 100.;

	strncpy(msg, "testing 1 2 3\n", sizeof(msg));

	/* case 1 - quite proper */
	dump(msg, sizeof(msg));

	/* case 2 - ok, but output will vary with machine */
	dump(&d, sizeof(d));

	/* case 3 - non-portable, may cause hardware error */
	dump(0x40, 4);
	exit(SUCCEED);
	}
###dump.c
/* dump - print memory bytes
 *		(Warning - some usages may be non-portable)
 */
#include "local.h"
#define LINESIZE 16
void dump(s, n)
	char s[];			/* byte address to be dumped */
	unsigned n;			/* number of bytes to dump */
	{
	unsigned i;		/* byte counter */

	for (i = 0; i < n; ++i)
		{
		if (i % LINESIZE == 0)
			printf("\n%08x: ", &s[i]);
		printf(" %02x", s[i]);
		}
	printf("\n");
	}
###echo.c
/* echo - print command-line arguments
 */
#include "local.h"
main(ac, av)
	unsigned ac;
	char *av[];
	{
	unsigned i;

	for (i = 1; i < ac; ++i)
		printf(i < ac-1 ? "%s " : "%s\n", av[i]);
	exit(SUCCEED);
	}
###error.c
/* error - print fatal error message
 */
#include "local.h"
void error(s1, s2)
	char s1[], s2[];
	{
	write(STDERR, s1, strlen(s1));
	write(STDERR, " ", 1);
	write(STDERR, s2, strlen(s2));
	write(STDERR, "\n", 1);
	exit(FAIL);
	}
###fact.c
/* factl - return n! (n factorial)
 */
#include "local.h"
long factl(n)
	long n;
	{
	if (n <= 1)
		return (1);
	else
		return (n * factl(n - 1));
	}
/* fact - demonstrate factl function
 */
main()
	{
	long factl();
	long result;

	result = factl((long)3);
	printf("3! = %ld\n", result);
	exit(SUCCEED);
	}
###factl.c
/* factl - return n! (n factorial)
 */
#include "local.h"
long factl(n)
	long n;
	{
	if (n <= 1)
		return (1);
	else
		return (n * factl(n - 1));
	}
#ifdef TRYMAIN
main()
	{
	long factl();

	if (factl((long)3) != (long)6)
		error("failed 3", "");
	if (factl((long)13) != 1932053504)
		error("failed 13", "");
	exit(SUCCEED);
	}
#endif
###fast.c
/* fast - count to one million
 */
#include "local.h"
main()
	{
    register short units;
    register short thous;

    thous = 0;
    while (++thous <= 1000)
    	{
        units = 0;
        while (++units <= 1000)
           ;
    	}
	}
###getbn.c
/* getbn - get a binary number and print it
 */
#include "local.h"
#include <ctype.h>
main()
	{
	metachar c;
	short n;

	n = 0;
	c = getchar();
	while (c != EOF && isspace(c))
		c = getchar();
	while (c == '0' || c == '1')
		{
		n = ((n << 1) | (c - '0'));
		c = getchar();
		}
	printf("%5u 0x%04x 0%06o\n", n, n, n);
	}
###getbn2.c
/* getbn2 - get and print binary numbers
 */
#include "local.h"
#include <ctype.h>
main()
	{
	short getbin();
	short number;

	while ((number = getbin()) != 0)
		printf("%5u 0x%04x 0%06o\n", number, number, number);
	}
/* getbin - get binary input number
 */
short getbin()
	{
	metachar c;
	short n;

	n = 0;
	c = getchar();
	while (c != EOF && isspace(c))
		c = getchar();
	while (c == '0' || c == '1')
		{
		n = ((n << 1) | (c - '0'));
		c = getchar();
		}
	return (n);
	}
###getpns.c
/* getpns - get pennies, in floating double
 */
#include "local.h"
double getpns()
	{
	long dols;
	short cents;

	if (scanf("%ld.%hd", &dols, &cents) == EOF)
		return (0.);
	return (100 * dols + cents);
	}
###gettt.c
/* gettt - read data into the task table (using gettask function)
 */
#include "local.h"
#include "task3.h"	/* revised to include storage for desc */
#define TSIZE 5
main()
	{
	TASK tt[TSIZE];				/* task table */
	short gettask();			/* function to get one TASK */
	short i;					/* index for printing */
	short n;					/* number of successful reads */

	n = 0;
	while (n < TSIZE && gettask(&tt[n]) == 4)
		++n;
	for (i = 0; i < n; ++i)
		printf("%20s %8ld %8ld %8ld\n",
			tt[i].desc, tt[i].plan, tt[i].start, tt[i].finish);
	}
/* gettask - get one TASK
 */
short gettask(ptask)
	TASK *ptask;
	{
	short ret;					/* returned value from scanf */

	ret = scanf("%20s%ld%ld%ld",
		ptask->desc, &ptask->plan, &ptask->start, &ptask->finish);
	return (ret);
	}
###guess.c
/* guess - guess a hidden number between 1 and 15, in 3 guesses
 */
#include "local.h"
main()
	{
	char line[BUFSIZ];	/* input line */
	tbool found;		/* have I found it? */
	short n;			/* how many guesses left */
	short range;		/* how much to ajust next guess */
	short try;			/* next number to try */
	metachar reply;		/* the user's reply */

	found = NO;
	n = 3;
	range = 4;
	try = 8;
	printf("Each time I guess, please answer\n");
	printf(" H if I'm high\n L if I'm low\n E if I guessed it\n");
	while (n > 0 && !found)
		{
		printf("I guess %d\n", try);
		if (getln(line, BUFSIZ) == EOF)
			error("Bye!", "");
		reply = line[0];
		if (reply == 'H' || reply == 'h')
			{
			try -= range;
			range /= 2;
			--n;
			}
		else if (reply == 'L' || reply == 'l')
			{
			try += range;
			range /= 2;
			--n;
			}
		else if (reply == 'E' || reply == 'e')
			found = YES;
		else
			printf("Please type H, L, or E\n");
		}
	printf("Your number is %d\nThanks for the game\n", try);
	exit(SUCCEED);
	}
###guess2.c
#include "local.h"
/* guess2 - guess a hidden number between 1 and 15, in 3 guesses
 */
main()
	{
	tbool found = NO;	/* have I found it? */
	short n = 3;		/* how many guesses left */
	short range = 4;	/* how much to ajust next guess */
	short try = 8;		/* next number to try */
	metachar reply;		/* the user's reply */

	printf("Each time I guess, please answer\n");
	printf("H if I'm high\nL if I'm low\E if I guessed it\n");
	while (n > 0 && !found)
		{
		printf("I guess %d\n", try);
		reply = getchar();
		while (getchar() != '\n')
			;
		switch (reply)
			{
		case 'H':
		case 'h':
			try -= range;
			range /= 2;
			--n;
			break;
		case 'L':
		case 'l':
			try += range;
			range /= 2;
			--n;
			break;
		case 'E':
		case 'e':
			found = YES;
			break;
		default:
			printf("Please type H, L, or E\n");
			break;
			}
		}
	printf("Your number is %d\nThanks for the game\n", try);
	exit(SUCCEED);
	}
###helbad.c
main()
	{
	write(1, "hello, world\n", 13)
	}
###helerr.c
main
	{
	write(1, "hello, world\n", 13);
	}
###hello.c
main()
	{
	write(1, "hello, world\n", 13);
	}
###hello2.c
/* hello2 - print greeting
 */
main()
	{
	write(1, "hello, world\n", 13);
	}
###hello3.c
/* hello3 - print greeting */
main
(
)
{
write
(
1
,
"hello, world\n"
,
13
)
;
}
###hello4.c
/* hello4 - print greeting
 */
#define STDOUT  1
#define MESSAGE "hello, world\n"
#define LENGTH  13
main()
	{
	write(STDOUT, MESSAGE, LENGTH);
	}
###hello5.c
/* hello5 - print greeting
 */
#include "local.h"
#define MESSAGE "hello, world\n"
#define LENGTH  13
main()
	{
	write(STDOUT, MESSAGE, LENGTH);
	}
###index.c
/* index - return index of first occurrence of char c in string s
 *	pointer version
 */
#include "local.h"
char *index(s, c)
	char s[], c;
	{
	while (*s != '\0' && *s != c)
		++s;
	return (*s == c ? s : NULL);
	}
###index0.c
/* index0 - return index of first occurrence of char c in string s
 *	subscripted version
 */
#include "local.h"
char *index0(s, c)
	char s[], c;
	{
	unsigned i = 0;

	while (s[i] != '\0' && s[i] != c)
		++i;
	return (s[i] == c ? &s[i] : NULL);
	}
###inits.c
/* inits - initialization examples
 */
#include "local.h"
main()
	{
	char c = 'x';
	short i = 1;
	short j = i * 2;

	printf("%d %d %c\n", i, j, c);
	exit(SUCCEED);
	}
###loadtt.c
/* loadtt - read data into the task table
 */
#include "local.h"
#include "task.h"	/* the original include-file: TIME == long */
#define TSIZE 5
main()
	{
	TASK tt[TSIZE];				/* task table */
	char tstring[TSIZE][21];	/* string storage */
	short i;					/* index for printing */
	short n;					/* number of successful reads */
	short ret;					/* returned value from scanf */

	n = 0;
	FOREVER
		{
		tt[n].desc = tstring[n];
		ret = scanf("%20s%ld%ld%ld",
			tt[n].desc, &tt[n].plan, &tt[n].start, &tt[n].finish);
		if (ret == EOF)
			break;
		else if (ret != 4)
			error("Data error", "");
		else if (++n >= TSIZE)
			break;
		}
	for (i = 0; i < n; ++i)
		printf("%20s %8ld %8ld %8ld\n",
			tt[i].desc, tt[i].plan, tt[i].start, tt[i].finish);
	}
###lpow.c
/* lpow - power function (for long data)
 */
long lpow(lnum, n)
	long lnum;		/* base */
	long n;			/* exponent */
	{
	long p;		/* local ("auto") result */

	p = 1;
	for ( ; n > 0; --n)
		p *= lnum;
	return (p);
	}
###maxmin.c
/* maxmin - print the max and min of two double inputs
 */
#include "local.h"
main()
	{
	double a, b;

	if (scanf("%lf %lf", &a, &b) == 2)
		{
		printf("  a = %12.6e\n", a);
		printf("  b = %12.6e\n", b);
		printf("max = %12.6e\n", a < b ? a : b);
		printf("min = %12.6e\n", a < b ? b : a);
		}
	}
###mortg.c
/* mortg - compute table of payments on mortgage
 */
#include "local.h"
#include <math.h>
main()
	{
	double intmo;	/* monthly interest */
	double intyr;	/* annual interest */
	double bal;		/* balance remaining */
	double pmt;		/* monthly payment */
	double prinpmt;	/* payment allocated to principal */
	double intpmt;	/* payment allocated to interest */
	double dnpmts;	/* number of payments, in double */
	short i;		/* loop index */
	short npmts;	/* number of payments */
	short nyears;	/* number of years */

	printf("Enter principal (e.g. 82500.00): ");
	scanf("%lf", &bal);
	printf("Enter annual interest rate (e.g. 16.25): ");
	scanf("%lf", &intyr);
	printf("Enter number of years: ");
	scanf("%hd", &nyears);
	printf("\nprincipal=%.2f  interest=%.4f%%  years=%d\n\n",
		bal, intyr, nyears);
	intyr /= 100.;
	intmo = intyr / 12.;
	npmts = nyears * 12;
	dnpmts = npmts;
	pmt = bal * (intmo / (1. - pow(1. + intmo, -dnpmts)));
	printf("%8s %10s %10s %10s %10s\n",
		"payment", "total", "interest", "principal", "balance");
	printf("%8s %10s %10s %10s\n",
		"number", "payment", "payment", "payment");
	printf("%8s %10s %10s %10s %10.2f\n",
		"", "", "", "", bal);
	for (i = 1; i <= npmts; ++i)
		{
		intpmt = bal * intmo;
		if (i < npmts)
			prinpmt = pmt - intpmt;
		else
			prinpmt = bal;
		bal -= prinpmt;
		printf("%8d %10.2f %10.2f %10.2f %10.2f\n",
			i, intpmt + prinpmt, intpmt, prinpmt, bal);
		}
	}
###mortg2.c
/* mortg2 - compute table of payments on mortgage
 */
#include "local.h"
#include <math.h>
#define ROUNDING .5	/* on machines that do rounding, make it 0 */
main()
	{
	double dbal;	/* balance, in double - dollars */
	double dnpmts;	/* number of payments, in double */
	double intmo;	/* monthly interest */
	double intyr;	/* annual interest */
	long bal;		/* balance remaining - pennies */
	long pmt;		/* monthly payment - pennies */
	long prinpmt;	/* payment allocated to principal - pennies */
	long intpmt;	/* payment allocated to interest - pennies */
	short i;		/* loop index */
	short npmts;	/* number of payments */
	short nyears;	/* number of years */

	printf("Enter principal (e.g. 82500.00): ");
	scanf("%lf", &dbal);
	bal = 100. * dbal;
	printf("Enter annual interest rate (e.g. 16.25): ");
	scanf("%lf", &intyr);
	printf("Enter number of years: ");
	scanf("%hd", &nyears);
	printf("\nprincipal=%10.2f", dbal);
	printf("  interest=%.4f%%  years=%d\n\n", intyr, nyears);
	intyr /= 100.;
	intmo = intyr / 12.;
	npmts = nyears * 12;
	dnpmts = npmts;
	pmt = ROUNDING + bal * (intmo / (1. - pow(1. + intmo, -dnpmts)));
	printf("%8s %10s %10s %10s %10s\n",
		"payment", "total", "interest", "principal", "balance");
	printf("%8s %10s %10s %10s\n",
		"number", "payment", "payment", "payment");
	printf("%8s %10s %10s %10s %10ld\n",
		"", "", "", "", bal);
	for (i = 1; i <= npmts; ++i)
		{
		intpmt = ROUNDING + bal * intmo;
		if (i < npmts)
			prinpmt = pmt - intpmt;
		else
			prinpmt = bal;
		bal -= prinpmt;
		printf("%8d %10ld %10ld %10ld %10ld\n",
			i, intpmt + prinpmt, intpmt, prinpmt, bal);
		}
	}
###nfrom.c
/* nfrom - return a number between low and high, inclusive
 */
#include "local.h"
short nfrom(low, high)
	short low, high;
	{
	short rand();
	short nb = high - low + 1;

	return (rand() % nb + low);
	}
###noinit.c
/* noinit - some illegal initializers
 */
#include "local.h"
short a = 0;
short b = a + 1;
short c[5] = {4, 3, 2, 1};
main()
	{
	short d = a + 2;
	short e[3] = {1, 2, 3};
	static short f = d + 1;
	static short g[2] = {4, 5, 6};

	printf("initializers\n");
	}
###onebox.c
/* onebox - draw a box around input file
 */
#include "local.h"
main(ac, av)
	unsigned ac;
	char *av[];
	{
	short max = 0;
	char s[BUFSIZ];
	char *index();
	static char rule[] =
		" ____________________________________________________________";
	FILE *fp;

	fp = fopen(av[1], "r") || error("can't open ", av[1]);
	while (fgets(s, BUFSIZ, fp))
		max = MAX(max, strlen(s) - 1);
	fclose(fp);
	fp = fopen(av[1], "r") || error("can't open ", av[1]);
	printf("%*s\n", max+3, rule);
	while (fgets(s, BUFSIZ, fp))
		{
		*index(s, '\n') = '\0';
		printf("| %*s |\n", max, s);
		}
	printf("%*s\n", max+3, rule);
	fclose(fp);
	}
###pegs.c
/* pegs.c - three functions (push, pop, dumppg) for Towers of Hanoi
 */
#include "local.h"
#include "pegs.h"
static short pegs[3][NDISKS] = 0;
static short ndisks[3] = 0;
/* push - put disk onto peg
 */
void push(peg, disk)
	short peg;	/* which peg: 0, 1, ... */
	short disk;	/* which disk: 1, 2, ... */
	{
	if (peg < 0 || 3 <= peg)
		{
		printf("Cannot push onto peg %d\n", peg);
		exit(FAIL);
		}
	else
		pegs[peg][ndisks[peg]++] = disk;
	}
/* pop - remove disk from peg
 */
short pop(peg)
	short peg;
	{
	if (peg < 0 || 3 <= peg)
		{
		printf("Cannot pop peg %d\n", peg);
		exit(FAIL);
		}
	else if (ndisks[peg] < 1)
		{
		printf("Cannot pop peg %d (it has %d disks)\n",
			peg, ndisks[peg]);
		exit(FAIL);
		}
	else
		return (pegs[peg][--ndisks[peg]]);
	}
/* dumppg - print status of disks and pegs
 */
void dumppg()
	{
	short i;	/* index over pegs */
	short j;	/* index over disks */

	for (i = 0; i < 3; ++i)
		{
		printf("Peg %d:", i);
		for (j = 0; j < ndisks[i]; ++j)
			printf(" %d", pegs[i][j]);
		printf("\n");
		}
	}
###pgm123.c
#include <stdio.h>
#include "local.h"
/* guesser - guess a hidden number between 1 and 15, in 3 guesses
 */
main()
	{
	bool found = NO;	/* have I found it? */
	short n = 3;		/* how many guesses left */
	short range = 4;	/* how much to ajust next guess */
	short try = 8;		/* next number to try */
	char reply;			/* the user's reply */

	printf("Each time I guess, please answer\n");
	printf("H if I'm high\nL if I'm low\E if I guessed it\n");
	while (n > 0 && !found)
		{
		printf("I guess %d\n", try);
		scanf("%c", &reply);
		if (reply == 'H')
			{
			try -= range;
			range /= 2;
			--n;
			}
		else if (reply == 'L')
			{
			try += range;
			range /= 2;
			--n;
			}
		else if (reply == 'E')
			found = YES;
		else
			printf("Please type H, L, or E\n");
		}
	printf("Your number is %d\nThanks for the game\n", try);
	}
###pow.c
/* pow - return (positive) x to the power y
 */
double pow(x, y)
	double x;	/* base */
	double y;	/* exponent */
	{
	double exp();	/* exponential function */
	double log();	/* natural log function */

	return (exp(log(x) * y));
	}
###powdem.c
/* powdem - demonstrate power function
 */
#include "local.h"
#include <math.h>
main()
	{
	short i;

	for (i = 0; i < 10; ++i)
		printf("2 to the power %d equals %.0f\n",
			i, pow(2., (double)i));
	exit(SUCCEED);
	}
/* pow - return (positive) x to the power y
 */
double pow(x, y)
	double x;	/* base */
	double y;	/* exponent */
	{
	return (exp(log(x) * y));
	}
###pr2a.c
/* pr2a - print the sum of two long inputs
 */
#include "local.h"
main()
	{
	long a, b;

	scanf("%lx %lx", &a, &b);
	printf("    a = %8lx\n", a);
	printf("    b = %8lx\n", b);
	printf("a + b = %8lx\n", a + b);
	}
###pr2b.c
/* pr2b - print the sum of two double inputs
 */
#include "local.h"
main()
	{
	double a, b;

	scanf("%lf %lf", &a, &b);
	printf("    a = %12.6e\n", a);
	printf("    b = %12.6e\n", b);
	printf("a + b = %12.6e\n", a + b);
	}
###prsam.c
/* prsam - print sampler
 */
#include "local.h"
main()
	{
	static char sampler[35][61] =
		{
		"LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVE",
		"L            OVELOVELOVELOVELOVELOVELOV         LOVELOVELOVE",
		"LOV        ELOVELOVELOVELOVELOVELOV                 LOVELOVE",
		"LOVE      VELOVELOVELOVELOVELOVEL                     VELOVE",
		"LOVE      VELOVELOVELOVELOVELOVE            LOVEL      ELOVE",
		"LOVE      VELOVELOVELOVELOVELOV           VELOVELO      LOVE",
		"LOVE      VELOVELOVELOVELOVELOV          OVELOVELOV     LOVE",
		"LOVE      VELOVELOVELOVELOVELOV         LOVELOVELOV     LOVE",
		"LOVE      VELOVELOVELOVELOVELOV        ELOVELOVELO      LOVE",
		"LOVE      VELOVELOVELOVELOVELOV       VELOVELOVEL       LOVE",
		"LOVE      VELOVELOVELOVELOVELOV      OVELOVELOVE        LOVE",
		"LOVE      VELOVELOVELOVELOVEL V     LOVELOVELOV         LOVE",
		"LOVE      VELOVELOVELOVELOVE  V     LOVELOVELO          LOVE",
		"LOVE      VELOVELOVELOVELOV   V      OVELOVEL           LOVE",
		"LOVE      VELOVELOVELOVEL     VE                       ELOVE",
		"L                             VELOV                 LOVELOVE",
		"L                             VELOVELOV         LOVELOVELOVE",
		"L             VELOV                                        E",
		"L             VELOV                                        E",
		"LOVE      VELOVELOVELOV   VELOVELOVE      VELOVELOVELO     E",
		"LOVEL      ELOVELOVELO   OVELOVELOVE      VELOVELOVELOVE   E",
		"LOVEL      ELOVELOVELO   OVELOVELOVE      VELOVELOVELOVEL  E",
		"LOVELO      LOVELOVEL   LOVELOVELOVE      VELOVELOVELOVELO E",
		"LOVELO      LOVELOVEL   LOVELOVELOVE      VELOVELOVELOVELOVE",
		"LOVELOV      OVELOVE   ELOVELOVELOVE      VELOVEL VELOVELOVE",
		"LOVELOV      OVELOVE   ELOVELOVELOVE              VELOVELOVE",
		"LOVELOVE      VELOV   VELOVELOVELOVE      VELOVE  VELOVELOVE",
		"LOVELOVE      VELOV   VELOVELOVELOVE      VELOVEL VELOVELOVE",
		"LOVELOVEL      ELO   OVELOVELOVELOVE      VELOVELOVELOVELO E",
		"LOVELOVEL      ELO   OVELOVELOVELOVE      VELOVELOVELOVEL  E",
		"LOVELOVELO      L   LOVELOVELOVELOVE      VELOVELOVELOVE   E",
		"LOVELOVELO          LOVELOVELOVELOVE      VELOVELOVELO     E",
		"LOVELOVELOV        ELOVELOVELOVE                           E",
		"LOVELOVELOV        ELOVELOVELOVE                           E",
		"LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVE"
		};
	short i;			/* row index for sampler */
	short j;			/* column index for sampler */
	short len;			/* length of s */
	char s[BUFSIZ];		/* user's message */

	printf("Enter a string:");
	if ((len = getln(s, BUFSIZ)) == EOF)
		error("Bye!", "");
	printf("\n");
	s[--len] = '\0';
	len = len < 60 ? len : 60;
	for (i = 0; i < 35; ++i)
		{
		for (j = 0; j < 60; ++j)
			if (sampler[i][j] != ' ')
				sampler[i][j] = s[j % len];
		printf("%s\n", sampler[i]);
		}
	}
###putbin.c
/* putbin - print number in binary format
 */
void putbin(n)
	short n;
	{
	short i;

	for (i = 15; i >= 0; i = i - 1)
		{
		if ((n & (1 << i)) == 0)
			printf("0");
		else
			printf("1");
		}
	}
###rand.c
/* rand, srand - generate random numbers
 */
#include "local.h"
static long rnum = 0;
void srand(n)
	short n;
	{
	rnum = (long)n;
	}
short rand()
	{
	rnum = rnum * 0x41C64E6D + 0x3039;
	return ((short)(rnum >> 16) & 0x7FFF);
	}
###receip.c
/* receip - deliver a unique receipt number
 */
#include "local.h"
short receip()
	{
	static short number = 1;

	return (number++);
	}
###recpt1.c
/* recpt1 - receipt example #1
 */
#include "local.h"
main()
	{
	short receip();

	printf("First = %d\n", receip());
	printf("Second = %d\n", receip());
	exit(SUCCEED);
	}
short receip()
	{
	short number = 1;

	return (number++);
	}
###recpt2.c
/* recpt2 - print two receipt numbers
 */
#include "local.h"
main()
	{
	short receip();

	printf("First = %d\n", receip());
	printf("Second = %d\n", receip());
	exit(SUCCEED);
	}
short receip()
	{
	static short number = 1;

	return (number++);
	}
###recpt3.c
/* recpt3 - print two receipt numbers
 */
#include "local.h"
main()
	{
	short receip();

	printf("First = %d\n", receip());
	printf("Second = %d\n", receip());
	}
###remark.c
/* remark - print non-fatal error message
 */
#include "local.h"
void remark(s1, s2)
	char s1[], s2[];
	{
	write(STDERR, s1, strlen(s1));
	write(STDERR, " ", 1);
	write(STDERR, s2, strlen(s2));
	write(STDERR, "\n", 1);
	}
###revers.c
/* revers - print input lines reversed
 */
#include "local.h"
main()
	{
	char line[BUFSIZ];	/* the line of input text */
	short len;			/* length of line */

	while ((len = getln(line, BUFSIZ)) != EOF)
		{
		if (line[len - 1] == '\n')
			line[--len] = '\0';
		reverse(line);
		printf("%s\n", line);
		}
	}
void reverse(s)
	char s[];
	{
	char t;
	short i, j;

	for (i = 0, j = strlen(s) - 1; i < j; ++i, --j)
		t = s[i], s[i] = s[j], s[j] = t;
	}
###shuf52.c
/* shuf52 - shuffle a deck of 52 cards and print result
 */
#include "local.h"
#define NCARDS 52
main()
	{
	short cards[NCARDS];
	short i;

	for (i = 0; i < NCARDS; ++i)
		cards[i] = i;
	shuffl(cards);
	for (i = 0; i < NCARDS; ++i)
		{
		printf("%2d ", cards[i]);
		if (i % 13 == 12)
			putchar('\n');
		}
	putchar('\n');
	}
/* shuffl - permute the cards
 */
void shuffl(deck)
	short deck[];
	{
	short t;		/* temporary for swap */
	short i;		/* index for loop over cards */
	short j;		/* index for swap */
	short nfrom();	/* fn to produce random no. */

	for (i = 0; i < NCARDS - 1; ++i)
		{
		j = nfrom(i, NCARDS - 1);
		t = deck[j], deck[j] = deck[i], deck[i] = t;
		}
	}
###sizes.c
/* sizes - report the size of some types and expressions
 */
#include "local.h"
main()
	{
	char c;
	char s[512];
	short n;
	short m[40];

	printf("%3d %3d\n", sizeof(c), sizeof(char));
	printf("%3d %3d\n", sizeof(s), sizeof(char[512]));
	printf("%3d %3d\n", sizeof(n), sizeof(short));
	printf("%3d %3d\n", sizeof(m), sizeof(short[40]));
	}
###slow.c
/* slow - count to one million
 */
#include "local.h"
main()
	{
    short units;
    short thous;

    thous = 0;
    while (++thous <= 1000)
    	{
        units = 0;
        while (++units <= 1000)
            ;
    	}
	}
###stack1.c
/* stack1 - stack example 1
 */
#include "local.h"
main()
	{
	f1(1);
	}
f1(n)
	short n;
	{
	f2(n + 1);
	}
f2(n)
	short n;
	{
	printf("%d\n", n);
	}
###strcpy.c
/* strcpy - copy characters from s2 to s1 
 */
void strcpy(s1, s2)
	char s1[], s2[];
	{
	unsigned i;

	i = 0;
	while (s2[i] != '\0')
		{
		s1[i] = s2[i];
		++i;
		}
	s1[i] = '\0';
	}
###strcpy2.c
/* strcpy2 - copy characters from s2 to s1 
 */
#include "local.h"
char *strcpy2(s1, s2)
	register char s1[], s2[];
	{
	char *s0 = s1;

	while ((*s1++ = *s2++) != '\0')
		;
	return (s0);
	}
###string.c
/* string - practice with character arrays
 */
#include "local.h"
main()
	{
	char a1[BUFSIZ];
	char a2[BUFSIZ];

	strcpy(a1, "every ");
	strcpy(a2, "good boy ");
	strcat(a2, "does ");
	if (strlen(a1) < strlen(a2))
		strcat(a2, "fine ");
	else
		strcat(a1, "very ");
	if (strcmp(a1, a2) < 0)
		{
		strcat(a1, a2);
		printf("%s\n", a1);
		}
	else
		{
		strcat(a2, a1);
		printf("%s\n", a2);
		}
	}
###strlen.c
/* strlen - return length of string s
 */
unsigned strlen(s)
	char s[];
	{
	unsigned i;

	for (i = 0; s[i] != '\0'; ++i)
		;
	return (i);
	}
###strncpy.c
/* strncpy - copy n bytes from s2 to s1 (using while)
 */
void strncpy(s1, s2, n)
	char s1[], s2[];
	unsigned n;
	{
	unsigned i;

	i = 0;
	while (i < n && s2[i] != '\0')
		{
		s1[i] = s2[i];
		++i;
		}
	while (i < n)
		{
		s1[i] = '\0';
		++i;
		}
	}
###strncpy2.c
/* strncpy - copy n bytes from s2 to s1 (using for)
 */
void strncpy(s1, s2, n)
	char s1[], s2[];
	unsigned n;
	{
	unsigned i;

	for (i = 0; i < n && s2[i] != '\0'; ++i)
		s1[i] = s2[i];
	for ( ; i < n; ++i)
		s1[i] = '\0';
	}
###strscn.c
/* strscn - return the index of c in string s
 */
unsigned strscn(s, c)
	char s[];	/* string to be scanned */
	char c;		/* char to be matched */
	{
	unsigned i;

	for (i = 0; s[i] != c && s[i] != '\0'; ++i)
		;
	return (i);
	}
###swap.c
/* swap - interchange two short integers
 */
#include "local.h"
void swap(pi, pj)
	short *pi, *pj;
	{
	short t;

	t = *pi, *pi = *pj, *pj = t;
	}
###testrand.c
#include "local.h"
short seed = 0;
short n = 0;
long rnum = 0;
short mysrand(n)
short n;
	{
	rnum = n;
	}
short myrand()
	{
	rnum = rnum * 0x41c64e6d + 0x3039;
	return ((rnum >> 16) & ~0x8000);
	}
main()
	{
	srand(seed);
	mysrand(seed);
	while (myrand() == rand())
		putchar('x');
	}
###tolower.c
/* tolower - convert upper-case letter to lower case
 */
metachar tolower(c)
	metachar c;
	{
	return (isupper(c) ? c + 'a' - 'A' : c);
	}
###toupper.c
/* toupper - convert lower-case letter to upper case
 */
metachar toupper(c)
	metachar c;
	{
	return (islower(c) ? c + 'A' - 'a' : c);
	}
###tower.c
/* tower - solve Tower of Hanoi
 */
#include <stdio.h>
#include "local.h"
#include "pegs.h"
main()
	{
	short i;	/* index over disks */

	for (i = 1; i <= NDISKS; ++i)
		push(0, NDISKS + 1 - i);
	printf("Start:\n");
	dumppg();
	move(NDISKS, 0, 2);
	printf("Finish:\n");
	dumppg();
	}
/* move - move n disks from peg p0 to peg p1
 */
void move(n, p0, p1)
short n;	/* how many disks to move */
short p0;	/* from which peg */
short p1;	/* to which peg */
	{
	short last;		/* the last disk to move */
	short other();

	if (1 < n)
		move(n - 1, p0, other(p0, p1));
	last = pop(p0);
	push(p1, last);
	printf("Move disk %d from %d to %d\n", last, p0, p1);
	if (1 < n)
		move(n - 1, other(p0, p1), p1);
	}
/* other - return the number of peg other than p0 and p1
 */
short other(p0, p1)
short p0;
short p1;
	{
	short n;
	static short otherpeg[8] = 	{9, 9, 9, 2, 9, 1, 0, 9};

	n = otherpeg[(1 << p0) + (1 << p1)];
	if (n == 9)
		{
		printf("No other peg for %d, %d\n", p0, p1);
		exit(FAIL);
		}
	else
		return (n);
	}
###vacat.c
/* vacat - structure practice
 */
#include "local.h"
#include "task.h"
main()
	{
	static TASK vacation = 	{"leave for Hawaii", 1984, 0, 0};

	vacation.start = vacation.finish = (long)1983;
	printf("vacation.desc = %s\n", vacation.desc);
	printf("vacation.plan = %ld\n", vacation.plan);
	printf("vacation.start = %ld\n", vacation.start);
	printf("vacation.finish = %ld\n", vacation.finish);
	}
###x.c
#include "local.h"
short a = 2;
static short b = 3;
main()
	{
	short c = a + b;

	xsub(c);
	}
xsub(d)
	short d;
	{
	short e = 7 * d;

	ysub(e);
	}
###ysub.c
#include "local.h"
ysub(f)
	short f;
	{
	extern short a;

	printf("%d\n", a + f);
	}
###EOF
