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

281 lines
6.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of TEMPNAM</TITLE>
</HEAD><BODY>
<H1>TEMPNAM</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>
tempnam - create a name for a temporary file
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>char *tempnam(const char *</B><I>dir</I><B>, const char *</B><I>pfx</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>tempnam</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.19:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.19&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_BSD_SOURCE&nbsp;||&nbsp;_SVID_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<I>Never use this function.</I>
Use
<B><A HREF="/cgi-bin/man/man2html?3+mkstemp">mkstemp</A></B>(3)
or
<B><A HREF="/cgi-bin/man/man2html?3+tmpfile">tmpfile</A></B>(3)
instead.
<P>
The
<B>tempnam</B>()
function returns a pointer to a string that is a valid filename,
and such that a file with this name did not exist when
<B>tempnam</B>()
checked.
The filename suffix of the pathname generated will start with
<I>pfx</I>
in case
<I>pfx</I>
is a non-NULL string of at most five bytes.
The directory prefix part of the pathname generated is required to
be &quot;appropriate&quot; (often that at least implies writable).
<P>
Attempts to find an appropriate directory go through the following
steps:
<DL COMPACT>
<DT id="1">a)<DD>
In case the environment variable
<B>TMPDIR</B>
exists and
contains the name of an appropriate directory, that is used.
<DT id="2">b)<DD>
Otherwise, if the
<I>dir</I>
argument is non-NULL and appropriate, it is used.
<DT id="3">c)<DD>
Otherwise,
<I>P_tmpdir</I>
(as defined in
<I>&lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</I>)
is used when appropriate.
<DT id="4">d)<DD>
Finally an implementation-defined directory may be used.
</DL>
<P>
The string returned by
<B>tempnam</B>()
is allocated using
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
and hence should be freed by
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, the
<B>tempnam</B>()
function returns a pointer to a unique temporary filename.
It returns NULL if a unique name cannot be generated, with
<I>errno</I>
set to indicate the cause of the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="5"><B>ENOMEM</B>
<DD>
Allocation of storage failed.
</DL>
<A NAME="lbAG">&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>tempnam</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe env<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
SVr4, 4.3BSD, POSIX.1-2001.
POSIX.1-2008 marks
<B>tempnam</B>()
as obsolete.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Although
<B>tempnam</B>()
generates names that are difficult to guess,
it is nevertheless possible that between the time that
<B>tempnam</B>()
returns a pathname, and the time that the program opens it,
another program might create that pathname using
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
or create it as a symbolic link.
This can lead to security holes.
To avoid such possibilities, use the
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
<B>O_EXCL</B>
flag to open the pathname.
Or better yet, use
<B><A HREF="/cgi-bin/man/man2html?3+mkstemp">mkstemp</A></B>(3)
or
<B><A HREF="/cgi-bin/man/man2html?3+tmpfile">tmpfile</A></B>(3).
<P>
SUSv2 does not mention the use of
<B>TMPDIR</B>;
glibc will use it only
when the program is not set-user-ID.
On SVr4, the directory used under <B>d)</B> is
<I>/tmp</I>
(and this is what glibc does).
<P>
Because it dynamically allocates memory used to return the pathname,
<B>tempnam</B>()
is reentrant, and thus thread safe, unlike
<B><A HREF="/cgi-bin/man/man2html?3+tmpnam">tmpnam</A></B>(3).
<P>
The
<B>tempnam</B>()
function generates a different string each time it is called,
up to
<B>TMP_MAX</B>
(defined in
<I>&lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</I>)
times.
If it is called more than
<B>TMP_MAX</B>
times,
the behavior is implementation defined.
<P>
<B>tempnam</B>()
uses at most the first five bytes from
<I>pfx</I>.
<P>
The glibc implementation of
<B>tempnam</B>()
fails with the error
<B>EEXIST</B>
upon failure to find a unique name.
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
The precise meaning of &quot;appropriate&quot; is undefined;
it is unspecified how accessibility of a directory is determined.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+mkstemp">mkstemp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mktemp">mktemp</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="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAI">NOTES</A><DD>
<DT id="14"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="15"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="16"><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:58 GMT, March 31, 2021
</BODY>
</HTML>