scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 9e878a06e2febcd985c35b619d5054ec63e37095
parent a4498a9b7e9ea66405c55fd88568d2ed0c08c79c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 29 May 2018 15:18:17 +0100

[lib/c] Fix __putc()

It was using a flsbuf() function that didn't exist. I created
a new file to avoid the dependency with flush() that the code
had, which was going to pull __putc.o even without any write.

Diffstat:
Mlib/c/__putc.c | 25+++++--------------------
Alib/c/_flsbuf.c | 21+++++++++++++++++++++
Mlib/c/fclose.c | 4+++-
Mlib/c/fseek.c | 4+++-
Mlib/c/setvbuf.c | 4+++-
Mlib/c/target/objlst.mk | 2+-
6 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/lib/c/__putc.c b/lib/c/__putc.c @@ -2,35 +2,20 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> -#include "syscall.h" -int -_fflush(FILE *fp) -{ - int lnbuf = fp->flags & _IOLBF; - size_t cnt; - - cnt = ((lnbuf) ? fp->lp : fp->wp) - fp->buf; - - if (_write(fp->fd, fp->buf, cnt) != cnt) { - fp->flags |= _IOERR; - return EOF; - } - fp->rp = fp->wp = fp->buf; - - return 0; -} +extern int _flsbuf(FILE *fp); int fflush(FILE *fp) { - int err = 0; + int err; if (fp) - return _flsbuf(fp); + return flsbuf(fp); + err = 0; for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) { - if ((fp->flags & _IOWRITE) == 0 && _flush(fp)) + if ((fp->flags & _IOWRITE) == 0 && _flsbuf(fp)) err = EOF; } return err; diff --git a/lib/c/_flsbuf.c b/lib/c/_flsbuf.c @@ -0,0 +1,21 @@ + +#include <errno.h> +#include <stdio.h> +#include "syscall.h" + +int +_flsbuf(FILE *fp) +{ + int lnbuf = fp->flags & _IOLBF; + size_t cnt; + + cnt = ((lnbuf) ? fp->lp : fp->wp) - fp->buf; + + if (_write(fp->fd, fp->buf, cnt) != cnt) { + fp->flags |= _IOERR; + return EOF; + } + fp->rp = fp->wp = fp->buf; + + return 0; +} diff --git a/lib/c/fclose.c b/lib/c/fclose.c @@ -2,6 +2,8 @@ #include <stdio.h> #undef fclose +extern int _flsbuf(FILE *fp); + int fclose(FILE *fp) { @@ -10,7 +12,7 @@ fclose(FILE *fp) if ((fp->flags & _IOSTRG) == 0 && fp->flags & (_IOWRITE | _IOREAD | _IOWR)) { r = 0; - if (fflush(fp) == EOF) + if (_flsbuf(fp) == EOF) r = EOF; if (close(fp->fd) < 0) r = EOF; diff --git a/lib/c/fseek.c b/lib/c/fseek.c @@ -3,13 +3,15 @@ #include "syscall.h" #undef fseek +extern int _flsbuf(FILE *fp); + int fseek(FILE *fp, long off, int whence) { if (fp->flags & _IOERR) return EOF; - if ((fp->flags & _IOWRITE) && fflush(fp)) + if ((fp->flags & _IOWRITE) && _flsbuf(fp)) return -1; else if (whence == SEEK_CUR && (fp->flags & _IOREAD)) off -= fp->wp - fp->rd; diff --git a/lib/c/setvbuf.c b/lib/c/setvbuf.c @@ -3,12 +3,14 @@ #include <stdio.h> #undef setvbuf +extern int _flsbuf(FILE *fp); + int setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size) { int flags, r; - if (fflush(fp) == EOF) + if (_flsbuf(fp) == EOF) return EOF; switch (mode) { diff --git a/lib/c/target/objlst.mk b/lib/c/target/objlst.mk @@ -10,7 +10,7 @@ OBJ = bsearch.o qsort.o \ getc.o putc.o __putc.o __getc.o \ rewind.o fseek.o ferror.o feof.o clearerr.o \ setbuf.o setvbuf.o \ - fclose.o fopen.c freopen.c _fpopen.o stdio.o \ + fclose.o fopen.c freopen.c _fpopen.o _flsbuf.o stdio.o \ realloc.o calloc.o malloc.o \ __assert.o strcpy.o strcmp.o strlen.o strchr.o \ strrchr.o strcat.o strncmp.o strncpy.o strncat.o strcoll.o \