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 int mkostemp(char *template, int flags);
13 int mkstemps(char *template, int suffixlen);
14 int mkostemps(char *template, int suffixlen, int flags);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 mkstemp():
19 _XOPEN_SOURCE >= 500
20 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
21 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
22
23 mkostemp():
24 _GNU_SOURCE
25
26 mkstemps():
27 /* Glibc since 2.19: */ _DEFAULT_SOURCE
28 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
29
30 mkostemps():
31 _GNU_SOURCE
32
34 The mkstemp() function generates a unique temporary filename from tem‐
35 plate, creates and opens the file, and returns an open file descriptor
36 for the file.
37
38 The last six characters of template must be "XXXXXX" and these are re‐
39 placed with a string that makes the filename unique. Since it will be
40 modified, template must not be a string constant, but should be de‐
41 clared as a character array.
42
43 The file is created with permissions 0600, that is, read plus write for
44 owner only. The returned file descriptor provides both read and write
45 access to the file. The file is opened with the open(2) O_EXCL flag,
46 guaranteeing that the caller is the process that creates the file.
47
48 The mkostemp() function is like mkstemp(), with the difference that the
49 following bits—with the same meaning as for open(2)—may be specified in
50 flags: O_APPEND, O_CLOEXEC, and O_SYNC. Note that when creating the
51 file, mkostemp() includes the values O_RDWR, O_CREAT, and O_EXCL in the
52 flags argument given to open(2); including these values in the flags
53 argument given to mkostemp() is unnecessary, and produces errors on
54 some systems.
55
56 The mkstemps() function is like mkstemp(), except that the string in
57 template contains a suffix of suffixlen characters. Thus, template is
58 of the form prefixXXXXXXsuffix, and the string XXXXXX is modified as
59 for mkstemp().
60
61 The mkostemps() function is to mkstemps() as mkostemp() is to mk‐
62 stemp().
63
65 On success, these functions return the file descriptor of the temporary
66 file. On error, -1 is returned, and errno is set to indicate the er‐
67 ror.
68
70 EEXIST Could not create a unique temporary filename. Now the contents
71 of template are undefined.
72
73 EINVAL For mkstemp() and mkostemp(): The last six characters of tem‐
74 plate were not XXXXXX; now template is unchanged.
75
76 For mkstemps() and mkostemps(): template is less than (6 + suf‐
77 fixlen) characters long, or the last 6 characters before the
78 suffix in template were not XXXXXX.
79
80 These functions may also fail with any of the errors described for
81 open(2).
82
84 mkostemp() is available since glibc 2.7. mkstemps() and mkostemps()
85 are available since glibc 2.11.
86
88 For an explanation of the terms used in this section, see at‐
89 tributes(7).
90
91 ┌────────────────────────────────────────────┬───────────────┬─────────┐
92 │Interface │ Attribute │ Value │
93 ├────────────────────────────────────────────┼───────────────┼─────────┤
94 │mkstemp(), mkostemp(), mkstemps(), │ Thread safety │ MT-Safe │
95 │mkostemps() │ │ │
96 └────────────────────────────────────────────┴───────────────┴─────────┘
97
99 mkstemp(): 4.3BSD, POSIX.1-2001.
100
101 mkstemps(): unstandardized, but appears on several other systems.
102
103 mkostemp() and mkostemps(): are glibc extensions.
104
106 In glibc versions 2.06 and earlier, the file is created with permis‐
107 sions 0666, that is, read and write for all users. This old behavior
108 may be a security risk, especially since other UNIX flavors use 0600,
109 and somebody might overlook this detail when porting programs.
110 POSIX.1-2008 adds a requirement that the file be created with mode
111 0600.
112
113 More generally, the POSIX specification of mkstemp() does not say any‐
114 thing about file modes, so the application should make sure its file
115 mode creation mask (see umask(2)) is set appropriately before calling
116 mkstemp() (and mkostemp()).
117
119 mkdtemp(3), mktemp(3), tempnam(3), tmpfile(3), tmpnam(3)
120
122 This page is part of release 5.12 of the Linux man-pages project. A
123 description of the project, information about reporting bugs, and the
124 latest version of this page, can be found at
125 https://www.kernel.org/doc/man-pages/.
126
127
128
129GNU 2021-03-22 MKSTEMP(3)