OrionLibC
Barry Importing existing Orion LibC 03048a9 (2 years, 3 months ago)diff --git a/stdio/getc.c b/stdio/getc.c new file mode 100644 index 0000000..e1a8a8f --- /dev/null +++ b/stdio/getc.c @@ -0,0 +1,48 @@ +#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); +}