]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/tempnam.3
crypt.3: srcfix: rewrap source lines
[thirdparty/man-pages.git] / man3 / tempnam.3
1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH TEMPNAM 3 2017-09-15 "" "Linux Programmer's Manual"
26 .SH NAME
27 tempnam \- create a name for a temporary file
28 .SH SYNOPSIS
29 .nf
30 .B #include <stdio.h>
31 .PP
32 .BI "char *tempnam(const char *" dir ", const char *" pfx );
33 .fi
34 .PP
35 .in -4n
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .in
39 .PP
40 .BR tempnam ():
41 Since glibc 2.19:
42 _DEFAULT_SOURCE
43 Glibc 2.19 and earlier:
44 _BSD_SOURCE || _SVID_SOURCE
45 .SH DESCRIPTION
46 .I "Never use this function."
47 Use
48 .BR mkstemp (3)
49 or
50 .BR tmpfile (3)
51 instead.
52 .PP
53 The
54 .BR tempnam ()
55 function returns a pointer to a string that is a valid filename,
56 and such that a file with this name did not exist when
57 .BR tempnam ()
58 checked.
59 The filename suffix of the pathname generated will start with
60 .I pfx
61 in case
62 .I pfx
63 is a non-NULL string of at most five bytes.
64 The directory prefix part of the pathname generated is required to
65 be "appropriate" (often that at least implies writable).
66 .PP
67 Attempts to find an appropriate directory go through the following
68 steps:
69 .TP 3
70 a)
71 In case the environment variable
72 .B TMPDIR
73 exists and
74 contains the name of an appropriate directory, that is used.
75 .TP
76 b)
77 Otherwise, if the
78 .I dir
79 argument is non-NULL and appropriate, it is used.
80 .TP
81 c)
82 Otherwise,
83 .I P_tmpdir
84 (as defined in
85 .IR <stdio.h> )
86 is used when appropriate.
87 .TP
88 d)
89 Finally an implementation-defined directory may be used.
90 .PP
91 The string returned by
92 .BR tempnam ()
93 is allocated using
94 .BR malloc (3)
95 and hence should be freed by
96 .BR free (3).
97 .SH RETURN VALUE
98 On success, the
99 .BR tempnam ()
100 function returns a pointer to a unique temporary filename.
101 It returns NULL if a unique name cannot be generated, with
102 .I errno
103 set to indicate the cause of the error.
104 .SH ERRORS
105 .TP
106 .B ENOMEM
107 Allocation of storage failed.
108 .SH ATTRIBUTES
109 For an explanation of the terms used in this section, see
110 .BR attributes (7).
111 .TS
112 allbox;
113 lb lb lb
114 l l l.
115 Interface Attribute Value
116 T{
117 .BR tempnam ()
118 T} Thread safety MT-Safe env
119 .TE
120 .SH CONFORMING TO
121 SVr4, 4.3BSD, POSIX.1-2001.
122 POSIX.1-2008 marks
123 .BR tempnam ()
124 as obsolete.
125 .SH NOTES
126 Although
127 .BR tempnam ()
128 generates names that are difficult to guess,
129 it is nevertheless possible that between the time that
130 .BR tempnam ()
131 returns a pathname, and the time that the program opens it,
132 another program might create that pathname using
133 .BR open (2),
134 or create it as a symbolic link.
135 This can lead to security holes.
136 To avoid such possibilities, use the
137 .BR open (2)
138 .B O_EXCL
139 flag to open the pathname.
140 Or better yet, use
141 .BR mkstemp (3)
142 or
143 .BR tmpfile (3).
144 .PP
145 SUSv2 does not mention the use of
146 .BR TMPDIR ;
147 glibc will use it only
148 when the program is not set-user-ID.
149 On SVr4, the directory used under \fBd)\fP is
150 .I /tmp
151 (and this is what glibc does).
152 .PP
153 Because it dynamically allocates memory used to return the pathname,
154 .BR tempnam ()
155 is reentrant, and thus thread safe, unlike
156 .BR tmpnam (3).
157 .PP
158 The
159 .BR tempnam ()
160 function generates a different string each time it is called,
161 up to
162 .B TMP_MAX
163 (defined in
164 .IR <stdio.h> )
165 times.
166 If it is called more than
167 .B TMP_MAX
168 times,
169 the behavior is implementation defined.
170 .PP
171 .BR tempnam ()
172 uses at most the first five bytes from
173 .IR pfx .
174 .PP
175 The glibc implementation of
176 .BR tempnam ()
177 fails with the error
178 .B EEXIST
179 upon failure to find a unique name.
180 .SH BUGS
181 The precise meaning of "appropriate" is undefined;
182 it is unspecified how accessibility of a directory is determined.
183 .SH SEE ALSO
184 .BR mkstemp (3),
185 .BR mktemp (3),
186 .BR tmpfile (3),
187 .BR tmpnam (3)