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():
16 Since glibc 2.19:
17 _DEFAULT_SOURCE
18 Glibc 2.19 and earlier:
19 _BSD_SOURCE || _SVID_SOURCE
20
22 Never use this function. Use mkstemp(3) or tmpfile(3) instead.
23
24 The tempnam() function returns a pointer to a string that is a valid
25 filename, and such that a file with this name did not exist when temp‐
26 nam() checked. The filename suffix of the pathname generated will
27 start with pfx in case pfx is a non-NULL string of at most five bytes.
28 The directory prefix part of the pathname generated is required to be
29 "appropriate" (often that at least implies writable).
30
31 Attempts to find an appropriate directory go through the following
32 steps:
33
34 a) In case the environment variable TMPDIR exists and contains the name
35 of an appropriate directory, that is used.
36
37 b) Otherwise, if the dir argument is non-NULL and appropriate, it is
38 used.
39
40 c) Otherwise, P_tmpdir (as defined in <stdio.h>) is used when appropri‐
41 ate.
42
43 d) Finally an implementation-defined directory may be used.
44
45 The string returned by tempnam() is allocated using malloc(3) and hence
46 should be freed by free(3).
47
49 On success, the tempnam() function returns a pointer to a unique tempo‐
50 rary filename. It returns NULL if a unique name cannot be generated,
51 with errno set to indicate the error.
52
54 ENOMEM Allocation of storage failed.
55
57 For an explanation of the terms used in this section, see at‐
58 tributes(7).
59
60 ┌────────────────────────────────────────┬───────────────┬─────────────┐
61 │Interface │ Attribute │ Value │
62 ├────────────────────────────────────────┼───────────────┼─────────────┤
63 │tempnam() │ Thread safety │ MT-Safe env │
64 └────────────────────────────────────────┴───────────────┴─────────────┘
65
67 SVr4, 4.3BSD, POSIX.1-2001. POSIX.1-2008 marks tempnam() as obsolete.
68
70 Although tempnam() generates names that are difficult to guess, it is
71 nevertheless possible that between the time that tempnam() returns a
72 pathname, and the time that the program opens it, another program might
73 create that pathname using open(2), or create it as a symbolic link.
74 This can lead to security holes. To avoid such possibilities, use the
75 open(2) O_EXCL flag to open the pathname. Or better yet, use mk‐
76 stemp(3) or tmpfile(3).
77
78 SUSv2 does not mention the use of TMPDIR; glibc will use it only when
79 the program is not set-user-ID. On SVr4, the directory used under d)
80 is /tmp (and this is what glibc does).
81
82 Because it dynamically allocates memory used to return the pathname,
83 tempnam() is reentrant, and thus thread safe, unlike tmpnam(3).
84
85 The tempnam() function generates a different string each time it is
86 called, up to TMP_MAX (defined in <stdio.h>) times. If it is called
87 more than TMP_MAX times, the behavior is implementation defined.
88
89 tempnam() uses at most the first five bytes from pfx.
90
91 The glibc implementation of tempnam() fails with the error EEXIST upon
92 failure to find a unique name.
93
95 The precise meaning of "appropriate" is undefined; it is unspecified
96 how accessibility of a directory is determined.
97
99 mkstemp(3), mktemp(3), tmpfile(3), tmpnam(3)
100
102 This page is part of release 5.13 of the Linux man-pages project. A
103 description of the project, information about reporting bugs, and the
104 latest version of this page, can be found at
105 https://www.kernel.org/doc/man-pages/.
106
107
108
109 2021-03-22 TEMPNAM(3)