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