CKTIS / C
tmpnam — generate a name without creating a file
tmpnam generates a filename but neither reserves it nor creates a file. Learn about the L_tmpnam buffer, NULL results, and the race when opening the name later.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdio.h>
char *tmpnam(char *s);tmpnam from <stdio.h> generates a filename that does not name an existing file at the time of the call. It creates no file. Non-null s requires a buffer of at least L_tmpnam characters; success writes the string there and returns s. Failure returns NULL.
tmpnam(NULL) uses static storage that later calls may overwrite; such calls may cause data races. TMP_MAX describes potential distinct names, not guaranteed successful calls.
Do not use tmpnam followed by fopen for secure file creation: someone may claim the name between calls. Supplying your own buffer does not remove this risk. Consider tmpfile() when you need a temporary stream. Files created using names from tmpnam require removal by the program.
The example only generates a name; it does not open it. Its fixed success message lets you check the result without assuming a particular filename.
Examples
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char name[L_tmpnam];
if (tmpnam(name) == NULL) {
puts("name unavailable");
return EXIT_FAILURE;
}
puts("name generated; no file created");
return EXIT_SUCCESS;
}Result
name generated; no file created
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.