1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2008, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
10 .\" Modified Sat Jul 24 18:48:48 1993 by Rik Faith (faith@cs.unc.edu)
11 .\" Modified 980310, aeb
12 .\" Modified 990328, aeb
13 .\" 2008-06-19, mtk, Added mkostemp(); various other changes
15 .TH mkstemp 3 (date) "Linux man-pages (unreleased)"
17 mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file
20 .RI ( libc ", " \-lc )
23 .B #include <stdlib.h>
25 .BI "int mkstemp(char *" template );
26 .BI "int mkostemp(char *" template ", int " flags );
27 .BI "int mkstemps(char *" template ", int " suffixlen );
28 .BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
32 Feature Test Macro Requirements for glibc (see
33 .BR feature_test_macros (7)):
39 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
40 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
41 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
51 /* Glibc since 2.19: */ _DEFAULT_SOURCE
52 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
62 function generates a unique temporary filename from
64 creates and opens the file,
65 and returns an open file descriptor for the file.
67 The last six characters of
69 must be "XXXXXX" and these are replaced with a string that makes the
71 Since it will be modified,
73 must not be a string constant, but should be declared as a character array.
75 The file is created with
76 permissions 0600, that is, read plus write for owner only.
77 The returned file descriptor provides both read and write access to the file.
78 The file is opened with the
81 flag, guaranteeing that the caller is the process that creates the file.
87 with the difference that the following bits\(emwith the same meaning as for
95 Note that when creating the file,
106 including these values in the
110 is unnecessary, and produces errors on some
111 .\" Reportedly, FreeBSD
118 except that the string in
126 .IR "prefixXXXXXXsuffix" ,
127 and the string XXXXXX is modified as for
139 On success, these functions return the file descriptor
140 of the temporary file.
141 On error, \-1 is returned, and
143 is set to indicate the error.
147 Could not create a unique temporary filename.
148 Now the contents of \fItemplate\fP are undefined.
155 The last six characters of \fItemplate\fP were not XXXXXX;
156 now \fItemplate\fP is unchanged.
165 characters long, or the last 6 characters before the suffix in
169 These functions may also fail with any of the errors described for
173 is available since glibc 2.7.
177 are available since glibc 2.11.
179 For an explanation of the terms used in this section, see
187 Interface Attribute Value
193 T} Thread safety MT-Safe
200 4.3BSD, POSIX.1-2001.
203 unstandardized, but appears on several other systems.
204 .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
210 are glibc extensions.
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
215 a security risk, especially since other UNIX flavors use 0600,
216 and somebody might overlook this detail when porting programs.
217 POSIX.1-2008 adds a requirement that the file be created with mode 0600.
219 More generally, the POSIX specification of
221 does not say anything
222 about file modes, so the application should make sure its
223 file mode creation mask (see
225 is set appropriately before calling
230 .\" The prototype for
234 .\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in