Names specified here
Name Description Notes Source Availability
L_tmpnam Maximum temporary-file name length M <stdio.h> C89 C90 C95 C99 C11
L_tmpnam_s Maximum temporary-file name length ? M <stdio.h> C11
TMP_MAX Maximum number of temporary files M <stdio.h> C89 C90 C95 C99 C11
TMP_MAX_S 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_LIB_EXT1__ 1
#include <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_tmpnam bytes long for tmpnam, or L_tmpnam_s bytes long for tmpnam_s. buflen has to be at least L_tmpnam_s, but no more than RSIZE_MAX. 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_LIB_EXT1__ 1
#include <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_MAX, 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_MAX_S, 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 has mkstemp and fdopen. */
  char namebuf = "/tmp/m5-pre-XXXXXX";
  int fd = mkstemp(namebuf);
  FILE *out = NULL;
  if (fd >= 0) {
    strncpy(tmp_name, namebuf, len);
    /* Set umask too? */
    out = fdopen(fd, "w");
    if (!out)
      close(fd);
  }
  return out;
#else
  /* C89 has tmpnam, but this might generate a linker warning. */
  tmpnam(tmp_name);
  return fopen(tmp_name, "w");
#endif
}

CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000