From b406087285dda97f2666a7f52f66b14582ed937b Mon Sep 17 00:00:00 2001 From: solar Date: Sat, 19 Jun 2010 18:40:21 +0000 Subject: [PATCH] tmpfile() implementation now based on /proc/sys/kernel/random/uuid. --- functions/stdio/fclose.c | 6 ++++++ platform/example/functions/stdio/tmpfile.c | 12 ++++++------ platform/example/internals/_PDCLIB_config.h | 4 ++-- platform/example_64/internals/_PDCLIB_config.h | 4 ++-- platform/example_cygwin/internals/_PDCLIB_config.h | 3 --- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/functions/stdio/fclose.c b/functions/stdio/fclose.c index dc27fe2..0e822eb 100644 --- a/functions/stdio/fclose.c +++ b/functions/stdio/fclose.c @@ -43,8 +43,14 @@ int fclose( struct _PDCLIB_file_t * stream ) { _PDCLIB_filelist = stream->next; } + /* Delete tmpfile() */ + if ( stream->status & _PDCLIB_DELONCLOSE ) + { + remove( stream->filename ); + } /* Free stream */ free( stream ); + return 0; } previous = current; diff --git a/platform/example/functions/stdio/tmpfile.c b/platform/example/functions/stdio/tmpfile.c index 4c55e5d..44b975d 100644 --- a/platform/example/functions/stdio/tmpfile.c +++ b/platform/example/functions/stdio/tmpfile.c @@ -24,7 +24,7 @@ extern struct _PDCLIB_file_t * _PDCLIB_filelist; /* This is an example implementation of tmpfile() fit for use with POSIX - POSIX kernels. + kernels. */ struct _PDCLIB_file_t * tmpfile( void ) { @@ -32,7 +32,7 @@ struct _PDCLIB_file_t * tmpfile( void ) /* This is the chosen way to get high-quality randomness. Replace as appropriate. */ - FILE * randomsource = fopen( "/dev/urandom", "rb" ); + FILE * randomsource = fopen( "/proc/sys/kernel/random/uuid", "rb" ); char filename[ L_tmpnam ]; _PDCLIB_fd_t fd; if ( randomsource == NULL ) @@ -48,9 +48,9 @@ struct _PDCLIB_file_t * tmpfile( void ) use high-quality randomness instead of a pseudo-random sequence to generate the filename candidate, which is *also* platform-dependent. */ - uint32_t random; - fscanf( randomsource, "%" SCNu32, &random ); - sprintf( filename, "/tmp/%010" PRNu32 ".tmp", random ); + unsigned int random; + fscanf( randomsource, "%u", &random ); + sprintf( filename, "/tmp/%u.tmp", random ); /* Check if file of this name exists. Note that fopen() is a very weak check, which does not take e.g. access permissions into account (file might exist but not readable). Replace with something more @@ -71,7 +71,7 @@ struct _PDCLIB_file_t * tmpfile( void ) close( fd ); return NULL; } - rc->status = _PDCLIB_filemode( "wb+" ) | _PDCLIB_LIBBUFFER | _IOLBF | _PDCLIB_DELONCLOSE; + rc->status = _PDCLIB_filemode( "wb+" ) | _IOLBF | _PDCLIB_DELONCLOSE; rc->handle = fd; rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t ); rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE; diff --git a/platform/example/internals/_PDCLIB_config.h b/platform/example/internals/_PDCLIB_config.h index 4251522..89028b5 100644 --- a/platform/example/internals/_PDCLIB_config.h +++ b/platform/example/internals/_PDCLIB_config.h @@ -258,8 +258,8 @@ typedef int _PDCLIB_fd_t; /* Length of the longest filename the implementation guarantees to support. */ #define _PDCLIB_FILENAME_MAX 128 -/* Buffer size for tmpnam(). */ -#define _PDCLIB_L_tmpnam 100 +/* Maximum length of filenames generated by tmpnam(). (See tmpfile.c.) */ +#define _PDCLIB_L_tmpnam 46 /* Number of distinct file names that can be generated by tmpnam(). */ #define _PDCLIB_TMP_MAX 50 diff --git a/platform/example_64/internals/_PDCLIB_config.h b/platform/example_64/internals/_PDCLIB_config.h index 65abc89..30d51b1 100644 --- a/platform/example_64/internals/_PDCLIB_config.h +++ b/platform/example_64/internals/_PDCLIB_config.h @@ -251,8 +251,8 @@ typedef int _PDCLIB_fd_t; /* Length of the longest filename the implementation guarantees to support. */ #define _PDCLIB_FILENAME_MAX 128 -/* Buffer size for tmpnam(). */ -#define _PDCLIB_L_tmpnam 100 +/* Maximum length of filenames generated by tmpnam(). (See tmpfile.c.) */ +#define _PDCLIB_L_tmpnam 46 /* Number of distinct file names that can be generated by tmpnam(). */ #define _PDCLIB_TMP_MAX 50 diff --git a/platform/example_cygwin/internals/_PDCLIB_config.h b/platform/example_cygwin/internals/_PDCLIB_config.h index 78397be..32e0906 100644 --- a/platform/example_cygwin/internals/_PDCLIB_config.h +++ b/platform/example_cygwin/internals/_PDCLIB_config.h @@ -258,9 +258,6 @@ typedef int _PDCLIB_fd_t; /* Length of the longest filename the implementation guarantees to support. */ #define _PDCLIB_FILENAME_MAX 128 -/* Buffer size for tmpnam(). */ -#define _PDCLIB_L_tmpnam 100 - /* Number of distinct file names that can be generated by tmpnam(). */ #define _PDCLIB_TMP_MAX 50 -- 2.40.0