2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" and Copyright (C) 2008, Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" References consulted:
8 .\" Linux libc source code
9 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
11 .\" Modified Sat Jul 24 18:48:48 1993 by Rik Faith (faith@cs.unc.edu)
12 .\" Modified 980310, aeb
13 .\" Modified 990328, aeb
14 .\" 2008-06-19, mtk, Added mkostemp(); various other changes
16 .TH mkstemp 3 (date) "Linux man-pages (unreleased)"
18 mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file
21 .RI ( libc ", " \-lc )
24 .B #include <stdlib.h>
26 .BI "int mkstemp(char *" template );
27 .BI "int mkostemp(char *" template ", int " flags );
28 .BI "int mkstemps(char *" template ", int " suffixlen );
29 .BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
33 Feature Test Macro Requirements for glibc (see
34 .BR feature_test_macros (7)):
40 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
41 || /* glibc >= 2.12: */ _POSIX_C_SOURCE >= 200809L
42 || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
52 /* glibc >= 2.19: */ _DEFAULT_SOURCE
53 || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
63 function generates a unique temporary filename from
65 creates and opens the file,
66 and returns an open file descriptor for the file.
68 The last six characters of
70 must be "XXXXXX" and these are replaced with a string that makes the
72 Since it will be modified,
74 must not be a string constant, but should be declared as a character array.
76 The file is created with
77 permissions 0600, that is, read plus write for owner only.
78 The returned file descriptor provides both read and write access to the file.
79 The file is opened with the
82 flag, guaranteeing that the caller is the process that creates the file.
88 with the difference that the following bits\[em]with the same meaning as for
96 Note that when creating the file,
107 including these values in the
111 is unnecessary, and produces errors on some
112 .\" Reportedly, FreeBSD
119 except that the string in
127 .IR "prefixXXXXXXsuffix" ,
128 and the string XXXXXX is modified as for
140 On success, these functions return the file descriptor
141 of the temporary file.
142 On error, \-1 is returned, and
144 is set to indicate the error.
148 Could not create a unique temporary filename.
149 Now the contents of \fItemplate\fP are undefined.
156 The last six characters of \fItemplate\fP were not XXXXXX;
157 now \fItemplate\fP is unchanged.
166 characters long, or the last 6 characters before the suffix in
170 These functions may also fail with any of the errors described for
174 is available since glibc 2.7.
178 are available since glibc 2.11.
180 For an explanation of the terms used in this section, see
188 Interface Attribute Value
194 T} Thread safety MT-Safe
201 4.3BSD, POSIX.1-2001.
204 unstandardized, but appears on several other systems.
205 .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
211 are glibc extensions.
213 In glibc versions 2.06 and earlier, the file is created with permissions 0666,
214 that is, read and write for all users.
215 This old behavior may be
216 a security risk, especially since other UNIX flavors use 0600,
217 and somebody might overlook this detail when porting programs.
218 POSIX.1-2008 adds a requirement that the file be created with mode 0600.
220 More generally, the POSIX specification of
222 does not say anything
223 about file modes, so the application should make sure its
224 file mode creation mask (see
226 is set appropriately before calling
231 .\" The prototype for
235 .\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in