1MKSTEMP(3) Linux Programmer's Manual MKSTEMP(3)
2
3
4
6 mkstemp, mkostemp - 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 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 mkstemp(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
18 mkostemp(): _GNU_SOURCE
19
21 The mkstemp() function generates a unique temporary filename from tem‐
22 plate, creates and opens the file, and returns an open file descriptor
23 for the file.
24
25 The last six characters of template must be "XXXXXX" and these are
26 replaced with a string that makes the filename unique. Since it will
27 be modified, template must not be a string constant, but should be
28 declared as a character array.
29
30 The file is created with permissions 0600, that is, read plus write for
31 owner only. (In glibc versions 2.06 and earlier, the file is created
32 with permissions 0666, that is, read and write for all users.) The
33 returned file descriptor provides both read and write access to the
34 file. The file is opened with the open(2) O_EXCL flag, guaranteeing
35 that the caller is the process that creates the file.
36
37 mkostemp() is like mkstemp(), with the difference that flags as for
38 open(2) may be specified in flags (e.g., O_APPEND, O_SYNC).
39
41 On success, these functions return the file descriptor of the temporary
42 file. On error, -1 is returned, and errno is set appropriately.
43
45 EEXIST Could not create a unique temporary filename. Now the contents
46 of template are undefined.
47
48 EINVAL The last six characters of template were not XXXXXX. Now tem‐
49 plate is unchanged.
50
51 These functions may also fail with any of the errors described for
52 open(2).
53
55 mkostemp() is available since glibc 2.7.
56
58 mkstemp(): 4.3BSD, POSIX.1-2001. mkostemp(): is a glibc extension.
59
61 The old behavior of creating a file with mode 0666 may be a security
62 risk, especially since other Unix flavors use 0600, and somebody might
63 overlook this detail when porting programs.
64
65 More generally, the POSIX specification of mkstemp() does not say any‐
66 thing about file modes, so the application should make sure its file
67 mode creation mask (see umask(2)) is set appropriately before calling
68 mkstemp() (and mkostemp()).
69
70 The prototype for mktemp() is in <unistd.h> for libc4, libc5, glibc1;
71 glibc2 follows POSIX.1 and has the prototype in <stdlib.h>.
72
74 mkdtemp(3), mktemp(3), tempnam(3), tmpfile(3), tmpnam(3)
75
77 This page is part of release 3.22 of the Linux man-pages project. A
78 description of the project, and information about reporting bugs, can
79 be found at http://www.kernel.org/doc/man-pages/.
80
81
82
83GNU 2008-06-19 MKSTEMP(3)