1*48351Scael.\" Copyright (c) 1988, 1991 The Regents of the University of California. 234631Sbostic.\" All rights reserved. 334631Sbostic.\" 443486Sbostic.\" %sccs.include.redist.man% 534631Sbostic.\" 6*48351Scael.\" @(#)tmpnam.3 5.13 (Berkeley) 04/19/91 734631Sbostic.\" 8*48351Scael.Dd 9*48351Scael.Dt TMPFILE 3 10*48351Scael.Os 11*48351Scael.Sh NAME 12*48351Scael.Nm tempnam , 13*48351Scael.Nm tmpfile , 14*48351Scael.Nm tmpnam 15*48351Scael.Nd temporary file routines 16*48351Scael.Sh SYNOPSIS 17*48351Scael.Fd #include <stdio.h> 18*48351Scael.Ft FILE * 19*48351Scael.Fn tmpfile void 20*48351Scael.Ft char * 21*48351Scael.Fn tmpnam "char *str" 22*48351Scael.Ft char * 23*48351Scael.Fn tempnam "const char *tmpdir" "const char *prefix" 24*48351Scael.Sh DESCRIPTION 25*48351ScaelThe 26*48351Scael.Fn tmpfile 27*48351Scaelfunction 2846559Sbosticreturns a pointer to a stream associated with a file descriptor returned 2946559Sbosticby the routine 30*48351Scael.Xr mkstemp 3 . 3143486SbosticThe created file is unlinked before 32*48351Scael.Fn tmpfile 3346559Sbosticreturns, causing the file to be automatically deleted when the last 3446559Sbosticreference to it is closed. 35*48351ScaelThe file is opened with the access value 36*48351Scael.Ql w+ . 37*48351Scael.Pp 38*48351ScaelThe 39*48351Scael.Fn tmpnam 40*48351Scaelfunction 41*48351Scaelreturns a pointer to a file name, in the 42*48351Scael.Dv P_tmpdir 43*48351Scaeldirectory, which 4443486Sbosticdid not reference an existing file at some indeterminate point in the 4543486Sbosticpast. 46*48351Scael.Dv P_tmpdir 47*48351Scaelis defined in the include file 48*48351Scael.Aq Pa stdio.h . 4943486SbosticIf the argument 50*48351Scael.Fa s 51*48351Scaelis 52*48351Scael.Pf non- Dv NULL , 53*48351Scaelthe file name is copied to the buffer it references. 5446559SbosticOtherwise, the file name is copied to a static buffer. 5543486SbosticIn either case, 56*48351Scael.Fn tmpnam 5746559Sbosticreturns a pointer to the file name. 58*48351Scael.Pp 5946559SbosticThe buffer referenced by 60*48351Scael.Fa s 61*48351Scaelis expected to be at least 62*48351Scael.Dv L_tmpnam 63*48351Scaelbytes in length. 64*48351Scael.Dv L_tmpnam 65*48351Scaelis defined in the include file 66*48351Scael.Aq Pa stdio.h . 67*48351Scael.Pp 68*48351ScaelThe 69*48351Scael.Fn tempnam 70*48351Scaelfunction 7143486Sbosticis similar to 72*48351Scael.Fn tmpnam , 7343486Sbosticbut provides the ability to specify the directory which will 7443486Sbosticcontain the temporary file and the file name prefix. 75*48351Scael.Pp 76*48351ScaelThe environment variable 77*48351Scael.Ev TMPDIR 78*48351Scael(if set), the argument 79*48351Scael.Fa dir 80*48351Scael(if 81*48351Scael.Pf non- Dv NULL ) , 82*48351Scaelthe directory 83*48351Scael.Dv P_tmpdir , 84*48351Scaeland the directory 85*48351Scael.Pa /tmp 8643486Sbosticare tried, in the listed order, as directories in which to store the 8743486Sbostictemporary file. 88*48351Scael.Pp 8946559SbosticThe argument 90*48351Scael.Fa prefix , 91*48351Scaelif 92*48351Scael.Pf non- Dv NULL , 93*48351Scaelis used to specify a file name prefix, which will be the 9446559Sbosticfirst part of the created file name. 95*48351Scael.Fn Tempnam 9643486Sbosticallocates memory in which to store the file name; the returned pointer 9743486Sbosticmay be used as a subsequent argument to 98*48351Scael.Xr free 3 . 99*48351Scael.Sh RETURN VALUES 100*48351ScaelThe 101*48351Scael.Fn tmpfile 102*48351Scaelfunction 103*48351Scaelreturns a pointer to an open file stream on success, and a 104*48351Scael.Dv NULL 105*48351Scaelpointer 10646559Sbosticon error. 107*48351Scael.Pp 108*48351ScaelThe 109*48351Scael.Fn tmpnam 11043486Sbosticand 111*48351Scael.Fn tempfile 112*48351Scaelfunctions 113*48351Scaelreturn a pointer to a file name on success, and a 114*48351Scael.Dv NULL 115*48351Scaelpointer 11646559Sbosticon error. 117*48351Scael.Sh ERRORS 118*48351ScaelThe 119*48351Scael.Fn tmpfile 120*48351Scaelfunction 121*48351Scaelmay fail and set the global variable 122*48351Scael.Va errno 12346559Sbosticfor any of the errors specified for the library functions 124*48351Scael.Xr fdopen 3 12546559Sbosticor 126*48351Scael.Xr mkstemp 3 . 127*48351Scael.Pp 128*48351ScaelThe 129*48351Scael.Fn tmpnam 130*48351Scaelfunction 13146559Sbosticmay fail and set 132*48351Scael.Va errno 13346559Sbosticfor any of the errors specified for the library function 134*48351Scael.Xr mktemp 3 . 135*48351Scael.Pp 136*48351ScaelThe 137*48351Scael.Fn tempnam 138*48351Scaelfunction 13946559Sbosticmay fail and set 140*48351Scael.Va errno 14146559Sbosticfor any of the errors specified for the library functions 142*48351Scael.Xr malloc 3 14346559Sbosticor 144*48351Scael.Xr mktemp 3 . 145*48351Scael.Sh SEE ALSO 146*48351Scael.Xr mkstemp 3 , 147*48351Scael.Xr mktemp 3 148*48351Scael.Sh STANDARDS 14943486SbosticThe 150*48351Scael.Fn tmpfile 151*48351Scaeland 152*48351Scael.Fn tmpnam 153*48351Scaelfunctions 154*48351Scaelconform to 155*48351Scael.St -ansiC . 156*48351Scael.Sh BUGS 157*48351ScaelThese interfaces are provided for System V and 158*48351Scael.Tn ANSI 159*48351Scaelcompatibility only. 160*48351ScaelThe 161*48351Scael.Xr mkstemp 3 16243486Sbosticinterface is strongly preferred. 163*48351Scael.Pp 16446559SbosticThere are four important problems with these interfaces (as well as 16543486Sbosticwith the historic 166*48351Scael.Xr mktemp 3 16743486Sbosticinterface). 16843486SbosticFirst, there is an obvious race between file name selection and file 16946559Sbosticcreation and deletion. 17046559SbosticSecond, most historic implementations provide only a limited number 17146559Sbosticof possible temporary file names (usually 26) before file names will 17246559Sbosticstart being recycled. 17346559SbosticThird, the System V implementations of these functions (and of 174*48351Scael.Xr mktemp ) 17543486Sbosticuse the 176*48351Scael.Xr access 2 17746559Sbosticfunction to determine whether or not the temporary file may be created. 17843486SbosticThis has obvious ramifications for setuid or setgid programs, complicating 17943486Sbosticthe portable use of these interfaces in such programs. 18046559SbosticFinally, there is no specification of the permissions with which the 18146559Sbostictemporary files are created. 182*48351Scael.Pp 18346559SbosticThis implementation does not have these flaws, but portable software 18446559Sbosticcannot depend on that. 18546559SbosticIn particular, the 186*48351Scael.Fn tmpfile 18746559Sbosticinterface should not be used in software expected to be used on other systems 18846559Sbosticif there is any possibility that the user does not wish the temporary file to 18946559Sbosticbe publicly readable and writable. 190