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