ref: 46d3eab0b1e077005fcc4a7bc5257be9a411d1a1
parent: dde4464771221d88577149222e38612b18cac4ce
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Dec 7 03:31:58 EST 2017
[lib/c] Add fseek() and rewind()
--- a/lib/c/src/Makefile
+++ b/lib/c/src/Makefile
@@ -8,7 +8,7 @@
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 \
--- /dev/null
+++ b/lib/c/src/fseek.c
@@ -1,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;
+}
--- /dev/null
+++ b/lib/c/src/rewind.c
@@ -1,0 +1,11 @@
+
+#include <stdio.h>
+#undef rewind
+
+void
+rewind(FILE *fp)
+{
+ fp->flags &= ~_IOERR;
+ fseek(fp, 0, SEEK_SET);
+ clearerr(fp);
+}