Sed wants to use the glibc invention of mkostemp as enhanced by gnulib, in
order to control the use of O_TEXT vs. O_BINARY vs. 0 (for native mount)
mode[1].  glibc added this interface for other reasons as well - it is
also useful to specify O_CLOEXEC, O_APPEND, and/or O_SYNC on some
temporary files.  In glibc, the flags argument explicitly excludes
O_ACCMODE bits, since temp files are already specified as O_RDWR, so I
copied that pattern.  (man-pages 3.23 documents mkostemp since glibc 2.7,
but fails to document mkostemps, even though it was added at the same time
as mkstemps in glibc 2.11).

[1] http://lists.gnu.org/archive/html/bug-coreutils/2010-07/msg00114.html

Okay to commit, along with a corresponding patch to doc/new-features.sgml
and a cygwin-specific patch to newlib's stdlib.h?

2010-07-19  Eric Blake  <[email protected]>

        * mktemp.cc (_gettemp): Add flags argument.  All callers updated.
        (mkostemp, mkostemps): New functions.
        * cygwin.din (mkostemp, mkostemps): Export.
        * posix.sgml: Document them.
        * include/cygwin/version.h: Bump version.

-- 
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
 winsup/cygwin/ChangeLog                |    8 ++++++++
 winsup/cygwin/cygwin.din               |    2 ++
 winsup/cygwin/include/cygwin/version.h |    3 ++-
 winsup/cygwin/mktemp.cc                |   28 +++++++++++++++++++++-------
 winsup/cygwin/posix.sgml               |    2 ++
 5 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index f0ecd19..d9a67d7 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
diff --git a/winsup/cygwin/cygwin.din b/winsup/cygwin/cygwin.din
index fa5c77d..9253d2c 100644
--- a/winsup/cygwin/cygwin.din
+++ b/winsup/cygwin/cygwin.din
@@ -995,6 +995,8 @@ mknod SIGFE
 _mknod = mknod SIGFE
 _mknod32 = mknod32 SIGFE
 mknodat SIGFE
+mkostemp SIGFE
+mkostemps SIGFE
 mkstemp SIGFE
 _mkstemp = mkstemp SIGFE
 mkstemps SIGFE
diff --git a/winsup/cygwin/include/cygwin/version.h 
b/winsup/cygwin/include/cygwin/version.h
index 3a95e51..9924e42 100644
--- a/winsup/cygwin/include/cygwin/version.h
+++ b/winsup/cygwin/include/cygwin/version.h
@@ -388,12 +388,13 @@ details. */
       226: Export __locale_mb_cur_max.
       227: Add pseudo_reloc_start, pseudo_reloc_end, image_base to per_process.
       228: CW_STRERROR added.
+      229: Add mkostemp, mkostemps.
      */

      /* Note that we forgot to bump the api for ualarm, strtoll, strtoull */

 #define CYGWIN_VERSION_API_MAJOR 0
-#define CYGWIN_VERSION_API_MINOR 228
+#define CYGWIN_VERSION_API_MINOR 229

      /* There is also a compatibity version number associated with the
        shared memory regions.  It is incremented when incompatible
diff --git a/winsup/cygwin/mktemp.cc b/winsup/cygwin/mktemp.cc
index b8a1381..7770c3b 100644
--- a/winsup/cygwin/mktemp.cc
+++ b/winsup/cygwin/mktemp.cc
@@ -10,7 +10,7 @@ See the copyright at the bottom of this file. */
 #include <sys/stat.h>
 #include <unistd.h>

-static int _gettemp(char *, int *, int, size_t);
+static int _gettemp(char *, int *, int, size_t, int);
 static uint32_t arc4random ();

 static const char padchar[] =
@@ -20,30 +20,44 @@ extern "C" int
 mkstemp(char *path)
 {
   int fd;
-  return _gettemp(path, &fd, 0, 0) ? fd : -1;
+  return _gettemp(path, &fd, 0, 0, O_BINARY) ? fd : -1;
 }

 extern "C" char *
 mkdtemp(char *path)
 {
-  return _gettemp(path, NULL, 1, 0) ? path : NULL;
+  return _gettemp(path, NULL, 1, 0, 0) ? path : NULL;
 }

 extern "C" int
 mkstemps(char *path, int len)
 {
   int fd;
-  return _gettemp(path, &fd, 0, len) ? fd : -1;
+  return _gettemp(path, &fd, 0, len, O_BINARY) ? fd : -1;
+}
+
+extern "C" int
+mkostemp(char *path, int flags)
+{
+  int fd;
+  return _gettemp(path, &fd, 0, 0, flags & ~O_ACCMODE) ? fd : -1;
+}
+
+extern "C" int
+mkostemps(char *path, int len, int flags)
+{
+  int fd;
+  return _gettemp(path, &fd, 0, len, flags & ~O_ACCMODE) ? fd : -1;
 }

 extern "C" char *
 mktemp(char *path)
 {
-  return _gettemp(path, NULL, 0, 0) ? path : (char *) NULL;
+  return _gettemp(path, NULL, 0, 0, 0) ? path : (char *) NULL;
 }

 static int
-_gettemp(char *path, int *doopen, int domkdir, size_t suffixlen)
+_gettemp(char *path, int *doopen, int domkdir, size_t suffixlen, int flags)
 {
   char *start, *trv, *suffp;
   char *pad;
@@ -105,7 +119,7 @@ _gettemp(char *path, int *doopen, int domkdir, size_t 
suffixlen)
     {
       if (doopen)
        {
-         if ((*doopen = open (path, O_CREAT | O_EXCL | O_RDWR | O_BINARY,
+         if ((*doopen = open (path, O_CREAT | O_EXCL | O_RDWR | flags,
                               S_IRUSR | S_IWUSR)) >= 0)
            return 1;
          if (errno != EEXIST)
diff --git a/winsup/cygwin/posix.sgml b/winsup/cygwin/posix.sgml
index 6a3bc22..fdd7589 100644
--- a/winsup/cygwin/posix.sgml
+++ b/winsup/cygwin/posix.sgml
@@ -1043,6 +1043,8 @@ also IEEE Std 1003.1-2008 (POSIX.1-2008).</para>
     lsetxattr
     memmem
     mempcpy
+    mkostemp
+    mkostemps
     pipe2
     pow10
     pow10f
-- 
1.7.1

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to