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