]> git.lizzy.rs Git - zlib.git/commitdiff
Add "x" (O_EXCL) and "e" (O_CLOEXEC) modes support to gzopen().
authorMark Adler <madler@alumni.caltech.edu>
Sat, 3 Mar 2012 18:24:44 +0000 (10:24 -0800)
committerMark Adler <madler@alumni.caltech.edu>
Sat, 3 Mar 2012 18:27:57 +0000 (10:27 -0800)
gzlib.c
zlib.h

diff --git a/gzlib.c b/gzlib.c
index 7aedab8e2dfda81b938116fab42857f0a9d49a12..fec7fbf7f23371762c2a4291ef91ac4690962b0b 100644 (file)
--- a/gzlib.c
+++ b/gzlib.c
@@ -94,6 +94,7 @@ local gzFile gz_open(path, fd, mode)
     const char *mode;
 {
     gz_statep state;
+    int cloexec = 0, exclusive = 0;
 
     /* check input */
     if (path == NULL)
@@ -133,6 +134,12 @@ local gzFile gz_open(path, fd, mode)
                 return NULL;
             case 'b':       /* ignore -- will request binary anyway */
                 break;
+            case 'e':
+                cloexec = 1;
+                break;
+            case 'x':
+                exclusive = 1;
+                break;
             case 'f':
                 state->strategy = Z_FILTERED;
                 break;
@@ -183,10 +190,13 @@ local gzFile gz_open(path, fd, mode)
 #endif
 #ifdef O_BINARY
             O_BINARY |
+#endif
+#ifdef O_CLOEXEC
+            (cloexec ? O_CLOEXEC : 0) |
 #endif
             (state->mode == GZ_READ ?
                 O_RDONLY :
-                (O_WRONLY | O_CREAT | (
+                (O_WRONLY | O_CREAT | (exclusive ? O_EXCL : 0) | (
                     state->mode == GZ_WRITE ?
                         O_TRUNC :
                         O_APPEND))),
diff --git a/zlib.h b/zlib.h
index 2cbd98182429745fe5db5bb4e602469cc90f1a91..ca6123c0ef2d3d3cf85e7c7ba90a384df3efe7bc 100644 (file)
--- a/zlib.h
+++ b/zlib.h
@@ -1217,7 +1217,10 @@ ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
 
      "a" can be used instead of "w" to request that the gzip stream that will
    be written be appended to the file.  "+" will result in an error, since
-   reading and writing to the same gzip file is not supported.
+   reading and writing to the same gzip file is not supported.  The addition of
+   "x" when writing will create the file exclusively, which fails if the file
+   already exists.  On systems that support it, the addition of "e" when
+   reading or writing will set the flag to close the file on an execve() call.
 
      These functions, as well as gzip, will read and decode a sequence of gzip
    streams in a file.  The append function of gzopen() can be used to create