1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2008, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" References consulted:
25 .\" Linux libc source code
26 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" Modified Sat Jul 24 18:48:48 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified 980310, aeb
30 .\" Modified 990328, aeb
31 .\" 2008-06-19, mtk, Added mkostemp(); various other changes
33 .TH MKSTEMP 3 2010-09-20 "GNU" "Linux Programmer's Manual"
35 mkstemp, mkostemp \- create a unique temporary file
38 .B #include <stdlib.h>
40 .BI "int mkstemp(char *" template );
42 .BI "int mkostemp (char *" template ", int " flags );
44 .BI "int mkstemps(char *" template ", int " suffixlen );
46 .BI "int mkostemps(char *" template ", int " suffixlen ", int " flags );
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
60 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
61 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED ||
62 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
65 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
66 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
75 _BSD_SOURCE || _SVID_SOURCE
82 function generates a unique temporary filename from
84 creates and opens the file,
85 and returns an open file descriptor for the file.
87 The last six characters of
89 must be "XXXXXX" and these are replaced with a string that makes the
91 Since it will be modified,
93 must not be a string constant, but should be declared as a character array.
95 The file is created with
96 permissions 0600, that is, read plus write for owner only.
97 (In glibc versions 2.06 and earlier, the file is created with permissions 0666,
98 that is, read and write for all users.)
99 The returned file descriptor provides both read and write access to the file.
100 The file is opened with the
103 flag, guaranteeing that the caller is the process that creates the file.
109 with the difference that flags as for
121 except that the string in
129 .IR "prefixXXXXXXsuffix" ,
130 and the string XXXXXX is modified as for
142 On success, these functions return the file descriptor
143 of the temporary file.
144 On error, \-1 is returned, and
146 is set appropriately.
150 Could not create a unique temporary filename.
151 Now the contents of \fItemplate\fP are undefined.
158 The last six characters of \fItemplate\fP were not XXXXXX;
159 now \fItemplate\fP is unchanged.
168 characters long, or the last 6 characters before the suffix in
172 These functions may also fail with any of the errors described for
176 is available since glibc 2.7.
180 are available since glibc 2.11.
183 4.3BSD, POSIX.1-2001.
186 unstandardized, but appears on several other systems.
187 .\" mkstemps() appears to be at least on the BSDs, Mac OS X, Solaris,
193 are glibc extensions.
195 The old behavior of creating a file with mode 0666 may be
196 a security risk, especially since other Unix flavors use 0600,
197 and somebody might overlook this detail when porting programs.
199 More generally, the POSIX specification of
201 does not say anything
202 about file modes, so the application should make sure its
203 file mode creation mask (see
205 is set appropriately before calling
214 for libc4, libc5, glibc1; glibc2 follows POSIX.1 and has the prototype in