In
computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, ...
,
mkstemp
is a
POSIX
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming inte ...
function for creating a
temporary file (a
computer file
A computer file is a computer resource for recording data in a computer storage device, primarily identified by its file name. Just as words can be written to paper, so can data be written to a computer file. Files can be shared with and trans ...
which usually ceases to exist when the
program, which opened the file, closes it or terminates).
It accepts an
argument
An argument is a statement or group of statements called premises intended to determine the degree of truth or acceptability of another statement called conclusion. Arguments can be studied from three main perspectives: the logical, the dialect ...
that determines the location of the temporary file, and the prefix of its generated filename.
After
mkstemp
was added to the
Single UNIX Specification, the function
tempnam()
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.
mkstemp
does not suffer from this problem.
Usage
Inclusion
; C
#include // per IEEE Std 1003.1, 2004
#include // for "legacy" systems
; C++
#include // per IEEE Std 1003.1, 2004
#include // for "legacy" systems
Declaration
int mkstemp(char* template);
Requirements
* The parameter
template
must be a modifiable, null-terminated character array.
* The contents of
template
must be in the format of a valid
file path, with six trailing 'X's.
* The parameter
template
must not have been used in a previous invocation of
mkstemp
.
Semantics
* The trailing 'X's in
template
are overwritten to generate a unique
file name for the resulting temporary file.
* The function reports a valid
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier ( handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically ...
to a
temporary file on success; on failure, it reports
-1
.
Example
The following code is an example of the usage of
mkstemp
; the local variable
filename
is modified by
mkstemp
and will contain the path to the new file:
#include
void example()
Error conditions
It is unspecified if
mkstemp
sets
errno
errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short for "error number").International Standard for Programming Language C ...
, and what values of
errno
errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short for "error number").International Standard for Programming Language C ...
are set, in the event of failure.
Mechanism
The
mkstemp
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
In computer science, a data buffer (or just buffer) is a region of a memory used to temporarily store data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device (such a ...
that was passed to the function with the template now containing the new filename.
The file can be deleted immediately after the
mkstemp
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
mkstemp
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
mkstemp
.
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
*
tmpfile
References
{{reflist, refs=
[mkstemp]
by OpenGroup
[{{Cite web
, url=http://pubs.opengroup.org/onlinepubs/9699919799/functions/tempnam.html
, title=tempnam
, publisher= OpenGroup
, work=Open Group Base Specifications
, edition=Issue 7
, year=2018
]
[{{cite book
, last1=Stevens , first1=W. Richard , author1-link=W. Richard Stevens
, last2=Rago , first2=Stephen A. , author2-link=Stephen A. Rago
, work= Advanced Programming in the Unix Environment
, year=2013
, publisher=]Addison-Wesley
Addison-Wesley is an American publisher of textbooks and computer literature. It is an imprint of Pearson PLC, a global publishing and education company. In addition to publishing books, Addison-Wesley also distributes its technical titles throug ...
, isbn=9780321638007
, title=Temporary Files , chapter=Standard Library Functions
, page=169
[{{cite book
, last1=Viega , first1=John , author1-link=John Viega
, last2=Messier , first2=Matt
, work=Secure Programming Cookbook for C and C++
, year=2003
, publisher=]O'Reilly Media
O'Reilly Media (formerly O'Reilly & Associates) is an American learning company established by Tim O'Reilly that publishes books, produces tech conferences, and provides an online learning platform. Its distinctive brand features a woodcut of ...
, isbn=9780596003944
, title=Temporary files on Unix , chapter=Access Control
, page=66
[{{cite journal
, last1=Chen , first1=Hao
, last2=Dean , first2=Drew
, last3=Wagner , first3=David A. , author3-link=David A. Wagner
, journal=]Network and Distributed System Security Symposium
The Internet Society (ISOC) is an American nonprofit advocacy organization founded in 1992 with local chapters around the world. Its mission is "to promote the open development, evolution, and use of the Internet for the benefit of all people ...
, volume=4
, year=2004
, publisher=Internet Society
The Internet Society (ISOC) is an American nonprofit advocacy organization founded in 1992 with local chapters around the world. Its mission is "to promote the open development, evolution, and use of the Internet for the benefit of all people ...
, url=http://seclab.cs.ucdavis.edu/papers/Hao-Chen-papers/ndss04.pdf , access-date=2019-05-18
, archive-url=https://web.archive.org/web/20151008061927/http://seclab.cs.ucdavis.edu/papers/Hao-Chen-papers/ndss04.pdf , archive-date=2015-10-08 , url-status=live
, title=Model Checking One Million Lines of C Code
[{{cite document
, last1=Drepper , first1=Ulrich , author1-link=Ulrich Drepper
, title=Defensive Programming for Red Hat Enterprise Linux (and What To Do If Something Goes Wrong)
, date=2009-04-08
, page=7
, s2cid=239879 , url=http://pdfs.semanticscholar.org/c613/325c8cb647f0e94fe2be85ce34060e30d313.pdf , access-date=2019-05-18
, archive-url=https://web.archive.org/web/20190305044042/http://pdfs.semanticscholar.org/c613/325c8cb647f0e94fe2be85ce34060e30d313.pdf , archive-date=2019-03-05 , url-status=dead
]
[{{cite book
, last1=Seacord , first1=Robert C. , author1-link=Robert C. Seacord
, work= The CERT C Coding Standard
, date=2014-04-25
, edition=2
, publisher=Addison-Wesley , isbn=9780133805291
, title=STR30-C. Do not attempt to modify string literals , chapter=Characters and Strings (STR)
, page=203
]
Unix file system-related software
C POSIX library