/** * \brief Replacement Procedures for the Standard C Library (stdlib) * * \author Mihael Schmidt * \date 27.05.2010 */ H nomain *------------------------------------------------------------------------- * Prototypen *------------------------------------------------------------------------- D mkstemp PR 10I 0 extproc('mkstemp') D template * value * /copy LIBC_H /copy IFSIO_H /** * \brief Create temporary file * * The mkstemp() function shall replace the contents of the string pointed * to by template by a unique filename, and return 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 filename with six trailing * 'X' s; mkstemp() replaces each 'X' with a character from the portable filename * character set. The characters are chosen such that the resulting name does not * duplicate the name of an existing file at the time of a call to mkstemp(). * * \param Template (null-terminated) * * \return File handle * * \info The open file handle must be closed by the calling program. */ P mkstemp B export D PI 10I 0 D template * value * D tname S 1024A based(template) D length S 10I 0 D index S 10I 0 D name S 20A varying D replacementString... D S 6A D flags S 10I 0 D mode S 10I 0 D fileHandle S 10I 0 D running S N inz(*on) /free length = strlen(template); index = %scan('XXXXXX' : tname); if (index = 0 or index > length); return -1; endif; dow (running); name = %str(tmpnamIFS(*omit)); replacementString = %subst(name : %len(name) - 5); %subst(tname : index : 6) = replacementString; if (access(tname : F_OK) < 0); running = *off; endif; enddo; // create file flags = O_RDWR + O_CREAT + O_EXCL; mode = S_IRUSR + S_IWUSR; fileHandle = open(tname : flags : mode); return fileHandle; /end-free P E