my-server
← Wiki

Mkstemp

In computing, <code>mkstemp</code> is a POSIX function for creating a temporary file (a computer file which usually ceases to exist when the program, which opened the file, closes it or terminates). It accepts an argument that determines the location of the temporary file, and the prefix of its generated filename. After <code>mkstemp</code> was added to the Single UNIX Specification, the function <code>tmpnam()</code> was deprecated, because the latter carried the risk that a temporary file with the same name could be created by another thread or process within the time from when the caller obtains the temporary filename and attempts to create it. <code>mkstemp</code> does not suffer from this problem.

Usage

Inclusion

In C, this is included by <code><stdlib.h></code> (or <code><cstdlib></code> in C++, if is not available) per IEEE Std 1003.1, 2004, otherwise it is included through <code><unistd.h></code> in legacy systems.

In C++, it can be replaced with <code>std::tmpnam()</code> or <code>std::filesystem::temp_directory_path()</code>.

Signature

Do note that <code>template</code> is a keyword in C++, for templates.

  • The parameter <code>template</code> must be a modifiable, null-terminated character array.
  • The contents of <code>template</code> must be in the format of a valid file path, with six trailing 'X's.
  • The parameter <code>template</code> must not have been used in a previous invocation of <code>mkstemp</code>.

Semantics

  • The trailing s in <code>template</code> are overwritten to generate a unique file name for the resulting temporary file.
  • The function reports a valid file descriptor to a temporary file on success; on failure, it reports <code>-1</code>.

Example

The following code is an example of the usage of <code>mkstemp</code>; the local variable <code>filename</code> is modified by <code>mkstemp</code> and will contain the path to the new file:

Error conditions

It is unspecified if <code>mkstemp</code> sets errno, and what values of errno are set, in the event of failure.

Mechanism

The <code>mkstemp</code> function generates a filename according to the supplied argument for the template, and attempts to create it. It repeats this process until a file has been successfully created. After this, it opens the file and returns the file descriptor to the caller, with the data buffer that was passed to the function with the template now containing the new filename. The file can be deleted immediately after the <code>mkstemp</code> call returns to prevent other processes from opening it, but the file can still be used because the calling process will still have a valid file descriptor. Older versions of <code>mkstemp</code> created the file with an umask of 0666, resulting in the temporary files being readable and writable to all users, and thus presenting a security vulnerability; this is mitigated by setting the umask manually before calling <code>mkstemp</code>. Newer versions of the function create the file with the umask 600, so that only the owner of the file may read from and write to it.

See also

References