xref: /freebsd/lib/libc/stdio/tmpnam.3 (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1.\" Copyright (c) 1988, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd August 7, 2020
33.Dt TMPFILE 3
34.Os
35.Sh NAME
36.Nm tempnam ,
37.Nm tmpfile ,
38.Nm tmpnam
39.Nd temporary file routines
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In stdio.h
44.Ft FILE *
45.Fn tmpfile void
46.Ft char *
47.Fn tmpnam "char *str"
48.Ft char *
49.Fn tempnam "const char *tmpdir" "const char *prefix"
50.Sh DESCRIPTION
51The
52.Fn tmpfile
53function
54returns a pointer to a stream associated with a file descriptor returned
55by the routine
56.Xr mkstemp 3 .
57The created file is unlinked before
58.Fn tmpfile
59returns, causing the file to be automatically deleted when the last
60reference to it is closed.
61The file is opened with the access value
62.Ql w+ .
63The file is created in the directory determined by the environment variable
64.Ev TMPDIR
65if set.
66The default location if
67.Ev TMPDIR
68is not set is
69.Pa /tmp .
70.Pp
71The
72.Fn tmpnam
73function
74returns a pointer to a file name, in the
75.Dv P_tmpdir
76directory, which
77did not reference an existing file at some indeterminate point in the
78past.
79.Dv P_tmpdir
80is defined in the include file
81.In stdio.h .
82If the argument
83.Fa str
84is
85.Pf non- Dv NULL ,
86the file name is copied to the buffer it references.
87Otherwise, the file name is copied to a static buffer.
88In either case,
89.Fn tmpnam
90returns a pointer to the file name.
91.Pp
92The buffer referenced by
93.Fa str
94is expected to be at least
95.Dv L_tmpnam
96bytes in length.
97.Dv L_tmpnam
98is defined in the include file
99.In stdio.h .
100.Pp
101The
102.Fn tempnam
103function
104is similar to
105.Fn tmpnam ,
106but provides the ability to specify the directory which will
107contain the temporary file and the file name prefix.
108.Pp
109The environment variable
110.Ev TMPDIR
111(if set), the argument
112.Fa tmpdir
113(if
114.Pf non- Dv NULL ) ,
115the directory
116.Dv P_tmpdir ,
117and the directory
118.Pa /tmp
119are tried, in the listed order, as directories in which to store the
120temporary file.
121.Pp
122The argument
123.Fa prefix ,
124if
125.Pf non- Dv NULL ,
126is used to specify a file name prefix, which will be the
127first part of the created file name.
128The
129.Fn tempnam
130function
131allocates memory in which to store the file name; the returned pointer
132may be used as a subsequent argument to
133.Xr free 3 .
134.Sh RETURN VALUES
135The
136.Fn tmpfile
137function
138returns a pointer to an open file stream on success, and a
139.Dv NULL
140pointer
141on error.
142.Pp
143The
144.Fn tmpnam
145and
146.Fn tempfile
147functions
148return a pointer to a file name on success, and a
149.Dv NULL
150pointer
151on error.
152.Sh ENVIRONMENT
153.Bl -tag -width Ds
154.It Ev TMPDIR
155.Pf [ Fn tempnam
156and
157.Fn tmpfile
158only]
159If set,
160the directory in which the temporary file is stored.
161.Ev TMPDIR
162is ignored for processes
163for which
164.Xr issetugid 2
165is true.
166.El
167.Sh COMPATIBILITY
168These interfaces are provided from System V and
169.Tn ANSI
170compatibility only.
171.Pp
172Most historic implementations of these functions provide
173only a limited number of possible temporary file names
174(usually 26)
175before file names will start being recycled.
176System V implementations of these functions
177(and of
178.Xr mktemp 3 )
179use the
180.Xr access 2
181system call to determine whether or not the temporary file
182may be created.
183This has obvious ramifications for setuid or setgid programs,
184complicating the portable use of these interfaces in such programs.
185.Pp
186The
187.Fn tmpfile
188interface should not be used in software expected to be used on other systems
189if there is any possibility that the user does not wish the temporary file to
190be publicly readable and writable.
191.Sh ERRORS
192The
193.Fn tmpfile
194function
195may fail and set the global variable
196.Va errno
197for any of the errors specified for the library functions
198.Xr fdopen 3
199or
200.Xr mkstemp 3 .
201.Pp
202The
203.Fn tmpnam
204function
205may fail and set
206.Va errno
207for any of the errors specified for the library function
208.Xr mktemp 3 .
209.Pp
210The
211.Fn tempnam
212function
213may fail and set
214.Va errno
215for any of the errors specified for the library functions
216.Xr malloc 3
217or
218.Xr mktemp 3 .
219.Sh SEE ALSO
220.Xr mkstemp 3 ,
221.Xr mktemp 3
222.Sh STANDARDS
223The
224.Fn tmpfile
225and
226.Fn tmpnam
227functions
228conform to
229.St -isoC .
230.Sh SECURITY CONSIDERATIONS
231The
232.Fn tmpnam
233and
234.Fn tempnam
235functions are susceptible to a race condition
236occurring between the selection of the file name
237and the creation of the file,
238which allows malicious users
239to potentially overwrite arbitrary files in the system,
240depending on the level of privilege of the running program.
241Additionally, there is no means by which
242file permissions may be specified.
243It is strongly suggested that
244.Xr mkstemp 3
245be used in place of these functions.
246