1MKSTEMP(3) Linux Programmer's Manual MKSTEMP(3)
2
3
4
6 mkstemp, mkostemp, mkstemps, mkostemps - create a unique temporary file
7
9 #include <stdlib.h>
10
11 int mkstemp(char *template);
12
13 int mkostemp(char *template, int flags);
14
15 int mkstemps(char *template, int suffixlen);
16
17 int mkostemps(char *template, int suffixlen, int flags);
18
19 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21 mkstemp():
22 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500 ||
23 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
24 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200112L
25
26 mkostemp(): _GNU_SOURCE
27 mkstemps(): _BSD_SOURCE || _SVID_SOURCE
28 mkostemps(): _GNU_SOURCE
29
31 The mkstemp() function generates a unique temporary filename from tem‐
32 plate, creates and opens the file, and returns an open file descriptor
33 for the file.
34
35 The last six characters of template must be "XXXXXX" and these are
36 replaced with a string that makes the filename unique. Since it will
37 be modified, template must not be a string constant, but should be
38 declared as a character array.
39
40 The file is created with permissions 0600, that is, read plus write for
41 owner only. The returned file descriptor provides both read and write
42 access to the file. The file is opened with the open(2) O_EXCL flag,
43 guaranteeing that the caller is the process that creates the file.
44
45 The mkostemp() function is like mkstemp(), with the difference that
46 flags as for open(2) may be specified in flags (e.g., O_APPEND,
47 O_SYNC).
48
49 The mkstemps() function is like mkstemp(), except that the string in
50 template contains a suffix of suffixlen characters. Thus, template is
51 of the form prefixXXXXXXsuffix, and the string XXXXXX is modified as
52 for mkstemp().
53
54 The mkostemps() function is to mkstemps() as mkostemp() is to
55 mkstemp().
56
58 On success, these functions return the file descriptor of the temporary
59 file. On error, -1 is returned, and errno is set appropriately.
60
62 EEXIST Could not create a unique temporary filename. Now the contents
63 of template are undefined.
64
65 EINVAL For mkstemp() and mkostemp(): The last six characters of tem‐
66 plate were not XXXXXX; now template is unchanged.
67
68 For mkstemps() and mkostemps(): template is less than (6 + suf‐
69 fixlen) characters long, or the last 6 characters before the
70 suffix in template were not XXXXXX.
71
72 These functions may also fail with any of the errors described for
73 open(2).
74
76 mkostemp() is available since glibc 2.7. mkstemps() and mkostemps()
77 are available since glibc 2.11.
78
80 mkstemp(): 4.3BSD, POSIX.1-2001.
81
82 mkstemps(): unstandardized, but appears on several other systems.
83
84 mkostemp() and mkostemps(): are glibc extensions.
85
87 In glibc versions 2.06 and earlier, the file is created with permis‐
88 sions 0666, that is, read and write for all users. This old behavior
89 may be a security risk, especially since other UNIX flavors use 0600,
90 and somebody might overlook this detail when porting programs.
91 POSIX.1-2008 adds a requirement that the file be created with mode
92 0600.
93
94 More generally, the POSIX specification of mkstemp() does not say any‐
95 thing about file modes, so the application should make sure its file
96 mode creation mask (see umask(2)) is set appropriately before calling
97 mkstemp() (and mkostemp()).
98
99 The prototype for mktemp() is in <unistd.h> for libc4, libc5, glibc1;
100 glibc2 follows POSIX.1 and has the prototype in <stdlib.h>.
101
103 mkdtemp(3), mktemp(3), tempnam(3), tmpfile(3), tmpnam(3)
104
106 This page is part of release 3.53 of the Linux man-pages project. A
107 description of the project, and information about reporting bugs, can
108 be found at http://www.kernel.org/doc/man-pages/.
109
110
111
112GNU 2012-12-21 MKSTEMP(3)