3 * Original Author: G. Haley
7 <<tmpnam>>, <<tempnam>>---name for a temporary file
20 char *tmpnam(char *<[s]>);
21 char *tempnam(char *<[dir]>, char *<[pfx]>);
22 char *_tmpnam_r(struct _reent *<[reent]>, char *<[s]>);
23 char *_tempnam_r(struct _reent *<[reent]>, char *<[dir]>, char *<[pfx]>);
26 Use either of these functions to generate a name for a temporary file.
27 The generated name is guaranteed to avoid collision with other files
28 (for up to <<TMP_MAX>> calls of either function).
30 <<tmpnam>> generates file names with the value of <<P_tmpdir>>
31 (defined in `<<stdio.h>>') as the leading directory component of the path.
33 You can use the <<tmpnam>> argument <[s]> to specify a suitable area
34 of memory for the generated filename; otherwise, you can call
35 <<tmpnam(NULL)>> to use an internal static buffer.
37 <<tempnam>> allows you more control over the generated filename: you
38 can use the argument <[dir]> to specify the path to a directory for
39 temporary files, and you can use the argument <[pfx]> to specify a
40 prefix for the base filename.
42 If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
43 environment variable <<TMPDIR>> instead; if there is no such value,
44 <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
46 If you don't need any particular prefix to the basename of temporary
47 files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
49 <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
50 and <<tempnam>> respectively. The extra argument <[reent]> is a
51 pointer to a reentrancy structure.
54 The generated filenames are suitable for temporary files, but do not
55 in themselves make files temporary. Files with these names must still
56 be explicitly removed when you no longer want them.
58 If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
59 that it has room for at least <<L_tmpnam>> elements of type <<char>>.
62 Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
66 ANSI C requires <<tmpnam>>, but does not specify the use of
67 <<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
68 both <<tmpnam>> and <<tempnam>>.
70 Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
71 <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
73 The global pointer <<environ>> is also required.
85 #ifdef _REENT_THREAD_LOCAL
86 _Thread_local
int _tls_inc
;
87 _Thread_local
char _tls_emergency
[_REENT_EMERGENCY_SIZE
];
90 /* Try to open the file specified, if it can't be opened then try
91 another one. Return nonzero if successful, otherwise zero. */
94 worker (struct _reent
*ptr
,
101 /* Generate the filename and make sure that there isn't one called
107 _sprintf_r (ptr
, result
, "%s/%s%x.%x", part1
, part2
, part3
, *part4
);
109 t
= _open_r (ptr
, result
, O_RDONLY
, 0);
112 if (_REENT_ERRNO(ptr
) == ENOSYS
)
125 _tmpnam_r (struct _reent
*p
,
133 /* ANSI states we must use an internal static buffer if s is NULL */
134 _REENT_CHECK_EMERGENCY(p
);
135 result
= _REENT_EMERGENCY(p
);
143 if (worker (p
, result
, P_tmpdir
, "t", pid
, &_REENT_INC(p
)))
153 _tempnam_r (struct _reent
*p
,
159 const char *prefix
= (pfx
) ? pfx
: "";
160 if (dir
== NULL
&& (dir
= getenv ("TMPDIR")) == NULL
)
163 /* two 8 digit numbers + . / */
164 length
= strlen (dir
) + strlen (prefix
) + (4 * sizeof (int)) + 2 + 1;
166 filename
= _malloc_r (p
, length
);
169 if (! worker (p
, filename
, dir
, prefix
,
170 _getpid_r (p
) ^ (int) (_POINTER_INT
) p
, &_REENT_INC(p
)))
179 tempnam (const char *dir
,
182 return _tempnam_r (_REENT
, dir
, pfx
);
188 return _tmpnam_r (_REENT
, s
);