xref: /csrg-svn/lib/libc/stdio/tmpnam.3 (revision 43486)
Copyright (c) 1988 The Regents of the University of California.
All rights reserved.

%sccs.include.redist.man%

@(#)tmpnam.3 5.10 (Berkeley) 06/22/90

TMPFILE 3 ""
C 7
NAME
tempnam, tmpfile, tmpnam - temporary file routines
SYNOPSIS
#include <stdio.h>

FILE *
tmpfile(void);

char *
tmpnam(char *str);

char *
tempnam(const char *tmpdir, const char *prefix);
DESCRIPTION
Tmpfile opens a file using a file name generated by the routine tmpnam (3), and returns a pointer to the stream associated with the file. The created file is unlinked before tmpfile returns, causing the contents of the file to be deleted automatically when the last reference to it is closed. The file is opened with the access value ``w+''. If tmpnam returns NULL, or if tmpfile is unable to open the file, a NULL pointer is returned.

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:

#define TMP_MAX 308915776

#define P_tmpdir "/usr/tmp"

#define L_tmpnam 1024

BUGS
These interfaces are provided for System V compatibility only. The mkstemp (3) interface is strongly preferred.

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.

SEE ALSO
fopen(3), mkstemp(3), mktemp(3)