xref: /netbsd/lib/libc/stdio/mktemp.3 (revision bf9ec67e)
1.\"	$NetBSD: mktemp.3,v 1.15 2002/02/07 07:00:26 ross 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 \*[Lt]stdlib.h\*[Gt]
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.Pp
97Please note that the permissions of the file or directory being created are
98subject to the restrictions imposed by the
99.Xr umask 2
100system call. It may thus happen that the created file is unreadable and/or
101unwritable.
102.Sh RETURN VALUES
103The
104.Fn mktemp
105and
106.Fn mkdtemp
107functions
108return a pointer to the template on success and
109.Dv NULL
110on failure.
111The
112.Fn mkstemp
113function
114returns \-1 if no suitable file could be created.
115If either call fails an error code is placed in the global variable
116.Va errno .
117.Sh ERRORS
118The
119.Fn mktemp ,
120.Fn mkstemp
121and
122.Fn mkdtemp
123functions
124may set
125.Va errno
126to one of the following values:
127.Bl -tag -width Er
128.It Bq Er ENOTDIR
129The pathname portion of the template is not an existing directory.
130.El
131.Pp
132The
133.Fn mktemp ,
134.Fn mkstemp
135and
136.Fn mkdtemp
137functions
138may also set
139.Va errno
140to any value specified by the
141.Xr stat 2
142function.
143.Pp
144The
145.Fn mkstemp
146function
147may also set
148.Va errno
149to any value specified by the
150.Xr open 2
151function.
152.Pp
153The
154.Fn mkdtemp
155function
156may also set
157.Va errno
158to any value specified by the
159.Xr mkdir 2
160function.
161.Sh SEE ALSO
162.Xr chmod 2 ,
163.Xr getpid 2 ,
164.Xr open 2 ,
165.Xr stat 2 ,
166.Xr umask 2
167.Sh HISTORY
168A
169.Fn mktemp
170function appeared in
171.At v7 .
172.Pp
173The
174.Fn mkstemp
175function appeared in
176.Bx 4.4 .
177.Pp
178The
179.Fn mkdtemp
180function appeared in
181.Nx 1.4 .
182.Sh SECURITY CONSIDERATIONS
183The use of
184.Fn mktemp
185should generally be avoided, as a hostile process can exploit a race
186condition in the time between the generation of a temporary filename by
187.Fn mktemp
188and the invoker's use of the temporary name.
189A link-time warning will be issued advising the use of
190.Fn mkstemp
191or
192.Fn mkdtemp
193instead.
194