From 4d2901b90e44acad24e29b4645f7b4f919861f8e Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 20 Nov 2015 21:52:32 +0100 Subject: [PATCH] Split stdin, stdout and stderr into their own files. --- libc/Makefile | 4 +- libc/include/sys/cdefs.h | 12 +++- libc/stdio/fflush.cpp | 5 ++ libc/stdio/stderr.cpp | 55 ++++++++++++++++++ libc/stdio/stdin.cpp | 56 +++++++++++++++++++ libc/stdio/stdio.cpp | 118 --------------------------------------- libc/stdio/stdout.cpp | 56 +++++++++++++++++++ libc/stdlib/exit.cpp | 33 +++++++---- 8 files changed, 207 insertions(+), 132 deletions(-) create mode 100644 libc/stdio/stderr.cpp create mode 100644 libc/stdio/stdin.cpp delete mode 100644 libc/stdio/stdio.cpp create mode 100644 libc/stdio/stdout.cpp diff --git a/libc/Makefile b/libc/Makefile index 252f472d..162c6847 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -453,7 +453,9 @@ stdio/remove.o \ stdio/renameat.o \ stdio/rename.o \ stdio/scanf.o \ -stdio/stdio.o \ +stdio/stderr.o \ +stdio/stdin.o \ +stdio/stdout.o \ stdio/tmpfile.o \ stdio/vfprintf.o \ stdio/vprintf.o \ diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h index 903219cd..9616516f 100644 --- a/libc/include/sys/cdefs.h +++ b/libc/include/sys/cdefs.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -52,6 +52,16 @@ #define __HAS_RESTRICT 2 #endif +/* Macro to declare a weak alias. */ +#if defined(__is_sortix_libc) +#ifdef __cplusplus +#define weak_alias_cxx(old, new, mangled) \ + extern "C" { extern __typeof(old) new __attribute__((weak, alias(mangled))); } +#endif +#define weak_alias(old, new) \ + extern __typeof(old) new __attribute__((weak, alias(#old))) +#endif + #define __pure2 __attribute__((__const__)) #endif diff --git a/libc/stdio/fflush.cpp b/libc/stdio/fflush.cpp index 3f4cda30..d4168bc0 100644 --- a/libc/stdio/fflush.cpp +++ b/libc/stdio/fflush.cpp @@ -25,12 +25,17 @@ #include #include +static FILE* volatile dummy_file = NULL; +weak_alias_cxx(dummy_file, __stdout_used, "_ZL10dummy_file"); + extern "C" int fflush(FILE* fp) { if ( !fp ) { int result = 0; pthread_mutex_lock(&__first_file_lock); + if ( __stdout_used ) + fflush(__stdout_used); for ( fp = __first_file; fp; fp = fp->next ) { flockfile(fp); diff --git a/libc/stdio/stderr.cpp b/libc/stdio/stderr.cpp new file mode 100644 index 00000000..ba951d6f --- /dev/null +++ b/libc/stdio/stderr.cpp @@ -0,0 +1,55 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2014, 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/stderr.cpp + Standard error. + +*******************************************************************************/ + +#include +#include + +#include "fdio.h" + +static struct fdio_state stderr_fdio = { NULL, 2 }; +static FILE stderr_file +{ + /* buffer = */ NULL, + /* user = */ &stderr_fdio, + /* free_user = */ NULL, + /* reopen_func = */ fdio_reopen, + /* read_func = */ fdio_read, + /* write_func = */ fdio_write, + /* seek_func = */ fdio_seek, + /* fileno_func = */ fdio_fileno, + /* close_func = */ fdio_close, + /* free_func = */ NULL, + /* file_lock = */ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, + /* fflush_indirect = */ NULL, + /* prev = */ NULL, + /* next = */ NULL, + /* flags = */ _FILE_REGISTERED | _FILE_WRITABLE, + /* buffer_mode = */ _IONBF, + /* offset_input_buffer = */ 0, + /* amount_input_buffered = */ 0, + /* amount_output_buffered = */ 0, +}; + +extern "C" { FILE* const stderr = &stderr_file; } +extern "C" { FILE* volatile __stderr_used = &stderr_file; } diff --git a/libc/stdio/stdin.cpp b/libc/stdio/stdin.cpp new file mode 100644 index 00000000..4f3436bf --- /dev/null +++ b/libc/stdio/stdin.cpp @@ -0,0 +1,56 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2014, 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/stdin.cpp + Standard input. + +*******************************************************************************/ + +#include +#include + +#include "fdio.h" + +static unsigned char stdin_buffer[BUFSIZ]; +static struct fdio_state stdin_fdio = { NULL, 0 }; +static FILE stdin_file = +{ + /* buffer = */ stdin_buffer, + /* user = */ &stdin_fdio, + /* free_user = */ NULL, + /* reopen_func = */ fdio_reopen, + /* read_func = */ fdio_read, + /* write_func = */ fdio_write, + /* seek_func = */ fdio_seek, + /* fileno_func = */ fdio_fileno, + /* close_func = */ fdio_close, + /* free_func = */ NULL, + /* file_lock = */ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, + /* fflush_indirect = */ NULL, + /* prev = */ NULL, + /* next = */ NULL, + /* flags = */ _FILE_REGISTERED | _FILE_READABLE, + /* buffer_mode = */ -1, + /* offset_input_buffer = */ 0, + /* amount_input_buffered = */ 0, + /* amount_output_buffered = */ 0, +}; + +extern "C" { FILE* const stdin = &stdin_file; } +extern "C" { FILE* volatile __stdin_used = &stdin_file; } diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp deleted file mode 100644 index 01692c56..00000000 --- a/libc/stdio/stdio.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 2014, 2015. - - This file is part of the Sortix C Library. - - The Sortix C Library is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or (at your - option) any later version. - - The Sortix C Library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with the Sortix C Library. If not, see . - - stdio/stdio.cpp - Sets up stdin, stdout, stderr. - -*******************************************************************************/ - -#include -#include -#include -#include - -#include "fdio.h" - -static unsigned char stdin_buffer[BUFSIZ]; -static unsigned char stdout_buffer[BUFSIZ]; - -static struct fdio_state stdin_fdio = { NULL, 0 }; -static struct fdio_state stdout_fdio = { NULL, 1 }; -static struct fdio_state stderr_fdio = { NULL, 2 }; - -extern "C" { - -extern FILE __stdin_file; -extern FILE __stdout_file; -extern FILE __stderr_file; - -FILE __stdin_file = -{ - /* buffer = */ stdin_buffer, - /* user = */ &stdin_fdio, - /* free_user = */ NULL, - /* reopen_func = */ fdio_reopen, - /* read_func = */ fdio_read, - /* write_func = */ fdio_write, - /* seek_func = */ fdio_seek, - /* fileno_func = */ fdio_fileno, - /* close_func = */ fdio_close, - /* free_func = */ NULL, - /* file_lock = */ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, - /* fflush_indirect = */ NULL, - /* prev = */ NULL, - /* next = */ &__stdout_file, - /* flags = */ _FILE_REGISTERED | _FILE_READABLE, - /* buffer_mode = */ -1, - /* offset_input_buffer = */ 0, - /* amount_input_buffered = */ 0, - /* amount_output_buffered = */ 0, -}; - -FILE __stdout_file -{ - /* buffer = */ stdout_buffer, - /* user = */ &stdout_fdio, - /* free_user = */ NULL, - /* reopen_func = */ fdio_reopen, - /* read_func = */ fdio_read, - /* write_func = */ fdio_write, - /* seek_func = */ fdio_seek, - /* fileno_func = */ fdio_fileno, - /* close_func = */ fdio_close, - /* free_func = */ NULL, - /* file_lock = */ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, - /* fflush_indirect = */ NULL, - /* prev = */ &__stdin_file, - /* next = */ &__stderr_file, - /* flags = */ _FILE_REGISTERED | _FILE_WRITABLE, - /* buffer_mode = */ -1, - /* offset_input_buffer = */ 0, - /* amount_input_buffered = */ 0, - /* amount_output_buffered = */ 0, -}; - -FILE __stderr_file -{ - /* buffer = */ NULL, - /* user = */ &stderr_fdio, - /* free_user = */ NULL, - /* reopen_func = */ fdio_reopen, - /* read_func = */ fdio_read, - /* write_func = */ fdio_write, - /* seek_func = */ fdio_seek, - /* fileno_func = */ fdio_fileno, - /* close_func = */ fdio_close, - /* free_func = */ NULL, - /* file_lock = */ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, - /* fflush_indirect = */ NULL, - /* prev = */ &__stdout_file, - /* next = */ NULL, - /* flags = */ _FILE_REGISTERED | _FILE_WRITABLE, - /* buffer_mode = */ _IONBF, - /* offset_input_buffer = */ 0, - /* amount_input_buffered = */ 0, - /* amount_output_buffered = */ 0, -}; - -FILE* const stdin = &__stdin_file; -FILE* const stdout = &__stdout_file; -FILE* const stderr = &__stderr_file; - -} /* extern "C" */ diff --git a/libc/stdio/stdout.cpp b/libc/stdio/stdout.cpp new file mode 100644 index 00000000..63735576 --- /dev/null +++ b/libc/stdio/stdout.cpp @@ -0,0 +1,56 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2014, 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/stdout.cpp + Standard output. + +*******************************************************************************/ + +#include +#include + +#include "fdio.h" + +static unsigned char stdout_buffer[BUFSIZ]; +static struct fdio_state stdout_fdio = { NULL, 1 }; +static FILE stdout_file +{ + /* buffer = */ stdout_buffer, + /* user = */ &stdout_fdio, + /* free_user = */ NULL, + /* reopen_func = */ fdio_reopen, + /* read_func = */ fdio_read, + /* write_func = */ fdio_write, + /* seek_func = */ fdio_seek, + /* fileno_func = */ fdio_fileno, + /* close_func = */ fdio_close, + /* free_func = */ NULL, + /* file_lock = */ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, + /* fflush_indirect = */ NULL, + /* prev = */ NULL, + /* next = */ NULL, + /* flags = */ _FILE_REGISTERED | _FILE_WRITABLE, + /* buffer_mode = */ -1, + /* offset_input_buffer = */ 0, + /* amount_input_buffered = */ 0, + /* amount_output_buffered = */ 0, +}; + +extern "C" { FILE* const stdout = &stdout_file; } +extern "C" { FILE* volatile __stdout_used = &stdout_file; } diff --git a/libc/stdlib/exit.cpp b/libc/stdlib/exit.cpp index e014081a..5fa70644 100644 --- a/libc/stdlib/exit.cpp +++ b/libc/stdlib/exit.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014, 2015. This file is part of the Sortix C Library. @@ -35,13 +35,26 @@ extern "C" { struct exit_handler* __exit_handler_stack = NULL; } static pthread_mutex_t exit_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; static bool currently_exiting = false; -extern "C" FILE __stdin_file; +static FILE* volatile dummy_file = NULL; // volatile due to constant folding bug +weak_alias_cxx(dummy_file, __stdin_used, "_ZL10dummy_file"); +weak_alias_cxx(dummy_file, __stdout_used, "_ZL10dummy_file"); extern "C" { DIR* __first_dir = NULL; } extern "C" { pthread_mutex_t __first_dir_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; } -extern "C" { FILE* __first_file = &__stdin_file; } +extern "C" { FILE* __first_file = NULL; } extern "C" { pthread_mutex_t __first_file_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; } +static void exit_file(FILE* fp) +{ + if ( !fp ) + return; + flockfile(fp); + if ( fp->fflush_indirect ) + fp->fflush_indirect(fp); + if ( fp->close_func ) + fp->close_func(fp->user); +} + extern "C" void exit(int status) { // It's undefined behavior to call this function more than once: If more @@ -51,7 +64,7 @@ extern "C" void exit(int status) // It's undefined behavior to call this function more than once: If a // cleanup function calls this function we'll self-destruct immediately. if ( currently_exiting ) - _Exit(status); + _exit(status); currently_exiting = true; while ( __exit_handler_stack ) @@ -62,14 +75,10 @@ extern "C" void exit(int status) pthread_mutex_lock(&__first_file_lock); + exit_file(__stdin_used); + exit_file(__stdout_used); for ( FILE* fp = __first_file; fp; fp = fp->next ) - { - flockfile(fp); - if ( fp->fflush_indirect ) - fp->fflush_indirect(fp); - if ( fp->close_func ) - fp->close_func(fp->user); - } + exit_file(fp); - _Exit(status); + _exit(status); }