1.\" Copyright (c) 1989, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93 33.\" 34.Dd February 11, 1998 35.Dt MKTEMP 3 36.Os 37.Sh NAME 38.Nm mktemp 39.Nd make temporary file name (unique) 40.Sh SYNOPSIS 41.Fd #include <unistd.h> 42.Ft char * 43.Fn mktemp "char *template" 44.Ft int 45.Fn mkstemp "char *template" 46.Ft char * 47.Fn mkdtemp "char *template" 48.Sh DESCRIPTION 49The 50.Fn mktemp 51function 52takes the given file name template and overwrites a portion of it 53to create a file name. 54This file name is unique and suitable for use 55by the application. 56The template may be any file name with some number of 57.Ql X Ns s 58appended 59to it, for example 60.Pa /tmp/temp.XXXX . 61The trailing 62.Ql X Ns s 63are replaced with the current process number and/or a 64unique letter combination. 65The number of unique file names 66.Fn mktemp 67can return depends on the number of 68.Ql X Ns s 69provided; six 70.Ql X Ns s 71will 72result in 73.Fn mktemp 74testing roughly 26 ** 6 combinations. 75.Pp 76The 77.Fn mkstemp 78function 79makes the same replacement to the template and creates the template file, 80mode 0600, returning a file descriptor opened for reading and writing. 81This avoids the race between testing for a file's existence and opening it 82for use. 83.Pp 84The 85.Fn mkdtemp 86function makes the same replacement to the template as in 87.Xr mktemp 3 88and creates the template directory, mode 0700. 89.Sh RETURN VALUES 90The 91.Fn mktemp 92and 93.Fn mkdtemp 94functions return a pointer to the template on success and 95.Dv NULL 96on failure. 97The 98.Fn mkstemp 99function 100returns \-1 if no suitable file could be created. 101If either call fails an error code is placed in the global variable 102.Va errno . 103.Sh ERRORS 104The 105.Fn mkstemp 106and 107.Fn mkdtemp 108functions 109may set 110.Va errno 111to one of the following values: 112.Bl -tag -width [ENOTDIR] 113.It Bq Er ENOTDIR 114The pathname portion of the template is not an existing directory. 115.El 116.Pp 117The 118.Fn mkstemp 119and 120.Fn mkdtemp 121functions 122may also set 123.Va errno 124to any value specified by the 125.Xr stat 2 126function. 127.Pp 128The 129.Fn mkstemp 130function 131may also set 132.Va errno 133to any value specified by the 134.Xr open 2 135function. 136.Pp 137The 138.Fn mkdtemp 139function 140may also set 141.Va errno 142to any value specified by the 143.Xr mkdir 2 144function. 145.Sh NOTES 146A common problem that results in a core dump is that the programmer 147passes in a read-only string to 148.Fn mktemp , 149.Fn mkstemp 150or 151.Fn mkdtemp . 152This is common with programs that were developed before 153.St -ansiC 154compilers were common. 155For example, calling 156.Fn mkstemp 157with an argument of 158.Qq /tmp/tempfile.XXXXXX 159will result in a core dump due to 160.Fn mkstemp 161attempting to modify the string constant that was given. 162If the program in question makes heavy use of that type 163of function call, you do have the option of compiling the program 164so that it will store string constants in a writable segment of memory. 165See 166.Xr gcc 1 167for more information. 168.Sh BUGS 169An attacker can guess the filenames produced by 170.Fn mktemp . 171Whenever it is possible 172.Fn mkstemp 173should be used instead. 174.Sh SEE ALSO 175.Xr chmod 2 , 176.Xr getpid 2 , 177.Xr mkdir 2 , 178.Xr open 2 , 179.Xr stat 2 180.Sh HISTORY 181A 182.Fn mktemp 183function appeared in 184.At v7 . 185The 186.Fn mkdtemp 187function first appeared in 188.Ox 2.2 . 189