tags:

views:

919

answers:

6

Let's say I'm creating a program in C that needs to use a tempfile. Creating an ad hoc tempfile in /tmp is probably not a good idea. Is there a function or OS call to supply me with a tempfile name so that I can begin to write and read from it?

+13  A: 

You can use the mkstemp(3) function for this purpose. Another alternative is the tmpfile(3) function. Which one of them you choose depends on whether you want the file to be opened as a C library file stream (which tmpfile does), or a direct file descriptor (mkstemp). The tmpfile function also deletes the file automatically when you program finishes.

The advantage of using these functions is that they avoid race conditions between determining the unique filename and creating the file -- so that two programs won't try to create the same file at the same time, for example.

See the man pages for both functions for more details.

gavrie
+1  A: 

Absolutely: man mkstemp.

The man page has example usage.

DMC
A: 

Not sure about anything in a C lib, but you can do this at the shell with mktemp.

Frep D-Oronge
A: 
garethm
+1  A: 

@garethm:

I believe that the function you're looking for is called tmpnam.

You should definitely not use tmpnam. It suffers from the race condition problem I mentioned in my answer: Between determining the name and opening it, another program may create the file or a symlink to it, which is a huge security hole.

The tmpnam man page specifically says not to use it, but to use mkstemp or tmpfile instead.

gavrie
A: 

The question is how to generate a temporary file name. Neither mkstemp nor tmpfile provide the caller with a name, they return a file descriptor or file handle, respectively.

Mark Borgerding
You are correct, Mark, the question asks for a temporary file name. But the question is wrong. The questioner doesn't really want a temporary file name, he really wants a temporary file.
Rob Adams
Then the question should be changed.
Mark Borgerding
Not true: mkstemp *does* provide the caller with a name, since it modifies the template it is given. The caller needs to provide a template, but this is a relatively simple matter of choosing a temporary directory (e.g. $TMPDIR, P_tmpdir, or "/tmp") and a prefix, followed by six X's.
Trevor Robinson
Ahh, subtle. So the only "good" way to do get a unique filename without actually leaving the file dangling is: 1) call mkstemp with a character buffer that ends in XXXXXX. 2) close the file descriptor mkstemp returns. 3) unlink the filename that mkstemp left in the "template" parameter. Could it be any less elegant?
Mark Borgerding
Yeah, I spent quite a while recently coming up with a portable Unix/Windows temp file class. On Linux, you basically have to choose between knowing the filename (mkstemp), and having the file closed automatically for you (tmpfile). On Windows, you have a delete-on-close flag for CreateFile, but no mkstemp and a tmpfile with lots of caveats (root directory?!). In the end, I just wrote my own unique name generator using the crypto PRNG to generate a 128-bit hex suffix.
Trevor Robinson