1MKDTEMP(3P) POSIX Programmer's Manual MKDTEMP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 mkdtemp, mkstemp — create a unique directory or file
14
16 #include <stdlib.h>
17
18 char *mkdtemp(char *template);
19 int mkstemp(char *template);
20
22 The mkdtemp() function uses the contents of template to construct a
23 unique directory name. The string provided in template shall be a path‐
24 name ending with six trailing 'X's. The mkdtemp() function shall
25 replace each 'X' with a character from the portable filename character
26 set. The characters are chosen such that the resulting name does not
27 duplicate the name of an existing file at the time of a call to
28 mkdtemp(). The unique directory name is used to attempt to create the
29 directory using mode 0700 as modified by the file creation mask.
30
31 The mkstemp() function shall replace the contents of the string pointed
32 to by template by a unique pathname, and return a file descriptor for
33 the file open for reading and writing. The mkstemp() function shall
34 create the file, and obtain a file descriptor for it, as if by a call
35 to:
36
37 open(pathname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR)
38
39 The function thus prevents any possible race condition between testing
40 whether the file exists and opening it for use. The string in template
41 should look like a pathname with six trailing 'X's; mkstemp() replaces
42 each 'X' with a character from the portable filename character set. The
43 characters are chosen such that the resulting name does not duplicate
44 the name of an existing file at the time of a call to mkstemp().
45
47 Upon successful completion, the mkdtemp() function shall return a
48 pointer to the string containing the directory name if it was created.
49 Otherwise, it shall return a null pointer and shall set errno to indi‐
50 cate the error.
51
52 Upon successful completion, the mkstemp() function shall return an open
53 file descriptor. Otherwise, it shall return −1 if no suitable file
54 could be created.
55
57 The mkdtemp() function shall fail if:
58
59 EACCES Search permission is denied on a component of the path prefix,
60 or write permission is denied on the parent directory of the
61 directory to be created.
62
63 EINVAL The string pointed to by template does not end in "XXXXXX".
64
65 ELOOP A loop exists in symbolic links encountered during resolution of
66 the path of the directory to be created.
67
68 EMLINK The link count of the parent directory would exceed {LINK_MAX}.
69
70 ENAMETOOLONG
71 The length of a component of a pathname is longer than
72 {NAME_MAX}.
73
74 ENOENT A component of the path prefix specified by the template argu‐
75 ment does not name an existing directory.
76
77 ENOSPC The file system does not contain enough space to hold the con‐
78 tents of the new directory or to extend the parent directory of
79 the new directory.
80
81 ENOTDIR
82 A component of the path prefix names an existing file that is
83 neither a directory nor a symbolic link to a directory.
84
85 EROFS The parent directory resides on a read-only file system.
86
87 The mkdtemp() function may fail if:
88
89 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
90 resolution of the path of the directory to be created.
91
92 ENAMETOOLONG
93 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
94 tion of a symbolic link produced an intermediate result with a
95 length that exceeds {PATH_MAX}.
96
97 The error conditions for the mkstemp() function are defined in open().
98
99 The following sections are informative.
100
102 Generating a Pathname
103 The following example creates a file with a 10-character name beginning
104 with the characters "file" and opens the file for reading and writing.
105 The value returned as the value of fd is a file descriptor that identi‐
106 fies the file.
107
108 #include <stdlib.h>
109 ...
110 char template[] = "/tmp/fileXXXXXX";
111 int fd;
112
113 fd = mkstemp(template);
114
116 It is possible to run out of letters.
117
118 The mkdtemp() and mkstemp() functions need not check to determine
119 whether the filename part of template exceeds the maximum allowable
120 filename length.
121
123 None.
124
126 None.
127
129 getpid(), mkdir(), open(), tmpfile(), tmpnam()
130
131 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
132
134 Portions of this text are reprinted and reproduced in electronic form
135 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
136 -- Portable Operating System Interface (POSIX), The Open Group Base
137 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
138 cal and Electronics Engineers, Inc and The Open Group. (This is
139 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
140 event of any discrepancy between this version and the original IEEE and
141 The Open Group Standard, the original IEEE and The Open Group Standard
142 is the referee document. The original Standard can be obtained online
143 at http://www.unix.org/online.html .
144
145 Any typographical or formatting errors that appear in this page are
146 most likely to have been introduced during the conversion of the source
147 files to man page format. To report such errors, see https://www.ker‐
148 nel.org/doc/man-pages/reporting_bugs.html .
149
150
151
152IEEE/The Open Group 2013 MKDTEMP(3P)