#include "stdio.c"
#include "alloc.c"
#include "crunt2.c"

extern	int	*allocs;
extern	int	errno;
#define	RECSIZ	128
main()
{

	char filename[14];
	int mode, fd, rec;
	char ch, instr[80], buffer[RECSIZ+1];

	allocs=0;
	puts("Enter filename : "); gets(filename);
	puts("\n");
	errno = 0;
	if((fd=open(filename,2))==-1) {
		puts("\ncannot open file: ");
		puts(filename);
		puts("errno is ");
		putdec(errno);
		puts("\n");
		exit(0);
	}
	for(;;) {
		puts("Enter record# (-1 to exit): ");
		gets(instr);
		puts("\n");
		rec=atoi(instr);
		if(rec==-1) break;
		if(seek(fd, rec * RECSIZ, 0)==-1) {
			puts("record out of range\n\n");
		}
		else {
			errno = 0;
			if(read(fd,buffer,RECSIZ)!=RECSIZ)
			{
				puts("Can't read; errno is ");
				putdec(errno);
				puts("\n");
			}
			buffer[RECSIZ]=0;
			puts(buffer);
			puts("\n\n");
		}
	}
}

atoi(n)
 char *n;
{
	register int val; 
	char c;
	int sign;

	while(iswhite(*n))
		++n;

	switch(*n) {
	case '-':
		sign = 1;
		++n;
		break;
	case '+':
		++n;
	default:
		sign = 0;
	}

	for(val=0; isdigit(c = *n++);)
		val = val*10 + c - '0';

	return sign? -val: val;
}

putdec(i)
 int	i;
{
	register int	n, x;

	if((n = i) < 0) {
		if((n = -n) < 0) {
			puts("-32768"); /* only on 16 bit int's */
			return;
		}
		putchar('-');
	}
	if(x = n / 10)
		putdec(x);
	putchar(n % 10 + '0');
}
errno is ");
		putdec(errno);
		puts("\n");
		exit(0);
	}
	for(;;) {
		puts("Enter record# (-1 to exit): ");