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