]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/mktemp.3
27470ffed8607e9c34bd328260acca22b4c361e4
[thirdparty/man-pages.git] / man3 / mktemp.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
8 .\" 386BSD man pages
9 .\" Modified Sat Jul 24 18:48:06 1993 by Rik Faith (faith@cs.unc.edu)
10 .\" Modified Fri Jun 23 01:26:34 1995 by Andries Brouwer (aeb@cwi.nl)
11 .\" (prompted by Scott Burkett <scottb@IntNet.net>)
12 .\" Modified Sun Mar 28 23:44:38 1999 by Andries Brouwer (aeb@cwi.nl)
13 .\"
14 .TH MKTEMP 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
15 .SH NAME
16 mktemp \- make a unique temporary filename
17 .SH LIBRARY
18 Standard C library
19 .RI ( libc ", " \-lc )
20 .SH SYNOPSIS
21 .nf
22 .B #include <stdlib.h>
23 .PP
24 .BI "char *mktemp(char *" template );
25 .fi
26 .PP
27 .RS -4
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
30 .RE
31 .PP
32 .BR mktemp ():
33 .nf
34 Since glibc 2.12:
35 (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200112L)
36 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
37 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
38 Before glibc 2.12:
39 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
40 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
41 .fi
42 .SH DESCRIPTION
43 .IR "Never use this function" ;
44 see BUGS.
45 .PP
46 The
47 .BR mktemp ()
48 function generates a unique temporary filename
49 from \fItemplate\fP.
50 The last six characters of \fItemplate\fP must
51 be XXXXXX and these are replaced with a string that makes the
52 filename unique.
53 Since it will be modified,
54 .I template
55 must not be a string constant, but should be declared as a character array.
56 .SH RETURN VALUE
57 The
58 .BR mktemp ()
59 function always returns \fItemplate\fP.
60 If a unique name was created, the last six bytes of \fItemplate\fP will
61 have been modified in such a way that the resulting name is unique
62 (i.e., does not exist already)
63 If a unique name could not be created,
64 \fItemplate\fP is made an empty string, and
65 .I errno
66 is set to indicate the error.
67 .SH ERRORS
68 .TP
69 .B EINVAL
70 The last six characters of \fItemplate\fP were not XXXXXX.
71 .SH ATTRIBUTES
72 For an explanation of the terms used in this section, see
73 .BR attributes (7).
74 .ad l
75 .nh
76 .TS
77 allbox;
78 lbx lb lb
79 l l l.
80 Interface Attribute Value
81 T{
82 .BR mktemp ()
83 T} Thread safety MT-Safe
84 .TE
85 .hy
86 .ad
87 .sp 1
88 .SH STANDARDS
89 4.3BSD, POSIX.1-2001.
90 POSIX.1-2008 removes the specification of
91 .BR mktemp ().
92 .\" .SH NOTES
93 .\" The prototype is in
94 .\" .I <unistd.h>
95 .\" for libc4, libc5, glibc1; glibc2 follows the Single UNIX Specification
96 .\" and has the prototype in
97 .\" .IR <stdlib.h> .
98 .SH BUGS
99 Never use
100 .BR mktemp ().
101 Some implementations follow 4.3BSD
102 and replace XXXXXX by the current process ID and a single letter,
103 so that at most 26 different names can be returned.
104 Since on the one hand the names are easy to guess, and on the other
105 hand there is a race between testing whether the name exists and
106 opening the file, every use of
107 .BR mktemp ()
108 is a security risk.
109 The race is avoided by
110 .BR mkstemp (3)
111 and
112 .BR mkdtemp (3).
113 .SH SEE ALSO
114 .BR mktemp (1),
115 .BR mkdtemp (3),
116 .BR mkstemp (3),
117 .BR tempnam (3),
118 .BR tmpfile (3),
119 .BR tmpnam (3)