9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 548f490090944f6f774e0833bef40f9c8bfce6e8
parent edba2453ded15fda8c0302757cafbc994003773c
Author: Roberto Vargas <roberto.vargas@arm.com>
Date:   Wed, 17 Oct 2018 18:26:28 +0100

[libc] Import __getc.c from scc

Change-Id: I3b3bdeda22c73b983b728770fd7a8aec0fe3ad34

Diffstat:
Msrc/libc/Makefile | 1+
Asrc/libc/__getc.c | 45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/src/libc/Makefile b/src/libc/Makefile @@ -15,6 +15,7 @@ COBJ = abort.o \ memset.o \ printk.o \ __putc.o \ + __getc.o \ ctype.o \ isalnum.o \ isalpha.o \ diff --git a/src/libc/__getc.c b/src/libc/__getc.c @@ -0,0 +1,45 @@ +#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++; +}