]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/tempnam.3
pow.3: Minor tweak to BUGS
[thirdparty/man-pages.git] / man3 / tempnam.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da 24.\"
4b8c67d9 25.TH TEMPNAM 3 2017-09-15 "" "Linux Programmer's Manual"
fea681da
MK
26.SH NAME
27tempnam \- create a name for a temporary file
28.SH SYNOPSIS
29.nf
30.B #include <stdio.h>
68e4db0a 31.PP
fea681da
MK
32.BI "char *tempnam(const char *" dir ", const char *" pfx );
33.fi
68e4db0a 34.PP
cc4615cc
MK
35.in -4n
36Feature Test Macro Requirements for glibc (see
37.BR feature_test_macros (7)):
38.in
68e4db0a 39.PP
cc4615cc 40.BR tempnam ():
51c612fb
MK
41 Since glibc 2.19:
42 _DEFAULT_SOURCE
43 Glibc 2.19 and earlier:
44 _BSD_SOURCE || _SVID_SOURCE
fea681da 45.SH DESCRIPTION
47ce47ea
MK
46.I "Never use this function."
47Use
48.BR mkstemp (3)
49or
50.BR tmpfile (3)
51instead.
847e0d88 52.PP
fea681da 53The
63aa9df0 54.BR tempnam ()
fea681da
MK
55function returns a pointer to a string that is a valid filename,
56and such that a file with this name did not exist when
63aa9df0 57.BR tempnam ()
fea681da
MK
58checked.
59The filename suffix of the pathname generated will start with
60.I pfx
61in case
62.I pfx
63is a non-NULL string of at most five bytes.
64The directory prefix part of the pathname generated is required to
2d986c92 65be "appropriate" (often that at least implies writable).
847e0d88 66.PP
fea681da 67Attempts to find an appropriate directory go through the following
c13182ef 68steps:
0c629015 69.TP 3
c631f73d 70a)
097585ed
MK
71In case the environment variable
72.B TMPDIR
73exists and
fea681da 74contains the name of an appropriate directory, that is used.
c631f73d
MK
75.TP
76b)
77Otherwise, if the
fea681da
MK
78.I dir
79argument is non-NULL and appropriate, it is used.
c631f73d
MK
80.TP
81c)
82Otherwise,
fea681da
MK
83.I P_tmpdir
84(as defined in
85.IR <stdio.h> )
86is used when appropriate.
c631f73d
MK
87.TP
88d)
89Finally an implementation-defined directory may be used.
c13182ef 90.PP
c631f73d
MK
91The string returned by
92.BR tempnam ()
93is allocated using
94.BR malloc (3)
95and hence should be freed by
96.BR free (3).
47297adb 97.SH RETURN VALUE
f528275c 98On success, the
63aa9df0 99.BR tempnam ()
52e58375
MK
100function returns a pointer to a unique temporary filename.
101It returns NULL if a unique name cannot be generated, with
102.I errno
103set to indicate the cause of the error.
fea681da
MK
104.SH ERRORS
105.TP
106.B ENOMEM
107Allocation of storage failed.
3fdf5198
PH
108.SH ATTRIBUTES
109For an explanation of the terms used in this section, see
110.BR attributes (7).
111.TS
112allbox;
113lb lb lb
114l l l.
115Interface Attribute Value
116T{
117.BR tempnam ()
118T} Thread safety MT-Safe env
119.TE
47297adb 120.SH CONFORMING TO
44a2c328 121SVr4, 4.3BSD, POSIX.1-2001.
d458f965
MK
122POSIX.1-2008 marks
123.BR tempnam ()
124as obsolete.
fea681da 125.SH NOTES
c13182ef 126Although
2777b1ca 127.BR tempnam ()
c631f73d
MK
128generates names that are difficult to guess,
129it is nevertheless possible that between the time that
2777b1ca 130.BR tempnam ()
c631f73d
MK
131returns a pathname, and the time that the program opens it,
132another program might create that pathname using
c13182ef 133.BR open (2),
c631f73d
MK
134or create it as a symbolic link.
135This can lead to security holes.
c13182ef 136To avoid such possibilities, use the
c631f73d
MK
137.BR open (2)
138.B O_EXCL
c13182ef
MK
139flag to open the pathname.
140Or better yet, use
c631f73d
MK
141.BR mkstemp (3)
142or
143.BR tmpfile (3).
847e0d88 144.PP
097585ed
MK
145SUSv2 does not mention the use of
146.BR TMPDIR ;
147glibc will use it only
880f5b4b 148when the program is not set-user-ID.
68e1685c 149On SVr4, the directory used under \fBd)\fP is
0daa9e92 150.I /tmp
c631f73d 151(and this is what glibc does).
dd3568a1 152.PP
c631f73d 153Because it dynamically allocates memory used to return the pathname,
4733f53c 154.BR tempnam ()
c631f73d
MK
155is reentrant, and thus thread safe, unlike
156.BR tmpnam (3).
dd3568a1 157.PP
fea681da 158The
63aa9df0 159.BR tempnam ()
fea681da 160function generates a different string each time it is called,
097585ed
MK
161up to
162.B TMP_MAX
163(defined in
fea681da 164.IR <stdio.h> )
c13182ef 165times.
097585ed 166If it is called more than
0daa9e92 167.B TMP_MAX
097585ed 168times,
d9bfdb9c 169the behavior is implementation defined.
dd3568a1 170.PP
c631f73d
MK
171.BR tempnam ()
172uses at most the first five bytes from
173.IR pfx .
847e0d88 174.PP
7bc7af37
MK
175The glibc implementation of
176.BR tempnam ()
26cd31fd 177fails with the error
7bc7af37
MK
178.B EEXIST
179upon failure to find a unique name.
fea681da 180.SH BUGS
ec5a588f 181The precise meaning of "appropriate" is undefined;
fea681da 182it is unspecified how accessibility of a directory is determined.
47297adb 183.SH SEE ALSO
fea681da
MK
184.BR mkstemp (3),
185.BR mktemp (3),
186.BR tmpfile (3),
187.BR tmpnam (3)