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
14 The tempnam() function returns a pointer to a string that is a valid
15 filename, and such that a file with this name did not exist when temp‐
16 nam() checked. The filename suffix of the pathname generated will
17 start with pfx in case pfx is a non-NULL string of at most five bytes.
18 The directory prefix part of the pathname generated is required to be
19 `appropriate' (often that at least implies writable).
20
21 Attempts to find an appropriate directory go through the following
22 steps:
23
24 a) In case the environment variable TMPDIR exists and contains the
25 name of an appropriate directory, that is used.
26
27 b) Otherwise, if the dir argument is non-NULL and appropriate, it
28 is used.
29
30 c) Otherwise, P_tmpdir (as defined in <stdio.h>) is used when
31 appropriate.
32
33 d) Finally an implementation-defined directory may be used.
34
35 The string returned by tempnam() is allocated using malloc(3) and hence
36 should be freed by free(3).
37
39 The tempnam() function returns a pointer to a unique temporary file‐
40 name, or NULL if a unique name cannot be generated.
41
43 ENOMEM Allocation of storage failed.
44
46 Although tempnam(3) generates names that are difficult to guess, it is
47 nevertheless possible that between the time that tempnam(3) returns a
48 pathname, and the time that the program opens it, another program might
49 create that pathname using open(2), or create it as a symbolic link.
50 This can lead to security holes. To avoid such possibilities, use the
51 open(2) O_EXCL flag to open the pathname. Or better yet, use
52 mkstemp(3) or tmpfile(3).
53
54 SUSv2 does not mention the use of TMPDIR; glibc will use it only when
55 the program is not set-user-ID. On SVr4, the directory used under d)
56 is /tmp (and this is what glibc does).
57
58 Because it dynamically allocates memory used to return the pathname,
59 tempnam() is reentrant, and thus thread safe, unlike tmpnam(3).
60
61 The tempnam() function generates a different string each time it is
62 called, up to TMP_MAX (defined in <stdio.h>) times. If it is called
63 more than TMP_MAX times, the behaviour is implementation defined.
64
65 tempnam() uses at most the first five bytes from pfx.
66
67 The glibc implementation of tempnam() will fail with the error EEXIST
68 upon failure to find a unique name.
69
71 The precise meaning of `appropriate' is undefined; it is unspecified
72 how accessibility of a directory is determined.
73
74 Never use this function. Use mkstemp(3) or tmpfile(3) instead.
75
77 SVr4, 4.3BSD, POSIX.1-2001
78
80 mkstemp(3), mktemp(3), tmpfile(3), tmpnam(3)
81
82
83
84 1999-06-14 TEMPNAM(3)