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