[PATCH libgpg-error 1/4] estream: Rework modestring handling.

Justus Winter justus at g10code.com
Wed Oct 19 12:20:41 CEST 2016


* src/estream.c (X_SAMETHREAD, X_SYSOPEN): New macros.
(parse_mode): Rework how information flows from here to 'es_create'.
Instead of using an integer flag per mode, use flags.
(init_stream_obj): Adapt accordingly.
(es_create): Likewise.
(_gpgrt_fopen): Likewise.
(_gpgrt_mopen): Likewise.
(_gpgrt_fopenmem): Likewise.
(_gpgrt_fopencookie): Likewise.
(_gpgrt_fdopen): Likewise.
(_gpgrt_fpopen): Likewise.
(do_w32open): Likewise.
(_gpgrt_freopen): Likewise.

Signed-off-by: Justus Winter <justus at g10code.com>
---
 src/estream.c | 90 +++++++++++++++++++++++++++++------------------------------
 1 file changed, 44 insertions(+), 46 deletions(-)

diff --git a/src/estream.c b/src/estream.c
index 95d7211..8901a80 100644
--- a/src/estream.c
+++ b/src/estream.c
@@ -1664,6 +1664,10 @@ func_file_create (void **cookie, int *filedes,
 
 
 
+/* Flags used by parse_mode and friends.  */
+#define X_SAMETHREAD	(1 << 0)
+#define X_SYSOPEN	(1 << 1)
+
 /* Parse the mode flags of fopen et al.  In addition to the POSIX
  * defined mode flags keyword parameters are supported.  These are
  * key/value pairs delimited by comma and optional white spaces.
@@ -1705,15 +1709,14 @@ func_file_create (void **cookie, int *filedes,
  */
 static int
 parse_mode (const char *modestr,
-            unsigned int *modeflags, int *samethread, int *sysopen,
+            unsigned int *modeflags,
+            unsigned int *r_xmode,
             unsigned int *r_cmode)
 {
   unsigned int omode, oflags, cmode;
   int got_cmode = 0;
 
-  *samethread = 0;
-  if (sysopen)
-    *sysopen = 0;
+  *r_xmode = 0;
 
   switch (*modestr)
     {
@@ -1795,7 +1798,7 @@ parse_mode (const char *modestr,
               _set_errno (EINVAL);
               return -1;
             }
-          *samethread = 1;
+          *r_xmode |= X_SAMETHREAD;
         }
       else if (!strncmp (modestr, "nonblock", 8))
         {
@@ -1815,8 +1818,7 @@ parse_mode (const char *modestr,
               _set_errno (EINVAL);
               return -1;
             }
-          if (sysopen)
-            *sysopen = 1;
+          *r_xmode |= X_SYSOPEN;
         }
     }
   if (!got_cmode)
@@ -1988,7 +1990,7 @@ static void
 init_stream_obj (estream_t stream,
                  void *cookie, es_syshd_t *syshd,
                  gpgrt_cookie_io_functions_t functions,
-                 unsigned int modeflags, int samethread)
+                 unsigned int modeflags, unsigned int xmode)
 {
   stream->intern->cookie = cookie;
   stream->intern->opaque = NULL;
@@ -2009,7 +2011,7 @@ init_stream_obj (estream_t stream,
   stream->intern->deallocate_buffer = 0;
   stream->intern->printable_fname = NULL;
   stream->intern->printable_fname_inuse = 0;
-  stream->intern->samethread = !!samethread;
+  stream->intern->samethread = !! (xmode & X_SAMETHREAD);
   stream->intern->onclose = NULL;
 
   stream->data_len = 0;
@@ -2073,7 +2075,7 @@ es_deinitialize (estream_t stream)
 static int
 es_create (estream_t *stream, void *cookie, es_syshd_t *syshd,
 	   gpgrt_cookie_io_functions_t functions, unsigned int modeflags,
-           int samethread, int with_locked_list)
+           unsigned int xmode, int with_locked_list)
 {
   estream_internal_t stream_internal_new;
   estream_t stream_new;
@@ -2102,7 +2104,7 @@ es_create (estream_t *stream, void *cookie, es_syshd_t *syshd,
   stream_new->unread_buffer_size = sizeof (stream_internal_new->unread_buffer);
   stream_new->intern = stream_internal_new;
 
-  init_stream_obj (stream_new, cookie, syshd, functions, modeflags, samethread);
+  init_stream_obj (stream_new, cookie, syshd, functions, modeflags, xmode);
   init_stream_lock (stream_new);
 
   err = do_list_add (stream_new, with_locked_list);
@@ -3037,8 +3039,8 @@ estream_t
 _gpgrt_fopen (const char *_GPGRT__RESTRICT path,
               const char *_GPGRT__RESTRICT mode)
 {
-  unsigned int modeflags, cmode;
-  int samethread, sysopen, create_called;
+  unsigned int modeflags, cmode, xmode;
+  int create_called;
   estream_t stream;
   void *cookie;
   int err;
@@ -3049,7 +3051,7 @@ _gpgrt_fopen (const char *_GPGRT__RESTRICT path,
   cookie = NULL;
   create_called = 0;
 
-  err = parse_mode (mode, &modeflags, &samethread, &sysopen, &cmode);
+  err = parse_mode (mode, &modeflags, &xmode, &cmode);
   if (err)
     goto out;
 
@@ -3061,7 +3063,7 @@ _gpgrt_fopen (const char *_GPGRT__RESTRICT path,
   syshd.u.fd = fd;
   create_called = 1;
   err = es_create (&stream, cookie, &syshd, estream_functions_fd, modeflags,
-                   samethread, 0);
+                   xmode, 0);
   if (err)
     goto out;
 
@@ -3101,12 +3103,11 @@ _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len,
   int create_called = 0;
   estream_t stream = NULL;
   void *cookie = NULL;
-  unsigned int modeflags;
-  int samethread;
+  unsigned int modeflags, xmode;
   int err;
   es_syshd_t syshd;
 
-  err = parse_mode (mode, &modeflags, &samethread, NULL, NULL);
+  err = parse_mode (mode, &modeflags, &xmode, NULL);
   if (err)
     goto out;
 
@@ -3119,7 +3120,7 @@ _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len,
   memset (&syshd, 0, sizeof syshd);
   create_called = 1;
   err = es_create (&stream, cookie, &syshd,
-                   estream_functions_mem, modeflags, samethread, 0);
+                   estream_functions_mem, modeflags, xmode, 0);
 
  out:
 
@@ -3134,15 +3135,14 @@ _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len,
 estream_t
 _gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode)
 {
-  unsigned int modeflags;
-  int samethread;
+  unsigned int modeflags, xmode;
   estream_t stream = NULL;
   void *cookie = NULL;
   es_syshd_t syshd;
 
   /* Memory streams are always read/write.  We use MODE only to get
      the append flag.  */
-  if (parse_mode (mode, &modeflags, &samethread, NULL, NULL))
+  if (parse_mode (mode, &modeflags, &xmode, NULL))
     return NULL;
   modeflags |= O_RDWR;
 
@@ -3154,7 +3154,7 @@ _gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode)
 
   memset (&syshd, 0, sizeof syshd);
   if (es_create (&stream, cookie, &syshd, estream_functions_mem, modeflags,
-                 samethread, 0))
+                 xmode, 0))
     (*estream_functions_mem.func_close) (cookie);
 
   if (stream)
@@ -3204,8 +3204,7 @@ _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie,
                     const char *_GPGRT__RESTRICT mode,
                     gpgrt_cookie_io_functions_t functions)
 {
-  unsigned int modeflags;
-  int samethread;
+  unsigned int modeflags, xmode;
   estream_t stream;
   int err;
   es_syshd_t syshd;
@@ -3213,13 +3212,13 @@ _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie,
   stream = NULL;
   modeflags = 0;
 
-  err = parse_mode (mode, &modeflags, &samethread, NULL, NULL);
+  err = parse_mode (mode, &modeflags, &xmode, NULL);
   if (err)
     goto out;
 
   memset (&syshd, 0, sizeof syshd);
   err = es_create (&stream, cookie, &syshd, functions, modeflags,
-                   samethread, 0);
+                   xmode, 0);
   if (err)
     goto out;
 
@@ -3232,8 +3231,8 @@ _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie,
 static estream_t
 do_fdopen (int filedes, const char *mode, int no_close, int with_locked_list)
 {
-  unsigned int modeflags;
-  int samethread, sysopen, create_called;
+  unsigned int modeflags, xmode;
+  int create_called;
   estream_t stream;
   void *cookie;
   int err;
@@ -3243,10 +3242,10 @@ do_fdopen (int filedes, const char *mode, int no_close, int with_locked_list)
   cookie = NULL;
   create_called = 0;
 
-  err = parse_mode (mode, &modeflags, &samethread, &sysopen, NULL);
+  err = parse_mode (mode, &modeflags, &xmode, NULL);
   if (err)
     goto out;
-  if (sysopen)
+  if (xmode & X_SYSOPEN)
     {
       /* Not allowed for fdopen.  */
       _set_errno (EINVAL);
@@ -3262,7 +3261,7 @@ do_fdopen (int filedes, const char *mode, int no_close, int with_locked_list)
   syshd.u.fd = filedes;
   create_called = 1;
   err = es_create (&stream, cookie, &syshd, estream_functions_fd,
-                   modeflags, samethread, with_locked_list);
+                   modeflags, xmode, with_locked_list);
 
   if (!err && stream)
     {
@@ -3296,8 +3295,8 @@ _gpgrt_fdopen_nc (int filedes, const char *mode)
 static estream_t
 do_fpopen (FILE *fp, const char *mode, int no_close, int with_locked_list)
 {
-  unsigned int modeflags, cmode;
-  int samethread, sysopen, create_called;
+  unsigned int modeflags, cmode, xmode;
+  int create_called;
   estream_t stream;
   void *cookie;
   int err;
@@ -3307,10 +3306,10 @@ do_fpopen (FILE *fp, const char *mode, int no_close, int with_locked_list)
   cookie = NULL;
   create_called = 0;
 
-  err = parse_mode (mode, &modeflags, &samethread, &sysopen, &cmode);
+  err = parse_mode (mode, &modeflags, &xmode, &cmode);
   if (err)
     goto out;
-  if (sysopen)
+  if (xmode & X_SYSOPEN)
     {
       /* Not allowed for fpopen.  */
       _set_errno (EINVAL);
@@ -3328,7 +3327,7 @@ do_fpopen (FILE *fp, const char *mode, int no_close, int with_locked_list)
   syshd.u.fd = fp? fileno (fp): -1;
   create_called = 1;
   err = es_create (&stream, cookie, &syshd, estream_functions_fp,
-                   modeflags, samethread, with_locked_list);
+                   modeflags, xmode, with_locked_list);
 
  out:
 
@@ -3368,8 +3367,7 @@ estream_t
 do_w32open (HANDLE hd, const char *mode,
             int no_close, int with_locked_list)
 {
-  unsigned int modeflags, cmode;
-  int samethread;
+  unsigned int modeflags, cmode, xmode;
   int create_called = 0;
   estream_t stream = NULL;
   void *cookie = NULL;
@@ -3377,7 +3375,7 @@ do_w32open (HANDLE hd, const char *mode,
   es_syshd_t syshd;
 
   /* For obvious reasons we ignore sysmode here.  */
-  err = parse_mode (mode, &modeflags, &samethread, NULL, &cmode);
+  err = parse_mode (mode, &modeflags, &xmode, &cmode);
   if (err)
     goto leave;
 
@@ -3389,7 +3387,7 @@ do_w32open (HANDLE hd, const char *mode,
   syshd.u.handle = hd;
   create_called = 1;
   err = es_create (&stream, cookie, &syshd, estream_functions_w32,
-                   modeflags, samethread, with_locked_list);
+                   modeflags, xmode, with_locked_list);
 
  leave:
   if (err && create_called)
@@ -3540,8 +3538,8 @@ _gpgrt_freopen (const char *_GPGRT__RESTRICT path,
 
   if (path)
     {
-      unsigned int modeflags, cmode;
-      int dummy, samethread, create_called;
+      unsigned int modeflags, cmode, xmode, dummy;
+      int create_called;
       void *cookie;
       int fd;
       es_syshd_t syshd;
@@ -3549,13 +3547,13 @@ _gpgrt_freopen (const char *_GPGRT__RESTRICT path,
       cookie = NULL;
       create_called = 0;
 
-      samethread = stream->intern->samethread;
+      xmode = stream->intern->samethread ? X_SAMETHREAD : 0;
 
       lock_stream (stream);
 
       es_deinitialize (stream);
 
-      err = parse_mode (mode, &modeflags, &dummy, NULL, &cmode);
+      err = parse_mode (mode, &modeflags, &dummy, &cmode);
       if (err)
 	goto leave;
       (void)dummy;
@@ -3568,7 +3566,7 @@ _gpgrt_freopen (const char *_GPGRT__RESTRICT path,
       syshd.u.fd = fd;
       create_called = 1;
       init_stream_obj (stream, cookie, &syshd, estream_functions_fd,
-                       modeflags, samethread);
+                       modeflags, xmode);
 
     leave:
 
-- 
2.9.3




More information about the Gnupg-devel mailing list