]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mkstemp.3
fuse.4: fuse_entry_out: rework discussion of uniqueness of nodeid + generation
[thirdparty/man-pages.git] / man3 / mkstemp.3
CommitLineData
fea681da 1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
6cefc996 2.\" and Copyright (C) 2008, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
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
6cefc996 33.\" 2008-06-19, mtk, Added mkostemp(); various other changes
fea681da 34.\"
b8efb414 35.TH MKSTEMP 3 2016-10-08 "GNU" "Linux Programmer's Manual"
fea681da 36.SH NAME
3c8d6131 37mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file
fea681da
MK
38.SH SYNOPSIS
39.nf
40.B #include <stdlib.h>
41.sp
42.BI "int mkstemp(char *" template );
6cefc996 43.sp
264951bb 44.BI "int mkostemp(char *" template ", int " flags );
5f58cee8
MK
45.sp
46.BI "int mkstemps(char *" template ", int " suffixlen );
47.sp
48.BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
fea681da 49.fi
cc4615cc
MK
50.sp
51.in -4n
52Feature Test Macro Requirements for glibc (see
53.BR feature_test_macros (7)):
54.in
55.sp
56.BR mkstemp ():
5ef28585
MK
57.ad l
58.RS 4
59.PD 0
2b1b0424 60_XOPEN_SOURCE\ >=\ 500
cf7fa0a1 61.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
c6785202 62 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
2b1b0424 63 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
5ef28585
MK
64.PD
65.RE
66.ad b
67.PP
6cefc996
MK
68.BR mkostemp ():
69_GNU_SOURCE
5f58cee8
MK
70.br
71.BR mkstemps ():
2b1b0424
MK
72 /* Glibc since 2.19: */ _DEFAULT_SOURCE
73 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
5f58cee8
MK
74.br
75.BR mkostemps ():
76_GNU_SOURCE
fea681da 77.SH DESCRIPTION
60a90ecd
MK
78The
79.BR mkstemp ()
6cefc996
MK
80function generates a unique temporary filename from
81.IR template ,
82creates and opens the file,
83and returns an open file descriptor for the file.
84
85The last six characters of
86.I template
87must be "XXXXXX" and these are replaced with a string that makes the
c13182ef 88filename unique.
fea681da
MK
89Since it will be modified,
90.I template
91must not be a string constant, but should be declared as a character array.
6cefc996
MK
92
93The file is created with
94permissions 0600, that is, read plus write for owner only.
6cefc996 95The returned file descriptor provides both read and write access to the file.
c13182ef 96The file is opened with the
c00a8870 97.BR open (2)
c13182ef 98.B O_EXCL
6cefc996
MK
99flag, guaranteeing that the caller is the process that creates the file.
100
5f58cee8 101The
6cefc996 102.BR mkostemp ()
5f58cee8 103function is like
6cefc996 104.BR mkstemp (),
b87b8bd0
MK
105with the difference that the following bits\(emwith the same meaning as for
106.BR open (2)\(emmay
107be specified in
108.IR flags :
6cefc996 109.BR O_APPEND ,
b87b8bd0
MK
110.BR O_CLOEXEC ,
111and
112.BR O_SYNC .
113Note that when creating the file,
114.BR mkostemp ()
115includes the values
116.BR O_RDWR ,
117.BR O_CREAT ,
118and
119.BR O_EXCL
120in the
121.I flags
122argument given to
123.BR open (2);
124including these values in the
125.I flags
126argument given to
127.BR mkostemp ()
128is unnecessary, and produces errors on some
129.\" Reportedly, FreeBSD
130systems.
5f58cee8
MK
131
132The
133.BR mkstemps ()
134function is like
135.BR mkstemp (),
136except that the string in
137.I template
138contains a suffix of
139.I suffixlen
140characters.
141Thus,
142.I template
143is of the form
144.IR "prefixXXXXXXsuffix" ,
145and the string XXXXXX is modified as for
146.BR mkstemp ().
147
148The
149.BR mkostemps ()
150function is to
151.BR mkstemps ()
152as
153.BR mkostemp ()
154is to
155.BR mkstemp ().
47297adb 156.SH RETURN VALUE
6cefc996 157On success, these functions return the file descriptor
cca657e2
MK
158of the temporary file.
159On error, \-1 is returned, and
160.I errno
161is set appropriately.
fea681da
MK
162.SH ERRORS
163.TP
164.B EEXIST
165Could not create a unique temporary filename.
166Now the contents of \fItemplate\fP are undefined.
167.TP
168.B EINVAL
5f58cee8
MK
169For
170.BR mkstemp ()
171and
172.BR mkostemp ():
173The last six characters of \fItemplate\fP were not XXXXXX;
174now \fItemplate\fP is unchanged.
175.sp
176For
177.BR mkstemps ()
178and
e26674fe 179.BR mkostemps ():
5f58cee8
MK
180.I template
181is less than
182.I "(6 + suffixlen)"
183characters long, or the last 6 characters before the suffix in
184.I template
185were not XXXXXX.
6cefc996
MK
186.PP
187These functions may also fail with any of the errors described for
188.BR open (2).
189.SH VERSIONS
190.BR mkostemp ()
191is available since glibc 2.7.
5f58cee8
MK
192.BR mkstemps ()
193and
194.BR mkostemps ()
195are available since glibc 2.11.
09b46b1f 196.SH ATTRIBUTES
19db86e8
PH
197For an explanation of the terms used in this section, see
198.BR attributes (7).
199.TS
200allbox;
201lbw23 lb lb
202l l l.
203Interface Attribute Value
204T{
09b46b1f
PH
205.BR mkstemp (),
206.BR mkostemp (),
19db86e8 207.br
09b46b1f 208.BR mkstemps (),
09b46b1f 209.BR mkostemps ()
19db86e8
PH
210T} Thread safety MT-Safe
211.TE
47297adb 212.SH CONFORMING TO
6cefc996
MK
213.BR mkstemp ():
2144.3BSD, POSIX.1-2001.
5f58cee8
MK
215
216.BR mkstemps ():
217unstandardized, 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 ()
222and
223.BR mkostemps ():
224are glibc extensions.
fea681da 225.SH NOTES
35d740b9
MK
226In glibc versions 2.06 and earlier, the file is created with permissions 0666,
227that is, read and write for all users.
228This old behavior may be
008f1ecc 229a security risk, especially since other UNIX flavors use 0600,
fea681da 230and somebody might overlook this detail when porting programs.
35d740b9 231POSIX.1-2008 adds a requirement that the file be created with mode 0600.
fea681da 232
6cefc996
MK
233More generally, the POSIX specification of
234.BR mkstemp ()
235does not say anything
236about file modes, so the application should make sure its
237file mode creation mask (see
238.BR umask (2))
fea681da 239is set appropriately before calling
6cefc996
MK
240.BR mkstemp ()
241(and
242.BR mkostemp ()).
e38998b1
MK
243.\"
244.\" The prototype for
e4b153ac 245.\" .BR mkstemp ()
e38998b1
MK
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> .
47297adb 250.SH SEE ALSO
fea681da
MK
251.BR mkdtemp (3),
252.BR mktemp (3),
253.BR tempnam (3),
254.BR tmpfile (3),
255.BR tmpnam (3)