mktemp()

Updated: April 19, 2023

Make a unique temporary filename

Synopsis:

#include <stdlib.h>

char* mktemp( char* template );

Arguments:

template
A template for the filename that you want to use. This template can be any file name with at least six X characters appended to it, for example /tmp/temp.XXXXXX.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The mktemp() function takes the given file name template and overwrites a portion of it to create a filename. This file name is unique and suitable for use by the application. The trailing Xs are replaced with the current process number and/or a unique letter combination. The number of unique file names mktemp() can return depends on the number of Xs provided; it tries at least 231 combinations before giving up.

Note: Avoid using mktemp(), because a hostile process can exploit a race condition in the time between the generation of a temporary filename by mktemp() and the invoker's use of the temporary name. Use mkstemp() or mkdtemp() instead.

Returns:

A pointer to the template, or NULL on failure (errno is set).

Errors:

EINVAL
The template doesn't include at least six X wildcard characters.
ENOENT
The pathname portion of the template isn't an existing directory.

This function may also set errno to any value specified by stat().

Classification:

OpenBSD

POSIX 2008.1 removed the specification for this function.

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

Contributing author:

OpenBSD