/* Copyright (C) 1981,1982 by Manx Software Systems */
/* Copyright (C) 1981  Thomas Fenwick */
#include "libc.h"

agetc(ptr)
register FILE *ptr;
{
	register int c;

top:
	switch ( c = getc(ptr) ) {
	case 0x1a:
		--ptr->_bp;
		return -1;
	case '\r':
	case 0:
		goto top;
	}
	return c;
}

aputc(c,ptr)
int c; FILE *ptr;
{
	if ( c == '\n' )
		if ( putc('\r',ptr) < 0 )
			return -1;
	return putc(c,ptr);
}

char *
fgets(s, n, stream)
char *s;
FILE *stream;
{
	register c;
	register char *string;

	string = s;
	while (--n > 0 && (c = agetc(stream)) >= 0) {
		*string++ = c;
		if (c == '\n')
			break;
	}
	if (c < 0 && string == s)
		return NULL;
	*string = '\0';
	return(s);
}

fputs(s,stream)
register char *s;
FILE *stream;
{
	register int i;

	while ( *s )
		if (aputc(*s++,stream) < 0)
			return(EOF);
	return 0;
}
