Split stdin, stdout and stderr into their own files.

This commit is contained in:
Jonas 'Sortie' Termansen 2015-11-20 21:52:32 +01:00
parent 2e4b15daed
commit 4d2901b90e
8 changed files with 207 additions and 132 deletions

View File

@ -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 \

View File

@ -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

View File

@ -25,12 +25,17 @@
#include <pthread.h>
#include <stdio.h>
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);

55
libc/stdio/stderr.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
stdio/stderr.cpp
Standard error.
*******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#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; }

56
libc/stdio/stdin.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
stdio/stdin.cpp
Standard input.
*******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#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; }

View File

@ -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 <http://www.gnu.org/licenses/>.
stdio/stdio.cpp
Sets up stdin, stdout, stderr.
*******************************************************************************/
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#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" */

56
libc/stdio/stdout.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
stdio/stdout.cpp
Standard output.
*******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#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; }

View File

@ -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);
}