From 29510491be6e3e0b95872920c371024ed444c1e5 Mon Sep 17 00:00:00 2001 From: solar Date: Sat, 19 Jun 2010 20:26:50 +0000 Subject: [PATCH] Streams are now closed on program exit. (I hope; untested.) --- Makefile | 2 +- functions/_PDCLIB/closeall.c | 35 +++++++++++++++++++++++++++++++++++ functions/stdlib/exit.c | 2 +- internals/_PDCLIB_int.h | 3 +++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 functions/_PDCLIB/closeall.c diff --git a/Makefile b/Makefile index 3331fa5..9894792 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ SRCFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c") # All header files of the project HDRFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.h") # All .c files in functions/_PDCLIB that do not have a regression test driver -INTFILES := _Exit atomax digits open print scan remove rename seed stdinit strtox_main strtox_prelim filemode eol errno seek prepread prepwrite allocpages tmpfilename +INTFILES := _Exit atomax digits open print scan remove rename seed stdinit strtox_main strtox_prelim filemode eol errno seek prepread prepwrite allocpages tmpfilename closeall # All object files in the library OBJFILES := $(patsubst %.c,%.o,$(SRCFILES)) # All test drivers (.t) diff --git a/functions/_PDCLIB/closeall.c b/functions/_PDCLIB/closeall.c new file mode 100644 index 0000000..3523f8c --- /dev/null +++ b/functions/_PDCLIB/closeall.c @@ -0,0 +1,35 @@ +/* $Id$ */ + +/* _PDCLIB_closeall( void ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +extern struct _PDCLIB_file_t * _PDCLIB_filelist; + +void _PDCLIB_closeall( void ) +{ + struct _PDCLIB_file_t * stream = _PDCLIB_filelist; + struct _PDCLIB_file_t * next; + while ( stream != NULL ) + { + next = stream->next; + fclose( stream ); + stream = next; + } +} + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + TESTCASE( NO_TESTDRIVER ); + return TEST_RESULTS; +} + +#endif + diff --git a/functions/stdlib/exit.c b/functions/stdlib/exit.c index 8d5aa17..2dbb35c 100644 --- a/functions/stdlib/exit.c +++ b/functions/stdlib/exit.c @@ -19,7 +19,7 @@ */ #define NUMBER_OF_SLOTS 40 -void (*_PDCLIB_regstack[ NUMBER_OF_SLOTS ])( void ); +void (*_PDCLIB_regstack[ NUMBER_OF_SLOTS ])( void ) = { _PDCLIB_closeall }; size_t _PDCLIB_regptr = NUMBER_OF_SLOTS; void exit( int status ) diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index 9c67f27..f6fedd1 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -382,6 +382,9 @@ int _PDCLIB_prepread( struct _PDCLIB_file_t * stream ); */ int _PDCLIB_prepwrite( struct _PDCLIB_file_t * stream ); +/* Closing all streams on program exit */ +void _PDCLIB_closeall( void ); + /* -------------------------------------------------------------------------- */ /* errno */ /* -------------------------------------------------------------------------- */ -- 2.40.0