Blame man3/tempnam.3

Packit 7cfc04
.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH TEMPNAM 3  2017-09-15 "" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
tempnam \- create a name for a temporary file
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <stdio.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *tempnam(const char *" dir ", const char *" pfx );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR tempnam ():
Packit 7cfc04
    Since glibc 2.19:
Packit 7cfc04
        _DEFAULT_SOURCE
Packit 7cfc04
    Glibc 2.19 and earlier:
Packit 7cfc04
        _BSD_SOURCE || _SVID_SOURCE
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.I "Never use this function."
Packit 7cfc04
Use
Packit 7cfc04
.BR mkstemp (3)
Packit 7cfc04
or
Packit 7cfc04
.BR tmpfile (3)
Packit 7cfc04
instead.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
function returns a pointer to a string that is a valid filename,
Packit 7cfc04
and such that a file with this name did not exist when
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
checked.
Packit 7cfc04
The filename suffix of the pathname generated will start with
Packit 7cfc04
.I pfx
Packit 7cfc04
in case
Packit 7cfc04
.I pfx
Packit 7cfc04
is a non-NULL string of at most five bytes.
Packit 7cfc04
The directory prefix part of the pathname generated is required to
Packit 7cfc04
be "appropriate" (often that at least implies writable).
Packit 7cfc04
.PP
Packit 7cfc04
Attempts to find an appropriate directory go through the following
Packit 7cfc04
steps:
Packit 7cfc04
.TP 3
Packit 7cfc04
a)
Packit 7cfc04
In case the environment variable
Packit 7cfc04
.B TMPDIR
Packit 7cfc04
exists and
Packit 7cfc04
contains the name of an appropriate directory, that is used.
Packit 7cfc04
.TP
Packit 7cfc04
b)
Packit 7cfc04
Otherwise, if the
Packit 7cfc04
.I dir
Packit 7cfc04
argument is non-NULL and appropriate, it is used.
Packit 7cfc04
.TP
Packit 7cfc04
c)
Packit 7cfc04
Otherwise,
Packit 7cfc04
.I P_tmpdir
Packit 7cfc04
(as defined in
Packit 7cfc04
.IR <stdio.h> )
Packit 7cfc04
is used when appropriate.
Packit 7cfc04
.TP
Packit 7cfc04
d)
Packit 7cfc04
Finally an implementation-defined directory may be used.
Packit 7cfc04
.PP
Packit 7cfc04
The string returned by
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
is allocated using
Packit 7cfc04
.BR malloc (3)
Packit 7cfc04
and hence should be freed by
Packit 7cfc04
.BR free (3).
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, the
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
function returns a pointer to a unique temporary filename.
Packit 7cfc04
It returns NULL if a unique name cannot be generated, with
Packit 7cfc04
.I errno
Packit 7cfc04
set to indicate the cause of the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Allocation of storage failed.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
T}	Thread safety	MT-Safe env
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
SVr4, 4.3BSD, POSIX.1-2001.
Packit 7cfc04
POSIX.1-2008 marks
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
as obsolete.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Although
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
generates names that are difficult to guess,
Packit 7cfc04
it is nevertheless possible that between the time that
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
returns a pathname, and the time that the program opens it,
Packit 7cfc04
another program might create that pathname using
Packit 7cfc04
.BR open (2),
Packit 7cfc04
or create it as a symbolic link.
Packit 7cfc04
This can lead to security holes.
Packit 7cfc04
To avoid such possibilities, use the
Packit 7cfc04
.BR open (2)
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
flag to open the pathname.
Packit 7cfc04
Or better yet, use
Packit 7cfc04
.BR mkstemp (3)
Packit 7cfc04
or
Packit 7cfc04
.BR tmpfile (3).
Packit 7cfc04
.PP
Packit 7cfc04
SUSv2 does not mention the use of
Packit 7cfc04
.BR TMPDIR ;
Packit 7cfc04
glibc will use it only
Packit 7cfc04
when the program is not set-user-ID.
Packit 7cfc04
On SVr4, the directory used under \fBd)\fP is
Packit 7cfc04
.I /tmp
Packit 7cfc04
(and this is what glibc does).
Packit 7cfc04
.PP
Packit 7cfc04
Because it dynamically allocates memory used to return the pathname,
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
is reentrant, and thus thread safe, unlike
Packit 7cfc04
.BR tmpnam (3).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
function generates a different string each time it is called,
Packit 7cfc04
up to
Packit 7cfc04
.B TMP_MAX
Packit 7cfc04
(defined in
Packit 7cfc04
.IR <stdio.h> )
Packit 7cfc04
times.
Packit 7cfc04
If it is called more than
Packit 7cfc04
.B TMP_MAX
Packit 7cfc04
times,
Packit 7cfc04
the behavior is implementation defined.
Packit 7cfc04
.PP
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
uses at most the first five bytes from
Packit 7cfc04
.IR pfx .
Packit 7cfc04
.PP
Packit 7cfc04
The glibc implementation of
Packit 7cfc04
.BR tempnam ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.B EEXIST
Packit 7cfc04
upon failure to find a unique name.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The precise meaning of "appropriate" is undefined;
Packit 7cfc04
it is unspecified how accessibility of a directory is determined.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR mkstemp (3),
Packit 7cfc04
.BR mktemp (3),
Packit 7cfc04
.BR tmpfile (3),
Packit 7cfc04
.BR tmpnam (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.