summaryrefslogtreecommitdiffstats
path: root/man3/mkstemp.3
blob: 76223dcd7272c10e90abe61a2fee69f67a2c40ef (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\" and Copyright (C) 2008, Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\" References consulted:
.\"     Linux libc source code
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
.\"     386BSD man pages
.\" Modified Sat Jul 24 18:48:48 1993 by Rik Faith (faith@cs.unc.edu)
.\" Modified 980310, aeb
.\" Modified 990328, aeb
.\" 2008-06-19, mtk, Added mkostemp(); various other changes
.\"
.TH MKSTEMP 3  2017-09-15 "GNU" "Linux Programmer's Manual"
.SH NAME
mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file
.SH SYNOPSIS
.nf
.B #include <stdlib.h>
.PP
.BI "int mkstemp(char *" template );
.PP
.BI "int mkostemp(char *" template ", int " flags );
.PP
.BI "int mkstemps(char *" template ", int " suffixlen );
.PP
.BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
.fi
.PP
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.PP
.BR mkstemp ():
.ad l
.RS 4
.PD 0
_XOPEN_SOURCE\ >=\ 500
.\"    || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
    || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
    || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
.PD
.RE
.ad b
.PP
.BR mkostemp ():
_GNU_SOURCE
.br
.BR mkstemps ():
    /* Glibc since 2.19: */ _DEFAULT_SOURCE
        || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
.br
.BR mkostemps ():
_GNU_SOURCE
.SH DESCRIPTION
The
.BR mkstemp ()
function generates a unique temporary filename from
.IR template ,
creates and opens the file,
and returns an open file descriptor for the file.
.PP
The last six characters of
.I template
must be "XXXXXX" and these are replaced with a string that makes the
filename unique.
Since it will be modified,
.I template
must not be a string constant, but should be declared as a character array.
.PP
The file is created with
permissions 0600, that is, read plus write for owner only.
The returned file descriptor provides both read and write access to the file.
The file is opened with the
.BR open (2)
.B O_EXCL
flag, guaranteeing that the caller is the process that creates the file.
.PP
The
.BR mkostemp ()
function is like
.BR mkstemp (),
with the difference that the following bits\(emwith the same meaning as for
.BR open (2)\(emmay
be specified in
.IR flags :
.BR O_APPEND ,
.BR O_CLOEXEC ,
and
.BR O_SYNC .
Note that when creating the file,
.BR mkostemp ()
includes the values
.BR O_RDWR ,
.BR O_CREAT ,
and
.BR O_EXCL
in the
.I flags
argument given to
.BR open (2);
including these values in the
.I flags
argument given to
.BR mkostemp ()
is unnecessary, and produces errors on some
.\" Reportedly, FreeBSD
systems.
.PP
The
.BR mkstemps ()
function is like
.BR mkstemp (),
except that the string in
.I template
contains a suffix of
.I suffixlen
characters.
Thus,
.I template
is of the form
.IR "prefixXXXXXXsuffix" ,
and the string XXXXXX is modified as for
.BR mkstemp ().
.PP
The
.BR mkostemps ()
function is to
.BR mkstemps ()
as
.BR mkostemp ()
is to
.BR mkstemp ().
.SH RETURN VALUE
On success, these functions return the file descriptor
of the temporary file.
On error, \-1 is returned, and
.I errno
is set appropriately.
.SH ERRORS
.TP
.B EEXIST
Could not create a unique temporary filename.
Now the contents of \fItemplate\fP are undefined.
.TP
.B EINVAL
For
.BR mkstemp ()
and
.BR mkostemp ():
The last six characters of \fItemplate\fP were not XXXXXX;
now \fItemplate\fP is unchanged.
.IP
For
.BR mkstemps ()
and
.BR mkostemps ():
.I template
is less than
.I "(6 + suffixlen)"
characters long, or the last 6 characters before the suffix in
.I template
were not XXXXXX.
.PP
These functions may also fail with any of the errors described for
.BR open (2).
.SH VERSIONS
.BR mkostemp ()
is available since glibc 2.7.
.BR mkstemps ()
and
.BR mkostemps ()
are available since glibc 2.11.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbw23 lb lb
l l l.
Interface	Attribute	Value
T{
.BR mkstemp (),
.BR mkostemp (),
.br
.BR mkstemps (),
.BR mkostemps ()
T}	Thread safety	MT-Safe
.TE
.SH CONFORMING TO
.BR mkstemp ():
4.3BSD, POSIX.1-2001.
.PP
.BR mkstemps ():
unstandardized, but appears on several other systems.
.\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
.\" and Tru64.
.PP
.BR mkostemp ()
and
.BR mkostemps ():
are glibc extensions.
.SH NOTES
In glibc versions 2.06 and earlier, the file is created with permissions 0666,
that is, read and write for all users.
This old behavior may be
a security risk, especially since other UNIX flavors use 0600,
and somebody might overlook this detail when porting programs.
POSIX.1-2008 adds a requirement that the file be created with mode 0600.
.PP
More generally, the POSIX specification of
.BR mkstemp ()
does not say anything
about file modes, so the application should make sure its
file mode creation mask (see
.BR umask (2))
is set appropriately before calling
.BR mkstemp ()
(and
.BR mkostemp ()).
.\"
.\" The prototype for
.\" .BR mkstemp ()
.\" is in
.\" .I <unistd.h>
.\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in
.\" .IR <stdlib.h> .
.SH SEE ALSO
.BR mkdtemp (3),
.BR mktemp (3),
.BR tempnam (3),
.BR tmpfile (3),
.BR tmpnam (3)
.SH COLOPHON
This page is part of release 5.10 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.