mkstemp(3C)


mkstemp -- make a unique file name

Synopsis

   #include <stdlib.h>
   
   int mkstemp(char *template);

Description

The mkstemp function replaces the contents of the string pointed to by template by a unique file name, and returns a file descriptor for the file open for reading and writing. The function thus prevents any possible race condition between testing whether the file exists and opening it for use.

The string in template should look like a file name with six trailing X characters (XXXXXX); mkstemp replaces each X with a character from the portable file name character set. The characters are chosen such that the resulting name does not duplicate the name of an existing file.

Return values

Upon successful completion, mkstemp returns an open file descriptor. Otherwise -1 is returned if no suitable file could be created.

Usage

It is possible to run out of letters.

The mkstemp function does not check to determine whether the file name part of template exceeds the maximum allowable file name length.

mkstemp actually changes the template string which you pass; this means that you cannot use the same template string more than once -- you need a fresh template for every unique file you want to open.

When mkstemp is creating a new unique filename it checks for the prior existence of a file with that name. This means that if you are creating more than one unique filename, it is bad practice to use the same root template for multiple invocations of mkstemp.

Compatibility

For portability with previous versions of the UNIX® system, tmpfile(3S) is preferred over this function.

References

getpid(2), open(2), tmpfile(3S), tmpnam(3S)

Standards conformance

This routine conforms to X/Open System Interfaces and Headers, Issue 4, Version 2.
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004