Initialize stdin, stdout and stderr at compile time.
This commit is contained in:
parent
8570f46734
commit
a25f1a931e
|
@ -67,6 +67,8 @@ typedef struct FILE FILE;
|
||||||
|
|
||||||
#define _FILE_MAX_PUSHBACK 8
|
#define _FILE_MAX_PUSHBACK 8
|
||||||
|
|
||||||
|
/* Note stdio/stdio.cpp's declarations of stdin/stdout/stderr also needs to be
|
||||||
|
changed if you make changes to this structure. */
|
||||||
struct FILE
|
struct FILE
|
||||||
{
|
{
|
||||||
/* This is non-standard, but useful. If you allocate your own FILE and
|
/* This is non-standard, but useful. If you allocate your own FILE and
|
||||||
|
|
|
@ -30,8 +30,6 @@
|
||||||
extern "C" { char* program_invocation_name; }
|
extern "C" { char* program_invocation_name; }
|
||||||
extern "C" { char* program_invocation_short_name; }
|
extern "C" { char* program_invocation_short_name; }
|
||||||
|
|
||||||
extern "C" void init_stdio();
|
|
||||||
|
|
||||||
static char* find_last_elem(char* str)
|
static char* find_last_elem(char* str)
|
||||||
{
|
{
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
|
@ -49,7 +47,4 @@ extern "C" void initialize_standard_library(int argc, char* argv[])
|
||||||
|
|
||||||
// Initialize pthreads.
|
// Initialize pthreads.
|
||||||
pthread_initialize();
|
pthread_initialize();
|
||||||
|
|
||||||
// Initialize stdio.
|
|
||||||
init_stdio();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -32,35 +33,89 @@ static struct fdio_state stdin_fdio = { NULL, 0 };
|
||||||
static struct fdio_state stdout_fdio = { NULL, 1 };
|
static struct fdio_state stdout_fdio = { NULL, 1 };
|
||||||
static struct fdio_state stderr_fdio = { NULL, 2 };
|
static struct fdio_state stderr_fdio = { NULL, 2 };
|
||||||
|
|
||||||
static FILE stdin_file;
|
extern "C" {
|
||||||
static FILE stdout_file;
|
|
||||||
static FILE stderr_file;
|
|
||||||
|
|
||||||
extern "C" { FILE* stdin = &stdin_file; }
|
extern FILE __stdin_file;
|
||||||
extern "C" { FILE* stdout = &stdout_file; }
|
extern FILE __stdout_file;
|
||||||
extern "C" { FILE* stderr = &stderr_file; }
|
extern FILE __stderr_file;
|
||||||
|
|
||||||
static void bootstrap_stdio(FILE* fp, struct fdio_state* fdio, int file_flags)
|
FILE __stdin_file =
|
||||||
{
|
{
|
||||||
fresetfile(fp);
|
/* buffersize = */ 0,
|
||||||
|
/* buffer = */ NULL,
|
||||||
|
/* 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,
|
||||||
|
/* buffer_free_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,
|
||||||
|
};
|
||||||
|
|
||||||
fp->flags |= file_flags;
|
FILE __stdout_file
|
||||||
fp->user = fdio;
|
|
||||||
fp->reopen_func = fdio_reopen;
|
|
||||||
fp->read_func = fdio_read;
|
|
||||||
fp->write_func = fdio_write;
|
|
||||||
fp->seek_func = fdio_seek;
|
|
||||||
fp->fileno_func = fdio_fileno;
|
|
||||||
fp->close_func = fdio_close;
|
|
||||||
|
|
||||||
fregister(fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" void init_stdio()
|
|
||||||
{
|
{
|
||||||
bootstrap_stdio(stdin, &stdin_fdio, _FILE_READABLE);
|
/* buffersize = */ 0,
|
||||||
bootstrap_stdio(stdout, &stdout_fdio, _FILE_WRITABLE);
|
/* buffer = */ NULL,
|
||||||
bootstrap_stdio(stderr, &stderr_fdio, _FILE_WRITABLE);
|
/* 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,
|
||||||
|
/* buffer_free_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,
|
||||||
|
};
|
||||||
|
|
||||||
stderr->buffer_mode = _IONBF;
|
FILE __stderr_file
|
||||||
}
|
{
|
||||||
|
/* buffersize = */ 0,
|
||||||
|
/* 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,
|
||||||
|
/* buffer_free_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* stdin = &__stdin_file;
|
||||||
|
FILE* stdout = &__stdout_file;
|
||||||
|
FILE* stderr = &__stderr_file;
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
|
|
|
@ -35,9 +35,11 @@ extern "C" { struct exit_handler* __exit_handler_stack = NULL; }
|
||||||
static pthread_mutex_t exit_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
static pthread_mutex_t exit_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
||||||
static bool currently_exiting = false;
|
static bool currently_exiting = false;
|
||||||
|
|
||||||
|
extern "C" FILE __stdin_file;
|
||||||
|
|
||||||
extern "C" { DIR* __first_dir = NULL; }
|
extern "C" { DIR* __first_dir = NULL; }
|
||||||
extern "C" { pthread_mutex_t __first_dir_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; }
|
extern "C" { pthread_mutex_t __first_dir_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; }
|
||||||
extern "C" { FILE* __first_file = NULL; }
|
extern "C" { FILE* __first_file = &__stdin_file; }
|
||||||
extern "C" { pthread_mutex_t __first_file_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; }
|
extern "C" { pthread_mutex_t __first_file_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; }
|
||||||
|
|
||||||
extern "C" void exit(int status)
|
extern "C" void exit(int status)
|
||||||
|
|
Loading…
Reference in New Issue