]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/tmpnam.3
Libc-1044.1.2.tar.gz
[apple/libc.git] / stdio / FreeBSD / tmpnam.3
CommitLineData
5b2abdfb
A
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.
5b2abdfb
A
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
1f2f436a 33.\" $FreeBSD: src/lib/libc/stdio/tmpnam.3,v 1.20 2007/03/16 21:46:24 maxim Exp $
5b2abdfb 34.\"
ad3c9f2a 35.Dd November 12, 2008
5b2abdfb
A
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 *
ad3c9f2a
A
48.Fo tmpfile
49.Fa "void"
50.Fc
5b2abdfb 51.Ft char *
ad3c9f2a
A
52.Fo tmpnam
53.Fa "char *s"
54.Fc
5b2abdfb 55.Ft char *
ad3c9f2a
A
56.Fo tempnam
57.Fa "const char *dir"
58.Fa "const char *pfx"
59.Fc
5b2abdfb
A
60.Sh DESCRIPTION
61The
62.Fn tmpfile
63function
64returns a pointer to a stream associated with a file descriptor returned
65by the routine
66.Xr mkstemp 3 .
67The created file is unlinked before
68.Fn tmpfile
69returns, causing the file to be automatically deleted when the last
70reference to it is closed.
71The file is opened with the access value
72.Ql w+ .
ad3c9f2a 73If the environment variable
5b2abdfb 74.Ev TMPDIR
ad3c9f2a
A
75is defined,
76the file is created in the specified directory.
77The default location, if
5b2abdfb 78.Ev TMPDIR
ad3c9f2a 79is not set, is
5b2abdfb
A
80.Pa /tmp .
81.Pp
82The
83.Fn tmpnam
84function
85returns a pointer to a file name, in the
86.Dv P_tmpdir
87directory, which
88did not reference an existing file at some indeterminate point in the
89past.
90.Dv P_tmpdir
91is defined in the include file
3d9156a7 92.In stdio.h .
5b2abdfb 93If the argument
ad3c9f2a 94.Fa s
5b2abdfb
A
95is
96.Pf non- Dv NULL ,
97the file name is copied to the buffer it references.
98Otherwise, the file name is copied to a static buffer.
99In either case,
100.Fn tmpnam
101returns a pointer to the file name.
102.Pp
103The buffer referenced by
ad3c9f2a 104.Fa s
5b2abdfb
A
105is expected to be at least
106.Dv L_tmpnam
107bytes in length.
108.Dv L_tmpnam
109is defined in the include file
3d9156a7 110.In stdio.h .
5b2abdfb
A
111.Pp
112The
113.Fn tempnam
114function
115is similar to
116.Fn tmpnam ,
117but provides the ability to specify the directory which will
118contain the temporary file and the file name prefix.
119.Pp
ad3c9f2a
A
120The argument
121.Fa dir
5b2abdfb
A
122(if
123.Pf non- Dv NULL ) ,
124the directory
125.Dv P_tmpdir ,
ad3c9f2a
A
126the environment variable
127.Ev TMPDIR
128(if set),
129the directory
5b2abdfb 130.Pa /tmp
ad3c9f2a 131and finally, the current directory,
5b2abdfb
A
132are tried, in the listed order, as directories in which to store the
133temporary file.
134.Pp
135The argument
ad3c9f2a 136.Fa pfx ,
5b2abdfb
A
137if
138.Pf non- Dv NULL ,
139is used to specify a file name prefix, which will be the
140first part of the created file name.
9385eb3d
A
141The
142.Fn tempnam
143function
5b2abdfb
A
144allocates memory in which to store the file name; the returned pointer
145may be used as a subsequent argument to
146.Xr free 3 .
147.Sh RETURN VALUES
148The
149.Fn tmpfile
150function
151returns a pointer to an open file stream on success, and a
152.Dv NULL
153pointer
154on error.
155.Pp
156The
157.Fn tmpnam
158and
159.Fn tempfile
160functions
161return a pointer to a file name on success, and a
162.Dv NULL
163pointer
164on error.
1f2f436a
A
165.Sh ENVIRONMENT
166.Bl -tag -width Ds
167.It Ev TMPDIR
168.Pf [ Fn tempnam
169only]
170If set,
171the directory in which the temporary file is stored.
172.Ev TMPDIR
173is ignored for processes
174for which
175.Xr issetugid 2
176is true.
177.El
178.Sh COMPATIBILITY
179These interfaces are provided from System V and
180.Tn ANSI
181compatibility only.
182.Pp
183Most historic implementations of these functions provide
184only a limited number of possible temporary file names
185(usually 26)
186before file names will start being recycled.
187System V implementations of these functions
188(and of
189.Xr mktemp 3 )
190use the
191.Xr access 2
192system call to determine whether or not the temporary file
193may be created.
194This has obvious ramifications for setuid or setgid programs,
195complicating the portable use of these interfaces in such programs.
196.Pp
197The
198.Fn tmpfile
199interface should not be used in software expected to be used on other systems
200if there is any possibility that the user does not wish the temporary file to
201be publicly readable and writable.
5b2abdfb
A
202.Sh ERRORS
203The
204.Fn tmpfile
205function
206may fail and set the global variable
207.Va errno
208for any of the errors specified for the library functions
209.Xr fdopen 3
210or
211.Xr mkstemp 3 .
212.Pp
213The
214.Fn tmpnam
215function
216may fail and set
217.Va errno
218for any of the errors specified for the library function
219.Xr mktemp 3 .
220.Pp
221The
222.Fn tempnam
223function
224may fail and set
225.Va errno
226for any of the errors specified for the library functions
227.Xr malloc 3
228or
229.Xr mktemp 3 .
9385eb3d 230.Sh SECURITY CONSIDERATIONS
5b2abdfb 231The
5b2abdfb 232.Fn tmpnam
9385eb3d
A
233and
234.Fn tempnam
235functions are susceptible to a race condition
3d9156a7 236occurring between the selection of the file name
9385eb3d
A
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(See
247the FSA.)
ad3c9f2a
A
248.Sh LEGACY DESCRIPTION
249In legacy mode, the order directories are tried by the
250.Fn tempnam
251function is different; the environment variable
252.Ev TMPDIR
253(if defined) is used first.
9385eb3d
A
254.Sh SEE ALSO
255.Xr mkstemp 3 ,
256.Xr mktemp 3
9385eb3d
A
257.Sh STANDARDS
258The
259.Fn tmpfile
260and
261.Fn tmpnam
262functions
263conform to
264.St -isoC .