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