| 1 |
'\" t |
| 2 |
.\" Copyright 1993, David Metcalfe <[email protected]> |
| 3 |
.\" Copyright 2008, Michael Kerrisk <[email protected]> |
| 4 |
.\" Copyright, the authors of the Linux man-pages project |
| 5 |
.\" |
| 6 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 7 |
.\" |
| 8 |
.TH mkstemp 3 2026-02-08 "Linux man-pages 6.18" |
| 9 |
.SH NAME |
| 10 |
mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <stdlib.h> |
| 17 |
.P |
| 18 |
.BI "int mkstemp(char *" template ); |
| 19 |
.BI "int mkostemp(char *" template ", int " flags ); |
| 20 |
.BI "int mkstemps(char *" template ", int " suffixlen ); |
| 21 |
.BI "int mkostemps(char *" template ", int " suffixlen ", int " flags ); |
| 22 |
.fi |
| 23 |
.P |
| 24 |
.RS -4 |
| 25 |
Feature Test Macro Requirements for glibc (see |
| 26 |
.BR feature_test_macros (7)): |
| 27 |
.RE |
| 28 |
.P |
| 29 |
.BR mkstemp (): |
| 30 |
.nf |
| 31 |
_XOPEN_SOURCE >= 500 |
| 32 |
.\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED |
| 33 |
|| /* glibc >= 2.12: */ _POSIX_C_SOURCE >= 200809L |
| 34 |
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE |
| 35 |
.fi |
| 36 |
.P |
| 37 |
.BR mkostemp (): |
| 38 |
.nf |
| 39 |
_GNU_SOURCE |
| 40 |
.fi |
| 41 |
.P |
| 42 |
.BR mkstemps (): |
| 43 |
.nf |
| 44 |
/* glibc >= 2.19: */ _DEFAULT_SOURCE |
| 45 |
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE |
| 46 |
.fi |
| 47 |
.P |
| 48 |
.BR mkostemps (): |
| 49 |
.nf |
| 50 |
_GNU_SOURCE |
| 51 |
.fi |
| 52 |
.SH DESCRIPTION |
| 53 |
The |
| 54 |
.BR mkstemp () |
| 55 |
function generates a unique temporary filename from |
| 56 |
.IR template , |
| 57 |
creates and opens the file, |
| 58 |
and returns an open file descriptor for the file. |
| 59 |
.P |
| 60 |
The last six characters of |
| 61 |
.I template |
| 62 |
must be "XXXXXX" and these are replaced with a string that makes the |
| 63 |
filename unique. |
| 64 |
Since it will be modified, |
| 65 |
.I template |
| 66 |
must not be a string constant, but should be declared as a character array. |
| 67 |
.P |
| 68 |
The file is created with |
| 69 |
permissions 0600, that is, read plus write for owner only. |
| 70 |
The returned file descriptor provides both read and write access to the file. |
| 71 |
The file is opened with the |
| 72 |
.BR open (2) |
| 73 |
.B O_EXCL |
| 74 |
flag, guaranteeing that the caller is the process that creates the file. |
| 75 |
.P |
| 76 |
The |
| 77 |
.BR mkostemp () |
| 78 |
function is like |
| 79 |
.BR mkstemp (), |
| 80 |
with the difference that the following bits\[em]with the same meaning as for |
| 81 |
.BR open (2)\[em]may |
| 82 |
be specified in |
| 83 |
.IR flags : |
| 84 |
.BR O_APPEND , |
| 85 |
.BR O_CLOEXEC , |
| 86 |
and |
| 87 |
.BR O_SYNC . |
| 88 |
Note that when creating the file, |
| 89 |
.BR mkostemp () |
| 90 |
includes the values |
| 91 |
.BR O_RDWR , |
| 92 |
.BR O_CREAT , |
| 93 |
and |
| 94 |
.B O_EXCL |
| 95 |
in the |
| 96 |
.I flags |
| 97 |
argument given to |
| 98 |
.BR open (2); |
| 99 |
including these values in the |
| 100 |
.I flags |
| 101 |
argument given to |
| 102 |
.BR mkostemp () |
| 103 |
is unnecessary, and produces errors on some |
| 104 |
.\" Reportedly, FreeBSD |
| 105 |
systems. |
| 106 |
.P |
| 107 |
The |
| 108 |
.BR mkstemps () |
| 109 |
function is like |
| 110 |
.BR mkstemp (), |
| 111 |
except that the string in |
| 112 |
.I template |
| 113 |
contains a suffix of |
| 114 |
.I suffixlen |
| 115 |
characters. |
| 116 |
Thus, |
| 117 |
.I template |
| 118 |
is of the form |
| 119 |
.IR "prefixXXXXXXsuffix" , |
| 120 |
and the string XXXXXX is modified as for |
| 121 |
.BR mkstemp (). |
| 122 |
.P |
| 123 |
The |
| 124 |
.BR mkostemps () |
| 125 |
function is to |
| 126 |
.BR mkstemps () |
| 127 |
as |
| 128 |
.BR mkostemp () |
| 129 |
is to |
| 130 |
.BR mkstemp (). |
| 131 |
.SH RETURN VALUE |
| 132 |
On success, these functions return the file descriptor |
| 133 |
of the temporary file. |
| 134 |
On error, \-1 is returned, and |
| 135 |
.I errno |
| 136 |
is set to indicate the error. |
| 137 |
.SH ERRORS |
| 138 |
.TP |
| 139 |
.B EEXIST |
| 140 |
Could not create a unique temporary filename. |
| 141 |
Now the contents of |
| 142 |
.I template |
| 143 |
are undefined. |
| 144 |
.TP |
| 145 |
.B EINVAL |
| 146 |
For |
| 147 |
.BR mkstemp () |
| 148 |
and |
| 149 |
.BR mkostemp (): |
| 150 |
The last six characters of |
| 151 |
.I template |
| 152 |
were not XXXXXX; |
| 153 |
now |
| 154 |
.I template |
| 155 |
is unchanged. |
| 156 |
.IP |
| 157 |
For |
| 158 |
.BR mkstemps () |
| 159 |
and |
| 160 |
.BR mkostemps (): |
| 161 |
.I template |
| 162 |
is less than |
| 163 |
.I "(6 + suffixlen)" |
| 164 |
characters long, or the last 6 characters before the suffix in |
| 165 |
.I template |
| 166 |
were not XXXXXX. |
| 167 |
.P |
| 168 |
These functions may also fail with any of the errors described for |
| 169 |
.BR open (2). |
| 170 |
.SH ATTRIBUTES |
| 171 |
For an explanation of the terms used in this section, see |
| 172 |
.BR attributes (7). |
| 173 |
.TS |
| 174 |
allbox; |
| 175 |
lbx lb lb |
| 176 |
l l l. |
| 177 |
Interface Attribute Value |
| 178 |
T{ |
| 179 |
.na |
| 180 |
.nh |
| 181 |
.BR mkstemp (), |
| 182 |
.BR mkostemp (), |
| 183 |
.BR mkstemps (), |
| 184 |
.BR mkostemps () |
| 185 |
T} Thread safety MT-Safe |
| 186 |
.TE |
| 187 |
.SH STANDARDS |
| 188 |
.TP |
| 189 |
.BR mkstemp () |
| 190 |
POSIX.1-2001. |
| 191 |
.TP |
| 192 |
.BR mkstemps () |
| 193 |
BSD. |
| 194 |
.\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris, |
| 195 |
.\" and Tru64. |
| 196 |
.TP |
| 197 |
.BR mkostemp () |
| 198 |
.TQ |
| 199 |
.BR mkostemps () |
| 200 |
GNU. |
| 201 |
.SH HISTORY |
| 202 |
.TP |
| 203 |
.BR mkstemp () |
| 204 |
4.3BSD, POSIX.1-2001. |
| 205 |
.TP |
| 206 |
.BR mkstemps () |
| 207 |
glibc 2.11. |
| 208 |
BSD, Mac OS X, Solaris, Tru64. |
| 209 |
.TP |
| 210 |
.BR mkostemp () |
| 211 |
glibc 2.7. |
| 212 |
.TP |
| 213 |
.BR mkostemps () |
| 214 |
glibc 2.11. |
| 215 |
.P |
| 216 |
In glibc versions 2.06 and earlier, the file is created with permissions 0666, |
| 217 |
that is, read and write for all users. |
| 218 |
This old behavior may be |
| 219 |
a security risk, especially since other UNIX flavors use 0600, |
| 220 |
and somebody might overlook this detail when porting programs. |
| 221 |
POSIX.1-2008 adds a requirement that the file be created with mode 0600. |
| 222 |
.P |
| 223 |
More generally, the POSIX specification of |
| 224 |
.BR mkstemp () |
| 225 |
does not say anything |
| 226 |
about file modes, so the application should make sure its |
| 227 |
file mode creation mask (see |
| 228 |
.BR umask (2)) |
| 229 |
is set appropriately before calling |
| 230 |
.BR mkstemp () |
| 231 |
(and |
| 232 |
.BR mkostemp ()). |
| 233 |
.\" |
| 234 |
.\" The prototype for |
| 235 |
.\" .BR mkstemp () |
| 236 |
.\" is in |
| 237 |
.\" .I <unistd.h> |
| 238 |
.\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in |
| 239 |
.\" .IR <stdlib.h> . |
| 240 |
.SH SEE ALSO |
| 241 |
.BR mkdtemp (3), |
| 242 |
.BR mktemp (3), |
| 243 |
.BR tempnam (3), |
| 244 |
.BR tmpfile (3), |
| 245 |
.BR tmpnam (3) |