Make this i386-compatible; massive clean-up.
[unix-history] / usr / src / lib / libc / stdio / tmpnam.3
CommitLineData
4789dcfb 1.\" Copyright (c) 1988 The Regents of the University of California.
ecd109e0
KB
2.\" All rights reserved.
3.\"
4789dcfb 4.\" %sccs.include.redist.man%
ecd109e0 5.\"
96ea859a 6.\" @(#)tmpnam.3 5.11 (Berkeley) %G%
ecd109e0 7.\"
4789dcfb 8.TH TMPFILE 3 ""
ecd109e0
KB
9.UC 7
10.SH NAME
4789dcfb 11tempnam, tmpfile, tmpnam \- temporary file routines
ecd109e0
KB
12.SH SYNOPSIS
13.nf
4789dcfb
KB
14.ft B
15#include <stdio.h>
16
17FILE *
18tmpfile(void);
19
20char *
21tmpnam(char *str);
22
23char *
24tempnam(const char *tmpdir, const char *prefix);
25.ft R
26.fi
ecd109e0 27.SH DESCRIPTION
4789dcfb 28.I Tmpfile
96ea859a
KB
29returns a pointer to a stream associated with a file descriptor returned
30by the routine
31.IR mkstemp (3).
4789dcfb
KB
32The created file is unlinked before
33.I tmpfile
96ea859a
KB
34returns, causing the file to be automatically deleted when the last
35reference to it is closed.
4789dcfb 36The file is opened with the access value ``w+''.
4789dcfb
KB
37.PP
38.I Tmpnam
96ea859a 39returns a pointer to a file name, in the ``P_tmpdir'' directory, which
4789dcfb
KB
40did not reference an existing file at some indeterminate point in the
41past.
96ea859a 42P_tmpdir is defined in the include file <stdio.h>.
4789dcfb
KB
43If the argument
44.I s
96ea859a
KB
45is non-NULL, the file name is copied to the buffer it references.
46Otherwise, the file name is copied to a static buffer.
4789dcfb
KB
47In either case,
48.I tmpnam
96ea859a 49returns a pointer to the file name.
4789dcfb 50.PP
96ea859a 51The buffer referenced by
4789dcfb 52.I s
96ea859a
KB
53is expected to be at least ``L_tmpnam'' bytes in length.
54L_tmpnam is defined in the include file <stdio.h>.
4789dcfb
KB
55.PP
56.I Tempnam
57is similar to
58.I tmpnam,
59but provides the ability to specify the directory which will
60contain the temporary file and the file name prefix.
61.PP
62The environmental variable ``TMPDIR'' (if set), the argument
63.I dir
96ea859a 64(if non-NULL), the directory P_tmpdir, and the directory ``/tmp''
4789dcfb
KB
65are tried, in the listed order, as directories in which to store the
66temporary file.
96ea859a 67.PP
4789dcfb
KB
68The argument
69.IR prefix ,
70if non-NULL, is used to specify a file name prefix, which will be the
71first part of the created file name.
96ea859a
KB
72.I Tempnam
73allocates memory in which to store the file name; the returned pointer
74may be used as a subsequent argument to
75.IR free (3).
76.SH "RETURN VALUES"
77.I Tmpfile
78returns a pointer to an open file stream on success, and a NULL pointer
79on error.
4789dcfb
KB
80.PP
81.I Tmpnam
82and
96ea859a
KB
83.I tempfile
84return a pointer to a file name on success, and a NULL pointer
85on error.
86.SH ERRORS
87.I Tmpfile
88may fail and set
89.I errno
90for any of the errors specified for the library functions
91.IR fdopen (3)
92or
93.IR mkstemp (3).
4789dcfb 94.PP
96ea859a
KB
95.I Tmpnam
96may fail and set
97.I errno
98for any of the errors specified for the library function
99.IR mktemp (3).
4789dcfb 100.PP
96ea859a
KB
101.I Tempnam
102may fail and set
103.I errno
104for any of the errors specified for the library functions
105.IR malloc (3)
106or
107.IR mktemp (3).
4789dcfb 108.SH BUGS
96ea859a 109These interfaces are provided for System V and ANSI compatibility only.
4789dcfb
KB
110The
111.IR mkstemp (3)
112interface is strongly preferred.
113.PP
96ea859a 114There are four important problems with these interfaces (as well as
4789dcfb
KB
115with the historic
116.IR mktemp (3)
117interface).
118First, there is an obvious race between file name selection and file
96ea859a
KB
119creation and deletion.
120Second, most historic implementations provide only a limited number
121of possible temporary file names (usually 26) before file names will
122start being recycled.
123Third, the System V implementations of these functions (and of
4789dcfb
KB
124.IR mktemp )
125use the
126.IR access (2)
96ea859a 127function to determine whether or not the temporary file may be created.
4789dcfb
KB
128This has obvious ramifications for setuid or setgid programs, complicating
129the portable use of these interfaces in such programs.
96ea859a
KB
130Finally, there is no specification of the permissions with which the
131temporary files are created.
4789dcfb 132.PP
96ea859a
KB
133This implementation does not have these flaws, but portable software
134cannot depend on that.
135In particular, the
4789dcfb 136.I tmpfile
96ea859a
KB
137interface should not be used in software expected to be used on other systems
138if there is any possibility that the user does not wish the temporary file to
139be publicly readable and writable.
140.SH STANDARDS
141.I Tmpfile
142and
143.I tmpnam
144conform to ANSI X3.159-1989 (``ANSI C'').
4789dcfb 145.SH SEE ALSO
96ea859a 146mkstemp(3), mktemp(3)