Paul Edwards <
mutazilah@gmail.com> wrote:
My reading of the ANSI C89 draft is that tmpfile() only
opens a single file. ie you're not expected to be able to do
two separate calls to tmpfile and get two separate files.
Unlike tmpnam, there is no mention of TMP_MAX to
suggest multiple opens are expected.
So what's the situation? Is my C library only expected to
support a single tmpfile() call until the file is closed?
I won't bother to dig up the ANSI standard, but the Linux man page says:
The tmpfile() function opens a unique temporary file
in binary read/write (w+b) mode. The file will be automatically
deleted when it is closed or the program terminates.
[ ... ]
STANDARDS
C11, POSIX.1-2008.
My reading is that a second call to tmpfile() would be just fine
and would open *another* unique temporary file. Also, I would expect
the deferred deletion to be done by unlinking the file immediately
after creation, leaving an anonymous file that disappears on last
close.
(If the OS doesn't have anonymous files the implementation might
have to register a cleanup function with atexit() or similar.)
This is what happens on Linux. Here's a test program: --------------------------------------------------
#include <stdio.h>
#include <stdlib.h> /* system() */
int main(int argc, char *argv[])
{
FILE *f1= tmpfile();
FILE *f2= tmpfile();
int fd1= fileno(f1);
int fd2= fileno(f2);
char buf[128];
int count;
/* FIXME error checks */
count= snprintf(buf, 128, "file /proc/self/fd/%d", fd1);
system(buf);
count= snprintf(buf, 128, "file /proc/self/fd/%d", fd2);
system(buf);
fclose(f2);
fclose(f1);
return 0;
}
--------------------------------------------------
Output:
--------------------------------------------------
/proc/self/fd/3: symbolic link to /tmp/#524307 (deleted)
/proc/self/fd/4: symbolic link to /tmp/#524308 (deleted) --------------------------------------------------
--
pa at panix dot com
--- PyGate Linux v1.5.14
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)