shithub: scc

Download patch

ref: 9c9021c8001fd168ccaf6f9085ecc46f04975be5
parent: 371b267234d3eed3c3db5f52bab1a89c83cb08d3
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue May 29 11:18:17 EDT 2018

[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.

--- 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;
+extern int _flsbuf(FILE *fp);
 
-	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;
-}
-
 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;
--- /dev/null
+++ b/lib/c/_flsbuf.c
@@ -1,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;
+}
--- 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 @@
 	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;
--- a/lib/c/fseek.c
+++ b/lib/c/fseek.c
@@ -3,6 +3,8 @@
 #include "syscall.h"
 #undef fseek
 
+extern int _flsbuf(FILE *fp);
+
 int
 fseek(FILE *fp, long off, int whence)
 {
@@ -9,7 +11,7 @@
 	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;
--- 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) {
--- a/lib/c/target/objlst.mk
+++ b/lib/c/target/objlst.mk
@@ -10,7 +10,7 @@
       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 \