From 71866ed7695fd24d50501ad27d0add5e0d73c4b3 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Fri, 14 Jan 2022 18:38:48 +0100 Subject: [PATCH] stdio: stdin/stdout/stderr, perror, final adjustments Support for will stop here for now. --- CMakeLists.txt | 2 ++ STATUS | 28 ++++++++++++++-------------- src/libc/stdio/perror.c | 15 +++++++++++++++ src/libc/stdio/printf/print.c | 2 +- src/libc/stdio/streams.c | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 src/libc/stdio/perror.c create mode 100644 src/libc/stdio/streams.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 494b786..5d5dbcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,7 @@ set(SOURCES src/libc/stdio/getc.c src/libc/stdio/getchar.c src/libc/stdio/gets.c + src/libc/stdio/perror.c src/libc/stdio/printf.c src/libc/stdio/printf/format_fixed.c src/libc/stdio/printf/format_fp.c @@ -158,6 +159,7 @@ set(SOURCES src/libc/stdio/setvbuf.c src/libc/stdio/snprintf.c src/libc/stdio/sprintf.c + src/libc/stdio/streams.c src/libc/stdio/ungetc.c src/libc/stdio/vasprintf.c src/libc/stdio/vdprintf.c diff --git a/STATUS b/STATUS index e3603a0..5178afa 100644 --- a/STATUS +++ b/STATUS @@ -84,8 +84,8 @@ TEST: Function/symbol/macro needs to be tested 7.18 => GCC 7.19 - 7.19.1 Introduction TEST (no wide-oriented streams *) - 7.19.1 stdin, stdout, stderr TODO + 7.19.1 Introduction - (no wide-oriented streams *) + 7.19.1 stdin, stdout, stderr - 7.19.4.1 remove TEST 7.19.4.2 rename TODO @@ -93,42 +93,42 @@ TEST: Function/symbol/macro needs to be tested 7.19.4.4 tmpnam TODO 7.19.5.1 fclose - - 7.19.5.2 fflush - + 7.19.5.2 fflush - (fflush(NULL) not supported yet) 7.19.5.3 fopen - (EXT) fdopen - 7.19.5.4 freopen - 7.19.5.5 setbuf - 7.19.5.6 setvbuf - - 7.19.6.1 fprintf TEST + 7.19.6.1 fprintf - 7.19.6.2 fscanf TODO - 7.19.6.3 printf LDEPS(stdout) + 7.19.6.3 printf - 7.19.6.4 scanf TODO 7.19.6.5 snprintf - 7.19.6.6 sprintf - 7.19.6.7 sscanf TODO - 7.19.6.8 vfprintf TEST + 7.19.6.8 vfprintf - 7.19.6.9 vfscanf TODO - 7.19.6.10 vprintf LDEPS(stdout) + 7.19.6.10 vprintf - 7.19.6.11 vscanf TODO 7.19.6.12 vsnprintf - 7.19.6.13 vsprintf - 7.19.6.14 vsscanf TODO (EXT) asprintf - (EXT) vasprintf - - (EXT) dprintf TEST - (EXT) vdprintf TEST + (EXT) dprintf - + (EXT) vdprintf - 7.19.7.1 fgetc - 7.19.7.2 fgets - 7.19.7.3 fputc - 7.19.7.4 fputs - 7.19.7.5 getc - - 7.19.7.6 getchar LDEPS(stdin) - 7.19.7.7 gets LDEPS(stdin) + 7.19.7.6 getchar - + 7.19.7.7 gets - 7.19.7.8 putc - - 7.19.7.9 putchar LDEPS(stdout) - 7.19.7.10 puts LDEPS(stdout) + 7.19.7.9 putchar - + 7.19.7.10 puts - 7.19.7.11 ungetc - 7.19.8.1 fread - @@ -143,7 +143,7 @@ TEST: Function/symbol/macro needs to be tested 7.19.10.1 clearerr - 7.19.10.2 feof - 7.19.10.3 ferror - - 7.19.10.4 perror TODO + 7.19.10.4 perror - 7.20 7.20 MB_CUR_MAX TODO diff --git a/src/libc/stdio/perror.c b/src/libc/stdio/perror.c new file mode 100644 index 0000000..2c953ad --- /dev/null +++ b/src/libc/stdio/perror.c @@ -0,0 +1,15 @@ +#include +#include +#include + +void perror(char const *s) +{ + int errno_copy = errno; + + if(s != NULL) { + fputs(s, stderr); + fputs(": ", stderr); + } + fputs(strerror(errno_copy), stderr); + fputc('\n', stderr); +} diff --git a/src/libc/stdio/printf/print.c b/src/libc/stdio/printf/print.c index 4744ca5..7b0efc3 100644 --- a/src/libc/stdio/printf/print.c +++ b/src/libc/stdio/printf/print.c @@ -107,7 +107,7 @@ void __printf_flush(struct __printf_output *out) /* File pointer: output with fwrite */ else if(out->fp) { - // fwrite(out->buffer, out->ptr - out->buffer, 1, out->fp); + fwrite(out->buffer, 1, out->ptr - out->buffer, out->fp); } /* File pointer: output with write */ else if(out->fd) diff --git a/src/libc/stdio/streams.c b/src/libc/stdio/streams.c new file mode 100644 index 0000000..1c3cc79 --- /dev/null +++ b/src/libc/stdio/streams.c @@ -0,0 +1,18 @@ +#include +#include + +FILE *stdin; +FILE *stdout; +FILE *stderr; + +__attribute__((constructor)) +static void initialize_streams(void) +{ + stdin = fdopen(STDIN_FILENO, "r"); + + stdout = fdopen(STDOUT_FILENO, "w"); + setvbuf(stdout, NULL, _IOLBF, BUFSIZ); + + stderr = fdopen(STDERR_FILENO, "w"); + setvbuf(stderr, NULL, _IONBF, 0); +}