man-pages/man3/mkstemp.3.html
2021-03-31 01:06:50 +01:00

371 lines
7.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MKSTEMP</TITLE>
</HEAD><BODY>
<H1>MKSTEMP</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
mkstemp, mkostemp, mkstemps, mkostemps - create a unique temporary file
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>int mkstemp(char *</B><I>template</I><B>);</B>
<B>int mkostemp(char *</B><I>template</I><B>, int </B><I>flags</I><B>);</B>
<B>int mkstemps(char *</B><I>template</I><B>, int </B><I>suffixlen</I><B>);</B>
<B>int mkostemps(char *</B><I>template</I><B>, int </B><I>suffixlen</I><B>, int </B><I>flags</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>mkstemp</B>():
<DL COMPACT><DT id="1"><DD>
_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Since&nbsp;glibc&nbsp;2.12:&nbsp;*/&nbsp;_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_SVID_SOURCE&nbsp;||&nbsp;_BSD_SOURCE
</DL>
<P>
<B>mkostemp</B>():
_GNU_SOURCE
<BR>
<B>mkstemps</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Glibc&nbsp;since&nbsp;2.19:&nbsp;*/&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_SVID_SOURCE&nbsp;||&nbsp;_BSD_SOURCE
<BR>
<B>mkostemps</B>():
_GNU_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>mkstemp</B>()
function generates a unique temporary filename from
<I>template</I>,
creates and opens the file,
and returns an open file descriptor for the file.
<P>
The last six characters of
<I>template</I>
must be &quot;XXXXXX&quot; and these are replaced with a string that makes the
filename unique.
Since it will be modified,
<I>template</I>
must not be a string constant, but should be declared as a character array.
<P>
The file is created with
permissions 0600, that is, read plus write for owner only.
The returned file descriptor provides both read and write access to the file.
The file is opened with the
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
<B>O_EXCL</B>
flag, guaranteeing that the caller is the process that creates the file.
<P>
The
<B>mkostemp</B>()
function is like
<B>mkstemp</B>(),
with the difference that the following bits---with the same meaning as for
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)---may
be specified in
<I>flags</I>:
<B>O_APPEND</B>,
<B>O_CLOEXEC</B>,
and
<B>O_SYNC</B>.
Note that when creating the file,
<B>mkostemp</B>()
includes the values
<B>O_RDWR</B>,
<B>O_CREAT</B>,
and
<B>O_EXCL</B>
in the
<I>flags</I>
argument given to
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2);
including these values in the
<I>flags</I>
argument given to
<B>mkostemp</B>()
is unnecessary, and produces errors on some
systems.
<P>
The
<B>mkstemps</B>()
function is like
<B>mkstemp</B>(),
except that the string in
<I>template</I>
contains a suffix of
<I>suffixlen</I>
characters.
Thus,
<I>template</I>
is of the form
<I>prefixXXXXXXsuffix</I>,
and the string XXXXXX is modified as for
<B>mkstemp</B>().
<P>
The
<B>mkostemps</B>()
function is to
<B>mkstemps</B>()
as
<B>mkostemp</B>()
is to
<B>mkstemp</B>().
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return the file descriptor
of the temporary file.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="2"><B>EEXIST</B>
<DD>
Could not create a unique temporary filename.
Now the contents of <I>template</I> are undefined.
<DT id="3"><B>EINVAL</B>
<DD>
For
<B>mkstemp</B>()
and
<B>mkostemp</B>():
The last six characters of <I>template</I> were not XXXXXX;
now <I>template</I> is unchanged.
<DT id="4"><DD>
For
<B>mkstemps</B>()
and
<B>mkostemps</B>():
<I>template</I>
is less than
<I>(6 + suffixlen)</I>
characters long, or the last 6 characters before the suffix in
<I>template</I>
were not XXXXXX.
</DL>
<P>
These functions may also fail with any of the errors described for
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>mkostemp</B>()
is available since glibc 2.7.
<B>mkstemps</B>()
and
<B>mkostemps</B>()
are available since glibc 2.11.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>mkstemp</B>(),
<B>mkostemp</B>(),
<BR>
<B>mkstemps</B>(),
<B>mkostemps</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>mkstemp</B>():
4.3BSD, POSIX.1-2001.
<P>
<B>mkstemps</B>():
unstandardized, but appears on several other systems.
<P>
<B>mkostemp</B>()
and
<B>mkostemps</B>():
are glibc extensions.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
In glibc versions 2.06 and earlier, the file is created with permissions 0666,
that is, read and write for all users.
This old behavior may be
a security risk, especially since other UNIX flavors use 0600,
and somebody might overlook this detail when porting programs.
POSIX.1-2008 adds a requirement that the file be created with mode 0600.
<P>
More generally, the POSIX specification of
<B>mkstemp</B>()
does not say anything
about file modes, so the application should make sure its
file mode creation mask (see
<B><A HREF="/cgi-bin/man/man2html?2+umask">umask</A></B>(2))
is set appropriately before calling
<B>mkstemp</B>()
(and
<B>mkostemp</B>()).
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+mkdtemp">mkdtemp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mktemp">mktemp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+tempnam">tempnam</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+tmpfile">tmpfile</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+tmpnam">tmpnam</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="11"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="14"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:48 GMT, March 31, 2021
</BODY>
</HTML>