shithub: scc

ref: bd41c3ee99a819ae92887538049cdef0ad482763
dir: /lib/c/__getc.c/

View raw version
#include <errno.h>
#include <stdio.h>
#include "syscall.h"
#undef getc

int
__getc(FILE *fp)
{
	int cnt;

	if (fp->flags & (_IOEOF | _IOERR))
		return EOF;

	if ((fp->flags & (_IOREAD | _IORW)) == 0) {
		fp->flags |= _IOERR;
		errno = EBADF;
		return EOF;
	}

	if (fp->flags & _IOSTRG) {
		fp->flags |= _IOEOF;
		return EOF;
	}

	if (fp->buf == NULL) {
		if ((fp->buf = malloc(BUFSIZ)) == NULL) {
			errno = ENOMEM;
			return EOF;
		}
		fp->len = BUFSIZ;
		fp->flags |= _IOALLOC;
		fp->lp = fp->rp = fp->wp = fp->buf;
	}

	if ((cnt = _read(fp->fd, fp->buf, fp->len)) <= 0) {
		fp->flags |= (cnt == 0) ? _IOEOF : _IOERR;
		return EOF;
	}

	fp->flags |= _IOREAD;
	fp->rp = fp->buf;
	fp->wp = fp->buf + cnt;

	return *fp->rp++;
}