]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/tempnam.3
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man3 / tempnam.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da 4.\"
4c1c5274 5.TH tempnam 3 (date) "Linux man-pages (unreleased)"
fea681da
MK
6.SH NAME
7tempnam \- create a name for a temporary file
99fd3650
AC
8.SH LIBRARY
9Standard C library
8fc3b2cf 10.RI ( libc ", " \-lc )
fea681da
MK
11.SH SYNOPSIS
12.nf
13.B #include <stdio.h>
68e4db0a 14.PP
fea681da
MK
15.BI "char *tempnam(const char *" dir ", const char *" pfx );
16.fi
68e4db0a 17.PP
d39ad78f 18.RS -4
cc4615cc
MK
19Feature Test Macro Requirements for glibc (see
20.BR feature_test_macros (7)):
d39ad78f 21.RE
68e4db0a 22.PP
cc4615cc 23.BR tempnam ():
9d281e06 24.nf
51c612fb
MK
25 Since glibc 2.19:
26 _DEFAULT_SOURCE
27 Glibc 2.19 and earlier:
28 _BSD_SOURCE || _SVID_SOURCE
9d281e06 29.fi
fea681da 30.SH DESCRIPTION
47ce47ea
MK
31.I "Never use this function."
32Use
33.BR mkstemp (3)
34or
35.BR tmpfile (3)
36instead.
847e0d88 37.PP
fea681da 38The
63aa9df0 39.BR tempnam ()
fea681da
MK
40function returns a pointer to a string that is a valid filename,
41and such that a file with this name did not exist when
63aa9df0 42.BR tempnam ()
fea681da
MK
43checked.
44The filename suffix of the pathname generated will start with
45.I pfx
46in case
47.I pfx
48is a non-NULL string of at most five bytes.
49The directory prefix part of the pathname generated is required to
2d986c92 50be "appropriate" (often that at least implies writable).
847e0d88 51.PP
fea681da 52Attempts to find an appropriate directory go through the following
c13182ef 53steps:
0c629015 54.TP 3
c631f73d 55a)
097585ed
MK
56In case the environment variable
57.B TMPDIR
58exists and
fea681da 59contains the name of an appropriate directory, that is used.
c631f73d
MK
60.TP
61b)
62Otherwise, if the
fea681da
MK
63.I dir
64argument is non-NULL and appropriate, it is used.
c631f73d
MK
65.TP
66c)
67Otherwise,
fea681da
MK
68.I P_tmpdir
69(as defined in
70.IR <stdio.h> )
71is used when appropriate.
c631f73d
MK
72.TP
73d)
74Finally an implementation-defined directory may be used.
c13182ef 75.PP
c631f73d
MK
76The string returned by
77.BR tempnam ()
78is allocated using
79.BR malloc (3)
80and hence should be freed by
81.BR free (3).
47297adb 82.SH RETURN VALUE
f528275c 83On success, the
63aa9df0 84.BR tempnam ()
52e58375
MK
85function returns a pointer to a unique temporary filename.
86It returns NULL if a unique name cannot be generated, with
87.I errno
855d489a 88set to indicate the error.
fea681da
MK
89.SH ERRORS
90.TP
91.B ENOMEM
92Allocation of storage failed.
3fdf5198
PH
93.SH ATTRIBUTES
94For an explanation of the terms used in this section, see
95.BR attributes (7).
c466875e
MK
96.ad l
97.nh
3fdf5198
PH
98.TS
99allbox;
c466875e 100lbx lb lb
3fdf5198
PH
101l l l.
102Interface Attribute Value
103T{
104.BR tempnam ()
105T} Thread safety MT-Safe env
106.TE
c466875e
MK
107.hy
108.ad
109.sp 1
3113c7f3 110.SH STANDARDS
44a2c328 111SVr4, 4.3BSD, POSIX.1-2001.
d458f965
MK
112POSIX.1-2008 marks
113.BR tempnam ()
114as obsolete.
fea681da 115.SH NOTES
c13182ef 116Although
2777b1ca 117.BR tempnam ()
c631f73d
MK
118generates names that are difficult to guess,
119it is nevertheless possible that between the time that
2777b1ca 120.BR tempnam ()
c631f73d
MK
121returns a pathname, and the time that the program opens it,
122another program might create that pathname using
c13182ef 123.BR open (2),
c631f73d
MK
124or create it as a symbolic link.
125This can lead to security holes.
c13182ef 126To avoid such possibilities, use the
c631f73d
MK
127.BR open (2)
128.B O_EXCL
c13182ef
MK
129flag to open the pathname.
130Or better yet, use
c631f73d
MK
131.BR mkstemp (3)
132or
133.BR tmpfile (3).
847e0d88 134.PP
097585ed
MK
135SUSv2 does not mention the use of
136.BR TMPDIR ;
137glibc will use it only
880f5b4b 138when the program is not set-user-ID.
68e1685c 139On SVr4, the directory used under \fBd)\fP is
0daa9e92 140.I /tmp
c631f73d 141(and this is what glibc does).
dd3568a1 142.PP
c631f73d 143Because it dynamically allocates memory used to return the pathname,
4733f53c 144.BR tempnam ()
c631f73d
MK
145is reentrant, and thus thread safe, unlike
146.BR tmpnam (3).
dd3568a1 147.PP
fea681da 148The
63aa9df0 149.BR tempnam ()
fea681da 150function generates a different string each time it is called,
097585ed
MK
151up to
152.B TMP_MAX
153(defined in
fea681da 154.IR <stdio.h> )
c13182ef 155times.
097585ed 156If it is called more than
0daa9e92 157.B TMP_MAX
097585ed 158times,
d9bfdb9c 159the behavior is implementation defined.
dd3568a1 160.PP
c631f73d
MK
161.BR tempnam ()
162uses at most the first five bytes from
163.IR pfx .
847e0d88 164.PP
7bc7af37
MK
165The glibc implementation of
166.BR tempnam ()
26cd31fd 167fails with the error
7bc7af37
MK
168.B EEXIST
169upon failure to find a unique name.
fea681da 170.SH BUGS
ec5a588f 171The precise meaning of "appropriate" is undefined;
fea681da 172it is unspecified how accessibility of a directory is determined.
47297adb 173.SH SEE ALSO
fea681da
MK
174.BR mkstemp (3),
175.BR mktemp (3),
176.BR tmpfile (3),
177.BR tmpnam (3)