]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mkstemp.3
Many pages: Use correct letter case in page titles (TH)
[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.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
5.\"
6.\" References consulted:
7.\" Linux libc source code
8.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9.\" 386BSD man pages
10.\" Modified Sat Jul 24 18:48:48 1993 by Rik Faith (faith@cs.unc.edu)
11.\" Modified 980310, aeb
12.\" Modified 990328, aeb
6cefc996 13.\" 2008-06-19, mtk, Added mkostemp(); various other changes
fea681da 14.\"
4c1c5274 15.TH mkstemp 3 (date) "Linux man-pages (unreleased)"
fea681da 16.SH NAME
3c8d6131 17mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file
058eee04
AC
18.SH LIBRARY
19Standard C library
8fc3b2cf 20.RI ( libc ", " \-lc )
fea681da
MK
21.SH SYNOPSIS
22.nf
23.B #include <stdlib.h>
68e4db0a 24.PP
fea681da 25.BI "int mkstemp(char *" template );
264951bb 26.BI "int mkostemp(char *" template ", int " flags );
5f58cee8 27.BI "int mkstemps(char *" template ", int " suffixlen );
5f58cee8 28.BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
fea681da 29.fi
68e4db0a 30.PP
d39ad78f 31.RS -4
cc4615cc
MK
32Feature Test Macro Requirements for glibc (see
33.BR feature_test_macros (7)):
d39ad78f 34.RE
68e4db0a 35.PP
cc4615cc 36.BR mkstemp ():
9d2adbae 37.nf
5c10d2c5
MK
38 _XOPEN_SOURCE >= 500
39.\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
40 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
9d2adbae
MK
41 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
42.fi
5ef28585 43.PP
6cefc996 44.BR mkostemp ():
9d2adbae
MK
45.nf
46 _GNU_SOURCE
47.fi
98c9347c 48.PP
5f58cee8 49.BR mkstemps ():
9d2adbae 50.nf
2b1b0424 51 /* Glibc since 2.19: */ _DEFAULT_SOURCE
7d8d2f09 52 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
9d2adbae 53.fi
98c9347c 54.PP
5f58cee8 55.BR mkostemps ():
9d2adbae
MK
56.nf
57 _GNU_SOURCE
58.fi
fea681da 59.SH DESCRIPTION
60a90ecd
MK
60The
61.BR mkstemp ()
6cefc996
MK
62function generates a unique temporary filename from
63.IR template ,
64creates and opens the file,
65and returns an open file descriptor for the file.
847e0d88 66.PP
6cefc996
MK
67The last six characters of
68.I template
69must be "XXXXXX" and these are replaced with a string that makes the
c13182ef 70filename unique.
fea681da
MK
71Since it will be modified,
72.I template
73must not be a string constant, but should be declared as a character array.
847e0d88 74.PP
6cefc996
MK
75The file is created with
76permissions 0600, that is, read plus write for owner only.
6cefc996 77The returned file descriptor provides both read and write access to the file.
c13182ef 78The file is opened with the
c00a8870 79.BR open (2)
c13182ef 80.B O_EXCL
6cefc996 81flag, guaranteeing that the caller is the process that creates the file.
847e0d88 82.PP
5f58cee8 83The
6cefc996 84.BR mkostemp ()
5f58cee8 85function is like
6cefc996 86.BR mkstemp (),
b87b8bd0
MK
87with the difference that the following bits\(emwith the same meaning as for
88.BR open (2)\(emmay
89be specified in
90.IR flags :
6cefc996 91.BR O_APPEND ,
b87b8bd0
MK
92.BR O_CLOEXEC ,
93and
94.BR O_SYNC .
95Note that when creating the file,
96.BR mkostemp ()
97includes the values
98.BR O_RDWR ,
99.BR O_CREAT ,
100and
1ae6b2c7 101.B O_EXCL
b87b8bd0
MK
102in the
103.I flags
104argument given to
105.BR open (2);
106including these values in the
107.I flags
108argument given to
109.BR mkostemp ()
110is unnecessary, and produces errors on some
111.\" Reportedly, FreeBSD
112systems.
847e0d88 113.PP
5f58cee8
MK
114The
115.BR mkstemps ()
116function is like
117.BR mkstemp (),
118except that the string in
119.I template
120contains a suffix of
121.I suffixlen
122characters.
123Thus,
124.I template
125is of the form
126.IR "prefixXXXXXXsuffix" ,
127and the string XXXXXX is modified as for
128.BR mkstemp ().
847e0d88 129.PP
5f58cee8
MK
130The
131.BR mkostemps ()
132function is to
133.BR mkstemps ()
134as
135.BR mkostemp ()
136is to
137.BR mkstemp ().
47297adb 138.SH RETURN VALUE
6cefc996 139On success, these functions return the file descriptor
cca657e2
MK
140of the temporary file.
141On error, \-1 is returned, and
142.I errno
f6a4078b 143is set to indicate the error.
fea681da
MK
144.SH ERRORS
145.TP
146.B EEXIST
147Could not create a unique temporary filename.
148Now the contents of \fItemplate\fP are undefined.
149.TP
150.B EINVAL
5f58cee8
MK
151For
152.BR mkstemp ()
153and
154.BR mkostemp ():
155The last six characters of \fItemplate\fP were not XXXXXX;
156now \fItemplate\fP is unchanged.
bdd915e2 157.IP
5f58cee8
MK
158For
159.BR mkstemps ()
160and
e26674fe 161.BR mkostemps ():
5f58cee8
MK
162.I template
163is less than
164.I "(6 + suffixlen)"
165characters long, or the last 6 characters before the suffix in
166.I template
167were not XXXXXX.
6cefc996
MK
168.PP
169These functions may also fail with any of the errors described for
170.BR open (2).
171.SH VERSIONS
172.BR mkostemp ()
173is available since glibc 2.7.
5f58cee8
MK
174.BR mkstemps ()
175and
176.BR mkostemps ()
177are available since glibc 2.11.
09b46b1f 178.SH ATTRIBUTES
19db86e8
PH
179For an explanation of the terms used in this section, see
180.BR attributes (7).
c466875e
MK
181.ad l
182.nh
19db86e8
PH
183.TS
184allbox;
c466875e 185lbx lb lb
19db86e8
PH
186l l l.
187Interface Attribute Value
188T{
09b46b1f
PH
189.BR mkstemp (),
190.BR mkostemp (),
191.BR mkstemps (),
09b46b1f 192.BR mkostemps ()
19db86e8
PH
193T} Thread safety MT-Safe
194.TE
c466875e
MK
195.hy
196.ad
197.sp 1
3113c7f3 198.SH STANDARDS
6cefc996
MK
199.BR mkstemp ():
2004.3BSD, POSIX.1-2001.
847e0d88 201.PP
5f58cee8
MK
202.BR mkstemps ():
203unstandardized, but appears on several other systems.
204.\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
205.\" and Tru64.
847e0d88 206.PP
5f58cee8
MK
207.BR mkostemp ()
208and
209.BR mkostemps ():
210are glibc extensions.
fea681da 211.SH NOTES
35d740b9
MK
212In glibc versions 2.06 and earlier, the file is created with permissions 0666,
213that is, read and write for all users.
214This old behavior may be
008f1ecc 215a security risk, especially since other UNIX flavors use 0600,
fea681da 216and somebody might overlook this detail when porting programs.
35d740b9 217POSIX.1-2008 adds a requirement that the file be created with mode 0600.
847e0d88 218.PP
6cefc996
MK
219More generally, the POSIX specification of
220.BR mkstemp ()
221does not say anything
222about file modes, so the application should make sure its
223file mode creation mask (see
224.BR umask (2))
fea681da 225is set appropriately before calling
6cefc996
MK
226.BR mkstemp ()
227(and
228.BR mkostemp ()).
e38998b1
MK
229.\"
230.\" The prototype for
e4b153ac 231.\" .BR mkstemp ()
e38998b1
MK
232.\" is in
233.\" .I <unistd.h>
234.\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in
235.\" .IR <stdlib.h> .
47297adb 236.SH SEE ALSO
fea681da
MK
237.BR mkdtemp (3),
238.BR mktemp (3),
239.BR tempnam (3),
240.BR tmpfile (3),
241.BR tmpnam (3)