]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/tempnam.3
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[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 2008-08-06 "" "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 .sp
32 .BI "char *tempnam(const char *" dir ", const char *" pfx );
33 .fi
34 .sp
35 .in -4n
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .in
39 .sp
40 .BR tempnam ():
41 _BSD_SOURCE || _SVID_SOURCE
42 .SH DESCRIPTION
43 The
44 .BR tempnam ()
45 function returns a pointer to a string that is a valid filename,
46 and such that a file with this name did not exist when
47 .BR tempnam ()
48 checked.
49 The filename suffix of the pathname generated will start with
50 .I pfx
51 in case
52 .I pfx
53 is a non-NULL string of at most five bytes.
54 The directory prefix part of the pathname generated is required to
55 be "appropriate" (often that at least implies writable).
56
57 Attempts to find an appropriate directory go through the following
58 steps:
59 .TP 3
60 a)
61 In case the environment variable
62 .B TMPDIR
63 exists and
64 contains the name of an appropriate directory, that is used.
65 .TP
66 b)
67 Otherwise, if the
68 .I dir
69 argument is non-NULL and appropriate, it is used.
70 .TP
71 c)
72 Otherwise,
73 .I P_tmpdir
74 (as defined in
75 .IR <stdio.h> )
76 is used when appropriate.
77 .TP
78 d)
79 Finally an implementation-defined directory may be used.
80 .PP
81 The string returned by
82 .BR tempnam ()
83 is allocated using
84 .BR malloc (3)
85 and hence should be freed by
86 .BR free (3).
87 .SH RETURN VALUE
88 The
89 .BR tempnam ()
90 function returns a pointer to a unique temporary
91 filename, or NULL if a unique name cannot be generated.
92 .SH ERRORS
93 .TP
94 .B ENOMEM
95 Allocation of storage failed.
96 .SH CONFORMING TO
97 SVr4, 4.3BSD, POSIX.1-2001.
98 POSIX.1-2008 marks
99 .BR tempnam ()
100 as obsolete.
101 .SH NOTES
102 Although
103 .BR tempnam ()
104 generates names that are difficult to guess,
105 it is nevertheless possible that between the time that
106 .BR tempnam ()
107 returns a pathname, and the time that the program opens it,
108 another program might create that pathname using
109 .BR open (2),
110 or create it as a symbolic link.
111 This can lead to security holes.
112 To avoid such possibilities, use the
113 .BR open (2)
114 .B O_EXCL
115 flag to open the pathname.
116 Or better yet, use
117 .BR mkstemp (3)
118 or
119 .BR tmpfile (3).
120
121 SUSv2 does not mention the use of
122 .BR TMPDIR ;
123 glibc will use it only
124 when the program is not set-user-ID.
125 On SVr4, the directory used under \fBd)\fP is
126 .I /tmp
127 (and this is what glibc does).
128 .LP
129 Because it dynamically allocates memory used to return the pathname,
130 .BR tempnam ()
131 is reentrant, and thus thread safe, unlike
132 .BR tmpnam (3).
133 .LP
134 The
135 .BR tempnam ()
136 function generates a different string each time it is called,
137 up to
138 .B TMP_MAX
139 (defined in
140 .IR <stdio.h> )
141 times.
142 If it is called more than
143 .B TMP_MAX
144 times,
145 the behavior is implementation defined.
146 .LP
147 .BR tempnam ()
148 uses at most the first five bytes from
149 .IR pfx .
150
151 The glibc implementation of
152 .BR tempnam ()
153 will fail with the error
154 .B EEXIST
155 upon failure to find a unique name.
156 .SH BUGS
157 The precise meaning of "appropriate" is undefined;
158 it is unspecified how accessibility of a directory is determined.
159
160 Never use this function.
161 Use
162 .BR mkstemp (3)
163 or
164 .BR tmpfile (3)
165 instead.
166 .SH SEE ALSO
167 .BR mkstemp (3),
168 .BR mktemp (3),
169 .BR tmpfile (3),
170 .BR tmpnam (3)