1MKDTEMP(3P)                POSIX Programmer's Manual               MKDTEMP(3P)
2
3
4

PROLOG

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

NAME

12       mkdtemp, mkstemp — create a unique directory or file
13

SYNOPSIS

15       #include <stdlib.h>
16
17       char *mkdtemp(char *template);
18       int mkstemp(char *template);
19

DESCRIPTION

21       The mkdtemp() function shall create a  directory  with  a  unique  name
22       derived  from  template.   The application shall ensure that the string
23       provided in template is a pathname ending with at  least  six  trailing
24       'X'  characters.  The  mkdtemp()  function shall modify the contents of
25       template by replacing six or more 'X' characters  at  the  end  of  the
26       pathname  with the same number of characters from the portable filename
27       character set. The characters shall be chosen such that  the  resulting
28       pathname does not duplicate the name of an existing file at the time of
29       the call to mkdtemp().  The mkdtemp() function shall use the  resulting
30       pathname to create the new directory as if by a call to:
31
32
33           mkdir(pathname, S_IRWXU)
34
35       The  mkstemp()  function shall create a regular file with a unique name
36       derived from template and return a file descriptor for  the  file  open
37       for  reading  and writing. The application shall ensure that the string
38       provided in template is a pathname ending with at  least  six  trailing
39       'X'  characters.  The  mkstemp()  function shall modify the contents of
40       template by replacing six or more 'X' characters  at  the  end  of  the
41       pathname  with the same number of characters from the portable filename
42       character set. The characters shall be chosen such that  the  resulting
43       pathname does not duplicate the name of an existing file at the time of
44       the call to mkstemp().  The mkstemp() function shall use the  resulting
45       pathname to create the file, and obtain a file descriptor for it, as if
46       by a call to:
47
48
49           open(pathname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR)
50
51       By behaving as if the O_EXCL flag for open() is set, the function  pre‐
52       vents  any  possible  race  condition  between testing whether the file
53       exists and opening it for use.
54

RETURN VALUE

56       Upon successful completion, the mkdtemp()  function  shall  return  the
57       value of template.  Otherwise, it shall return a null pointer and shall
58       set errno to indicate the error.
59
60       Upon successful completion, the mkstemp() function shall return an open
61       file  descriptor.  Otherwise, it shall return -1 and shall set errno to
62       indicate the error.
63

ERRORS

65       The mkdtemp() function shall fail if:
66
67       EACCES Search permission is denied on a component of the  path  prefix,
68              or  write  permission  is  denied on the parent directory of the
69              directory to be created.
70
71       EINVAL The string pointed to by template does not end in "XXXXXX".
72
73       ELOOP  A loop exists in symbolic links encountered during resolution of
74              the path of the directory to be created.
75
76       EMLINK The link count of the parent directory would exceed {LINK_MAX}.
77
78       ENAMETOOLONG
79              The  length  of  a  component  of  a  pathname  is  longer  than
80              {NAME_MAX}.
81
82       ENOENT A component of the path prefix specified by the  template  argu‐
83              ment does not name an existing directory.
84
85       ENOSPC The  file  system does not contain enough space to hold the con‐
86              tents of the new directory or to extend the parent directory  of
87              the new directory.
88
89       ENOTDIR
90              A  component  of  the path prefix names an existing file that is
91              neither a directory nor a symbolic link to a directory.
92
93       EROFS  The parent directory resides on a read-only file system.
94
95       The mkdtemp() function may fail if:
96
97       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
98              resolution of the path of the directory to be created.
99
100       ENAMETOOLONG
101              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
102              tion of a symbolic link produced an intermediate result  with  a
103              length that exceeds {PATH_MAX}.
104
105       The error conditions for the mkstemp() function are defined in open().
106
107       The following sections are informative.
108

EXAMPLES

110   Generating a Pathname
111       The following example creates a file with a 10-character name beginning
112       with the characters "file" and opens the file for reading and  writing.
113       The value returned as the value of fd is a file descriptor that identi‐
114       fies the file.
115
116
117           #include <stdlib.h>
118           ...
119           char template[] = "/tmp/fileXXXXXX";
120           int fd;
121
122           fd = mkstemp(template);
123

APPLICATION USAGE

125       It is possible to run out of letters.
126
127       Portable applications should pass exactly six trailing 'X's in the tem‐
128       plate  and  no  more; implementations may treat any additional trailing
129       'X's as either a fixed or replaceable part of the template. To be  sure
130       of  only  passing six, a fixed string of at least one non-'X' character
131       should precede the six 'X's.
132
133       Since 'X' is in the  portable  filename  character  set,  some  of  the
134       replacement  characters  can be 'X's, leaving part (or even all) of the
135       template effectively unchanged.
136

RATIONALE

138       None.
139

FUTURE DIRECTIONS

141       None.
142

SEE ALSO

144       getpid(), mkdir(), open(), tmpfile(), tmpnam()
145
146       The Base Definitions volume of POSIX.1‐2017, <stdlib.h>
147
149       Portions of this text are reprinted and reproduced in  electronic  form
150       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
151       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
152       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
153       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
154       event of any discrepancy between this version and the original IEEE and
155       The Open Group Standard, the original IEEE and The Open Group  Standard
156       is  the  referee document. The original Standard can be obtained online
157       at http://www.opengroup.org/unix/online.html .
158
159       Any typographical or formatting errors that appear  in  this  page  are
160       most likely to have been introduced during the conversion of the source
161       files to man page format. To report such errors,  see  https://www.ker
162       nel.org/doc/man-pages/reporting_bugs.html .
163
164
165
166IEEE/The Open Group                  2017                          MKDTEMP(3P)
Impressum