shithub: scc

Download patch

ref: 3c2cec93a9475a27f854ac2541f69c4a7a0b3657
parent: 831becab55913062b3a314c34cec38fa511bb21b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Oct 15 09:06:47 EDT 2018

[lib/c] Add ftell()

--- 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) {
--- /dev/null
+++ b/lib/c/ftell.c
@@ -1,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;
+}
--- a/lib/c/target/script/objlst.mk
+++ b/lib/c/target/script/objlst.mk
@@ -8,7 +8,7 @@
          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 \
@@ -132,6 +132,9 @@
 
 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