X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2Fstdio%2Ftmpfile.c;h=cb942ff345114d58680939725c8777cacf2d230b;hb=d865c4403fc91d1f1ac95ba76febcee9f429bb97;hp=4c55e5d3542a4c4db4f16f14fcd7d8425b6050ec;hpb=8024ab6eb8c841b330458354788d7f3a86bee7dd;p=pdclib diff --git a/platform/example/functions/stdio/tmpfile.c b/platform/example/functions/stdio/tmpfile.c index 4c55e5d..cb942ff 100644 --- a/platform/example/functions/stdio/tmpfile.c +++ b/platform/example/functions/stdio/tmpfile.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* tmpfile( void ) This file is part of the Public Domain C Library (PDCLib). @@ -10,12 +8,12 @@ #ifndef REGTEST +#include "_PDCLIB_glue.h" + #include #include #include -#include <_PDCLIB_glue.h> - #include #include #include @@ -24,7 +22,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 +30,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 +46,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 +69,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; @@ -88,13 +86,29 @@ struct _PDCLIB_file_t * tmpfile( void ) #endif #ifdef TEST -#include <_PDCLIB_test.h> -int main() +#include "_PDCLIB_test.h" + +#include + +int main( void ) { - TESTCASE( NO_TESTDRIVER ); + FILE * fh; +#ifndef REGTEST + char filename[ L_tmpnam ]; + FILE * fhtest; +#endif + TESTCASE( ( fh = tmpfile() ) != NULL ); + TESTCASE( fputc( 'x', fh ) == 'x' ); + /* Checking that file is actually there */ + TESTCASE_NOREG( strcpy( filename, fh->filename ) == filename ); + TESTCASE_NOREG( ( fhtest = fopen( filename, "r" ) ) != NULL ); + TESTCASE_NOREG( fclose( fhtest ) == 0 ); + /* Closing tmpfile */ + TESTCASE( fclose( fh ) == 0 ); + /* Checking that file was deleted */ + TESTCASE_NOREG( fopen( filename, "r" ) == NULL ); return TEST_RESULTS; } #endif -