xref: /netbsd-src/lib/libc/stdio/mktemp.3 (revision 38023541164cff097d5fadec63134189b1453b8c)
1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
2.\" 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.\"     from: @(#)mktemp.3	6.10 (Berkeley) 4/19/91
33.\"	$Id: mktemp.3,v 1.3 1993/11/29 22:06:55 jtc Exp $
34.\"
35.Dd April 19, 1991
36.Dt MKTEMP 3
37.Os
38.Sh NAME
39.Nm mktemp
40.Nd make temporary file name (unique)
41.Sh SYNOPSIS
42.Fd #include <unistd.h>
43.Ft char *
44.Fn mktemp "char *template"
45.Ft int
46.Fn mkstemp "char *template"
47.Sh DESCRIPTION
48The
49.Fn mktemp
50function
51takes the given file name template and overwrites a portion of it
52to create a file name.
53This file name is unique and suitable for use
54by the application.
55The template may be any file name with some number of
56.Ql X Ns s
57appended
58to it, for example
59.Pa /tmp/temp.XXXX .
60The trailing
61.Ql X Ns s
62are replaced with the current process number and/or a
63unique letter combination.
64The number of unique file names
65.Fn mktemp
66can return depends on the number of
67.Ql X Ns s
68provided; six
69.Ql X Ns s
70will
71result in
72.Fn mktemp
73testing roughly 26 ** 6 combinations.
74.Pp
75The
76.Fn mkstemp
77function
78makes the same replacement to the template and creates the template file,
79mode 0600, returning a file descriptor opened for reading and writing.
80This avoids the race between testing for a file's existence and opening it
81for use.
82.Sh RETURN VALUES
83The
84.Fn mktemp
85function
86returns a pointer to the template on success and
87.Dv NULL
88on failure.
89The
90.Fn mkstemp
91function
92returns \-1 if no suitable file could be created.
93If either call fails an error code is placed in the global variable
94.Va errno .
95.Sh ERRORS
96The
97.Fn mktemp
98and
99.Fn mkstemp
100functions
101may set
102.Va errno
103to one of the following values:
104.Bl -tag -width Er
105.It Bq Er ENOTDIR
106The pathname portion of the template is not an existing directory.
107.El
108.Pp
109The
110.Fn mktemp
111and
112.Fn mkstemp
113functions
114may also set
115.Va errno
116to any value specified by the
117.Xr stat 2
118function.
119.Pp
120The
121.Fn mkstemp
122function
123may also set
124.Va errno
125to any value specified by the
126.Xr open 2
127function.
128.Sh SEE ALSO
129.Xr chmod 2 ,
130.Xr getpid 2 ,
131.Xr open 2 ,
132.Xr stat 2
133.Sh HISTORY
134A
135.Nm mktemp
136function appeared in
137.At v7 .
138