All rights reserved.
%sccs.include.redist.man%
@(#)tmpnam.3 5.10 (Berkeley) 06/22/90
#include <stdio.h> FILE * tmpfile(void); char * tmpnam(char *str); char * tempnam(const char *tmpdir, const char *prefix);
Tmpnam returns a pointer to a file name, in the directory ``/usr/tmp'', which did not reference an existing file at some indeterminate point in the past. If the argument s is non-NULL, this file name is copied to the buffer it references. Otherwise, memory to contain this file name is allocated by tmpnam . In either case, tmpnam returns a pointer to the file name; in the latter case, the return value may be used as a subsequent argument to free (3).
In the current implementation, the memory buffer referenced by s must be at least 16 bytes long.
Tempnam is similar to tmpnam, but provides the ability to specify the directory which will contain the temporary file and the file name prefix.
The environmental variable ``TMPDIR'' (if set), the argument dir (if non-NULL), the directory ``/usr/tmp'' and the directory ``/tmp'' are tried, in the listed order, as directories in which to store the temporary file. Tempnam allocates memory in which to store the file name; the returned pointer may be used as a subsequent argument to free (3). The argument prefix , if non-NULL, is used to specify a file name prefix, which will be the first part of the created file name.
Tmpnam and tempname return a NULL pointer if unable to allocate memory or find a file which may be created.
The manifest constants ``TMP_MAX'', ``P_tmpdir'' and ``L_tmpnam'', documented for the routines tmpnam and tempnam in other systems, are not available in this implementation. If the source code requires them, simply use:
There are three important problems with these interfaces (as well as with the historic mktemp (3) interface). First, there is an obvious race between file name selection and file creation. Second, most implementations provide only a limited number (usually 26) of possible temporary file names before file names will start being recycled. Third, the System V implementations of these functions (and mktemp ) use the access (2) system call to determine whether or not the temporary file may be created. This has obvious ramifications for setuid or setgid programs, complicating the portable use of these interfaces in such programs.
The mkstemp (3) interface has none of these problems; the mktemp(3) implementation in this system suffers only from the race condition described above.
The tmpfile interface should not be used if there is any possibility that the user does not wish the temporary file to be publicly readable or writable.