1TEMPNAM(3) Linux Programmer's Manual TEMPNAM(3)
2
3
4
6 tempnam - create a name for a temporary file
7
9 #include <stdio.h>
10
11 char *tempnam(const char *dir, const char *pfx);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 tempnam(): _BSD_SOURCE || _SVID_SOURCE
16
18 The tempnam() function returns a pointer to a string that is a valid
19 filename, and such that a file with this name did not exist when temp‐
20 nam() checked. The filename suffix of the pathname generated will
21 start with pfx in case pfx is a non-NULL string of at most five bytes.
22 The directory prefix part of the pathname generated is required to be
23 "appropriate" (often that at least implies writable).
24
25 Attempts to find an appropriate directory go through the following
26 steps:
27
28 a) In case the environment variable TMPDIR exists and contains the name
29 of an appropriate directory, that is used.
30
31 b) Otherwise, if the dir argument is non-NULL and appropriate, it is
32 used.
33
34 c) Otherwise, P_tmpdir (as defined in <stdio.h>) is used when appropri‐
35 ate.
36
37 d) Finally an implementation-defined directory may be used.
38
39 The string returned by tempnam() is allocated using malloc(3) and hence
40 should be freed by free(3).
41
43 The tempnam() function returns a pointer to a unique temporary file‐
44 name, or NULL if a unique name cannot be generated.
45
47 ENOMEM Allocation of storage failed.
48
50 SVr4, 4.3BSD, POSIX.1-2001. POSIX.1-2008 marks tempnam() as obsolete.
51
53 Although tempnam() generates names that are difficult to guess, it is
54 nevertheless possible that between the time that tempnam() returns a
55 pathname, and the time that the program opens it, another program might
56 create that pathname using open(2), or create it as a symbolic link.
57 This can lead to security holes. To avoid such possibilities, use the
58 open(2) O_EXCL flag to open the pathname. Or better yet, use
59 mkstemp(3) or tmpfile(3).
60
61 SUSv2 does not mention the use of TMPDIR; glibc will use it only when
62 the program is not set-user-ID. On SVr4, the directory used under d)
63 is /tmp (and this is what glibc does).
64
65 Because it dynamically allocates memory used to return the pathname,
66 tempnam() is reentrant, and thus thread safe, unlike tmpnam(3).
67
68 The tempnam() function generates a different string each time it is
69 called, up to TMP_MAX (defined in <stdio.h>) times. If it is called
70 more than TMP_MAX times, the behavior is implementation defined.
71
72 tempnam() uses at most the first five bytes from pfx.
73
74 The glibc implementation of tempnam() will fail with the error EEXIST
75 upon failure to find a unique name.
76
78 The precise meaning of "appropriate" is undefined; it is unspecified
79 how accessibility of a directory is determined.
80
81 Never use this function. Use mkstemp(3) or tmpfile(3) instead.
82
84 mkstemp(3), mktemp(3), tmpfile(3), tmpnam(3)
85
87 This page is part of release 3.25 of the Linux man-pages project. A
88 description of the project, and information about reporting bugs, can
89 be found at http://www.kernel.org/doc/man-pages/.
90
91
92
93 2008-08-06 TEMPNAM(3)