commit 51eb42752da10e6dfc9340fd05efe0345dfe3af5
parent 6f6ecd55378f6008a6527adf994869f57b7421f8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 15 Oct 2018 13:06:47 +0100
[lib/c] Add ftell()
Diffstat:
3 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/lib/c/_flsbuf.c b/lib/c/_flsbuf.c
@@ -5,11 +5,10 @@
int
_flsbuf(FILE *fp)
{
- int lnbuf = fp->flags & _IOLBF;
unsigned char *p;
size_t cnt;
- p = (lnbuf) ? fp->lp : fp->wp;
+ p = (fp->flags & _IOLBF) ? fp->lp : fp->wp;
cnt = p - fp->buf;
if (_write(fp->fd, fp->buf, cnt) != cnt) {
diff --git a/lib/c/ftell.c b/lib/c/ftell.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include "syscall.h"
+#undef ftell
+
+long
+ftell(FILE *fp)
+{
+ long off;
+ unsigned char *p;
+
+ if (fp->flags & _IOERR)
+ return EOF;
+
+ if ((off = _lseek(fp->fd, 0, SEEK_CUR)) < 0) {
+ fp->flags |= _IOERR;
+ return EOF;
+ }
+
+ if (fp->flags & _IOREAD)
+ return off - (fp->wp - fp->rp);
+
+ if (fp->flags & _IOWRITE) {
+ p = (fp->flags & _IOLBF) ? fp->lp : fp->wp;
+ return off + (p - fp->buf);
+ }
+ return off;
+}
diff --git a/lib/c/target/script/objlst.mk b/lib/c/target/script/objlst.mk
@@ -8,7 +8,7 @@ LIBOBJ = bsearch.o qsort.o \
fgets.o gets.o fgetc.o fputc.o getchar.o putchar.o \
fputs.o puts.o fread.o fwrite.o \
getc.o putc.o __putc.o __getc.o \
- rewind.o fseek.o ferror.o feof.o clearerr.o \
+ ftell.o rewind.o fseek.o ferror.o feof.o clearerr.o \
setbuf.o setvbuf.o \
fclose.o fopen.o freopen.o _fpopen.o _flsbuf.o stdio.o \
realloc.o calloc.o malloc.o \
@@ -133,6 +133,9 @@ freopen.o: ../../freopen.c
fseek.o: ../../fseek.c
$(CC) $(SCC_CFLAGS) ../../fseek.c -c
+ftell.o: ../../ftell.c
+ $(CC) $(SCC_CFLAGS) ../../ftell.c -c
+
fwrite.o: ../../fwrite.c
$(CC) $(SCC_CFLAGS) ../../fwrite.c -c