Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
L_ |
Maximum temporary-file name length | M | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
L_ |
Maximum temporary-file name length | ? | M | <stdio.h> |
C11 | ||||||
TMP_ |
Maximum number of temporary files | M | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
TMP_ |
Maximum number of temporary files | ? | M | <stdio.h> |
C11 | ||||||
tmpfile() |
Create temporary file | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
tmpfile_s() |
Create temporary file | ? | (·) | <stdio.h> |
C11 | ||||||
tmpnam() |
Create temporary file name | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
tmpnam_s() |
Create temporary file name | ? | (·) | <stdio.h> |
C11 |
#include <stdio.h>
char *tmpnam(char *buf);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <stdio.h>
errno_t tmpnam_s(char *buf, rsize_t buflen);
tmpnam
and tmpnam_s
generate a name for a file to be used as temporary storage,
storing it in the array starting at buf
. tmpnam(NULL)
instead uses static
storage, and returns a pointer to it. Otherwise, the supplied
array must be at least L_
bytes long for tmpnam
,
or L_
bytes long for tmpnam_s
.
buflen
has to be at least L_
,
but no more than RSIZE_
. On failure,
tmpnam
returns NULL
, while tmpnam_s
returns non-zero.
Each call generates a different name, but there is no
guarantee that no other program will create a file with that
name before the program that asked for it actually tries to
open it. Also, the sets of names generated by tmpnam
and tmpnam_s
respectively may intersect.
#include <stdio.h>
FILE *tmpfile(void);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <stdio.h>
errno_t tmpfile_s(FILE **strp);
tmpfile
and tmpfile_s
each create temporary files opened as if with fopen
and a mode of wb+.
tmpfile
returns the stream handle, while tmpfile_s
assigns it to *strp
. tmpfile
returns NULL
if it could not open a new
file, while tmpfile_s
returns non-zero. Files created with these funtions are
deleted upon normal termination.
The number of distinct files that tmpfile
can open, plus the number of distinct filenames that
tmpnam
can generate, is at least TMP_
,
which is at least 25. The number of distinct files that
tmpfile_s
can open, plus the number of distinct filenames that
tmpnam_s
can generate, is at least TMP_
,
which is also at least 25.
POSIX provides
mkstemp
, which is supposed to be more secure
than tmpnam
:
FILE *open_temp(const char *tmp_name, size_t len) { #if (_XOPEN_SOURCE - 0) >= 500 /* POSIX 1003.1-2001 hasmkstemp
andfdopen
. */ char namebuf = "/tmp/m5-pre-XXXXXX"; int fd = mkstemp(namebuf); FILE *out = NULL; if (fd >= 0) { strncpy(tmp_name, namebuf, len); /* Setumask
too? */ out = fdopen(fd, "w"); if (!out) close(fd); } return out; #else /* C89 hastmpnam
, but this might generate a linker warning. */ tmpnam(tmp_name); return fopen(tmp_name, "w"); #endif }