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