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 June 4, 1993 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.Sh DESCRIPTION 47The 48.Fn mktemp 49function 50takes the given file name template and overwrites a portion of it 51to create a file name. 52This file name is unique and suitable for use 53by the application. 54The template may be any file name with some number of 55.Ql X Ns s 56appended 57to it, for example 58.Pa /tmp/temp.XXXX . 59The trailing 60.Ql X Ns s 61are replaced with the current process number and/or a 62unique letter combination. 63The number of unique file names 64.Fn mktemp 65can return depends on the number of 66.Ql X Ns s 67provided; six 68.Ql X Ns s 69will 70result in 71.Fn mktemp 72testing roughly 26 ** 6 combinations. 73.Pp 74The 75.Fn mkstemp 76function 77makes the same replacement to the template and creates the template file, 78mode 0600, returning a file descriptor opened for reading and writing. 79This avoids the race between testing for a file's existence and opening it 80for use. 81.Sh RETURN VALUES 82The 83.Fn mktemp 84function 85returns a pointer to the template on success and 86.Dv NULL 87on failure. 88The 89.Fn mkstemp 90function 91returns \-1 if no suitable file could be created. 92If either call fails an error code is placed in the global variable 93.Va errno . 94.Sh ERRORS 95The 96.Fn mktemp 97and 98.Fn mkstemp 99functions 100may set 101.Va errno 102to one of the following values: 103.Bl -tag -width [ENOTDIR] 104.It Bq Er ENOTDIR 105The pathname portion of the template is not an existing directory. 106.El 107.Pp 108The 109.Fn mktemp 110and 111.Fn mkstemp 112functions 113may also set 114.Va errno 115to any value specified by the 116.Xr stat 2 117function. 118.Pp 119The 120.Fn mkstemp 121function 122may also set 123.Va errno 124to any value specified by the 125.Xr open 2 126function. 127.Sh NOTES 128A common problem that results in a core dump is that the programmer 129passes in a read-only string to 130.Fn mktemp 131or 132.Fn mkstemp . 133This is common with programs that were developed before 134.St -ansiC 135compilers were common.. 136For example, calling 137.Fn mkstemp 138with an argument of 139.Qq /tmp/tempfile.XXXXXX 140will result in a core dump due to 141.Fn mkstemp 142attempting to modify the string constant that was given. 143If the program in question makes heavy use of that type 144of function call, you do have the option of compiling the program 145so that it will store string constants in a writable segment of memory. 146See 147.Xr gcc 1 148for more information. 149.Sh SEE ALSO 150.Xr chmod 2 , 151.Xr getpid 2 , 152.Xr open 2 , 153.Xr stat 2 154.Sh HISTORY 155A 156.Nm mktemp 157function appeared in 158.At v7 . 159