]>
Commit | Line | Data |
---|---|---|
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 | .\" |
4b8c67d9 | 35 | .TH MKSTEMP 3 2017-09-15 "GNU" "Linux Programmer's Manual" |
fea681da | 36 | .SH NAME |
3c8d6131 | 37 | mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file |
fea681da MK |
38 | .SH SYNOPSIS |
39 | .nf | |
40 | .B #include <stdlib.h> | |
68e4db0a | 41 | .PP |
fea681da | 42 | .BI "int mkstemp(char *" template ); |
68e4db0a | 43 | .PP |
264951bb | 44 | .BI "int mkostemp(char *" template ", int " flags ); |
68e4db0a | 45 | .PP |
5f58cee8 | 46 | .BI "int mkstemps(char *" template ", int " suffixlen ); |
68e4db0a | 47 | .PP |
5f58cee8 | 48 | .BI "int mkostemps(char *" template ", int " suffixlen ", int " flags ); |
fea681da | 49 | .fi |
68e4db0a | 50 | .PP |
cc4615cc MK |
51 | .in -4n |
52 | Feature Test Macro Requirements for glibc (see | |
53 | .BR feature_test_macros (7)): | |
54 | .in | |
68e4db0a | 55 | .PP |
cc4615cc | 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 |
78 | The |
79 | .BR mkstemp () | |
6cefc996 MK |
80 | function generates a unique temporary filename from |
81 | .IR template , | |
82 | creates and opens the file, | |
83 | and returns an open file descriptor for the file. | |
847e0d88 | 84 | .PP |
6cefc996 MK |
85 | The last six characters of |
86 | .I template | |
87 | must be "XXXXXX" and these are replaced with a string that makes the | |
c13182ef | 88 | filename unique. |
fea681da MK |
89 | Since it will be modified, |
90 | .I template | |
91 | must not be a string constant, but should be declared as a character array. | |
847e0d88 | 92 | .PP |
6cefc996 MK |
93 | The file is created with |
94 | permissions 0600, that is, read plus write for owner only. | |
6cefc996 | 95 | The returned file descriptor provides both read and write access to the file. |
c13182ef | 96 | The file is opened with the |
c00a8870 | 97 | .BR open (2) |
c13182ef | 98 | .B O_EXCL |
6cefc996 | 99 | flag, guaranteeing that the caller is the process that creates the file. |
847e0d88 | 100 | .PP |
5f58cee8 | 101 | The |
6cefc996 | 102 | .BR mkostemp () |
5f58cee8 | 103 | function is like |
6cefc996 | 104 | .BR mkstemp (), |
b87b8bd0 MK |
105 | with the difference that the following bits\(emwith the same meaning as for |
106 | .BR open (2)\(emmay | |
107 | be specified in | |
108 | .IR flags : | |
6cefc996 | 109 | .BR O_APPEND , |
b87b8bd0 MK |
110 | .BR O_CLOEXEC , |
111 | and | |
112 | .BR O_SYNC . | |
113 | Note that when creating the file, | |
114 | .BR mkostemp () | |
115 | includes the values | |
116 | .BR O_RDWR , | |
117 | .BR O_CREAT , | |
118 | and | |
119 | .BR O_EXCL | |
120 | in the | |
121 | .I flags | |
122 | argument given to | |
123 | .BR open (2); | |
124 | including these values in the | |
125 | .I flags | |
126 | argument given to | |
127 | .BR mkostemp () | |
128 | is unnecessary, and produces errors on some | |
129 | .\" Reportedly, FreeBSD | |
130 | systems. | |
847e0d88 | 131 | .PP |
5f58cee8 MK |
132 | The |
133 | .BR mkstemps () | |
134 | function is like | |
135 | .BR mkstemp (), | |
136 | except that the string in | |
137 | .I template | |
138 | contains a suffix of | |
139 | .I suffixlen | |
140 | characters. | |
141 | Thus, | |
142 | .I template | |
143 | is of the form | |
144 | .IR "prefixXXXXXXsuffix" , | |
145 | and the string XXXXXX is modified as for | |
146 | .BR mkstemp (). | |
847e0d88 | 147 | .PP |
5f58cee8 MK |
148 | The |
149 | .BR mkostemps () | |
150 | function is to | |
151 | .BR mkstemps () | |
152 | as | |
153 | .BR mkostemp () | |
154 | is to | |
155 | .BR mkstemp (). | |
47297adb | 156 | .SH RETURN VALUE |
6cefc996 | 157 | On success, these functions return the file descriptor |
cca657e2 MK |
158 | of the temporary file. |
159 | On error, \-1 is returned, and | |
160 | .I errno | |
161 | is set appropriately. | |
fea681da MK |
162 | .SH ERRORS |
163 | .TP | |
164 | .B EEXIST | |
165 | Could not create a unique temporary filename. | |
166 | Now the contents of \fItemplate\fP are undefined. | |
167 | .TP | |
168 | .B EINVAL | |
5f58cee8 MK |
169 | For |
170 | .BR mkstemp () | |
171 | and | |
172 | .BR mkostemp (): | |
173 | The last six characters of \fItemplate\fP were not XXXXXX; | |
174 | now \fItemplate\fP is unchanged. | |
bdd915e2 | 175 | .IP |
5f58cee8 MK |
176 | For |
177 | .BR mkstemps () | |
178 | and | |
e26674fe | 179 | .BR mkostemps (): |
5f58cee8 MK |
180 | .I template |
181 | is less than | |
182 | .I "(6 + suffixlen)" | |
183 | characters long, or the last 6 characters before the suffix in | |
184 | .I template | |
185 | were not XXXXXX. | |
6cefc996 MK |
186 | .PP |
187 | These functions may also fail with any of the errors described for | |
188 | .BR open (2). | |
189 | .SH VERSIONS | |
190 | .BR mkostemp () | |
191 | is available since glibc 2.7. | |
5f58cee8 MK |
192 | .BR mkstemps () |
193 | and | |
194 | .BR mkostemps () | |
195 | are available since glibc 2.11. | |
09b46b1f | 196 | .SH ATTRIBUTES |
19db86e8 PH |
197 | For an explanation of the terms used in this section, see |
198 | .BR attributes (7). | |
199 | .TS | |
200 | allbox; | |
201 | lbw23 lb lb | |
202 | l l l. | |
203 | Interface Attribute Value | |
204 | T{ | |
09b46b1f PH |
205 | .BR mkstemp (), |
206 | .BR mkostemp (), | |
19db86e8 | 207 | .br |
09b46b1f | 208 | .BR mkstemps (), |
09b46b1f | 209 | .BR mkostemps () |
19db86e8 PH |
210 | T} Thread safety MT-Safe |
211 | .TE | |
47297adb | 212 | .SH CONFORMING TO |
6cefc996 MK |
213 | .BR mkstemp (): |
214 | 4.3BSD, POSIX.1-2001. | |
847e0d88 | 215 | .PP |
5f58cee8 MK |
216 | .BR mkstemps (): |
217 | unstandardized, but appears on several other systems. | |
218 | .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris, | |
219 | .\" and Tru64. | |
847e0d88 | 220 | .PP |
5f58cee8 MK |
221 | .BR mkostemp () |
222 | and | |
223 | .BR mkostemps (): | |
224 | are glibc extensions. | |
fea681da | 225 | .SH NOTES |
35d740b9 MK |
226 | In glibc versions 2.06 and earlier, the file is created with permissions 0666, |
227 | that is, read and write for all users. | |
228 | This old behavior may be | |
008f1ecc | 229 | a security risk, especially since other UNIX flavors use 0600, |
fea681da | 230 | and somebody might overlook this detail when porting programs. |
35d740b9 | 231 | POSIX.1-2008 adds a requirement that the file be created with mode 0600. |
847e0d88 | 232 | .PP |
6cefc996 MK |
233 | More generally, the POSIX specification of |
234 | .BR mkstemp () | |
235 | does not say anything | |
236 | about file modes, so the application should make sure its | |
237 | file mode creation mask (see | |
238 | .BR umask (2)) | |
fea681da | 239 | is 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) |