#include	"all.h"
#include	<fio.h>


char*
Frdline(int fd)
{
	Fbuffer *f;
	int n;
	char *nextnl;

	FIOSET(f, fd);
	while((nextnl = memchr(f->next, '\n', f->end - f->next)) == 0) {
		if(f->next == f->buf) {
			if(f->end >= &f->buf[FIOBSIZE])
				f->end = &f->buf[FIOBSIZE-64];	/* full buffer, no newline */
		} else {
			n = f->next-f->buf;
			memmove(f->buf, f->next, FIOBSIZE-n);
			f->next -= n;
			f->end -= n;
		}
		if(_F_fillbuf(fd) <= 0)
			return 0;
	}
	f->lnext = f->next;
	f->next = nextnl+1;
	*nextnl = 0;
	f->flags |= FIO_RDLAST;
	f->flags &= ~FIO_WRITING;
	return f->lnext;
}
