1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
<h1 id="firstHeading" class="firstHeading">tmpnam, tmpnam_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">char *tmpnam( char *filename );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t tmpnam_s(char *filename_s, rsize_t maxsize);</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define TMP_MAX /*unspecified*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">#define TMP_MAX_S /*unspecified*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define L_tmpnam /*unspecified*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">#define L_tmpnam_s /*unspecified*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Creates a unique valid file name (no longer than <code><a href="../io" title="c/io">L_tmpnam</a></code> in length) and stores it in character string pointed to by <code>filename</code>. The function is capable of generating up to <code><a href="../io" title="c/io">TMP_MAX</a></code> of unique filenames, but some or all of them may be in use in the filesystem and thus not suitable return values.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that up to <code>TMP_MAX_S</code> names may be generated, no longer than <code>L_tmpnam_s</code> in length, and the following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl>
<dd>
<ul>
<li> <code>filename_s</code> is a null pointer </li>
<li> <code>maxsize</code> is greater than <code>RSIZE_MAX</code> </li>
<li> <code>maxsize</code> is less than the generated file name string </li>
</ul> </dd>
<dd> As with all bounds-checked functions, <code>tmpnam_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../io" title="c/io"><code><stdio.h></code></a>.</dd>
</dl>
</div> <p><code>tmpnam</code> and <code>tmpnam_s</code> modify static state (which may be shared between these functions) and are not required to be thread-safe.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> filename </td> <td> - </td> <td> pointer to the character array capable of holding at least <code><a href="../io" title="c/io">L_tmpnam</a></code> bytes, to be used as a result buffer. If null pointer is passed, a pointer to an internal static buffer is returned. </td>
</tr> <tr class="t-par"> <td> filename_s </td> <td> - </td> <td> pointer to the character array capable of holding at least <code>L_tmpnam_s</code> bytes, to be used as a result buffer. </td>
</tr> <tr class="t-par"> <td> maxsize </td> <td> - </td> <td> maximum number of characters the function is allowed to write (typically the size of the <code>filename_s</code> array). </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> <code>filename</code> if <code>filename</code> was not a null pointer. Otherwise a pointer to an internal static buffer is returned. If no suitable filename can be generated, null pointer is returned.</div> <div class="t-li1">
<span class="t-li">2)</span> Returns zero and writes the file name to <code>filename_s</code> on success. On error, returns non-zero and writes the null character to <code>filename_s[0]</code> (only if <code>filename_s</code> is not null and <code>maxsize</code> is not zero and is not greater than <code>RSIZE_MAX</code>).</div> <h3 id="Notes"> Notes</h3> <p>Although the names generated by <code>tmpnam</code> are difficult to guess, it is possible that a file with that name is created by another process between the moment <code>tmpnam</code> returns and the moment this program attempts to use the returned name to create a file. The standard function <code><a href="tmpfile" title="c/io/tmpfile">tmpfile</a></code> and the POSIX function <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdtemp.html"><code>mkstemp</code></a> do not have this problem (creating a unique directory using only the standard C library still requires the use of <code>tmpnam</code>).</p>
<p>POSIX systems additionally define the similarly named function <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/tempnam.html"><code>tempnam</code></a>, which offers the choice of a directory (which defaults to the optionally defined macro <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdio.h.html"><code>P_tmpdir</code></a>).</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// Note, the compiler/linker may issue a security warning, e.g. GCC:
// "warning: the use of `tmpnam' is dangerous, better use `mkstemp'"
char* name1 = tmpnam(NULL);
printf("temporary file name: %s\n", name1);
char name2[L_tmpnam];
if (tmpnam(name2))
printf("temporary file name: %s\n", name2);
// POSIX offers mkstemp. The following declaration might be
// necessary as mkstemp is absent in the standard C <stdlib.h>.
int mkstemp(char*);
char name3[] = "/tmp/fileXXXXXX"; // at least six 'X' required ^_^
int file_descriptor = mkstemp(name3);
if (file_descriptor != -1)
printf("temporary file name: %s\n", name3);
else
perror("mkstemp");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">temporary file name: /tmp/file90dLlR
temporary file name: /tmp/fileY9LWAg
temporary file name: /tmp/filexgv8PF</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul>
<li> 7.21.4.4 The tmpnam function (p: TBD) </li>
<li> K.3.5.1.2 The tmpnam_s function (p: TBD) </li>
</ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.21.4.4 The tmpnam function (p: 222) </li>
<li> K.3.5.1.2 The tmpnam_s function (p: 427-428) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.21.4.4 The tmpnam function (p: 303-304) </li>
<li> K.3.5.1.2 The tmpnam_s function (p: 587-588) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.4.4 The tmpnam function (p: 269-270) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.4.4 The tmpnam function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="tmpfile" title="c/io/tmpfile"> <span class="t-lines"><span>tmpfile</span><span>tmpfile_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> returns a pointer to a temporary file <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/tmpnam" title="cpp/io/c/tmpnam">C++ documentation</a></span> for <code>tmpnam</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/io/tmpnam" class="_attribution-link">https://en.cppreference.com/w/c/io/tmpnam</a>
</p>
</div>
|