Date: Sun, 8 Mar 1998 14:44:04 +0200 (IST) From: Eli Zaretskii To: DJ Delorie cc: djgpp-workers AT delorie DOT com Subject: mkstemp made safer Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk Here's a change in `mkstemp' analogous to the change in `tmpfile'. *** src/libc/compat/stdio/mkstemp.c~0 Sun Jun 4 07:04:24 1995 --- src/libc/compat/stdio/mkstemp.c Fri Mar 6 20:00:08 1998 *************** *** 1,15 **** /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include #include #include int mkstemp (char *_template) { ! if (mktemp (_template)) ! return creat (_template, 0666); ! else { errno = ENOENT; ! return -1; ! } } --- 1,25 ---- + /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include #include #include + #include int mkstemp (char *_template) { ! char tmp_name[FILENAME_MAX]; ! int fd = -1; ! ! /* Make sure we create a non-exisiting file, even ! if race conditions exist with other processes. */ ! do ! strcpy(tmp_name, _template); ! while (mktemp (tmp_name) != NULL ! && (fd = _creatnew(tmp_name, 0, SH_DENYRW)) == -1); ! ! if (fd == -1) errno = ENOENT; ! ! return fd; } *** src/libc/compat/stdio/mkstemp.t~0 Sun Jul 23 09:46:40 1995 --- src/libc/compat/stdio/mkstemp.txh Fri Mar 6 20:02:22 1998 *************** *** 12,18 **** @var{template} is a file specification that ends with six trailing @code{X} characters. This function replaces the @code{XXXXXX} with a set of characters such that the resulting file name names a nonexisting ! file. It then creates and opens the file. Note that since MS-DOS is limited to eight characters for the file name, and since none of the @code{X}'s get replaced by a dot, you can only --- 12,19 ---- @var{template} is a file specification that ends with six trailing @code{X} characters. This function replaces the @code{XXXXXX} with a set of characters such that the resulting file name names a nonexisting ! file. It then creates and opens the file in a way which guarantees that ! no other process can access this file. Note that since MS-DOS is limited to eight characters for the file name, and since none of the @code{X}'s get replaced by a dot, you can only *** src/docs/kb/wc202.t~5 Fri Mar 6 19:37:42 1998 --- src/docs/kb/wc202.txi Fri Mar 6 20:07:06 1998 *************** *** 346,348 **** --- 346,353 ---- though the named file already existed (created by another process). This window is now closed. @findex open + + @code{mkstemp} now makes sure the file it creates is unique even if + other processes can create files in the same directory, and opens the + file in DENY_ALL mode. + @findex mkstemp