mkstemp(), mkstemps()
Make a unique temporary filename and open the file
Synopsis:
#include <stdlib.h>
int mkstemp( char* template );
int mkstemps( char *template,
int slen );
Arguments:
- template
- A template for the filename that you want to use. For mkstemp(), this template can be any file name with at least six X characters appended to it (for example, /tmp/temp.XXXXXX); for mkstemps(), it also includes a suffix (e.g., /tmp/temp.XXXXXXsuffix).
- slen
- (mkstemps() only) The length of the suffix included in the template.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The mkstemp() and mkstemps() functions take the given file name template and overwrite a portion of it to create a filename.
The generated file name is unique and suitable for use by the application. The Xs are replaced with the current process number and/or a unique letter combination. The number of unique file names that these functions can return depends on the number of Xs provided; the functions try at least 231 combinations before giving up.
The mkstemp() and mkstemps() functions (unlike mktemp()) create the template file, mode 0600 (i.e., read-write for the owner), returning a file descriptor opened for reading and writing. This avoids the race between testing for a file's existence and opening it for use.
Returns:
The file descriptor of the temporary file, or -1 if no suitable file could be created (errno is set).
Errors:
- EINVAL
- The template doesn't include at least six X wildcard characters, or (for mkstemps()) the length of the template is less than slen.
- EEXIST
- All the names tried already exist. Consider adding more wildcard characters to the template.
This function may also set errno to any value specified by open().
Classification:
mkstemp() is POSIX 1003.1; mkstemps() is OpenBSD
Safety: | |
---|---|
Cancellation point | Yes |
Signal handler | Yes |
Thread | Yes |