Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

tempnam - create pathname for temporary file

&pagelevel(4)&pagelevel

Syntax

#include <stdio.h>

char *tempnam(const char *dir, const char *pfx); 

Description

tempnam() generates a pathname that may be used for a temporary file.
tempnam() allows the user to control the choice of a directory.

dir points to the name of the directory in which the file is to be created. If the environment
variable TMPDIR is set, the directory specified there is used; otherwise, the one named
under *dir. If dir is a null pointer and the directory {P_tmpdir} does not name an
accessible directory, the file names are generated with the directory name /tmp. If this is
not accessible either, 0 is returned.

P_tmpdir is defined in stdio.h as "/var/tmp" as the directory in which temporary files
are created.

Many applications prefer their temporary files to have specific initial letter sequences in their
names. The pfx argument should be used for this. This argument may be a null pointer or
point to a string of up to five bytes to be used as the first bytes in the name of the temporary
file.

The name component generated by tempnam() is made up of two parts: the first comprises
three uppercase letters (AAA, BAA, ..., ZAA, ZBA, ..., ZZZ); the second consists of a letter
and the last five characters of the process ID. If the process ID consists of less than five
characters, it is padded to five characters with leading zeros. For example, a complete
name produced would be: /var/tmp/AAAa00123.

tempnam() uses malloc() to obtain space for the generated file name and returns a
pointer to that area. Thus, any pointer value returned by tempnam() can serve as an
argument to free() (see malloc()). If tempnam() cannot return the expected result for
some reason, e.g. because malloc() failed or no appropriate directory could be found, a
null pointer is returned.

tempnam() will fail if not enough memory is available.

Return val.

Pointer to a string containing the generated pathname



if successful.


Null pointer

if an error occurs. errno is set to indicate the error.


Null pointer

if /tmp is not accessible. errno remains unchanged

Errors

tempnam() will fail if:

 

ENOMEM

There is not enough memory available for the new pathname.

 

Extension

 

EINVAL

tempnam() is not called from POSIX-environment, i.e. the PROGRAM-ENVIRONMENT variable is not set to SHELL. (End)

Notes

tempnam() is executed only for POSIX files.

tempnam() generates a different pathname at each call.

Files created using tempnam() and either fopen() or creat() are temporary only in the
sense that they reside in a directory intended for temporary use, and their names are
unique. It is the user's responsibility to remove a file when it is no longer needed. If this
function is called more than {TMP_MAX} (defined in stdio.h) times in a single process, the
names created earlier will be reused.

Between the time a pathname is created and the file is opened, it is possible for some other
process to create a file with the same name. However, this will not occur if the other process
is using tempnam() or mktemp() and if the pathname is chosen so as to render duplication
by other means unlikely.

See also

fopen(), free(), open(), tmpfile(), tmpnam(), unlink(), stdio.h.