5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
16 #include <_PDCLIB_glue.h>
19 /* get O_CLOEXEC without sys/types.h being awful */
20 #include <asm/fcntl.h>
21 int open(const char *fname, int flags, ...);
26 extern const _PDCLIB_fileops_t _PDCLIB_fileops;
28 FILE* _PDCLIB_nothrow tmpfile( void )
31 /* Good quality random source */
32 int urandom = open( "/dev/urandom", O_RDONLY | O_CLOEXEC );
40 char filename[ L_tmpnam ];
44 if( read(urandom, &randnum, sizeof randnum ) != sizeof randnum )
51 sprintf( filename, "/tmp/%llx.tmp", randnum );
52 /* Check if file of this name exists. Note that fopen() is a very weak
53 check, which does not take e.g. access permissions into account
54 (file might exist but not readable). Replace with something more
57 fd = open( filename, O_CREAT | O_EXCL | O_RDWR, 0600 );
65 FILE* rc = _PDCLIB_fvopen(((_PDCLIB_fd_t){ .sval = fd}), &_PDCLIB_fileops,
66 _PDCLIB_FWRITE | _PDCLIB_FRW |
67 _PDCLIB_DELONCLOSE, filename);
80 #include <_PDCLIB_test.h>
87 char filename[ L_tmpnam ];
90 TESTCASE( ( fh = tmpfile() ) != NULL );
91 TESTCASE( fputc( 'x', fh ) == 'x' );
92 /* Checking that file is actually there */
93 TESTCASE_NOREG( strcpy( filename, fh->filename ) == filename );
94 TESTCASE_NOREG( ( fhtest = fopen( filename, "r" ) ) != NULL );
95 TESTCASE_NOREG( fclose( fhtest ) == 0 );
97 TESTCASE( fclose( fh ) == 0 );
98 /* Checking that file was deleted */
99 TESTCASE_NOREG( fopen( filename, "r" ) == NULL );