date and time created 90/06/23 16:23:27 by bostic
[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.\"
4789dcfb 6.\" @(#)tmpnam.3 5.10 (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
KB
28.I Tmpfile
29opens a file using a file name generated by the routine
30.IR tmpnam (3),
31and returns a pointer to the stream associated with the file.
32The created file is unlinked before
33.I tmpfile
34returns, causing the contents of the file to be deleted automatically
35when the last reference to it is closed.
36The file is opened with the access value ``w+''.
37If
38.I tmpnam
39returns NULL, or if
40.I tmpfile
41is unable to open the file, a NULL pointer is returned.
42.PP
43.I Tmpnam
44returns a pointer to a file name, in the directory ``/usr/tmp'', which
45did not reference an existing file at some indeterminate point in the
46past.
47If the argument
48.I s
49is non-NULL, this file name is copied to the buffer it references.
50Otherwise, memory to contain this file name is allocated by
51.IR tmpnam .
52In either case,
53.I tmpnam
54returns a pointer to the file name; in the latter case, the return
55value may be used as a subsequent argument to
56.IR free (3).
57.PP
58In the current implementation, the memory buffer referenced by
59.I s
60must be at least 16 bytes long.
61.PP
62.I Tempnam
63is similar to
64.I tmpnam,
65but provides the ability to specify the directory which will
66contain the temporary file and the file name prefix.
67.PP
68The environmental variable ``TMPDIR'' (if set), the argument
69.I dir
70(if non-NULL), the directory ``/usr/tmp'' and the directory ``/tmp''
71are tried, in the listed order, as directories in which to store the
72temporary file.
73.I Tempnam
74allocates memory in which to store the file name; the returned pointer
75may be used as a subsequent argument to
76.IR free (3).
77The argument
78.IR prefix ,
79if non-NULL, is used to specify a file name prefix, which will be the
80first part of the created file name.
81.PP
82.I Tmpnam
83and
84.I tempname
85return a NULL pointer if unable to allocate memory or find a file
86which may be created.
87.PP
88The manifest constants ``TMP_MAX'', ``P_tmpdir'' and ``L_tmpnam'',
89documented for the routines
90.I tmpnam
91and
92.I tempnam
93in other systems, are not available in this implementation.
94If the source code requires them, simply use:
95.sp
96.RS
97#define TMP_MAX 308915776
98.br
99#define P_tmpdir "/usr/tmp"
100.br
101#define L_tmpnam 1024
102.RE
103.PP
104.SH BUGS
105These interfaces are provided for System V compatibility only.
106The
107.IR mkstemp (3)
108interface is strongly preferred.
109.PP
110There are three important problems with these interfaces (as well as
111with the historic
112.IR mktemp (3)
113interface).
114First, there is an obvious race between file name selection and file
115creation.
116Second, most implementations provide only a limited number (usually
11726) of possible temporary file names before file names will start being
118recycled.
119Third, the System V implementations of these functions (and
120.IR mktemp )
121use the
122.IR access (2)
123system call to determine whether or not the temporary file may be created.
124This has obvious ramifications for setuid or setgid programs, complicating
125the portable use of these interfaces in such programs.
126.PP
127The
128.IR mkstemp (3)
129interface has none of these problems;
130the
131.IR mktemp(3)
132implementation in this system suffers only from the race condition described
133above.
134.PP
135The
136.I tmpfile
137interface should not be used if there is any possibility that the user
138does not wish the temporary file to be publicly readable or writable.
139.PP
140.SH SEE ALSO
141fopen(3), mkstemp(3), mktemp(3)