scc

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

commit fb8066c837ba6dc3da72fdb4d687e6b4f5ec025b
parent 346d4d01e0614d93034e8b4209a1e91e4504ea02
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  7 Dec 2017 08:31:58 +0000

[lib/c] Add fseek() and rewind()

Diffstat:
Mlib/c/src/Makefile | 2+-
Alib/c/src/fseek.c | 27+++++++++++++++++++++++++++
Alib/c/src/rewind.c | 11+++++++++++
3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -8,7 +8,7 @@ OBJ = bsearch.o \ fgets.o gets.of fgetc.o fputc.o getchar.o putchar.o \ fputs.o puts.o fread.o fwrite.o \ getc.o putc.o __putc.o __getc.o \ - ferror.o feof.o clearerr.o \ + rewind.o fseek.o ferror.o feof.o clearerr.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 \ diff --git a/lib/c/src/fseek.c b/lib/c/src/fseek.c @@ -0,0 +1,27 @@ + +#include <stdio.h> +#include "syscall.h" +#undef fseek + +int +fseek(FILE *fp, long off, int whence) +{ + if (fp->flags & _IOERR) + return EOF; + + if ((fp->flags & _IOWRITE) && fflush(fp)) + return -1; + else if (whence == SEEK_CUR && (fp->flags & _IOREAD)) + off -= fp->wp - fp->rd; + + if (_seek(fp->fd, off, type) < 0) { + fp->flags |= _IOERR; + return EOF; + } + + if (fp->flags & _IORW) + fp->flags &= ~(_IOREAD | _IOWRITE); + fp->flags &= ~_IOEOF; + + return 0; +} diff --git a/lib/c/src/rewind.c b/lib/c/src/rewind.c @@ -0,0 +1,11 @@ + +#include <stdio.h> +#undef rewind + +void +rewind(FILE *fp) +{ + fp->flags &= ~_IOERR; + fseek(fp, 0, SEEK_SET); + clearerr(fp); +}