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