Changes: Ready for 5.13
[man-pages.git] / man3 / tempnam.3
blobeed91b8368119537d632cbb759ba85f554929336
1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH TEMPNAM 3  2021-03-22 "" "Linux Programmer's Manual"
26 .SH NAME
27 tempnam \- create a name for a temporary file
28 .SH SYNOPSIS
29 .nf
30 .B #include <stdio.h>
31 .PP
32 .BI "char *tempnam(const char *" dir ", const char *" pfx );
33 .fi
34 .PP
35 .RS -4
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .RE
39 .PP
40 .BR tempnam ():
41 .nf
42     Since glibc 2.19:
43         _DEFAULT_SOURCE
44     Glibc 2.19 and earlier:
45         _BSD_SOURCE || _SVID_SOURCE
46 .fi
47 .SH DESCRIPTION
48 .I "Never use this function."
49 Use
50 .BR mkstemp (3)
52 .BR tmpfile (3)
53 instead.
54 .PP
55 The
56 .BR tempnam ()
57 function returns a pointer to a string that is a valid filename,
58 and such that a file with this name did not exist when
59 .BR tempnam ()
60 checked.
61 The filename suffix of the pathname generated will start with
62 .I pfx
63 in case
64 .I pfx
65 is a non-NULL string of at most five bytes.
66 The directory prefix part of the pathname generated is required to
67 be "appropriate" (often that at least implies writable).
68 .PP
69 Attempts to find an appropriate directory go through the following
70 steps:
71 .TP 3
73 In case the environment variable
74 .B TMPDIR
75 exists and
76 contains the name of an appropriate directory, that is used.
77 .TP
79 Otherwise, if the
80 .I dir
81 argument is non-NULL and appropriate, it is used.
82 .TP
84 Otherwise,
85 .I P_tmpdir
86 (as defined in
87 .IR <stdio.h> )
88 is used when appropriate.
89 .TP
91 Finally an implementation-defined directory may be used.
92 .PP
93 The string returned by
94 .BR tempnam ()
95 is allocated using
96 .BR malloc (3)
97 and hence should be freed by
98 .BR free (3).
99 .SH RETURN VALUE
100 On success, the
101 .BR tempnam ()
102 function returns a pointer to a unique temporary filename.
103 It returns NULL if a unique name cannot be generated, with
104 .I errno
105 set to indicate the error.
106 .SH ERRORS
108 .B ENOMEM
109 Allocation of storage failed.
110 .SH ATTRIBUTES
111 For an explanation of the terms used in this section, see
112 .BR attributes (7).
113 .ad l
116 allbox;
117 lbx lb lb
118 l l l.
119 Interface       Attribute       Value
121 .BR tempnam ()
122 T}      Thread safety   MT-Safe env
126 .sp 1
127 .SH CONFORMING TO
128 SVr4, 4.3BSD, POSIX.1-2001.
129 POSIX.1-2008 marks
130 .BR tempnam ()
131 as obsolete.
132 .SH NOTES
133 Although
134 .BR tempnam ()
135 generates names that are difficult to guess,
136 it is nevertheless possible that between the time that
137 .BR tempnam ()
138 returns a pathname, and the time that the program opens it,
139 another program might create that pathname using
140 .BR open (2),
141 or create it as a symbolic link.
142 This can lead to security holes.
143 To avoid such possibilities, use the
144 .BR open (2)
145 .B O_EXCL
146 flag to open the pathname.
147 Or better yet, use
148 .BR mkstemp (3)
150 .BR tmpfile (3).
152 SUSv2 does not mention the use of
153 .BR TMPDIR ;
154 glibc will use it only
155 when the program is not set-user-ID.
156 On SVr4, the directory used under \fBd)\fP is
157 .I /tmp
158 (and this is what glibc does).
160 Because it dynamically allocates memory used to return the pathname,
161 .BR tempnam ()
162 is reentrant, and thus thread safe, unlike
163 .BR tmpnam (3).
166 .BR tempnam ()
167 function generates a different string each time it is called,
168 up to
169 .B TMP_MAX
170 (defined in
171 .IR <stdio.h> )
172 times.
173 If it is called more than
174 .B TMP_MAX
175 times,
176 the behavior is implementation defined.
178 .BR tempnam ()
179 uses at most the first five bytes from
180 .IR pfx .
182 The glibc implementation of
183 .BR tempnam ()
184 fails with the error
185 .B EEXIST
186 upon failure to find a unique name.
187 .SH BUGS
188 The precise meaning of "appropriate" is undefined;
189 it is unspecified how accessibility of a directory is determined.
190 .SH SEE ALSO
191 .BR mkstemp (3),
192 .BR mktemp (3),
193 .BR tmpfile (3),
194 .BR tmpnam (3)