OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / man3 / tempnam.3
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.
11 .\"
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.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .TH TEMPNAM 3  2008-08-06 "" "Linux Programmer's Manual"
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
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
40 .SH DESCRIPTION
41 The
42 .BR tempnam ()
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
45 .BR tempnam ()
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).
54
55 Attempts to find an appropriate directory go through the following
56 steps:
57 .TP 3
58 a)
59 In case the environment variable
60 .B TMPDIR
61 exists and
62 contains the name of an appropriate directory, that is used.
63 .TP
64 b)
65 Otherwise, if the
66 .I dir
67 argument is non-NULL and appropriate, it is used.
68 .TP
69 c)
70 Otherwise,
71 .I P_tmpdir
72 (as defined in
73 .IR <stdio.h> )
74 is used when appropriate.
75 .TP
76 d)
77 Finally an implementation-defined directory may be used.
78 .PP
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).
85 .SH "RETURN VALUE"
86 The
87 .BR tempnam ()
88 function returns a pointer to a unique temporary
89 filename, or NULL if a unique name cannot be generated.
90 .SH ERRORS
91 .TP
92 .B ENOMEM
93 Allocation of storage failed.
94 .SH "CONFORMING TO"
95 SVr4, 4.3BSD, POSIX.1-2001.
96 POSIX.1-2008 marks
97 .BR tempnam ()
98 as obsolete.
99 .SH NOTES
100 Although
101 .BR tempnam ()
102 generates names that are difficult to guess,
103 it is nevertheless possible that between the time that
104 .BR tempnam ()
105 returns a pathname, and the time that the program opens it,
106 another program might create that pathname using
107 .BR open (2),
108 or create it as a symbolic link.
109 This can lead to security holes.
110 To avoid such possibilities, use the
111 .BR open (2)
112 .B O_EXCL
113 flag to open the pathname.
114 Or better yet, use
115 .BR mkstemp (3)
116 or
117 .BR tmpfile (3).
118
119 SUSv2 does not mention the use of
120 .BR TMPDIR ;
121 glibc will use it only
122 when the program is not set-user-ID.
123 On SVr4, the directory used under \fBd)\fP is
124 .I /tmp
125 (and this is what glibc does).
126 .LP
127 Because it dynamically allocates memory used to return the pathname,
128 .BR tempnam ()
129 is reentrant, and thus thread safe, unlike
130 .BR tmpnam (3).
131 .LP
132 The
133 .BR tempnam ()
134 function generates a different string each time it is called,
135 up to
136 .B TMP_MAX
137 (defined in
138 .IR <stdio.h> )
139 times.
140 If it is called more than
141 .B TMP_MAX
142 times,
143 the behavior is implementation defined.
144 .LP
145 .BR tempnam ()
146 uses at most the first five bytes from
147 .IR pfx .
148
149 The glibc implementation of
150 .BR tempnam ()
151 will fail with the error
152 .B EEXIST
153 upon failure to find a unique name.
154 .SH BUGS
155 The precise meaning of "appropriate" is undefined;
156 it is unspecified how accessibility of a directory is determined.
157
158 Never use this function.
159 Use
160 .BR mkstemp (3)
161 or
162 .BR tmpfile (3)
163 instead.
164 .SH "SEE ALSO"
165 .BR mkstemp (3),
166 .BR mktemp (3),
167 .BR tmpfile (3),
168 .BR tmpnam (3)