1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2008, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References consulted:
27 .\" Linux libc source code
28 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" Modified Sat Jul 24 18:48:48 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Modified 980310, aeb
32 .\" Modified 990328, aeb
33 .\" 2008-06-19, mtk, Added mkostemp(); various other changes
35 .TH MKSTEMP 3 2017-09-15 "GNU" "Linux Programmer's Manual"
37 mkstemp, mkostemp, mkstemps, mkostemps \- create a unique temporary file
40 .B #include <stdlib.h>
42 .BI "int mkstemp(char *" template );
44 .BI "int mkostemp(char *" template ", int " flags );
46 .BI "int mkstemps(char *" template ", int " suffixlen );
48 .BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
52 Feature Test Macro Requirements for glibc (see
53 .BR feature_test_macros (7)):
60 _XOPEN_SOURCE\ >=\ 500
61 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
62 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
63 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
72 /* Glibc since 2.19: */ _DEFAULT_SOURCE
73 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
80 function generates a unique temporary filename from
82 creates and opens the file,
83 and returns an open file descriptor for the file.
85 The last six characters of
87 must be "XXXXXX" and these are replaced with a string that makes the
89 Since it will be modified,
91 must not be a string constant, but should be declared as a character array.
93 The file is created with
94 permissions 0600, that is, read plus write for owner only.
95 The returned file descriptor provides both read and write access to the file.
96 The file is opened with the
99 flag, guaranteeing that the caller is the process that creates the file.
105 with the difference that the following bits\(emwith the same meaning as for
113 Note that when creating the file,
124 including these values in the
128 is unnecessary, and produces errors on some
129 .\" Reportedly, FreeBSD
136 except that the string in
144 .IR "prefixXXXXXXsuffix" ,
145 and the string XXXXXX is modified as for
157 On success, these functions return the file descriptor
158 of the temporary file.
159 On error, \-1 is returned, and
161 is set appropriately.
165 Could not create a unique temporary filename.
166 Now the contents of \fItemplate\fP are undefined.
173 The last six characters of \fItemplate\fP were not XXXXXX;
174 now \fItemplate\fP is unchanged.
183 characters long, or the last 6 characters before the suffix in
187 These functions may also fail with any of the errors described for
191 is available since glibc 2.7.
195 are available since glibc 2.11.
197 For an explanation of the terms used in this section, see
203 Interface Attribute Value
210 T} Thread safety MT-Safe
214 4.3BSD, POSIX.1-2001.
217 unstandardized, but appears on several other systems.
218 .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
224 are glibc extensions.
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
229 a security risk, especially since other UNIX flavors use 0600,
230 and somebody might overlook this detail when porting programs.
231 POSIX.1-2008 adds a requirement that the file be created with mode 0600.
233 More generally, the POSIX specification of
235 does not say anything
236 about file modes, so the application should make sure its
237 file mode creation mask (see
239 is set appropriately before calling
244 .\" The prototype for
248 .\" for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in