xref: /freebsd/lib/libc/stdio/tmpnam.3 (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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.\"     @(#)tmpnam.3	8.2 (Berkeley) 11/17/93
33.\"
34.Dd August 7, 2020
35.Dt TMPFILE 3
36.Os
37.Sh NAME
38.Nm tempnam ,
39.Nm tmpfile ,
40.Nm tmpnam
41.Nd temporary file routines
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In stdio.h
46.Ft FILE *
47.Fn tmpfile void
48.Ft char *
49.Fn tmpnam "char *str"
50.Ft char *
51.Fn tempnam "const char *tmpdir" "const char *prefix"
52.Sh DESCRIPTION
53The
54.Fn tmpfile
55function
56returns a pointer to a stream associated with a file descriptor returned
57by the routine
58.Xr mkstemp 3 .
59The created file is unlinked before
60.Fn tmpfile
61returns, causing the file to be automatically deleted when the last
62reference to it is closed.
63The file is opened with the access value
64.Ql w+ .
65The file is created in the directory determined by the environment variable
66.Ev TMPDIR
67if set.
68The default location if
69.Ev TMPDIR
70is not set is
71.Pa /tmp .
72.Pp
73The
74.Fn tmpnam
75function
76returns a pointer to a file name, in the
77.Dv P_tmpdir
78directory, which
79did not reference an existing file at some indeterminate point in the
80past.
81.Dv P_tmpdir
82is defined in the include file
83.In stdio.h .
84If the argument
85.Fa str
86is
87.Pf non- Dv NULL ,
88the file name is copied to the buffer it references.
89Otherwise, the file name is copied to a static buffer.
90In either case,
91.Fn tmpnam
92returns a pointer to the file name.
93.Pp
94The buffer referenced by
95.Fa str
96is expected to be at least
97.Dv L_tmpnam
98bytes in length.
99.Dv L_tmpnam
100is defined in the include file
101.In stdio.h .
102.Pp
103The
104.Fn tempnam
105function
106is similar to
107.Fn tmpnam ,
108but provides the ability to specify the directory which will
109contain the temporary file and the file name prefix.
110.Pp
111The environment variable
112.Ev TMPDIR
113(if set), the argument
114.Fa tmpdir
115(if
116.Pf non- Dv NULL ) ,
117the directory
118.Dv P_tmpdir ,
119and the directory
120.Pa /tmp
121are tried, in the listed order, as directories in which to store the
122temporary file.
123.Pp
124The argument
125.Fa prefix ,
126if
127.Pf non- Dv NULL ,
128is used to specify a file name prefix, which will be the
129first part of the created file name.
130The
131.Fn tempnam
132function
133allocates memory in which to store the file name; the returned pointer
134may be used as a subsequent argument to
135.Xr free 3 .
136.Sh RETURN VALUES
137The
138.Fn tmpfile
139function
140returns a pointer to an open file stream on success, and a
141.Dv NULL
142pointer
143on error.
144.Pp
145The
146.Fn tmpnam
147and
148.Fn tempfile
149functions
150return a pointer to a file name on success, and a
151.Dv NULL
152pointer
153on error.
154.Sh ENVIRONMENT
155.Bl -tag -width Ds
156.It Ev TMPDIR
157.Pf [ Fn tempnam
158and
159.Fn tmpfile
160only]
161If set,
162the directory in which the temporary file is stored.
163.Ev TMPDIR
164is ignored for processes
165for which
166.Xr issetugid 2
167is true.
168.El
169.Sh COMPATIBILITY
170These interfaces are provided from System V and
171.Tn ANSI
172compatibility only.
173.Pp
174Most historic implementations of these functions provide
175only a limited number of possible temporary file names
176(usually 26)
177before file names will start being recycled.
178System V implementations of these functions
179(and of
180.Xr mktemp 3 )
181use the
182.Xr access 2
183system call to determine whether or not the temporary file
184may be created.
185This has obvious ramifications for setuid or setgid programs,
186complicating the portable use of these interfaces in such programs.
187.Pp
188The
189.Fn tmpfile
190interface should not be used in software expected to be used on other systems
191if there is any possibility that the user does not wish the temporary file to
192be publicly readable and writable.
193.Sh ERRORS
194The
195.Fn tmpfile
196function
197may fail and set the global variable
198.Va errno
199for any of the errors specified for the library functions
200.Xr fdopen 3
201or
202.Xr mkstemp 3 .
203.Pp
204The
205.Fn tmpnam
206function
207may fail and set
208.Va errno
209for any of the errors specified for the library function
210.Xr mktemp 3 .
211.Pp
212The
213.Fn tempnam
214function
215may fail and set
216.Va errno
217for any of the errors specified for the library functions
218.Xr malloc 3
219or
220.Xr mktemp 3 .
221.Sh SEE ALSO
222.Xr mkstemp 3 ,
223.Xr mktemp 3
224.Sh STANDARDS
225The
226.Fn tmpfile
227and
228.Fn tmpnam
229functions
230conform to
231.St -isoC .
232.Sh SECURITY CONSIDERATIONS
233The
234.Fn tmpnam
235and
236.Fn tempnam
237functions are susceptible to a race condition
238occurring between the selection of the file name
239and the creation of the file,
240which allows malicious users
241to potentially overwrite arbitrary files in the system,
242depending on the level of privilege of the running program.
243Additionally, there is no means by which
244file permissions may be specified.
245It is strongly suggested that
246.Xr mkstemp 3
247be used in place of these functions.
248