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