161180Sbostic.\" Copyright (c) 1988, 1991, 1993 261180Sbostic.\" The Regents of the University of California. All rights reserved. 334631Sbostic.\" 450334Sbostic.\" This code is derived from software contributed to Berkeley by 550334Sbostic.\" the American National Standards Committee X3, on Information 650334Sbostic.\" Processing Systems. 750334Sbostic.\" 843486Sbostic.\" %sccs.include.redist.man% 934631Sbostic.\" 10*64907Sbostic.\" @(#)tmpnam.3 8.2 (Berkeley) 11/17/93 1134631Sbostic.\" 1248351Scael.Dd 1348351Scael.Dt TMPFILE 3 1448351Scael.Os 1548351Scael.Sh NAME 1648351Scael.Nm tempnam , 1748351Scael.Nm tmpfile , 1848351Scael.Nm tmpnam 1948351Scael.Nd temporary file routines 2048351Scael.Sh SYNOPSIS 2148351Scael.Fd #include <stdio.h> 2248351Scael.Ft FILE * 2348351Scael.Fn tmpfile void 2448351Scael.Ft char * 2548351Scael.Fn tmpnam "char *str" 2648351Scael.Ft char * 2748351Scael.Fn tempnam "const char *tmpdir" "const char *prefix" 2848351Scael.Sh DESCRIPTION 2948351ScaelThe 3048351Scael.Fn tmpfile 3148351Scaelfunction 3246559Sbosticreturns a pointer to a stream associated with a file descriptor returned 3346559Sbosticby the routine 3448351Scael.Xr mkstemp 3 . 3543486SbosticThe created file is unlinked before 3648351Scael.Fn tmpfile 3746559Sbosticreturns, causing the file to be automatically deleted when the last 3846559Sbosticreference to it is closed. 3948351ScaelThe file is opened with the access value 4048351Scael.Ql w+ . 4148351Scael.Pp 4248351ScaelThe 4348351Scael.Fn tmpnam 4448351Scaelfunction 4548351Scaelreturns a pointer to a file name, in the 4648351Scael.Dv P_tmpdir 4748351Scaeldirectory, which 4843486Sbosticdid not reference an existing file at some indeterminate point in the 4943486Sbosticpast. 5048351Scael.Dv P_tmpdir 5148351Scaelis defined in the include file 5248351Scael.Aq Pa stdio.h . 5343486SbosticIf the argument 5448351Scael.Fa s 5548351Scaelis 5648351Scael.Pf non- Dv NULL , 5748351Scaelthe file name is copied to the buffer it references. 5846559SbosticOtherwise, the file name is copied to a static buffer. 5943486SbosticIn either case, 6048351Scael.Fn tmpnam 6146559Sbosticreturns a pointer to the file name. 6248351Scael.Pp 6346559SbosticThe buffer referenced by 6448351Scael.Fa s 6548351Scaelis expected to be at least 6648351Scael.Dv L_tmpnam 6748351Scaelbytes in length. 6848351Scael.Dv L_tmpnam 6948351Scaelis defined in the include file 7048351Scael.Aq Pa stdio.h . 7148351Scael.Pp 7248351ScaelThe 7348351Scael.Fn tempnam 7448351Scaelfunction 7543486Sbosticis similar to 7648351Scael.Fn tmpnam , 7743486Sbosticbut provides the ability to specify the directory which will 7843486Sbosticcontain the temporary file and the file name prefix. 7948351Scael.Pp 8048351ScaelThe environment variable 8148351Scael.Ev TMPDIR 8248351Scael(if set), the argument 83*64907Sbostic.Fa tmpdir 8448351Scael(if 8548351Scael.Pf non- Dv NULL ) , 8648351Scaelthe directory 8748351Scael.Dv P_tmpdir , 8848351Scaeland the directory 8948351Scael.Pa /tmp 9043486Sbosticare tried, in the listed order, as directories in which to store the 9143486Sbostictemporary file. 9248351Scael.Pp 9346559SbosticThe argument 9448351Scael.Fa prefix , 9548351Scaelif 9648351Scael.Pf non- Dv NULL , 9748351Scaelis used to specify a file name prefix, which will be the 9846559Sbosticfirst part of the created file name. 9948351Scael.Fn Tempnam 10043486Sbosticallocates memory in which to store the file name; the returned pointer 10143486Sbosticmay be used as a subsequent argument to 10248351Scael.Xr free 3 . 10348351Scael.Sh RETURN VALUES 10448351ScaelThe 10548351Scael.Fn tmpfile 10648351Scaelfunction 10748351Scaelreturns a pointer to an open file stream on success, and a 10848351Scael.Dv NULL 10948351Scaelpointer 11046559Sbosticon error. 11148351Scael.Pp 11248351ScaelThe 11348351Scael.Fn tmpnam 11443486Sbosticand 11548351Scael.Fn tempfile 11648351Scaelfunctions 11748351Scaelreturn a pointer to a file name on success, and a 11848351Scael.Dv NULL 11948351Scaelpointer 12046559Sbosticon error. 12148351Scael.Sh ERRORS 12248351ScaelThe 12348351Scael.Fn tmpfile 12448351Scaelfunction 12548351Scaelmay fail and set the global variable 12648351Scael.Va errno 12746559Sbosticfor any of the errors specified for the library functions 12848351Scael.Xr fdopen 3 12946559Sbosticor 13048351Scael.Xr mkstemp 3 . 13148351Scael.Pp 13248351ScaelThe 13348351Scael.Fn tmpnam 13448351Scaelfunction 13546559Sbosticmay fail and set 13648351Scael.Va errno 13746559Sbosticfor any of the errors specified for the library function 13848351Scael.Xr mktemp 3 . 13948351Scael.Pp 14048351ScaelThe 14148351Scael.Fn tempnam 14248351Scaelfunction 14346559Sbosticmay fail and set 14448351Scael.Va errno 14546559Sbosticfor any of the errors specified for the library functions 14648351Scael.Xr malloc 3 14746559Sbosticor 14848351Scael.Xr mktemp 3 . 14948351Scael.Sh SEE ALSO 15048351Scael.Xr mkstemp 3 , 15148351Scael.Xr mktemp 3 15248351Scael.Sh STANDARDS 15343486SbosticThe 15448351Scael.Fn tmpfile 15548351Scaeland 15648351Scael.Fn tmpnam 15748351Scaelfunctions 15848351Scaelconform to 15948351Scael.St -ansiC . 16048351Scael.Sh BUGS 16148351ScaelThese interfaces are provided for System V and 16248351Scael.Tn ANSI 16348351Scaelcompatibility only. 16448351ScaelThe 16548351Scael.Xr mkstemp 3 16643486Sbosticinterface is strongly preferred. 16748351Scael.Pp 16846559SbosticThere are four important problems with these interfaces (as well as 16943486Sbosticwith the historic 17048351Scael.Xr mktemp 3 17143486Sbosticinterface). 17243486SbosticFirst, there is an obvious race between file name selection and file 17346559Sbosticcreation and deletion. 17446559SbosticSecond, most historic implementations provide only a limited number 17546559Sbosticof possible temporary file names (usually 26) before file names will 17646559Sbosticstart being recycled. 17746559SbosticThird, the System V implementations of these functions (and of 17848351Scael.Xr mktemp ) 17943486Sbosticuse the 18048351Scael.Xr access 2 18146559Sbosticfunction to determine whether or not the temporary file may be created. 18243486SbosticThis has obvious ramifications for setuid or setgid programs, complicating 18343486Sbosticthe portable use of these interfaces in such programs. 18446559SbosticFinally, there is no specification of the permissions with which the 18546559Sbostictemporary files are created. 18648351Scael.Pp 18746559SbosticThis implementation does not have these flaws, but portable software 18846559Sbosticcannot depend on that. 18946559SbosticIn particular, the 19048351Scael.Fn tmpfile 19146559Sbosticinterface should not be used in software expected to be used on other systems 19246559Sbosticif there is any possibility that the user does not wish the temporary file to 19346559Sbosticbe publicly readable and writable. 194