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