HOME

TheInfoList



OR:

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 inter ...
function for creating a
temporary file A temporary file is a file created to store information temporarily, either for a program's intermediate use or for transfer to a permanent file when complete. It may be created by computer programs for a variety of purposes, such as when a program ...
(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 Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Progra ...
, 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 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 A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The del ...
, 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 A filename or file name is a name used to uniquely identify a computer file in a directory structure. Different file systems impose different restrictions on filename lengths. A filename may (depending on the file system) include: * name &ndas ...
for the resulting temporary file. * The function reports a valid file descriptor to a
temporary file A temporary file is a file created to store information temporarily, either for a program's intermediate use or for transfer to a permanent file when complete. It may be created by computer programs for a variety of purposes, such as when a program ...
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 In computing, umask is a command that determines the settings of a mask that controls how file permissions are set for newly created files. It may also affect how the file permissions are changed explicitly. is also a function that sets the ma ...
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 throu ...
, 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 , 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 , 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