]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl) |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
fea681da | 24 | .\" |
4b8c67d9 | 25 | .TH TEMPNAM 3 2017-09-15 "" "Linux Programmer's Manual" |
fea681da MK |
26 | .SH NAME |
27 | tempnam \- create a name for a temporary file | |
28 | .SH SYNOPSIS | |
29 | .nf | |
30 | .B #include <stdio.h> | |
68e4db0a | 31 | .PP |
fea681da MK |
32 | .BI "char *tempnam(const char *" dir ", const char *" pfx ); |
33 | .fi | |
68e4db0a | 34 | .PP |
cc4615cc MK |
35 | .in -4n |
36 | Feature Test Macro Requirements for glibc (see | |
37 | .BR feature_test_macros (7)): | |
38 | .in | |
68e4db0a | 39 | .PP |
cc4615cc | 40 | .BR tempnam (): |
51c612fb MK |
41 | Since glibc 2.19: |
42 | _DEFAULT_SOURCE | |
43 | Glibc 2.19 and earlier: | |
44 | _BSD_SOURCE || _SVID_SOURCE | |
fea681da | 45 | .SH DESCRIPTION |
47ce47ea MK |
46 | .I "Never use this function." |
47 | Use | |
48 | .BR mkstemp (3) | |
49 | or | |
50 | .BR tmpfile (3) | |
51 | instead. | |
847e0d88 | 52 | .PP |
fea681da | 53 | The |
63aa9df0 | 54 | .BR tempnam () |
fea681da MK |
55 | function returns a pointer to a string that is a valid filename, |
56 | and such that a file with this name did not exist when | |
63aa9df0 | 57 | .BR tempnam () |
fea681da MK |
58 | checked. |
59 | The filename suffix of the pathname generated will start with | |
60 | .I pfx | |
61 | in case | |
62 | .I pfx | |
63 | is a non-NULL string of at most five bytes. | |
64 | The directory prefix part of the pathname generated is required to | |
2d986c92 | 65 | be "appropriate" (often that at least implies writable). |
847e0d88 | 66 | .PP |
fea681da | 67 | Attempts to find an appropriate directory go through the following |
c13182ef | 68 | steps: |
0c629015 | 69 | .TP 3 |
c631f73d | 70 | a) |
097585ed MK |
71 | In case the environment variable |
72 | .B TMPDIR | |
73 | exists and | |
fea681da | 74 | contains the name of an appropriate directory, that is used. |
c631f73d MK |
75 | .TP |
76 | b) | |
77 | Otherwise, if the | |
fea681da MK |
78 | .I dir |
79 | argument is non-NULL and appropriate, it is used. | |
c631f73d MK |
80 | .TP |
81 | c) | |
82 | Otherwise, | |
fea681da MK |
83 | .I P_tmpdir |
84 | (as defined in | |
85 | .IR <stdio.h> ) | |
86 | is used when appropriate. | |
c631f73d MK |
87 | .TP |
88 | d) | |
89 | Finally an implementation-defined directory may be used. | |
c13182ef | 90 | .PP |
c631f73d MK |
91 | The string returned by |
92 | .BR tempnam () | |
93 | is allocated using | |
94 | .BR malloc (3) | |
95 | and hence should be freed by | |
96 | .BR free (3). | |
47297adb | 97 | .SH RETURN VALUE |
f528275c | 98 | On success, the |
63aa9df0 | 99 | .BR tempnam () |
52e58375 MK |
100 | function returns a pointer to a unique temporary filename. |
101 | It returns NULL if a unique name cannot be generated, with | |
102 | .I errno | |
103 | set to indicate the cause of the error. | |
fea681da MK |
104 | .SH ERRORS |
105 | .TP | |
106 | .B ENOMEM | |
107 | Allocation of storage failed. | |
3fdf5198 PH |
108 | .SH ATTRIBUTES |
109 | For an explanation of the terms used in this section, see | |
110 | .BR attributes (7). | |
111 | .TS | |
112 | allbox; | |
113 | lb lb lb | |
114 | l l l. | |
115 | Interface Attribute Value | |
116 | T{ | |
117 | .BR tempnam () | |
118 | T} Thread safety MT-Safe env | |
119 | .TE | |
47297adb | 120 | .SH CONFORMING TO |
44a2c328 | 121 | SVr4, 4.3BSD, POSIX.1-2001. |
d458f965 MK |
122 | POSIX.1-2008 marks |
123 | .BR tempnam () | |
124 | as obsolete. | |
fea681da | 125 | .SH NOTES |
c13182ef | 126 | Although |
2777b1ca | 127 | .BR tempnam () |
c631f73d MK |
128 | generates names that are difficult to guess, |
129 | it is nevertheless possible that between the time that | |
2777b1ca | 130 | .BR tempnam () |
c631f73d MK |
131 | returns a pathname, and the time that the program opens it, |
132 | another program might create that pathname using | |
c13182ef | 133 | .BR open (2), |
c631f73d MK |
134 | or create it as a symbolic link. |
135 | This can lead to security holes. | |
c13182ef | 136 | To avoid such possibilities, use the |
c631f73d MK |
137 | .BR open (2) |
138 | .B O_EXCL | |
c13182ef MK |
139 | flag to open the pathname. |
140 | Or better yet, use | |
c631f73d MK |
141 | .BR mkstemp (3) |
142 | or | |
143 | .BR tmpfile (3). | |
847e0d88 | 144 | .PP |
097585ed MK |
145 | SUSv2 does not mention the use of |
146 | .BR TMPDIR ; | |
147 | glibc will use it only | |
880f5b4b | 148 | when the program is not set-user-ID. |
68e1685c | 149 | On SVr4, the directory used under \fBd)\fP is |
0daa9e92 | 150 | .I /tmp |
c631f73d | 151 | (and this is what glibc does). |
dd3568a1 | 152 | .PP |
c631f73d | 153 | Because it dynamically allocates memory used to return the pathname, |
4733f53c | 154 | .BR tempnam () |
c631f73d MK |
155 | is reentrant, and thus thread safe, unlike |
156 | .BR tmpnam (3). | |
dd3568a1 | 157 | .PP |
fea681da | 158 | The |
63aa9df0 | 159 | .BR tempnam () |
fea681da | 160 | function generates a different string each time it is called, |
097585ed MK |
161 | up to |
162 | .B TMP_MAX | |
163 | (defined in | |
fea681da | 164 | .IR <stdio.h> ) |
c13182ef | 165 | times. |
097585ed | 166 | If it is called more than |
0daa9e92 | 167 | .B TMP_MAX |
097585ed | 168 | times, |
d9bfdb9c | 169 | the behavior is implementation defined. |
dd3568a1 | 170 | .PP |
c631f73d MK |
171 | .BR tempnam () |
172 | uses at most the first five bytes from | |
173 | .IR pfx . | |
847e0d88 | 174 | .PP |
7bc7af37 MK |
175 | The glibc implementation of |
176 | .BR tempnam () | |
26cd31fd | 177 | fails with the error |
7bc7af37 MK |
178 | .B EEXIST |
179 | upon failure to find a unique name. | |
fea681da | 180 | .SH BUGS |
ec5a588f | 181 | The precise meaning of "appropriate" is undefined; |
fea681da | 182 | it is unspecified how accessibility of a directory is determined. |
47297adb | 183 | .SH SEE ALSO |
fea681da MK |
184 | .BR mkstemp (3), |
185 | .BR mktemp (3), | |
186 | .BR tmpfile (3), | |
187 | .BR tmpnam (3) |