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