| 1 |
'\" t |
| 2 |
.\" Copyright 1993, David Metcalfe <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH mktemp 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
mktemp \- make a unique temporary filename |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <stdlib.h> |
| 16 |
.P |
| 17 |
.BI "char *mktemp(char *" template ); |
| 18 |
.fi |
| 19 |
.P |
| 20 |
.RS -4 |
| 21 |
Feature Test Macro Requirements for glibc (see |
| 22 |
.BR feature_test_macros (7)): |
| 23 |
.RE |
| 24 |
.P |
| 25 |
.BR mktemp (): |
| 26 |
.nf |
| 27 |
Since glibc 2.12: |
| 28 |
(_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200112L) |
| 29 |
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE |
| 30 |
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE |
| 31 |
Before glibc 2.12: |
| 32 |
_BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500 |
| 33 |
.\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED |
| 34 |
.fi |
| 35 |
.SH DESCRIPTION |
| 36 |
.IR "Never use this function" ; |
| 37 |
see BUGS. |
| 38 |
.P |
| 39 |
The |
| 40 |
.BR mktemp () |
| 41 |
function generates a unique temporary filename |
| 42 |
from |
| 43 |
.IR template . |
| 44 |
The last six characters of |
| 45 |
.I template |
| 46 |
must be XXXXXX |
| 47 |
and these are replaced with a string that makes the |
| 48 |
filename unique. |
| 49 |
Since it will be modified, |
| 50 |
.I template |
| 51 |
must not be a string constant, but should be declared as a character array. |
| 52 |
.SH RETURN VALUE |
| 53 |
The |
| 54 |
.BR mktemp () |
| 55 |
function always returns |
| 56 |
.IR template . |
| 57 |
If a unique name was created, the last six bytes of |
| 58 |
.I template |
| 59 |
will have been modified in such a way that the resulting name is unique |
| 60 |
(i.e., does not exist already) |
| 61 |
If a unique name could not be created, |
| 62 |
.I template |
| 63 |
is made an empty string, and |
| 64 |
.I errno |
| 65 |
is set to indicate the error. |
| 66 |
.SH ERRORS |
| 67 |
.TP |
| 68 |
.B EINVAL |
| 69 |
The last six characters of |
| 70 |
.I template |
| 71 |
were not XXXXXX. |
| 72 |
.SH ATTRIBUTES |
| 73 |
For an explanation of the terms used in this section, see |
| 74 |
.BR attributes (7). |
| 75 |
.TS |
| 76 |
allbox; |
| 77 |
lbx lb lb |
| 78 |
l l l. |
| 79 |
Interface Attribute Value |
| 80 |
T{ |
| 81 |
.na |
| 82 |
.nh |
| 83 |
.BR mktemp () |
| 84 |
T} Thread safety MT-Safe |
| 85 |
.TE |
| 86 |
.SH STANDARDS |
| 87 |
None. |
| 88 |
.SH HISTORY |
| 89 |
4.3BSD, POSIX.1-2001. |
| 90 |
Removed in POSIX.1-2008. |
| 91 |
.\" .SH NOTES |
| 92 |
.\" The prototype is in |
| 93 |
.\" .I <unistd.h> |
| 94 |
.\" for libc4, libc5, glibc1; glibc2 follows the Single UNIX Specification |
| 95 |
.\" and has the prototype in |
| 96 |
.\" .IR <stdlib.h> . |
| 97 |
.SH BUGS |
| 98 |
Never use |
| 99 |
.BR mktemp (). |
| 100 |
Some implementations follow 4.3BSD |
| 101 |
and replace XXXXXX by the current process ID and a single letter, |
| 102 |
so that at most 26 different names can be returned. |
| 103 |
Since on the one hand the names are easy to guess, and on the other |
| 104 |
hand there is a race between testing whether the name exists and |
| 105 |
opening the file, every use of |
| 106 |
.BR mktemp () |
| 107 |
is a security risk. |
| 108 |
The race is avoided by |
| 109 |
.BR mkstemp (3) |
| 110 |
and |
| 111 |
.BR mkdtemp (3). |
| 112 |
.SH SEE ALSO |
| 113 |
.BR mktemp (1), |
| 114 |
.BR mkdtemp (3), |
| 115 |
.BR mkstemp (3), |
| 116 |
.BR tempnam (3), |
| 117 |
.BR tmpfile (3), |
| 118 |
.BR tmpnam (3) |