| 1 |
'\" t |
| 2 |
.\" Copyright 1999, Andries E. Brouwer <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH tmpnam 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
tmpnam, tmpnam_r \- create a name for a temporary file |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <stdio.h> |
| 16 |
.P |
| 17 |
.BI "[[deprecated]] char *tmpnam(char *" s ); |
| 18 |
.BI "[[deprecated]] char *tmpnam_r(char *" s ); |
| 19 |
.fi |
| 20 |
.P |
| 21 |
.RS -4 |
| 22 |
Feature Test Macro Requirements for glibc (see |
| 23 |
.BR feature_test_macros (7)): |
| 24 |
.RE |
| 25 |
.P |
| 26 |
.BR tmpnam_r () |
| 27 |
.nf |
| 28 |
Since glibc 2.19: |
| 29 |
_DEFAULT_SOURCE |
| 30 |
Up to and including glibc 2.19: |
| 31 |
_BSD_SOURCE || _SVID_SOURCE |
| 32 |
.fi |
| 33 |
.SH DESCRIPTION |
| 34 |
.B Note: |
| 35 |
avoid using these functions; |
| 36 |
use |
| 37 |
.BR mkstemp (3) |
| 38 |
or |
| 39 |
.BR tmpfile (3) |
| 40 |
instead. |
| 41 |
.P |
| 42 |
The |
| 43 |
.BR tmpnam () |
| 44 |
function returns a pointer to a string that is a valid filename, |
| 45 |
and such that a file with this name did not exist at some point |
| 46 |
in time, so that naive programmers may think it |
| 47 |
a suitable name for a temporary file. |
| 48 |
If the argument |
| 49 |
.I s |
| 50 |
is NULL, this name is generated in an internal static buffer |
| 51 |
and may be overwritten by the next call to |
| 52 |
.BR tmpnam (). |
| 53 |
If |
| 54 |
.I s |
| 55 |
is not NULL, the name is copied to the character array (of length |
| 56 |
at least |
| 57 |
.IR L_tmpnam ) |
| 58 |
pointed to by |
| 59 |
.I s |
| 60 |
and the value |
| 61 |
.I s |
| 62 |
is returned in case of success. |
| 63 |
.P |
| 64 |
The created pathname has a directory prefix |
| 65 |
.IR P_tmpdir . |
| 66 |
(Both |
| 67 |
.I L_tmpnam |
| 68 |
and |
| 69 |
.I P_tmpdir |
| 70 |
are defined in |
| 71 |
.IR <stdio.h> , |
| 72 |
just like the |
| 73 |
.B TMP_MAX |
| 74 |
mentioned below.) |
| 75 |
.P |
| 76 |
The |
| 77 |
.BR tmpnam_r () |
| 78 |
function performs the same task as |
| 79 |
.BR tmpnam (), |
| 80 |
but returns NULL (to indicate an error) if |
| 81 |
.I s |
| 82 |
is NULL. |
| 83 |
.SH RETURN VALUE |
| 84 |
These functions return a pointer to a unique temporary |
| 85 |
filename, or NULL if a unique name cannot be generated. |
| 86 |
.SH ERRORS |
| 87 |
No errors are defined. |
| 88 |
.SH ATTRIBUTES |
| 89 |
For an explanation of the terms used in this section, see |
| 90 |
.BR attributes (7). |
| 91 |
.TS |
| 92 |
allbox; |
| 93 |
lbx lb lb |
| 94 |
l l l. |
| 95 |
Interface Attribute Value |
| 96 |
T{ |
| 97 |
.na |
| 98 |
.nh |
| 99 |
.BR tmpnam () |
| 100 |
T} Thread safety MT-Unsafe race:tmpnam/!s |
| 101 |
T{ |
| 102 |
.na |
| 103 |
.nh |
| 104 |
.BR tmpnam_r () |
| 105 |
T} Thread safety MT-Safe |
| 106 |
.TE |
| 107 |
.SH STANDARDS |
| 108 |
.TP |
| 109 |
.BR tmpnam () |
| 110 |
C11, POSIX.1-2008. |
| 111 |
.TP |
| 112 |
.BR tmpnam_r () |
| 113 |
None. |
| 114 |
.SH HISTORY |
| 115 |
.TP |
| 116 |
.BR tmpnam () |
| 117 |
SVr4, 4.3BSD, C89, POSIX.1-2001. |
| 118 |
Obsolete in POSIX.1-2008. |
| 119 |
.TP |
| 120 |
.BR tmpnam_r () |
| 121 |
Solaris. |
| 122 |
.SH NOTES |
| 123 |
The |
| 124 |
.BR tmpnam () |
| 125 |
function generates a different string each time it is called, |
| 126 |
up to |
| 127 |
.B TMP_MAX |
| 128 |
times. |
| 129 |
If it is called more than |
| 130 |
.B TMP_MAX |
| 131 |
times, |
| 132 |
the behavior is implementation defined. |
| 133 |
.P |
| 134 |
Although these functions generate names that are difficult to guess, |
| 135 |
it is nevertheless possible that between the time that |
| 136 |
the pathname is returned and the time that the program opens it, |
| 137 |
another program might create that pathname using |
| 138 |
.BR open (2), |
| 139 |
or create it as a symbolic link. |
| 140 |
This can lead to security holes. |
| 141 |
To avoid such possibilities, use the |
| 142 |
.BR open (2) |
| 143 |
.B O_EXCL |
| 144 |
flag to open the pathname. |
| 145 |
Or better yet, use |
| 146 |
.BR mkstemp (3) |
| 147 |
or |
| 148 |
.BR tmpfile (3). |
| 149 |
.P |
| 150 |
Portable applications that use threads cannot call |
| 151 |
.BR tmpnam () |
| 152 |
with a NULL argument if either |
| 153 |
.B _POSIX_THREADS |
| 154 |
or |
| 155 |
.B _POSIX_THREAD_SAFE_FUNCTIONS |
| 156 |
is defined. |
| 157 |
.SH BUGS |
| 158 |
Never use these functions. |
| 159 |
Use |
| 160 |
.BR mkstemp (3) |
| 161 |
or |
| 162 |
.BR tmpfile (3) |
| 163 |
instead. |
| 164 |
.SH SEE ALSO |
| 165 |
.BR mkstemp (3), |
| 166 |
.BR mktemp (3), |
| 167 |
.BR tempnam (3), |
| 168 |
.BR tmpfile (3) |