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