X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2Fstdio%2Ftmpfile.c;h=78273422d5c831555a6dd3e8e4c4a4d29bc75337;hp=b778f3f9dabe374c63a54b551b4e2bbf38d61669;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=a7a8d2f1c85c2d7760d4d3479e90466cc3a81b04 diff --git a/platform/example/functions/stdio/tmpfile.c b/platform/example/functions/stdio/tmpfile.c index b778f3f..7827342 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,104 +8,25 @@ #ifndef REGTEST -#include -#include -#include - -#include <_PDCLIB_glue.h> - -#include -#include -#include -#include +#include +#include "_PDCLIB_glue.h" -extern struct _PDCLIB_file_t * _PDCLIB_filelist; - -/* This is an example implementation of tmpfile() fit for use with POSIX - kernels. +/* This is a stub implementation of tmpfile */ -struct _PDCLIB_file_t * tmpfile( void ) +FILE* tmpfile( void ) { - FILE * rc; - /* This is the chosen way to get high-quality randomness. Replace as - appropriate. - */ - FILE * randomsource = fopen( "/proc/sys/kernel/random/uuid", "rb" ); - char filename[ L_tmpnam ]; - _PDCLIB_fd_t fd; - if ( randomsource == NULL ) - { - return NULL; - } - for ( ;; ) - { - /* Get a filename candidate. What constitutes a valid filename and - where temporary files are usually located is platform-dependent, - which is one reason why this function is located in the platform - overlay. The other reason is that a *good* implementation should - use high-quality randomness instead of a pseudo-random sequence to - generate the filename candidate, which is *also* platform-dependent. - */ - 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 - appropriate. - */ - fd = open( filename, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR ); - if ( fd != -1 ) - { - break; - } - close( fd ); - } - fclose( randomsource ); - /* See fopen(). */ - if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + L_tmpnam + BUFSIZ ) ) == NULL ) - { - /* No memory to set up FILE structure */ - close( fd ); - return NULL; - } - 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; - rc->buffer = rc->filename + L_tmpnam; - strcpy( rc->filename, filename ); - rc->bufsize = BUFSIZ; - rc->bufidx = 0; - rc->ungetidx = 0; - rc->next = _PDCLIB_filelist; - _PDCLIB_filelist = rc; - return rc; + errno = ENOTSUP; + return NULL; } #endif #ifdef TEST -#include <_PDCLIB_test.h> +#include "_PDCLIB_test.h" #include -int main() +int main( void ) { - 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; }