1.\" $NetBSD: mktemp.3,v 1.8 1997/11/14 02:04:50 mrg 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 March 24, 1997 37.Dt MKTEMP 3 38.Os 39.Sh NAME 40.Nm mktemp , 41.Nm mkstemp 42.Nd make temporary file name (unique) 43.Sh SYNOPSIS 44.Fd #include <unistd.h> 45.Ft char * 46.Fn mktemp "char *template" 47.Ft int 48.Fn mkstemp "char *template" 49.Sh DESCRIPTION 50The 51.Fn mktemp 52function 53takes the given file name template and overwrites a portion of it 54to create a file name. 55This file name is unique and suitable for use 56by the application. 57The template may be any file name with some number of 58.Ql X Ns s 59appended 60to it, for example 61.Pa /tmp/temp.XXXX . 62The trailing 63.Ql X Ns s 64are replaced with the current process number and/or a 65unique letter combination. 66The number of unique file names 67.Fn mktemp 68can return depends on the number of 69.Ql X Ns s 70provided; six 71.Ql X Ns s 72will 73result in 74.Fn mktemp 75testing roughly 26 ** 6 combinations. 76.Pp 77The 78.Fn mkstemp 79function 80makes the same replacement to the template and creates the template file, 81mode 0600, returning a file descriptor opened for reading and writing. 82This avoids the race between testing for a file's existence and opening it 83for use. 84.Sh RETURN VALUES 85The 86.Fn mktemp 87function 88returns a pointer to the template on success and 89.Dv NULL 90on failure. 91The 92.Fn mkstemp 93function 94returns \-1 if no suitable file could be created. 95If either call fails an error code is placed in the global variable 96.Va errno . 97.Sh ERRORS 98The 99.Fn mktemp 100and 101.Fn mkstemp 102functions 103may set 104.Va errno 105to one of the following values: 106.Bl -tag -width Er 107.It Bq Er ENOTDIR 108The pathname portion of the template is not an existing directory. 109.El 110.Pp 111The 112.Fn mktemp 113and 114.Fn mkstemp 115functions 116may also set 117.Va errno 118to any value specified by the 119.Xr stat 2 120function. 121.Pp 122The 123.Fn mkstemp 124function 125may also set 126.Va errno 127to any value specified by the 128.Xr open 2 129function. 130.Sh SEE ALSO 131.Xr chmod 2 , 132.Xr getpid 2 , 133.Xr open 2 , 134.Xr stat 2 135.Sh HISTORY 136A 137.Fn mktemp 138function appeared in 139.At v7 . 140.Sh BUGS 141The use of 142.Fn mktemp 143should generally be avoided, as a hostile process can exploit a race 144condition in the time between the generation of a temporary filename by 145.Fn mktemp 146and the invoker's use of the temporary name. 147A link-time warning will be issued to advise the use of 148.Fn mkstemp 149instead. 150