shithub: scc

Download patch

ref: fd277d39d846ec05a4940fd7f7f95333900db1c9
parent: b5c01b5d701dc1c3bbca24175041390a3c4a3db3
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 27 07:44:57 EDT 2021

libc: Update stdio to last version

This code is being updated out of the tree of scc and
it is time to synchroniza both copies now.

--- a/src/libc/objs/common-objs.mk
+++ b/src/libc/objs/common-objs.mk
@@ -30,6 +30,7 @@
 	stdio/fclose.$O\
 	stdio/feof.$O\
 	stdio/ferror.$O\
+	stdio/fflush.$O\
 	stdio/fgetc.$O\
 	stdio/fgets.$O\
 	stdio/fopen.$O\
--- a/src/libc/stdio/Makefile
+++ b/src/libc/stdio/Makefile
@@ -12,6 +12,7 @@
 	fclose.$O\
 	feof.$O\
 	ferror.$O\
+	fflush.$O\
 	fgetc.$O\
 	fgets.$O\
 	fopen.$O\
--- a/src/libc/stdio/__getc.c
+++ b/src/libc/stdio/__getc.c
@@ -1,6 +1,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+
 #include "../libc.h"
 #include "../syscall.h"
 
--- a/src/libc/stdio/__putc.c
+++ b/src/libc/stdio/__putc.c
@@ -4,22 +4,6 @@
 
 #include "../libc.h"
 
-int
-fflush(FILE *fp)
-{
-	int err;
-
-	if (fp)
-		return _flsbuf(fp);
-
-	err = 0;
-	for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) {
-		if ((fp->flags & _IOWRITE) == 0 && _flsbuf(fp))
-			err = EOF;
-	}
-	return err;
-}
-
 static void
 cleanup(void)
 {
@@ -46,7 +30,7 @@
 	}
 
 	if (fp->buf == NULL && _allocbuf(fp))
-			return EOF;
+		return EOF;
 
 	if (first) {
 		if (atexit(cleanup)) {
--- a/src/libc/stdio/_allocbuf.c
+++ b/src/libc/stdio/_allocbuf.c
@@ -1,6 +1,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+
 #include "../libc.h"
 
 int
--- a/src/libc/stdio/_fpopen.c
+++ b/src/libc/stdio/_fpopen.c
@@ -1,14 +1,15 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+
 #include <sys.h>
+
 #include "../syscall.h"
 #include "../libc.h"
-#undef fopen
 
 FILE *
-_fpopen(const char * restrict fname,
-        const char * restrict mode,
+_fpopen(const char *restrict fname,
+        const char *restrict mode,
         FILE * restrict fp)
 {
 	int i, flags, fd, rw, bin;
--- a/src/libc/stdio/clearerr.c
+++ b/src/libc/stdio/clearerr.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef clearerr
 
 void
--- a/src/libc/stdio/fclose.c
+++ b/src/libc/stdio/fclose.c
@@ -1,8 +1,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#include "../syscall.h"
 #include "../libc.h"
+#include "../syscall.h"
+
 #undef fclose
 
 int
--- a/src/libc/stdio/feof.c
+++ b/src/libc/stdio/feof.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef feof
 
 int
--- a/src/libc/stdio/ferror.c
+++ b/src/libc/stdio/ferror.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef ferror
 
 int
--- /dev/null
+++ b/src/libc/stdio/fflush.c
@@ -1,0 +1,21 @@
+#include <stdio.h>
+
+#include "../libc.h"
+
+#undef fflush
+
+int
+fflush(FILE *fp)
+{
+	int err;
+
+	if (fp)
+		return _flsbuf(fp);
+
+	err = 0;
+	for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) {
+		if ((fp->flags & _IOWRITE) == 0 && _flsbuf(fp))
+			err = EOF;
+	}
+	return err;
+}
--- a/src/libc/stdio/fgetc.c
+++ b/src/libc/stdio/fgetc.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef fgetc
 
 int
--- a/src/libc/stdio/fgets.c
+++ b/src/libc/stdio/fgets.c
@@ -1,14 +1,17 @@
 #include <stdio.h>
+
 #undef fgets
 
 char *
-fgets(char * restrict s, int n, FILE * restrict fp)
+fgets(char *restrict s, int n, FILE *restrict fp)
 {
 	int ch = 0;
 	char *t = s;
 
-	while (--n > 0 && (ch = getc(fp)) != EOF) {
-		if ((*t++ = ch) == '\n')
+	for (--n; n > 0; --n) {
+		if ((ch = getc(fp)) == EOF)
+			break;
+		if  ((*t++ = ch) == '\n')
 			break;
 	}
 	if (ch == EOF && s == t)
--- a/src/libc/stdio/fopen.c
+++ b/src/libc/stdio/fopen.c
@@ -1,11 +1,10 @@
 #include <errno.h>
 #include <stdio.h>
 
-#include "../syscall.h"
 #include "../libc.h"
+
 #undef fopen
 
-
 FILE *
 fopen(const char * restrict name, const char * restrict mode)
 {
@@ -19,5 +18,6 @@
 		errno = ENOMEM;
 		return NULL;
 	}
+
 	return _fpopen(name, mode, fp);
 }
--- a/src/libc/stdio/fprintf.c
+++ b/src/libc/stdio/fprintf.c
@@ -1,5 +1,6 @@
 #include <stdarg.h>
 #include <stdio.h>
+
 #undef fprintf
 
 int
@@ -11,5 +12,6 @@
 	va_start(va, fmt);
 	cnt = vfprintf(fp, fmt, va);
 	va_end(va);
+
 	return cnt;
 }
--- a/src/libc/stdio/fputc.c
+++ b/src/libc/stdio/fputc.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef fputc
 
 int
--- a/src/libc/stdio/fputs.c
+++ b/src/libc/stdio/fputs.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef fputs
 
 int
@@ -6,7 +7,8 @@
 {
 	int r, ch;
 
-	while (ch = *bp++)
+	while ((ch = *bp++) != '\0')
 		r = putc(ch, fp);
+
 	return r;
 }
--- a/src/libc/stdio/fread.c
+++ b/src/libc/stdio/fread.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef fread
 
 size_t
--- a/src/libc/stdio/freopen.c
+++ b/src/libc/stdio/freopen.c
@@ -2,6 +2,7 @@
 
 #include "../syscall.h"
 #include "../libc.h"
+
 #undef freopen
 
 FILE *
--- a/src/libc/stdio/ftell.c
+++ b/src/libc/stdio/ftell.c
@@ -1,5 +1,7 @@
 #include <stdio.h>
+
 #include "../syscall.h"
+
 #undef ftell
 
 long
--- a/src/libc/stdio/fwrite.c
+++ b/src/libc/stdio/fwrite.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef fwrite
 
 size_t
--- a/src/libc/stdio/getc.c
+++ b/src/libc/stdio/getc.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef getc
 
 int
--- a/src/libc/stdio/getchar.c
+++ b/src/libc/stdio/getchar.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef getchar
 
 int
--- a/src/libc/stdio/gets.c
+++ b/src/libc/stdio/gets.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef gets
 
 char *
--- a/src/libc/stdio/perror.c
+++ b/src/libc/stdio/perror.c
@@ -1,6 +1,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
+
 #undef perror
 
 void
--- a/src/libc/stdio/printf.c
+++ b/src/libc/stdio/printf.c
@@ -1,9 +1,10 @@
 #include <stdarg.h>
 #include <stdio.h>
+
 #undef printf
 
 int
-printf(const char * restrict fmt, ...)
+printf(const char *restrict fmt, ...)
 {
 	int cnt;
 	va_list va;
--- a/src/libc/stdio/putc.c
+++ b/src/libc/stdio/putc.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef putc
 
 int
--- a/src/libc/stdio/putchar.c
+++ b/src/libc/stdio/putchar.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef putchar
 
 int
--- a/src/libc/stdio/puts.c
+++ b/src/libc/stdio/puts.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef puts
 
 int
--- a/src/libc/stdio/rewind.c
+++ b/src/libc/stdio/rewind.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 #undef rewind
 
 void
--- a/src/libc/stdio/setbuf.c
+++ b/src/libc/stdio/setbuf.c
@@ -1,8 +1,9 @@
 #include <stdio.h>
+
 #undef setbuf
 
 void
-setbuf(FILE * restrict fp, char * restrict buf)
+setbuf(FILE *restrict fp, char *restrict buf)
 {
 	setvbuf(fp, buf, (buf) ? _IOFBF : _IONBF, BUFSIZ);
 }
--- a/src/libc/stdio/setvbuf.c
+++ b/src/libc/stdio/setvbuf.c
@@ -3,10 +3,11 @@
 #include <stdlib.h>
 
 #include "../libc.h"
+
 #undef setvbuf
 
 int
-setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size)
+setvbuf(FILE *restrict fp, char *restrict buf, int mode, size_t size)
 {
 	int flags;
 	char *p;
--- a/src/libc/stdio/snprintf.c
+++ b/src/libc/stdio/snprintf.c
@@ -1,9 +1,10 @@
 #include <stdarg.h>
 #include <stdio.h>
+
 #undef snprintf
 
 int
-snprintf(char * restrict s, size_t siz, const char * restrict fmt, ...)
+snprintf(char *restrict s, size_t siz, const char *restrict fmt, ...)
 {
 	int r;
 	va_list va;
--- a/src/libc/stdio/sprintf.c
+++ b/src/libc/stdio/sprintf.c
@@ -1,9 +1,10 @@
 #include <stdarg.h>
 #include <stdio.h>
+
 #undef sprintf
 
 int
-sprintf(char * restrict s, const char * restrict fmt, ...)
+sprintf(char *restrict s, const char *restrict fmt, ...)
 {
 	int r;
 
--- a/src/libc/stdio/tmpnam.c
+++ b/src/libc/stdio/tmpnam.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <string.h>
+
 #include "../syscall.h"
+
 #undef tmpnam
 
 char *
--- a/src/libc/stdio/vfprintf.c
+++ b/src/libc/stdio/vfprintf.c
@@ -4,7 +4,9 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
-#include <wchar.h>
+
+#define MAXPREC    50
+
 #undef vfprintf
 
 enum {
@@ -20,8 +22,6 @@
 	ALTFORM  = 1 << 9,
 };
 
-#define MAXPREC    50
-
 struct conv {
 	int sign;
 	int prec;
@@ -78,6 +78,7 @@
 
 	for (*buf = '\0'; val > 0; val /= base)
 		*--buf = conv->digs[val % base];
+
 	while (buf0 - buf < prec)
 		*--buf = '0';
 
@@ -146,7 +147,7 @@
 }
 
 static size_t
-strout(char *s, size_t len, int width, int fill, FILE * restrict fp)
+strout(char *s, size_t len, int width, int fill, FILE *restrict fp)
 {
 	int left = 0, adjust, ch, prefix;
 	size_t cnt = 0;
@@ -156,7 +157,7 @@
 		width = -width;
 	}
 
-	adjust = (len < width) ? width - len : 0;
+	adjust = (len > 0 && len < width) ? width - len : 0;
 	cnt = adjust + len;
 	if (left)
 		adjust = -adjust;
@@ -177,8 +178,13 @@
 	for ( ; adjust > 0; adjust--)
 		putc(fill, fp);
 
-	while (ch = *s++)
-		putc(ch, fp);
+	if (len == -1) {
+		for (cnt = 0; ch = *s++; ++cnt)
+			putc(ch, fp);
+	} else {
+		while (len-- > 0 && (ch = *s++))
+			putc(ch, fp);
+	}
 
 	for ( ; adjust < 0; adjust++)
 		putc(' ', fp);
@@ -186,16 +192,6 @@
 	return cnt;
 }
 
-static size_t
-strnlen(const char *s, size_t maxlen)
-{
-	size_t n;
-
-	for (n = 0; n < maxlen && *s++; ++n)
-		;
-	return n;
-}
-
 int
 vfprintf(FILE * restrict fp, const char * restrict fmt, va_list va)
 {
@@ -243,10 +239,11 @@
 				for (n = 0; isdigit(ch = *fmt); fmt++)
 					n = n * 10 + ch - '0';
 			}
+			if (n < 0)
+				n = 0;
 			if (n > MAXPREC)
 				n = MAXPREC;
-			if (n > 0)
-				conv.prec = n;
+			conv.prec = n;
 			goto flags;
 		case '*':
 			width = va_arg(va2, int);
--- a/src/libc/stdio/vprintf.c
+++ b/src/libc/stdio/vprintf.c
@@ -1,9 +1,10 @@
 #include <stdarg.h>
 #include <stdio.h>
+
 #undef vprintf
 
 int
-vprintf(const char * restrict fmt, va_list ap)
+vprintf(const char *restrict fmt, va_list ap)
 {
 	va_list ap2;
 
--- a/src/libc/stdio/vsnprintf.c
+++ b/src/libc/stdio/vsnprintf.c
@@ -1,9 +1,10 @@
 #include <stdarg.h>
 #include <stdio.h>
+
 #undef vsnprintf
 
 int
-vsnprintf(char * restrict s, size_t siz, const char * restrict fmt, va_list ap)
+vsnprintf(char *restrict s, size_t siz, const char *restrict fmt, va_list ap)
 {
 	FILE f;
 	int r;
--- a/src/libc/stdio/vsprintf.c
+++ b/src/libc/stdio/vsprintf.c
@@ -2,11 +2,11 @@
 #include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
+
 #undef vsprintf
 
-
 int
-vsprintf(char * restrict s, const char * restrict fmt, va_list va)
+vsprintf(char *restrict s, const char *restrict fmt, va_list va)
 {
 	return vsnprintf(s, SIZE_MAX, fmt, va);
 }