Contents|Index|Previous|Next

mktemp, mkstemp
[generate unused file name]

SYNOPSIS
#include <stdio.h>
char *mktemp(char *path);
int mkstemp(char *path);
char *_mktemp_r(void *reent, char *path);
int *_mkstemp_r(void *reent, char *path);

DESCRIPTION
mktemp and mkstemp attempt to generate a file name that is not yet in use for any existing file. mkstemp creates the file and opens it for reading and writing; mktemp simply generates the file name.

You supply a simple pattern for the generated file name, as the string at path. The pattern should be a valid filename (including path information if you wish) ending with some number of x characters. The generated filename will match the leading part of the name you supply, with the trailing x characters replaced by some combination of digits and letters.

The alternate functions, _mktemp_r and _mkstemp_r, are reentrant versions. The extra argument, reent, is a pointer to a reentrancy structure.

RETURNS
mktemp returns the pointer, path, to the modified string representing an unused filename, unless it could not generate one, or the pattern you provided is not suitable for a filename; in that case, it returns NULL.

mkstemp returns a file descriptor to the newly created file, unless it could not generate an unused filename, or the pattern you provided is not suitable for a filename; in that case, it returns -1.

COMPLIANCE
ANSI C does not require either mktemp or mkstemp; the System V Interface Definition requires mktemp as of Issue 2.

Supporting OS subroutines required: getpid, open, stat.