xref: /original-bsd/lib/libc/stdio/mktemp.3 (revision ff2bc52d)
1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)mktemp.3	6.10 (Berkeley) 04/19/91
7.\"
8.Dd
9.Dt MKTEMP 3
10.Os
11.Sh NAME
12.Nm mktemp
13.Nd make temporary file name (unique)
14.Sh SYNOPSIS
15.Fd #include <unistd.h>
16.Ft char *
17.Fn mktemp "char *template"
18.Ft int
19.Fn mkstemp "char *template"
20.Sh DESCRIPTION
21The
22.Fn mktemp
23function
24takes the given file name template and overwrites a portion of it
25to create a file name.
26This file name is unique and suitable for use
27by the application.
28The template may be any file name with some number of
29.Ql X Ns s
30appended
31to it, for example
32.Pa /tmp/temp.XXXX .
33The trailing
34.Ql X Ns s
35are replaced with the current process number and/or a
36unique letter combination.
37The number of unique file names
38.Fn mktemp
39can return depends on the number of
40.Ql X Ns s
41provided; six
42.Ql X Ns s
43will
44result in
45.Fn mktemp
46testing roughly 26 ** 6 combinations.
47.Pp
48The
49.Fn mkstemp
50function
51makes the same replacement to the template and creates the template file,
52mode 0600, returning a file descriptor opened for reading and writing.
53This avoids the race between testing for a file's existence and opening it
54for use.
55.Sh RETURN VALUES
56The
57.Fn mktemp
58function
59returns a pointer to the template on success and
60.Dv NULL
61on failure.
62The
63.Fn mkstemp
64function
65returns \-1 if no suitable file could be created.
66If either call fails an error code is placed in the global variable
67.Va errno .
68.Sh ERRORS
69The
70.Fn mktemp
71and
72.Fn mkstemp
73functions
74may set
75.Va errno
76to one of the following values:
77.Bl -tag -width [ENOTDIR]
78.It Bq Er ENOTDIR
79The pathname portion of the template is not an existing directory.
80.El
81.Pp
82The
83.Fn mktemp
84and
85.Fn mkstemp
86functions
87may also set
88.Va errno
89to any value specified by the
90.Xr stat 2
91function.
92.Pp
93The
94.Fn mkstemp
95function
96may also set
97.Va errno
98to any value specified by the
99.Xr open 2
100function.
101.Sh SEE ALSO
102.Xr chmod 2 ,
103.Xr getpid 2 ,
104.Xr open 2 ,
105.Xr stat 2
106.Sh HISTORY
107A
108.Nm mktemp
109function appeared in
110.At v7 .
111