MKDTEMP(3)                 POSIX Programmer's Manual                MKDTEMP(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       mkdtemp, mkstemp -- create a unique directory or file

SSYYNNOOPPSSIISS
       #include <stdlib.h>

       char *mkdtemp(char *_t_e_m_p_l_a_t_e);
       int mkstemp(char *_t_e_m_p_l_a_t_e);

DDEESSCCRRIIPPTTIIOONN
       The _m_k_d_t_e_m_p() function shall create a directory with a unique name
       derived from _t_e_m_p_l_a_t_e.  The application shall ensure that the string
       provided in _t_e_m_p_l_a_t_e is a pathname ending with at least six trailing
       ''XX'' characters. The _m_k_d_t_e_m_p() function shall modify the contents of
       _t_e_m_p_l_a_t_e by replacing six or more ''XX'' characters at the end of the
       pathname with the same number of characters from the portable filename
       character set. The characters shall be chosen such that the resulting
       pathname does not duplicate the name of an existing file at the time of
       the call to _m_k_d_t_e_m_p().  The _m_k_d_t_e_m_p() function shall use the resulting
       pathname to create the new directory as if by a call to:


           mkdir(pathname, S_IRWXU)

       The _m_k_s_t_e_m_p() function shall create a regular file with a unique name
       derived from _t_e_m_p_l_a_t_e and return a file descriptor for the file open
       for reading and writing. The application shall ensure that the string
       provided in _t_e_m_p_l_a_t_e is a pathname ending with at least six trailing
       ''XX'' characters. The _m_k_s_t_e_m_p() function shall modify the contents of
       _t_e_m_p_l_a_t_e by replacing six or more ''XX'' characters at the end of the
       pathname with the same number of characters from the portable filename
       character set. The characters shall be chosen such that the resulting
       pathname does not duplicate the name of an existing file at the time of
       the call to _m_k_s_t_e_m_p().  The _m_k_s_t_e_m_p() function shall use the resulting
       pathname to create the file, and obtain a file descriptor for it, as if
       by a call to:


           open(pathname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR)

       By behaving as if the O_EXCL flag for _o_p_e_n() is set, the function
       prevents any possible race condition between testing whether the file
       exists and opening it for use.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _m_k_d_t_e_m_p() function shall return the
       value of _t_e_m_p_l_a_t_e.  Otherwise, it shall return a null pointer and shall
       set _e_r_r_n_o to indicate the error.

       Upon successful completion, the _m_k_s_t_e_m_p() function shall return an open
       file descriptor. Otherwise, it shall return -1 and shall set _e_r_r_n_o to
       indicate the error.

EERRRROORRSS
       The _m_k_d_t_e_m_p() function shall fail if:

       EEAACCCCEESS Search permission is denied on a component of the path prefix,
              or write permission is denied on the parent directory of the
              directory to be created.

       EEIINNVVAALL The string pointed to by _t_e_m_p_l_a_t_e does not end in ""XXXXXXXXXXXX"".

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the path of the directory to be created.

       EEMMLLIINNKK The link count of the parent directory would exceed {LINK_MAX}.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of the path prefix specified by the _t_e_m_p_l_a_t_e
              argument does not name an existing directory.

       EENNOOSSPPCC The file system does not contain enough space to hold the
              contents of the new directory or to extend the parent directory
              of the new directory.

       EENNOOTTDDIIRR
              A component of the path prefix names an existing file that is
              neither a directory nor a symbolic link to a directory.

       EERROOFFSS  The parent directory resides on a read-only file system.

       The _m_k_d_t_e_m_p() function may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the path of the directory to be created.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       The error conditions for the _m_k_s_t_e_m_p() function are defined in _o_p_e_n().

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   GGeenneerraattiinngg aa PPaatthhnnaammee
       The following example creates a file with a 10-character name beginning
       with the characters ""ffiillee"" and opens the file for reading and writing.
       The value returned as the value of _f_d is a file descriptor that
       identifies the file.


           #include <stdlib.h>
           ...
           char template[] = "/tmp/fileXXXXXX";
           int fd;

           fd = mkstemp(template);

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       It is possible to run out of letters.

       Portable applications should pass exactly six trailing ''XX''s in the
       template and no more; implementations may treat any additional trailing
       ''XX''s as either a fixed or replaceable part of the template. To be sure
       of only passing six, a fixed string of at least one non-''XX'' character
       should precede the six ''XX''s.

       Since ''XX'' is in the portable filename character set, some of the
       replacement characters can be ''XX''s, leaving part (or even all) of the
       template effectively unchanged.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _g_e_t_p_i_d(), _m_k_d_i_r(), _o_p_e_n(), _t_m_p_f_i_l_e(), _t_m_p_n_a_m()

       The Base Definitions volume of POSIX.1-2017, <<ssttddlliibb..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                           MKDTEMP(3)
