]>
Commit | Line | Data |
---|---|---|
fea681da MK |
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. | |
c13182ef | 11 | .\" |
fea681da MK |
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. | |
c13182ef | 19 | .\" |
fea681da MK |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" | |
cc4615cc | 23 | .TH TEMPNAM 3 2007-07-26 "" "Linux Programmer's Manual" |
fea681da MK |
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 | |
cc4615cc MK |
32 | .sp |
33 | .in -4n | |
34 | Feature Test Macro Requirements for glibc (see | |
35 | .BR feature_test_macros (7)): | |
36 | .in | |
37 | .sp | |
38 | .BR tempnam (): | |
39 | _BSD_SOURCE || _SVID_SOURCE | |
fea681da MK |
40 | .SH DESCRIPTION |
41 | The | |
63aa9df0 | 42 | .BR tempnam () |
fea681da MK |
43 | function returns a pointer to a string that is a valid filename, |
44 | and such that a file with this name did not exist when | |
63aa9df0 | 45 | .BR tempnam () |
fea681da MK |
46 | checked. |
47 | The filename suffix of the pathname generated will start with | |
48 | .I pfx | |
49 | in case | |
50 | .I pfx | |
51 | is a non-NULL string of at most five bytes. | |
52 | The directory prefix part of the pathname generated is required to | |
53 | be `appropriate' (often that at least implies writable). | |
c631f73d | 54 | |
fea681da | 55 | Attempts to find an appropriate directory go through the following |
c13182ef | 56 | steps: |
c631f73d MK |
57 | .TP |
58 | a) | |
097585ed MK |
59 | In case the environment variable |
60 | .B TMPDIR | |
61 | exists and | |
fea681da | 62 | contains the name of an appropriate directory, that is used. |
c631f73d MK |
63 | .TP |
64 | b) | |
65 | Otherwise, if the | |
fea681da MK |
66 | .I dir |
67 | argument is non-NULL and appropriate, it is used. | |
c631f73d MK |
68 | .TP |
69 | c) | |
70 | Otherwise, | |
fea681da MK |
71 | .I P_tmpdir |
72 | (as defined in | |
73 | .IR <stdio.h> ) | |
74 | is used when appropriate. | |
c631f73d MK |
75 | .TP |
76 | d) | |
77 | Finally an implementation-defined directory may be used. | |
c13182ef | 78 | .PP |
c631f73d MK |
79 | The string returned by |
80 | .BR tempnam () | |
81 | is allocated using | |
82 | .BR malloc (3) | |
83 | and hence should be freed by | |
84 | .BR free (3). | |
fea681da MK |
85 | .SH "RETURN VALUE" |
86 | The | |
63aa9df0 | 87 | .BR tempnam () |
c13182ef | 88 | function returns a pointer to a unique temporary |
fea681da MK |
89 | filename, or NULL if a unique name cannot be generated. |
90 | .SH ERRORS | |
91 | .TP | |
92 | .B ENOMEM | |
93 | Allocation of storage failed. | |
2b2581ee MK |
94 | .SH "CONFORMING TO" |
95 | SVr4, 4.3BSD, POSIX.1-2001 | |
fea681da | 96 | .SH NOTES |
c13182ef | 97 | Although |
c631f73d MK |
98 | .BR tempnam (3) |
99 | generates names that are difficult to guess, | |
100 | it is nevertheless possible that between the time that | |
101 | .BR tempnam (3) | |
102 | returns a pathname, and the time that the program opens it, | |
103 | another program might create that pathname using | |
c13182ef | 104 | .BR open (2), |
c631f73d MK |
105 | or create it as a symbolic link. |
106 | This can lead to security holes. | |
c13182ef | 107 | To avoid such possibilities, use the |
c631f73d MK |
108 | .BR open (2) |
109 | .B O_EXCL | |
c13182ef MK |
110 | flag to open the pathname. |
111 | Or better yet, use | |
c631f73d MK |
112 | .BR mkstemp (3) |
113 | or | |
114 | .BR tmpfile (3). | |
115 | ||
097585ed MK |
116 | SUSv2 does not mention the use of |
117 | .BR TMPDIR ; | |
118 | glibc will use it only | |
880f5b4b | 119 | when the program is not set-user-ID. |
68e1685c | 120 | On SVr4, the directory used under \fBd)\fP is |
c13182ef | 121 | .IR /tmp |
c631f73d MK |
122 | (and this is what glibc does). |
123 | .LP | |
124 | Because it dynamically allocates memory used to return the pathname, | |
4733f53c | 125 | .BR tempnam () |
c631f73d MK |
126 | is reentrant, and thus thread safe, unlike |
127 | .BR tmpnam (3). | |
fea681da MK |
128 | .LP |
129 | The | |
63aa9df0 | 130 | .BR tempnam () |
fea681da | 131 | function generates a different string each time it is called, |
097585ed MK |
132 | up to |
133 | .B TMP_MAX | |
134 | (defined in | |
fea681da | 135 | .IR <stdio.h> ) |
c13182ef | 136 | times. |
097585ed MK |
137 | If it is called more than |
138 | .BR TMP_MAX | |
139 | times, | |
d9bfdb9c | 140 | the behavior is implementation defined. |
fea681da | 141 | .LP |
c631f73d MK |
142 | .BR tempnam () |
143 | uses at most the first five bytes from | |
144 | .IR pfx . | |
7bc7af37 MK |
145 | |
146 | The glibc implementation of | |
147 | .BR tempnam () | |
c13182ef | 148 | will fail with the error |
7bc7af37 MK |
149 | .B EEXIST |
150 | upon failure to find a unique name. | |
fea681da MK |
151 | .SH BUGS |
152 | The precise meaning of `appropriate' is undefined; | |
153 | it is unspecified how accessibility of a directory is determined. | |
c631f73d | 154 | |
c13182ef MK |
155 | Never use this function. |
156 | Use | |
fea681da | 157 | .BR mkstemp (3) |
c631f73d MK |
158 | or |
159 | .BR tmpfile (3) | |
fea681da | 160 | instead. |
fea681da MK |
161 | .SH "SEE ALSO" |
162 | .BR mkstemp (3), | |
163 | .BR mktemp (3), | |
164 | .BR tmpfile (3), | |
165 | .BR tmpnam (3) |