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