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