OSDN Git Service

84b474b3357c591b7db651df483116ba4585a604
[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 .\" 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-26 "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 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
59 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
60 .br
61 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200112L
62 .PD
63 .RE
64 .ad b
65 .PP
66 .BR mkostemp ():
67 _GNU_SOURCE
68 .br
69 .BR mkstemps ():
70 _BSD_SOURCE || _SVID_SOURCE
71 .br
72 .BR mkostemps ():
73 _GNU_SOURCE
74 .SH DESCRIPTION
75 The
76 .BR mkstemp ()
77 function generates a unique temporary filename from
78 .IR template ,
79 creates and opens the file,
80 and returns an open file descriptor for the file.
81
82 The last six characters of
83 .I template
84 must be "XXXXXX" and these are replaced with a string that makes the
85 filename unique.
86 Since it will be modified,
87 .I template
88 must not be a string constant, but should be declared as a character array.
89
90 The file is created with
91 permissions 0600, that is, read plus write for owner only.
92 (In glibc versions 2.06 and earlier, the file is created with permissions 0666,
93 that is, read and write for all users.)
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 flags as for
105 .BR open (2)
106 may be specified in
107 .IR flags
108 (e.g.,
109 .BR O_APPEND ,
110 .BR O_SYNC ).
111
112 The
113 .BR mkstemps ()
114 function is like
115 .BR mkstemp (),
116 except that the string in
117 .I template
118 contains a suffix of
119 .I suffixlen
120 characters.
121 Thus,
122 .I template
123 is of the form
124 .IR "prefixXXXXXXsuffix" ,
125 and the string XXXXXX is modified as for
126 .BR mkstemp ().
127
128 The
129 .BR mkostemps ()
130 function is to
131 .BR mkstemps ()
132 as
133 .BR mkostemp ()
134 is to
135 .BR mkstemp ().
136 .SH "RETURN VALUE"
137 On success, these functions return the file descriptor
138 of the temporary file.
139 On error, \-1 is returned, and
140 .I errno
141 is set appropriately.
142 .SH ERRORS
143 .TP
144 .B EEXIST
145 Could not create a unique temporary filename.
146 Now the contents of \fItemplate\fP are undefined.
147 .TP
148 .B EINVAL
149 For
150 .BR mkstemp ()
151 and
152 .BR mkostemp ():
153 The last six characters of \fItemplate\fP were not XXXXXX;
154 now \fItemplate\fP is unchanged.
155 .sp
156 For
157 .BR mkstemps ()
158 and
159 .BR mkostemps ()
160 .I template
161 is less than
162 .I "(6 + suffixlen)"
163 characters long, or the last 6 characters before the suffix in
164 .I template
165 were not XXXXXX.
166 .PP
167 These functions may also fail with any of the errors described for
168 .BR open (2).
169 .SH VERSIONS
170 .BR mkostemp ()
171 is available since glibc 2.7.
172 .BR mkstemps ()
173 and
174 .BR mkostemps ()
175 are available since glibc 2.11.
176 .SH "CONFORMING TO"
177 .BR mkstemp ():
178 4.3BSD, POSIX.1-2001.
179
180 .BR mkstemps ():
181 unstandardized, but appears on several other systems.
182 .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
183 .\" and Tru64.
184
185 .BR mkostemp ()
186 and
187 .BR mkostemps ():
188 are glibc extensions.
189 .SH NOTES
190 The old behavior of creating a file with mode 0666 may be
191 a security risk, especially since other UNIX flavors use 0600,
192 and somebody might overlook this detail when porting programs.
193
194 More generally, the POSIX specification of
195 .BR mkstemp ()
196 does not say anything
197 about file modes, so the application should make sure its
198 file mode creation mask (see
199 .BR umask (2))
200 is set appropriately before calling
201 .BR mkstemp ()
202 (and
203 .BR mkostemp ()).
204
205 The prototype for
206 .BR mktemp ()
207 is in
208 .I <unistd.h>
209 for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in
210 .IR <stdlib.h> .
211 .SH "SEE ALSO"
212 .BR mkdtemp (3),
213 .BR mktemp (3),
214 .BR tempnam (3),
215 .BR tmpfile (3),
216 .BR tmpnam (3)