add mkstemps, mkdtemp

Eric Blake
Mon Jun 29 14:53:00 GMT 2009

Hash: SHA1

According to Eric Blake on 6/27/2009 4:41 PM:
> The ability to have an arbitrary file suffix on a temporary file is quite
> useful for programs that react to a file's extension; in fact, git wants
> to use mk[s]temps when available, even though no standards bodies have
> adopted it yet.  According to google, mkstemps is available on several
> systems (sometimes by the more confusing name mktemps), but there is no
> consistency on whether it should be in <stdlib.h> or <unistd.h>.  I went
> with <stdlib.h>, since that is where POSIX puts mkstemp.  This also fixes
> the documentation (mktemp is NOT in <stdio.h>), but does not move the
> files to a more appropriate subdirectory.

Updated patch.  Now that POSIX 2008 requires mkdtemp, we should also be
implementing that function at the same time.  POSIX also requires mkdtemp
to fail with EINVAL if there are not six trailing 'X' (with mktemp, the
behavior with less than 6 'X' was implementation defined, but it is
easiest to define it to do the same as mkdtemp).  mktemp is ELIX level 2,
but I'm guessing that mkdtemp and mkstemps should be ELIX level 4.  Cygwin
doesn't use newlib's mktemp.c any more (the getpid() implementation is
puny, so cygwin replaced it with an arc4random() implementation).  Also,
fixes a theoretical bug of moving into the suffix and calling isdigit on
an 8-bit signed char if you have all 36**6 file name candidates occupied
(in reality, I doubt anyone will ever have a file system that full).

2009-06-29  Eric Blake  <>

	Add mkdtemp, mkstemps.
	* libc/stdio/mktemp.c: Fix documentation.
	(_gettemp): Add domkdir and length parameters.  Check for
	insufficient 'X' suffix.  Drop cygwin-specific code.
	(_mkstemp_r, _mktemp_r, mkstemp, mktemp): Adjust clients.
	(_mkdtemp_r, _mkstemps_r, mkdtemp, mkstemps): New functions.
	* libc/include/stdlib.h (_mkdtemp_r, _mkstemps_r, mkdtemp)
	(mkstemps): Declare them.

- --
Don't work too hard, make some time for fun as well!

Eric Blake   
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at
Comment: Using GnuPG with Mozilla -

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: newlib.patch36
URL: <>

More information about the Newlib mailing list