]>
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 | .\" |
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 | 17 | mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file |
058eee04 AC |
18 | .SH LIBRARY |
19 | Standard 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 |
32 | Feature 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 |
60 | The |
61 | .BR mkstemp () | |
6cefc996 MK |
62 | function generates a unique temporary filename from |
63 | .IR template , | |
64 | creates and opens the file, | |
65 | and returns an open file descriptor for the file. | |
847e0d88 | 66 | .PP |
6cefc996 MK |
67 | The last six characters of |
68 | .I template | |
69 | must be "XXXXXX" and these are replaced with a string that makes the | |
c13182ef | 70 | filename unique. |
fea681da MK |
71 | Since it will be modified, |
72 | .I template | |
73 | must not be a string constant, but should be declared as a character array. | |
847e0d88 | 74 | .PP |
6cefc996 MK |
75 | The file is created with |
76 | permissions 0600, that is, read plus write for owner only. | |
6cefc996 | 77 | The returned file descriptor provides both read and write access to the file. |
c13182ef | 78 | The file is opened with the |
c00a8870 | 79 | .BR open (2) |
c13182ef | 80 | .B O_EXCL |
6cefc996 | 81 | flag, guaranteeing that the caller is the process that creates the file. |
847e0d88 | 82 | .PP |
5f58cee8 | 83 | The |
6cefc996 | 84 | .BR mkostemp () |
5f58cee8 | 85 | function is like |
6cefc996 | 86 | .BR mkstemp (), |
b87b8bd0 MK |
87 | with the difference that the following bits\(emwith the same meaning as for |
88 | .BR open (2)\(emmay | |
89 | be specified in | |
90 | .IR flags : | |
6cefc996 | 91 | .BR O_APPEND , |
b87b8bd0 MK |
92 | .BR O_CLOEXEC , |
93 | and | |
94 | .BR O_SYNC . | |
95 | Note that when creating the file, | |
96 | .BR mkostemp () | |
97 | includes the values | |
98 | .BR O_RDWR , | |
99 | .BR O_CREAT , | |
100 | and | |
1ae6b2c7 | 101 | .B O_EXCL |
b87b8bd0 MK |
102 | in the |
103 | .I flags | |
104 | argument given to | |
105 | .BR open (2); | |
106 | including these values in the | |
107 | .I flags | |
108 | argument given to | |
109 | .BR mkostemp () | |
110 | is unnecessary, and produces errors on some | |
111 | .\" Reportedly, FreeBSD | |
112 | systems. | |
847e0d88 | 113 | .PP |
5f58cee8 MK |
114 | The |
115 | .BR mkstemps () | |
116 | function is like | |
117 | .BR mkstemp (), | |
118 | except that the string in | |
119 | .I template | |
120 | contains a suffix of | |
121 | .I suffixlen | |
122 | characters. | |
123 | Thus, | |
124 | .I template | |
125 | is of the form | |
126 | .IR "prefixXXXXXXsuffix" , | |
127 | and the string XXXXXX is modified as for | |
128 | .BR mkstemp (). | |
847e0d88 | 129 | .PP |
5f58cee8 MK |
130 | The |
131 | .BR mkostemps () | |
132 | function is to | |
133 | .BR mkstemps () | |
134 | as | |
135 | .BR mkostemp () | |
136 | is to | |
137 | .BR mkstemp (). | |
47297adb | 138 | .SH RETURN VALUE |
6cefc996 | 139 | On success, these functions return the file descriptor |
cca657e2 MK |
140 | of the temporary file. |
141 | On error, \-1 is returned, and | |
142 | .I errno | |
f6a4078b | 143 | is set to indicate the error. |
fea681da MK |
144 | .SH ERRORS |
145 | .TP | |
146 | .B EEXIST | |
147 | Could not create a unique temporary filename. | |
148 | Now the contents of \fItemplate\fP are undefined. | |
149 | .TP | |
150 | .B EINVAL | |
5f58cee8 MK |
151 | For |
152 | .BR mkstemp () | |
153 | and | |
154 | .BR mkostemp (): | |
155 | The last six characters of \fItemplate\fP were not XXXXXX; | |
156 | now \fItemplate\fP is unchanged. | |
bdd915e2 | 157 | .IP |
5f58cee8 MK |
158 | For |
159 | .BR mkstemps () | |
160 | and | |
e26674fe | 161 | .BR mkostemps (): |
5f58cee8 MK |
162 | .I template |
163 | is less than | |
164 | .I "(6 + suffixlen)" | |
165 | characters long, or the last 6 characters before the suffix in | |
166 | .I template | |
167 | were not XXXXXX. | |
6cefc996 MK |
168 | .PP |
169 | These functions may also fail with any of the errors described for | |
170 | .BR open (2). | |
171 | .SH VERSIONS | |
172 | .BR mkostemp () | |
173 | is available since glibc 2.7. | |
5f58cee8 MK |
174 | .BR mkstemps () |
175 | and | |
176 | .BR mkostemps () | |
177 | are available since glibc 2.11. | |
09b46b1f | 178 | .SH ATTRIBUTES |
19db86e8 PH |
179 | For 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 |
184 | allbox; | |
c466875e | 185 | lbx lb lb |
19db86e8 PH |
186 | l l l. |
187 | Interface Attribute Value | |
188 | T{ | |
09b46b1f PH |
189 | .BR mkstemp (), |
190 | .BR mkostemp (), | |
191 | .BR mkstemps (), | |
09b46b1f | 192 | .BR mkostemps () |
19db86e8 PH |
193 | T} Thread safety MT-Safe |
194 | .TE | |
c466875e MK |
195 | .hy |
196 | .ad | |
197 | .sp 1 | |
3113c7f3 | 198 | .SH STANDARDS |
6cefc996 MK |
199 | .BR mkstemp (): |
200 | 4.3BSD, POSIX.1-2001. | |
847e0d88 | 201 | .PP |
5f58cee8 MK |
202 | .BR mkstemps (): |
203 | unstandardized, 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 () |
208 | and | |
209 | .BR mkostemps (): | |
210 | are glibc extensions. | |
fea681da | 211 | .SH NOTES |
35d740b9 MK |
212 | In glibc versions 2.06 and earlier, the file is created with permissions 0666, |
213 | that is, read and write for all users. | |
214 | This old behavior may be | |
008f1ecc | 215 | a security risk, especially since other UNIX flavors use 0600, |
fea681da | 216 | and somebody might overlook this detail when porting programs. |
35d740b9 | 217 | POSIX.1-2008 adds a requirement that the file be created with mode 0600. |
847e0d88 | 218 | .PP |
6cefc996 MK |
219 | More generally, the POSIX specification of |
220 | .BR mkstemp () | |
221 | does not say anything | |
222 | about file modes, so the application should make sure its | |
223 | file mode creation mask (see | |
224 | .BR umask (2)) | |
fea681da | 225 | is 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) |