xref: /freebsd/lib/libc/stdio/mktemp.3 (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 int
47.Fn mkstemps "char *template" "int suffixlen"
48.Ft char *
49.Fn mkdtemp "char *template"
50.Sh DESCRIPTION
51The
52.Fn mktemp
53function
54takes the given file name template and overwrites a portion of it
55to create a file name.
56This file name is unique and suitable for use
57by the application.
58The template may be any file name with some number of
59.Ql X Ns s
60appended
61to it, for example
62.Pa /tmp/temp.XXXX .
63The trailing
64.Ql X Ns s
65are replaced with the current process number and/or a
66unique letter combination.
67The number of unique file names
68.Fn mktemp
69can return depends on the number of
70.Ql X Ns s
71provided; six
72.Ql X Ns s
73will
74result in
75.Fn mktemp
76testing roughly 26 ** 6 combinations.
77.Pp
78The
79.Fn mkstemp
80function
81makes the same replacement to the template and creates the template file,
82mode 0600, returning a file descriptor opened for reading and writing.
83This avoids the race between testing for a file's existence and opening it
84for use.
85.Pp
86The
87.Fn mkstemps
88function acts the same as
89.Fn mkstemp ,
90except it permits a suffix to exist in the template.
91The template should be of the form
92.Pa /tmp/tmpXXXXXXsuffix .
93.Fn mkstemps
94is told the length of the suffix string.
95.Pp
96The
97.Fn mkdtemp
98function makes the same replacement to the template as in
99.Xr mktemp 3
100and creates the template directory, mode 0700.
101.Sh RETURN VALUES
102The
103.Fn mktemp
104and
105.Fn mkdtemp
106functions return a pointer to the template on success and
107.Dv NULL
108on failure.
109The
110.Fn mkstemp
111and
112.Fn mkstemps
113functions
114return \-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 mkstemp ,
120.Fn mkstemps
121and
122.Fn mkdtemp
123functions
124may set
125.Va errno
126to one of the following values:
127.Bl -tag -width [ENOTDIR]
128.It Bq Er ENOTDIR
129The pathname portion of the template is not an existing directory.
130.El
131.Pp
132The
133.Fn mkstemp ,
134.Fn mkstemps
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
146and
147.Fn mkstemps
148functions
149may also set
150.Va errno
151to any value specified by the
152.Xr open 2
153function.
154.Pp
155The
156.Fn mkdtemp
157function
158may also set
159.Va errno
160to any value specified by the
161.Xr mkdir 2
162function.
163.Sh NOTES
164A common problem that results in a core dump is that the programmer
165passes in a read-only string to
166.Fn mktemp ,
167.Fn mkstemp ,
168.Fn mkstemps
169or
170.Fn mkdtemp .
171This is common with programs that were developed before
172.St -ansiC
173compilers were common.
174For example, calling
175.Fn mkstemp
176with an argument of
177.Qq /tmp/tempfile.XXXXXX
178will result in a core dump due to
179.Fn mkstemp
180attempting to modify the string constant that was given.
181If the program in question makes heavy use of that type
182of function call, you do have the option of compiling the program
183so that it will store string constants in a writable segment of memory.
184See
185.Xr gcc 1
186for more information.
187.Sh BUGS
188An attacker can guess the filenames produced by
189.Fn mktemp .
190Whenever it is possible
191.Fn mkstemp
192should be used instead.
193.Sh SEE ALSO
194.Xr chmod 2 ,
195.Xr getpid 2 ,
196.Xr mkdir 2 ,
197.Xr open 2 ,
198.Xr stat 2
199.Sh HISTORY
200A
201.Fn mktemp
202function appeared in
203.At v7 .
204The
205.Fn mkdtemp
206function first appeared in
207.Ox 2.2 .
208