tmpnam, tempnam
Purpose
Constructs the name for a temporary file.
Library
Standard I/O Library (libc.a)
Syntax
#include <stdio.h>
char *tmpnam (s) char *tempnam (dir, pfx)
char *s; char *dir, *pfx;
Description
The tmpnam and tempnam subroutines generate file names
for temporary files.
The tmpnam subroutine generates a file name using the
path name defined as P_tmpdir in the stdio.h header file.
If the s parameter is NULL, the tmpnam subroutine places
its result into an internal static area and returns a
pointer to that area. The next call to this subroutine
destroys the contents of the area.
If the s parameter is not NULL, it is assumed to be the
address of an array of at least the number of bytes spec-
ified by L_tmpnam. L_tmpnam is a constant defined in
stdio.h. The tmpnam subroutine places its results into
that array and returns the value of the s parameter.
The tempnam subroutine allows you to control the choice
of a directory. The dir parameter points to the path
name of the directory in which the file is to be created.
If the dir parameter is NULL or points to a string which
is not a path name for an appropriate directory, the path
name defined as P_tmpdir in the stdio.h header file is
used. If that path name is not accessible, /tmp is used.
You can bypass the selection of a path name by providing
an environment variable, TMPDIR, in the user's environ-
ment. The value of the TMPDIR variable is a path name
for the desired temporary-file directory. If the TMPDIR
variable is used, both the dir parameter and L_tmpnam are
ignored.
The pfx parameter of the tempname subroutine allows you
to specify an initial character sequence with which the
file name begins. The pfx parameter can be NULL, or it
can point to a string of up to five characters to be used
as the first few characters of the temporary file name.
The tempnam subroutine uses the malloc subroutine to
obtain space for the constructed file name. The return
value is a pointer to this space. Therefore, the pointer
value returned by tempnam can be used as a parameter to
the free subroutine.
If the tempnam subroutine cannot return the expected
result for any reason (for example, if the malloc subrou-
tine fails, or if an appropriate directory cannot be
found), then it returns a NULL pointer.
Warning: The tmpnam and tempnam subroutines generate a
different file name each time they are called. If they
are called more than 4,096 times by a single process,
they start recycling previously used names.
Files created using these subroutines reside in a direc-
tory intended for temporary use, and their names are
unique. It is your responsibility to use the unlink
system call to remove the file when no longer needed.
Between the time a file name is created and the file is
opened, it is possible for some other process to create a
file with the same name. This should not happen if that
other process uses these subroutines or the mktemp sub-
routine, and if the file names are chosen to make dupli-
cation by other means unlikely.
Related Information
In this book: "creat," "unlink," "fopen, freopen,
fdopen," "malloc, free, realloc, calloc," "mktemp,"
"tmpfile," and "environment."