scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 4fdfc69873149fd2ee9faf7fb85ea73e41254cb1
parent fed392c7899bb4e94df0e1790dc4bd01e253f578
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 27 Aug 2021 11:44:57 +0200

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.

Diffstat:
Msrc/libc/objs/common-objs.mk | 1+
Msrc/libc/stdio/Makefile | 1+
Msrc/libc/stdio/__getc.c | 1+
Msrc/libc/stdio/__putc.c | 18+-----------------
Msrc/libc/stdio/_allocbuf.c | 1+
Msrc/libc/stdio/_fpopen.c | 7++++---
Msrc/libc/stdio/clearerr.c | 1+
Msrc/libc/stdio/fclose.c | 3++-
Msrc/libc/stdio/feof.c | 1+
Msrc/libc/stdio/ferror.c | 1+
Asrc/libc/stdio/fflush.c | 21+++++++++++++++++++++
Msrc/libc/stdio/fgetc.c | 1+
Msrc/libc/stdio/fgets.c | 9++++++---
Msrc/libc/stdio/fopen.c | 4++--
Msrc/libc/stdio/fprintf.c | 2++
Msrc/libc/stdio/fputc.c | 1+
Msrc/libc/stdio/fputs.c | 4+++-
Msrc/libc/stdio/fread.c | 1+
Msrc/libc/stdio/freopen.c | 1+
Msrc/libc/stdio/ftell.c | 2++
Msrc/libc/stdio/fwrite.c | 1+
Msrc/libc/stdio/getc.c | 1+
Msrc/libc/stdio/getchar.c | 1+
Msrc/libc/stdio/gets.c | 1+
Msrc/libc/stdio/perror.c | 1+
Msrc/libc/stdio/printf.c | 3++-
Msrc/libc/stdio/putc.c | 1+
Msrc/libc/stdio/putchar.c | 1+
Msrc/libc/stdio/puts.c | 1+
Msrc/libc/stdio/rewind.c | 1+
Msrc/libc/stdio/setbuf.c | 3++-
Msrc/libc/stdio/setvbuf.c | 3++-
Msrc/libc/stdio/snprintf.c | 3++-
Msrc/libc/stdio/sprintf.c | 3++-
Msrc/libc/stdio/tmpnam.c | 2++
Msrc/libc/stdio/vfprintf.c | 35++++++++++++++++-------------------
Msrc/libc/stdio/vprintf.c | 3++-
Msrc/libc/stdio/vsnprintf.c | 3++-
Msrc/libc/stdio/vsprintf.c | 4++--
39 files changed, 97 insertions(+), 55 deletions(-)

diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk @@ -30,6 +30,7 @@ COMMON_OBJS =\ stdio/fclose.$O\ stdio/feof.$O\ stdio/ferror.$O\ + stdio/fflush.$O\ stdio/fgetc.$O\ stdio/fgets.$O\ stdio/fopen.$O\ diff --git a/src/libc/stdio/Makefile b/src/libc/stdio/Makefile @@ -12,6 +12,7 @@ OBJS =\ fclose.$O\ feof.$O\ ferror.$O\ + fflush.$O\ fgetc.$O\ fgets.$O\ fopen.$O\ diff --git 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" diff --git 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 @@ __putc(int ch, FILE *fp) } if (fp->buf == NULL && _allocbuf(fp)) - return EOF; + return EOF; if (first) { if (atexit(cleanup)) { diff --git 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 diff --git 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; diff --git a/src/libc/stdio/clearerr.c b/src/libc/stdio/clearerr.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef clearerr void diff --git 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 diff --git a/src/libc/stdio/feof.c b/src/libc/stdio/feof.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef feof int diff --git a/src/libc/stdio/ferror.c b/src/libc/stdio/ferror.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef ferror int diff --git a/src/libc/stdio/fflush.c b/src/libc/stdio/fflush.c @@ -0,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; +} diff --git a/src/libc/stdio/fgetc.c b/src/libc/stdio/fgetc.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef fgetc int diff --git 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) diff --git a/src/libc/stdio/fopen.c b/src/libc/stdio/fopen.c @@ -1,10 +1,9 @@ #include <errno.h> #include <stdio.h> -#include "../syscall.h" #include "../libc.h" -#undef fopen +#undef fopen FILE * fopen(const char * restrict name, const char * restrict mode) @@ -19,5 +18,6 @@ fopen(const char * restrict name, const char * restrict mode) errno = ENOMEM; return NULL; } + return _fpopen(name, mode, fp); } diff --git 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 @@ fprintf(FILE * restrict fp, const char * restrict fmt, ...) va_start(va, fmt); cnt = vfprintf(fp, fmt, va); va_end(va); + return cnt; } diff --git a/src/libc/stdio/fputc.c b/src/libc/stdio/fputc.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef fputc int diff --git 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 @@ fputs(const char * restrict bp, FILE * restrict fp) { int r, ch; - while (ch = *bp++) + while ((ch = *bp++) != '\0') r = putc(ch, fp); + return r; } diff --git a/src/libc/stdio/fread.c b/src/libc/stdio/fread.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef fread size_t diff --git 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 * diff --git 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 diff --git a/src/libc/stdio/fwrite.c b/src/libc/stdio/fwrite.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef fwrite size_t diff --git a/src/libc/stdio/getc.c b/src/libc/stdio/getc.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef getc int diff --git a/src/libc/stdio/getchar.c b/src/libc/stdio/getchar.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef getchar int diff --git a/src/libc/stdio/gets.c b/src/libc/stdio/gets.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef gets char * diff --git 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 diff --git 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; diff --git a/src/libc/stdio/putc.c b/src/libc/stdio/putc.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef putc int diff --git a/src/libc/stdio/putchar.c b/src/libc/stdio/putchar.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef putchar int diff --git a/src/libc/stdio/puts.c b/src/libc/stdio/puts.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef puts int diff --git a/src/libc/stdio/rewind.c b/src/libc/stdio/rewind.c @@ -1,4 +1,5 @@ #include <stdio.h> + #undef rewind void diff --git 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); } diff --git 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; diff --git 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; diff --git 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; diff --git 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 * diff --git 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 @@ enum { ALTFORM = 1 << 9, }; -#define MAXPREC 50 - struct conv { int sign; int prec; @@ -78,6 +78,7 @@ numtostr(uintmax_t val, int flags, struct conv *conv, char *buf) for (*buf = '\0'; val > 0; val /= base) *--buf = conv->digs[val % base]; + while (buf0 - buf < prec) *--buf = '0'; @@ -146,7 +147,7 @@ wstrout(wchar_t *ws, size_t len, int width, int fill, FILE * restrict fp) } 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 @@ strout(char *s, size_t len, int width, int fill, FILE * restrict fp) 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 @@ strout(char *s, size_t len, int width, int fill, FILE * restrict fp) 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 @@ strout(char *s, size_t len, int width, int fill, FILE * restrict fp) 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 @@ flags: 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); diff --git 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; diff --git 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; diff --git 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 +#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); }