BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionLibC / blob / master / stdio / getc.c

// Related

OrionLibC

Barry Importing existing Orion LibC 03048a9 (2 years, 3 months ago)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* Allocate and fill a file's buffer */
static int
_fillbuf(FILE *fp)
{
	int bufsize;

	if ((fp->flags & (FILE_READ | FILE_EOF | FILE_ERR)) != FILE_READ)
		return EOF;
	bufsize = (fp->flags & FILE_UNBUF) ? 1 : 1024;

	/* Create buffer */
	if (fp->buf == NULL) {
		fp->buf = malloc(bufsize);
		if (!fp->buf)
			return EOF;
	}
	fp->ptr = fp->buf;
	fp->count = read(fp->fd, fp->ptr, bufsize);
	if (--fp->count < 0) {
		if (fp->count == -1)
			fp->flags |= FILE_EOF;
		else
			fp->flags |= FILE_ERR;
		fp->count = 0;
		return EOF;
	}
	return (unsigned char) *fp->ptr++;
}

/* Get a character from a stream */
inline int
getc(FILE *stream)
{
	if (--stream->count >= 0)
		return (unsigned char) *stream->ptr++;
	return _fillbuf(stream);
}

/* Get a character from standard input */
int
getchar(void)
{
	getc(stdin);
}