OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / 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  2014-08-19 "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 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
61 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
62 .br
63 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200112L
64 .PD
65 .RE
66 .ad b
67 .PP
68 .BR mkostemp ():
69 _GNU_SOURCE
70 .br
71 .BR mkstemps ():
72 _BSD_SOURCE || _SVID_SOURCE
73 .br
74 .BR mkostemps ():
75 _GNU_SOURCE
76 .SH DESCRIPTION
77 The
78 .BR mkstemp ()
79 function generates a unique temporary filename from
80 .IR template ,
81 creates and opens the file,
82 and returns an open file descriptor for the file.
83
84 The last six characters of
85 .I template
86 must be "XXXXXX" and these are replaced with a string that makes the
87 filename unique.
88 Since it will be modified,
89 .I template
90 must not be a string constant, but should be declared as a character array.
91
92 The file is created with
93 permissions 0600, that is, read plus write for owner only.
94 The returned file descriptor provides both read and write access to the file.
95 The file is opened with the
96 .BR open (2)
97 .B O_EXCL
98 flag, guaranteeing that the caller is the process that creates the file.
99
100 The
101 .BR mkostemp ()
102 function is like
103 .BR mkstemp (),
104 with the difference that the following bits\(emwith the same meaning as for
105 .BR open (2)\(emmay
106 be specified in
107 .IR flags :
108 .BR O_APPEND ,
109 .BR O_CLOEXEC ,
110 and
111 .BR O_SYNC .
112 Note that when creating the file,
113 .BR mkostemp ()
114 includes the values
115 .BR O_RDWR ,
116 .BR O_CREAT ,
117 and
118 .BR O_EXCL
119 in the
120 .I flags
121 argument given to
122 .BR open (2);
123 including these values in the
124 .I flags
125 argument given to
126 .BR mkostemp ()
127 is unnecessary, and produces errors on some
128 .\" Reportedly, FreeBSD
129 systems.
130
131 The
132 .BR mkstemps ()
133 function is like
134 .BR mkstemp (),
135 except that the string in
136 .I template
137 contains a suffix of
138 .I suffixlen
139 characters.
140 Thus,
141 .I template
142 is of the form
143 .IR "prefixXXXXXXsuffix" ,
144 and the string XXXXXX is modified as for
145 .BR mkstemp ().
146
147 The
148 .BR mkostemps ()
149 function is to
150 .BR mkstemps ()
151 as
152 .BR mkostemp ()
153 is to
154 .BR mkstemp ().
155 .SH RETURN VALUE
156 On success, these functions return the file descriptor
157 of the temporary file.
158 On error, \-1 is returned, and
159 .I errno
160 is set appropriately.
161 .SH ERRORS
162 .TP
163 .B EEXIST
164 Could not create a unique temporary filename.
165 Now the contents of \fItemplate\fP are undefined.
166 .TP
167 .B EINVAL
168 For
169 .BR mkstemp ()
170 and
171 .BR mkostemp ():
172 The last six characters of \fItemplate\fP were not XXXXXX;
173 now \fItemplate\fP is unchanged.
174 .sp
175 For
176 .BR mkstemps ()
177 and
178 .BR mkostemps ():
179 .I template
180 is less than
181 .I "(6 + suffixlen)"
182 characters long, or the last 6 characters before the suffix in
183 .I template
184 were not XXXXXX.
185 .PP
186 These functions may also fail with any of the errors described for
187 .BR open (2).
188 .SH VERSIONS
189 .BR mkostemp ()
190 is available since glibc 2.7.
191 .BR mkstemps ()
192 and
193 .BR mkostemps ()
194 are available since glibc 2.11.
195 .SH ATTRIBUTES
196 .SS Multithreading (see pthreads(7))
197 The
198 .BR mkstemp (),
199 .BR mkostemp (),
200 .BR mkstemps (),
201 and
202 .BR mkostemps ()
203 functions are thread-safe.
204 .SH CONFORMING TO
205 .BR mkstemp ():
206 4.3BSD, POSIX.1-2001.
207
208 .BR mkstemps ():
209 unstandardized, but appears on several other systems.
210 .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
211 .\" and Tru64.
212
213 .BR mkostemp ()
214 and
215 .BR mkostemps ():
216 are glibc extensions.
217 .SH NOTES
218 In glibc versions 2.06 and earlier, the file is created with permissions 0666,
219 that is, read and write for all users.
220 This old behavior may be
221 a security risk, especially since other UNIX flavors use 0600,
222 and somebody might overlook this detail when porting programs.
223 POSIX.1-2008 adds a requirement that the file be created with mode 0600.
224
225 More generally, the POSIX specification of
226 .BR mkstemp ()
227 does not say anything
228 about file modes, so the application should make sure its
229 file mode creation mask (see
230 .BR umask (2))
231 is set appropriately before calling
232 .BR mkstemp ()
233 (and
234 .BR mkostemp ()).
235 .\"
236 .\" The prototype for
237 .\" .BR mktemp ()
238 .\" is in
239 .\" .I <unistd.h>
240 .\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in
241 .\" .IR <stdlib.h> .
242 .SH SEE ALSO
243 .BR mkdtemp (3),
244 .BR mktemp (3),
245 .BR tempnam (3),
246 .BR tmpfile (3),
247 .BR tmpnam (3)
248 .SH COLOPHON
249 This page is part of release 3.79 of the Linux
250 .I man-pages
251 project.
252 A description of the project,
253 information about reporting bugs,
254 and the latest version of this page,
255 can be found at
256 \%http://www.kernel.org/doc/man\-pages/.