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