5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
17 #include <_PDCLIB_glue.h>
19 #include <sys/types.h>
24 extern const _PDCLIB_fileops_t _PDCLIB_fileops;
26 FILE* _PDCLIB_nothrow tmpfile( void )
29 /* Good quality random source */
30 int urandom = open( "/dev/urandom", O_RDONLY | O_CLOEXEC );
38 char filename[ L_tmpnam ];
42 if( read(urandom, &randnum, sizeof randnum ) != sizeof randnum )
49 sprintf( filename, "/tmp/%llx.tmp", randnum );
50 /* Check if file of this name exists. Note that fopen() is a very weak
51 check, which does not take e.g. access permissions into account
52 (file might exist but not readable). Replace with something more
55 fd = open( filename, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR );
63 FILE* rc = _PDCLIB_fvopen(((_PDCLIB_fd_t){ .sval = fd}), &_PDCLIB_fileops,
64 _PDCLIB_FWRITE | _PDCLIB_FRW |
65 _PDCLIB_DELONCLOSE, filename);
78 #include <_PDCLIB_test.h>
85 char filename[ L_tmpnam ];
88 TESTCASE( ( fh = tmpfile() ) != NULL );
89 TESTCASE( fputc( 'x', fh ) == 'x' );
90 /* Checking that file is actually there */
91 TESTCASE_NOREG( strcpy( filename, fh->filename ) == filename );
92 TESTCASE_NOREG( ( fhtest = fopen( filename, "r" ) ) != NULL );
93 TESTCASE_NOREG( fclose( fhtest ) == 0 );
95 TESTCASE( fclose( fh ) == 0 );
96 /* Checking that file was deleted */
97 TESTCASE_NOREG( fopen( filename, "r" ) == NULL );