3 * Original Author: G. Haley
8 <<tmpnam>>, <<tempnam>>---name for a temporary file
21 char *tmpnam(char *<[s]>);
22 char *tempnam(char *<[dir]>, char *<[pfx]>);
23 char *_tmpnam_r(void *<[reent]>, char *<[s]>);
24 char *_tempnam_r(void *<[reent]>, char *<[dir]>, char *<[pfx]>);
31 char *tempnam(<[dir]>, <[pfx]>)
35 char *_tmpnam_r(<[reent]>, <[s]>)
39 char *_tempnam_r(<[reent]>, <[dir]>, <[pfx]>)
45 Use either of these functions to generate a name for a temporary file.
46 The generated name is guaranteed to avoid collision with other files
47 (for up to <<TMP_MAX>> calls of either function).
49 <<tmpnam>> generates file names with the value of <<P_tmpdir>>
50 (defined in `<<stdio.h>>') as the leading directory component of the path.
52 You can use the <<tmpnam>> argument <[s]> to specify a suitable area
53 of memory for the generated filename; otherwise, you can call
54 <<tmpnam(NULL)>> to use an internal static buffer.
56 <<tempnam>> allows you more control over the generated filename: you
57 can use the argument <[dir]> to specify the path to a directory for
58 temporary files, and you can use the argument <[pfx]> to specify a
59 prefix for the base filename.
61 If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
62 environment variable <<TMPDIR>> instead; if there is no such value,
63 <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
65 If you don't need any particular prefix to the basename of temporary
66 files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
68 <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
69 and <<tempnam>> respectively. The extra argument <[reent]> is a
70 pointer to a reentrancy structure.
73 The generated filenames are suitable for temporary files, but do not
74 in themselves make files temporary. Files with these names must still
75 be explicitly removed when you no longer want them.
77 If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
78 that it has room for at least <<L_tmpnam>> elements of type <<char>>.
81 Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
85 ANSI C requires <<tmpnam>>, but does not specify the use of
86 <<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
87 both <<tmpnam>> and <<tempnam>>.
89 Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
90 <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
92 The global pointer <<environ>> is also required.
103 /* Try to open the file specified, if it can't be opened then try
104 another one. Return nonzero if successful, otherwise zero. */
107 worker (ptr
, result
, part1
, part2
, part3
, part4
)
115 /* Generate the filename and make sure that there isn't one called
121 _sprintf_r (ptr
, result
, "%s/%s%x.%x", part1
, part2
, part3
, *part4
);
123 t
= _open_r (ptr
, result
, O_RDONLY
, 0);
126 if (ptr
->_errno
== ENOSYS
)
139 _DEFUN (_tmpnam_r
, (p
, s
),
140 struct _reent
*p _AND
148 /* ANSI states we must use an internal static buffer if s is NULL */
149 _REENT_CHECK_EMERGENCY(p
);
150 result
= _REENT_EMERGENCY(p
);
158 if (worker (p
, result
, P_tmpdir
, "t", pid
, &p
->_inc
))
168 _DEFUN (_tempnam_r
, (p
, dir
, pfx
),
169 struct _reent
*p _AND
170 _CONST
char *dir _AND
175 _CONST
char *prefix
= (pfx
) ? pfx
: "";
176 if (dir
== NULL
&& (dir
= getenv ("TMPDIR")) == NULL
)
179 /* two 8 digit numbers + . / */
180 length
= strlen (dir
) + strlen (prefix
) + (4 * sizeof (int)) + 2 + 1;
182 filename
= _malloc_r (p
, length
);
185 if (! worker (p
, filename
, dir
, prefix
,
186 _getpid_r (p
) ^ (int) (_POINTER_INT
) p
, &p
->_inc
))
195 _DEFUN (tempnam
, (dir
, pfx
),
196 _CONST
char *dir _AND
199 return _tempnam_r (_REENT
, dir
, pfx
);
206 return _tmpnam_r (_REENT
, s
);