mkstemp - make a unique filename
[XSI]
#include <stdlib.h>
int mkstemp(char *template);
The mkstemp() function shall replace the contents of the string pointed to bytemplate by a unique filename, andreturn a file descriptor for the file open for reading and writing. The function thus prevents any possible race condition betweentesting whether the file exists and opening it for use. The string intemplate should look like a filename with six trailing'X' s;mkstemp() replaces each 'X' with a character from the portable filename character set. Thecharacters are chosen such that the resulting name does not duplicate the name of an existing file at the time of a call tomkstemp().
Upon successful completion, mkstemp() shall return an open file descriptor. Otherwise, -1 shall be returned if nosuitable file could be created.
No errors are defined.
Generating a Filename
The following example creates a file with a 10-character name beginning with the characters"file" and opens the filefor reading and writing. The value returned as the value offd is a file descriptor that identifies the file.
#include <stdlib.h> ... char template[] = "/tmp/fileXXXXXX"; int fd; fd = mkstemp(template);
It is possible to run out of letters.
The mkstemp() function need not check to determine whether the filename part oftemplate exceeds the maximumallowable filename length.
#########################################################################################################################
tempnam - create a name for a temporary file
[XSI]
#include <stdio.h>
char *tempnam(const char *dir, const char *pfx);
The tempnam() function shall generate a pathname that may be used for a temporary file.
The tempnam() function allows the user to control the choice of a directory. Thedir argument points to the nameof the directory in which the file is to be created. Ifdir is a null pointer or points to a string which is not a name foran appropriate directory, the path prefix defined as P_tmpdir in the<stdio.h>header shall be used. If that directory is not accessible, an implementation-defined directory may be used.
Many applications prefer their temporary files to have certain initial letter sequences in their names. Thepfx argumentshould 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 beginningof the filename.
Some implementations of tempnam() may use tmpnam() internally. On suchimplementations, if called more than {TMP_MAX} times in a single process, the behavior is implementation-defined.
Upon successful completion, tempnam() shall allocate space for a string, put the generated pathname in that space, andreturn a pointer to it. The pointer shall be suitable for use in a subsequent call tofree(). Otherwise, it shall return a null pointer and seterrno to indicate theerror.
The tempnam() function shall fail if:
- [ENOMEM]
- Insufficient storage space is available.
Generating a Pathname
The following example generates a pathname for a temporary file in directory /tmp, with the prefix file. After thefilename has been created, the call tofree() deallocates the space used to store thefilename.
#include <stdio.h> #include <stdlib.h> ... char *directory = "/tmp"; char *fileprefix = "file"; char *file; file = tempnam(directory, fileprefix); free(file);
This function only creates pathnames. It is the application's responsibility to create and remove the files. Between the time apathname is created and the file is opened, it is possible for some other process to create a file with the same name. Applicationsmay find tmpfile() more useful.