commit-mailutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU Mailutils branch, master, updated. release-2.2-281-g5416e04


From: Sergey Poznyakoff
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-281-g5416e04
Date: Sat, 11 Dec 2010 09:07:18 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Mailutils".

http://git.savannah.gnu.org/cgit/mailutils.git/commit/?id=5416e044b0995e3f49676152da15866fcf29d323

The branch, master has been updated
       via  5416e044b0995e3f49676152da15866fcf29d323 (commit)
       via  1ef742582942bb2a7ef4ce8531cf3b306b3f82d7 (commit)
       via  841fc4842ae81579edab2a7752b0347944575d2c (commit)
      from  8f8d9036cc943e4884b21ad050a1d443e102e47e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 5416e044b0995e3f49676152da15866fcf29d323
Author: Sergey Poznyakoff <address@hidden>
Date:   Sat Dec 11 01:16:33 2010 +0200

    Implement mu_onexit functions; use MU stdstream in mh and mimeview.
    
    * include/mailutils/types.hin (mu_onexit_t): New typedef.
    * include/mailutils/util.h (mu_onexit_reset, mu_onexit): New protos.
    * libmailutils/base/onexit.c: New file.
    * libmailutils/base/Makefile.am (libbase_la_SOURCES): Add onexit.c
    
    * libmailutils/stdstream/basestr.c (stdstream_flushall): New function.
    (mu_stdstream_setup): Rewrite the fd checking algorithm.
    Register stdstream_flushall as an onexit function.
    
    * mh/anno.c: Use MU stdstream.
    * mh/mh.h: Include mailutils/stdstream.h.
    * mh/mh_init.c (mh_init): Call mu_stdstream_setup; use MU stdstream.
    * mh/mh_whatnow.c (_whatnow): Use MU stdstream.
    * mh/mhl.c (open_output,list_message): Likewise.
    * mh/mhn.c (mhn_show,store_handler): Likewise.
    * pop3d/popauth.c: Use MU stdstream.
    
    * mimeview/mimetypes.y: Use MU stdstream.
    * mimeview/mimeview.c: Likewise.
    (mimeview_fp): Remove.
    (mimeview_stream): New variable.
    * mimeview/mimeview.h (mimeview_fp): Remove.
    (mimeview_stream): New variable.
    
    * lib/mailcap.c (find_entry): Don't report ENOENT.

commit 1ef742582942bb2a7ef4ce8531cf3b306b3f82d7
Author: Sergey Poznyakoff <address@hidden>
Date:   Fri Dec 10 21:33:00 2010 +0200

    Remove the MU_STREAM_FD_AUTOCLOSE stream flag.
    
    This is the default behavior now.  The reverse can be requested by
    a special MU_IOCTL_FD,MU_IOCTL_FD_SET_BORROW ioctl.
    
    * include/mailutils/stream.h (MU_STREAM_FD_AUTOCLOSE): Remove. This
    is the default behavior now.
    (MU_IOCTL_FD): New ioctl.
    (MU_IOCTL_FD_GET_BORROW, MU_IOCTL_FD_SET_BORROW): New opcodes.
    * include/mailutils/sys/file_stream.h (_MU_FILE_STREAM_FD_BORROWED): New 
flag.
    
    * libmailutils/stream/file_stream.c (fd_close): Close the fd unless
    _MU_FILE_STREAM_FD_BORROWED bit is set.
    (fd_open): Clear the _MU_FILE_STREAM_FD_BORROWED bit.
    (fd_ioctl): Handle MU_IOCTL_FD.
    (mu_file_stream_create): Remove MU_STREAM_FD_AUTOCLOSE.
    
    * ibmailutils/base/version.c (mu_print_options): Set borrow mode on the
    temporary stdout stream.
    * libmailutils/cfg/driver.c (mu_cfg_tree_reduce): Set borrow mode on the
    temporary stderr stream.
    * libmailutils/stdstream/strerr.c (mu_stdstream_strerr_create): Set borrow
    mode on the stderr stream.
    * libmailutils/tests/tempfile.c: set borrow mode on the temporary stream,
    because it may be reopened later (verify mode).
    
    * examples/mta.c: Remove MU_STREAM_FD_AUTOCLOSE.
    * imap4d/io.c: Likewise.
    * libmailutils/stream/prog_stream.c: Likewise.
    * libmailutils/stream/socket_stream.c: Likewise.
    * libmailutils/stream/temp_file_stream.c: Likewise.
    * maidag/lmtp.c: Likewise.
    * pop3d/extra.c: Likewise.
    
    These will disappear when the corresponding utilities are rewritten to
    use MU stdstream:
    
    * mail/escape.c (quote0): set borrow mode on the output stream.
    * mimeview/mimeview.c (display_file): set borrow mode on the input stream.
    
    Likewise, these will disappear when MH uses MU stdstream:
    
    * mh/anno.c (main): Set borrow mode on the input stream.
    * mh/mh_init.c (mh_real_install): Likewise.
    * mh/mh_whatnow.c (_whatnow): Likewise.
    * mh/mhl.c (open_output): Set borrow mode on the output stream.
    * mh/mhn.c (mhn_show,store_handler): Likewise.

commit 841fc4842ae81579edab2a7752b0347944575d2c
Author: Sergey Poznyakoff <address@hidden>
Date:   Thu Dec 9 14:28:37 2010 +0200

    Remove MU_STREAM_AUTOCLOSE flag.  Use mu_stream_unref, if necessary.
    
    * examples/mta.c: Use MU_STREAM_FD_AUTOCLOSE.
    * imap4d/io.c: Likewise.
    * libmailutils/stream/file_stream.c: Likewise.
    * libmailutils/stream/prog_stream.c: Likewise.
    * libmailutils/stream/socket_stream.c: Likewise.
    * libmailutils/stream/temp_file_stream.c: Likewise.
    * maidag/lmtp.c: Likewise.
    * pop3d/extra.c: Likewise.
    
    * imap4d/preauth.c: Do not use MU_STREAM_AUTOCLOSE.  Instead unref the
    transport stream after creating the derived one.
    * libmailutils/base/rfc2047.c: Likewise.
    * libmailutils/stream/fltstream.c: Likewise.
    * libmailutils/tests/fltst.c: Likewise.
    * libmu_auth/ldap.c: Likewise.
    * libmu_auth/tls.c: Likewise.
    * libmu_sieve/extensions/vacation.c: Likewise.
    * libproto/pop/pop3_stream.c: Likewise.
    * mail/decode.c: Likewise.
    * mh/mhn.c: Likewise.
    
    * libmailutils/filter/filter_iconv.c: Do not handle MU_STREAM_AUTOCLOSE.
    * libmailutils/stream/rdcache_stream.c: Likewise.
    * libmailutils/stream/streamref.c: Likewise.
    * libmailutils/stream/xscript-stream.c: Likewise.
    
    * include/mailutils/stream.h (MU_STREAM_AUTOCLOSE): Rename to
    MU_STREAM_FD_AUTOCLOSE.

-----------------------------------------------------------------------

Summary of changes:
 comsat/action.c                        |    8 +--
 examples/mta.c                         |    3 +-
 imap4d/io.c                            |    6 +-
 imap4d/preauth.c                       |    4 +-
 include/mailutils/stream.h             |   16 +++-
 include/mailutils/sys/file_stream.h    |    5 +-
 include/mailutils/types.hin            |    4 +-
 include/mailutils/util.h               |    5 +
 lib/mailcap.c                          |    5 +-
 libmailutils/base/Makefile.am          |    1 +
 libmailutils/base/onexit.c             |   99 ++++++++++++++++++++++
 libmailutils/base/rfc2047.c            |    4 +-
 libmailutils/base/version.c            |    2 +
 libmailutils/cfg/driver.c              |    3 +
 libmailutils/filter/filter_iconv.c     |    3 +-
 libmailutils/stdstream/basestr.c       |   37 ++++++--
 libmailutils/stdstream/strerr.c        |    5 +
 libmailutils/stream/file_stream.c      |   26 +++++-
 libmailutils/stream/fltstream.c        |    3 +-
 libmailutils/stream/prog_stream.c      |    4 +-
 libmailutils/stream/rdcache_stream.c   |    5 +-
 libmailutils/stream/socket_stream.c    |    2 +-
 libmailutils/stream/streamref.c        |    3 +-
 libmailutils/stream/temp_file_stream.c |    3 +-
 libmailutils/stream/xscript-stream.c   |    8 +-
 libmailutils/tests/fltst.c             |    4 +-
 libmailutils/tests/tempfile.c          |    3 +
 libmu_argp/muinit.c                    |    2 +
 libmu_auth/ldap.c                      |   16 ++--
 libmu_auth/tls.c                       |   11 +--
 libmu_sieve/extensions/vacation.c      |    3 +-
 libproto/pop/pop3_stream.c             |    3 +-
 maidag/lmtp.c                          |    3 +-
 mail/decode.c                          |    7 +-
 mail/escape.c                          |    6 +-
 mh/anno.c                              |   15 +---
 mh/mh.h                                |    1 +
 mh/mh_init.c                           |   41 ++++-----
 mh/mh_whatnow.c                        |   16 +---
 mh/mhl.c                               |   12 ++-
 mh/mhn.c                               |   28 +++----
 mimeview/mimetypes.y                   |  143 ++++++++++++++++++--------------
 mimeview/mimeview.c                    |   23 ++----
 mimeview/mimeview.h                    |    2 +-
 pop3d/extra.c                          |    6 +-
 pop3d/popauth.c                        |   48 ++++++-----
 46 files changed, 409 insertions(+), 248 deletions(-)
 create mode 100644 libmailutils/base/onexit.c

diff --git a/comsat/action.c b/comsat/action.c
index 59f3c6f..e9cf2b0 100644
--- a/comsat/action.c
+++ b/comsat/action.c
@@ -570,13 +570,9 @@ eval_biffrc (struct biffrc_environ *env)
          if (!sp)
            report_error (env, _("unknown keyword"));
          else if (ws.ws_wordc < sp->argmin)
-           {
-             report_error (env, _("too few arguments"));
-           }
+           report_error (env, _("too few arguments"));
          else if (sp->argmax != -1 && ws.ws_wordc > sp->argmax)
-           {
-             report_error (env, _("too many arguments"));
-           }
+           report_error (env, _("too many arguments"));
          else
            {
              if (sp->expand)
diff --git a/examples/mta.c b/examples/mta.c
index 31b76d1..ecd8372 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -824,8 +824,7 @@ mta_smtp (int argc, char **argv)
          return 1;
        }
 
-      rc = mu_fd_stream_create (&str, NULL, sfd,
-                               MU_STREAM_RDWR|MU_STREAM_AUTOCLOSE);
+      rc = mu_fd_stream_create (&str, NULL, sfd, MU_STREAM_RDWR);
       if (rc)
        {
          mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
diff --git a/imap4d/io.c b/imap4d/io.c
index 0e9d71a..660602c 100644
--- a/imap4d/io.c
+++ b/imap4d/io.c
@@ -29,13 +29,11 @@ io_setio (int ifd, int ofd)
   if (ofd == -1)
     imap4d_bye (ERR_NO_OFILE);
 
-  if (mu_stdio_stream_create (&istream, ifd,
-                             MU_STREAM_READ | MU_STREAM_AUTOCLOSE))
+  if (mu_stdio_stream_create (&istream, ifd, MU_STREAM_READ))
     imap4d_bye (ERR_STREAM_CREATE);
   mu_stream_set_buffer (istream, mu_buffer_line, 0);
   
-  if (mu_stdio_stream_create (&ostream, ofd, 
-                              MU_STREAM_WRITE | MU_STREAM_AUTOCLOSE))
+  if (mu_stdio_stream_create (&ostream, ofd, MU_STREAM_WRITE))
     imap4d_bye (ERR_STREAM_CREATE);
   mu_stream_set_buffer (ostream, mu_buffer_line, 0);
   
diff --git a/imap4d/preauth.c b/imap4d/preauth.c
index 63cc5dc..1842647 100644
--- a/imap4d/preauth.c
+++ b/imap4d/preauth.c
@@ -193,8 +193,8 @@ decode64_buf (const char *name, unsigned char **pbuf, 
size_t *psize)
   name++;
   namelen = strlen (name) - 1;
   mu_static_memory_stream_create (&str, name, namelen);
-  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
-                   MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, MU_STREAM_READ);
+  mu_stream_unref (str);
   mu_stream_read (flt, buf, sizeof buf, &size);
   mu_stream_destroy (&flt);
   *pbuf = malloc (size);
diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index 9b88097..ec6164c 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -43,7 +43,7 @@ enum mu_buffer_type
 #define MU_STREAM_CREAT              0x00000010
 /* So far used only by TCP streams. */
 #define MU_STREAM_NONBLOCK    0x00000020
-#define MU_STREAM_AUTOCLOSE   0x00000040
+/* Not used                   0x00000040 */
 /* Not used. Intended for mailboxes only. */
 #define MU_STREAM_NONLOCK     0x00000080
 /* Not used as well           0x00000100  */
@@ -70,7 +70,8 @@ enum mu_buffer_type
 #define MU_IOCTL_NULLSTREAM       6 /* Null stream (see below) */
 #define MU_IOCTL_LOGSTREAM        7 /* Log stream (see below) */
 #define MU_IOCTL_XSCRIPTSTREAM    8 /* Transcript stream (see below) */
-
+#define MU_IOCTL_FD               9 /* File descriptor manipulation */
+  
   /* Opcodes common for various families */
 #define MU_IOCTL_OP_GET 0
 #define MU_IOCTL_OP_SET 1  
@@ -146,6 +147,17 @@ enum mu_buffer_type
      New transcript level is set to *X.  Previous level is stored in X.
   */
 #define MU_IOCTL_XSCRIPTSTREAM_LEVEL 0  
+
+  /* Opcodes for MU_IOCTL_FD */
+  /* Get "borrow state".  Borrowed descriptors remain in open state
+     after the stream is closed.
+     Arg: int *
+  */
+#define MU_IOCTL_FD_GET_BORROW 0
+  /* Set borrow state.
+     Arg: int *
+  */
+#define MU_IOCTL_FD_SET_BORROW 1
   
 #define MU_TRANSPORT_INPUT  0
 #define MU_TRANSPORT_OUTPUT 1
diff --git a/include/mailutils/sys/file_stream.h 
b/include/mailutils/sys/file_stream.h
index 581d289..e0e71d5 100644
--- a/include/mailutils/sys/file_stream.h
+++ b/include/mailutils/sys/file_stream.h
@@ -21,8 +21,9 @@
 #include <mailutils/stream.h>
 #include <mailutils/sys/stream.h>
 
-#define _MU_FILE_STREAM_TEMP     0x01
-#define _MU_FILE_STREAM_ECHO_OFF 0x02
+#define _MU_FILE_STREAM_TEMP        0x01
+#define _MU_FILE_STREAM_ECHO_OFF    0x02
+#define _MU_FILE_STREAM_FD_BORROWED 0x04
 
 struct _mu_file_stream
 {
diff --git a/include/mailutils/types.hin b/include/mailutils/types.hin
index 1f57b5b..78802f1 100644
--- a/include/mailutils/types.hin
+++ b/include/mailutils/types.hin
@@ -118,7 +118,9 @@ typedef struct _mu_opool *mu_opool_t;
 typedef struct _mu_progmailer *mu_progmailer_t;
 typedef struct _mu_secret *mu_secret_t;
 typedef struct _mu_mime_io_buffer *mu_mime_io_buffer_t;
-  
+
+typedef void (*mu_onexit_t) (void*);
+
 #define MU_FOLDER_ATTRIBUTE_DIRECTORY  0x001
 #define MU_FOLDER_ATTRIBUTE_FILE       0x002
 #define MU_FOLDER_ATTRIBUTE_LINK       0x004
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index e56b066..e020dcc 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -187,6 +187,11 @@ int mu_fd_wait (int fd, int *pflags, struct timeval *tvp);
 int mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab,
                           int *perr);
 
+/* Reset the onexit list. */
+void mu_onexit_reset (void);
+/* Register the onexit function and associated data */
+int mu_onexit (mu_onexit_t func, void *data);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/mailcap.c b/lib/mailcap.c
index d675ebc..27acc41 100644
--- a/lib/mailcap.c
+++ b/lib/mailcap.c
@@ -620,8 +620,9 @@ find_entry (const char *file, struct mime_context *ctx)
   status = mu_file_stream_create (&stream, file, MU_STREAM_READ);
   if (status)
     {
-      mu_error ("cannot create file stream %s: %s",
-               file, mu_strerror (status));
+      if (status != ENOENT)
+       mu_error ("cannot open input stream %s: %s",
+                 file, mu_strerror (status));
       return -1;
     }
 
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index 0fdc1a4..c2a4976 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -49,6 +49,7 @@ libbase_la_SOURCES = \
  nls.c\
  nullrec.c\
  observer.c\
+ onexit.c\
  opool.c\
  parsedate.c\
  permstr.c\
diff --git a/libmailutils/base/onexit.c b/libmailutils/base/onexit.c
new file mode 100644
index 0000000..81193ee
--- /dev/null
+++ b/libmailutils/base/onexit.c
@@ -0,0 +1,99 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/error.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/nls.h>
+#include <mailutils/stream.h>
+#include <mailutils/stdstream.h>
+
+struct onexit_closure
+{
+  mu_onexit_t function;
+  void *data;
+};
+
+static mu_list_t onexit_list;
+
+void
+_mu_onexit_run (void)
+{
+  mu_iterator_t itr;
+  int rc, status = 0;
+  
+  rc = mu_list_get_iterator (onexit_list, &itr);
+  if (rc)
+    {
+      mu_error (_("cannot create iterator, onexit aborted: %s"),
+               mu_strerror (rc));
+      mu_stream_destroy (&mu_strerr);
+      _exit (127);
+    }
+  
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      struct onexit_closure *cp;
+      int rc = mu_iterator_current (itr, (void**)&cp);
+      if (rc)
+       {
+         status = 127;
+         mu_error (_("cannot obtain current item while traversing the"
+                     " onexit action list: %s"), mu_strerror (rc));
+       }
+      else
+       cp->function (cp->data);
+      mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+    }
+  mu_iterator_destroy (&itr);
+  mu_list_destroy (&onexit_list);
+  if (status)
+    _exit (status);
+}
+
+int
+mu_onexit (mu_onexit_t func, void *data)
+{
+  struct onexit_closure *clos = malloc (sizeof (*clos));
+  if (!clos)
+    return ENOMEM;
+  clos->function = func;
+  clos->data = data;
+  if (!onexit_list)
+    {
+      int rc = mu_list_create (&onexit_list);
+      mu_list_set_destroy_item (onexit_list, mu_list_free_item);
+      if (rc)
+       return rc;
+      atexit (_mu_onexit_run);
+    }
+  return mu_list_append (onexit_list, clos);
+}
+
+void
+mu_onexit_reset (void)
+{
+  mu_list_clear (onexit_list);
+}
diff --git a/libmailutils/base/rfc2047.c b/libmailutils/base/rfc2047.c
index d634960..56b176c 100644
--- a/libmailutils/base/rfc2047.c
+++ b/libmailutils/base/rfc2047.c
@@ -269,8 +269,8 @@ mu_rfc2047_encode (const char *charset, const char 
*encoding,
   if (rc)
     return rc;
   rc = mu_filter_create (&output_stream, input_stream,
-                        encoding, MU_FILTER_ENCODE,
-                        MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+                        encoding, MU_FILTER_ENCODE, MU_STREAM_READ);
+  mu_stream_unref (input_stream);
   if (rc == 0)
     {
       /* Assume strlen(qp_encoded_text) <= strlen(text) * 3 */
diff --git a/libmailutils/base/version.c b/libmailutils/base/version.c
index 2373772..b5f6cd1 100644
--- a/libmailutils/base/version.c
+++ b/libmailutils/base/version.c
@@ -150,6 +150,7 @@ mu_print_options ()
     mu_stream_ref (mu_strout);
   else
     {
+      int yes = 1;
       int rc = mu_stdio_stream_create (&mu_strout, MU_STDOUT_FD, 0);
       if (rc)
        {
@@ -157,6 +158,7 @@ mu_print_options ()
                   MU_STDOUT_FD, mu_strerror (rc));
          abort ();
        }
+      mu_stream_ioctl (mu_strout, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);  
     }
   mu_format_options (mu_strout, 1);
   mu_stream_unref (mu_strout);
diff --git a/libmailutils/cfg/driver.c b/libmailutils/cfg/driver.c
index e9b1d52..3de716e 100644
--- a/libmailutils/cfg/driver.c
+++ b/libmailutils/cfg/driver.c
@@ -586,8 +586,11 @@ mu_cfg_tree_reduce (mu_cfg_tree_t *parse_tree, const char 
*progname,
     return 0;
   if (flags & MU_PARSE_CONFIG_DUMP)
     {
+      int yes = 1;
       mu_stream_t stream;
+      
       mu_stdio_stream_create (&stream, MU_STDERR_FD, 0);
+      mu_stream_ioctl (stream, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
       mu_cfg_format_parse_tree (stream, parse_tree, MU_CFG_FMT_LOCUS);
       mu_stream_destroy (&stream);
     }
diff --git a/libmailutils/filter/filter_iconv.c 
b/libmailutils/filter/filter_iconv.c
index cab442d..ae39370 100644
--- a/libmailutils/filter/filter_iconv.c
+++ b/libmailutils/filter/filter_iconv.c
@@ -453,8 +453,7 @@ mu_filter_iconv_create (mu_stream_t *s, mu_stream_t 
transport,
       return ENOMEM;
     }
   
-  if (!(flags & MU_STREAM_AUTOCLOSE))
-    mu_stream_ref (transport);
+  mu_stream_ref (transport);
   iptr->transport = transport;
   iptr->fallback_mode = fallback_mode;
   iptr->cd = cd;
diff --git a/libmailutils/stdstream/basestr.c b/libmailutils/stdstream/basestr.c
index ea9479f..a845927 100644
--- a/libmailutils/stdstream/basestr.c
+++ b/libmailutils/stdstream/basestr.c
@@ -27,11 +27,20 @@
 #include <mailutils/log.h>
 #include <mailutils/stream.h>
 #include <mailutils/stdstream.h>
+#include <mailutils/util.h>
 
 mu_stream_t mu_strin;
 mu_stream_t mu_strout;
 mu_stream_t mu_strerr;
 
+static void
+stdstream_flushall (void *data MU_ARG_UNUSED)
+{
+  mu_stream_flush (mu_strin);
+  mu_stream_flush (mu_strout);
+  mu_stream_flush (mu_strerr);
+}
+
 void
 mu_stdstream_setup ()
 {
@@ -44,20 +53,30 @@ mu_stdstream_setup ()
   mu_stream_destroy (&mu_strerr);
   
   /* Ensure that first 3 descriptors are open in proper mode */
-  fd = open ("/dev/null", O_WRONLY);
+  fd = open ("/dev/null", O_RDWR);
   switch (fd)
     {
-    case 2:
-      /* keep it open */;
-      break;
+    case 0:
+      /* Keep it and try to open 1 */
+      fd = open ("/dev/null", O_WRONLY);
+      if (fd != 1)
+       {
+         if (fd > 2)
+           close (fd);
+         break;
+       }
 
     case 1:
-      /* keep it open and try 0 */
-      fd = open ("/dev/null", O_RDONLY);
-      if (fd != 0)
+      /* keep it open and try 2 */
+      fd = open ("/dev/null", O_WRONLY);
+      if (fd != 2)
        close (fd);
       break;
-      
+
+    case 2:
+      /* keep it open */;
+      break;
+
     default:
       close (fd);
       break;
@@ -82,7 +101,7 @@ mu_stdstream_setup ()
   if (mu_stdstream_strerr_create (&mu_strerr, MU_STRERR_STDERR, 0, 0,
                                  mu_program_name, NULL))
     abort ();
-  /* FIXME: atexit (flushall) */
+  mu_onexit (stdstream_flushall, NULL);
 }
 
 int
diff --git a/libmailutils/stdstream/strerr.c b/libmailutils/stdstream/strerr.c
index 9028ae9..59cd5d2 100644
--- a/libmailutils/stdstream/strerr.c
+++ b/libmailutils/stdstream/strerr.c
@@ -43,6 +43,7 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, 
int facility,
     {
     case MU_STRERR_STDERR:
       {
+        int yes = 1;
        mu_stream_t str;
       
        rc = mu_stdio_stream_create (&str, MU_STDERR_FD, 0);
@@ -52,6 +53,10 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, 
int facility,
                     tag, mu_strerror (rc));
            return MU_ERR_FAILURE;
          }
+        /* Make sure 2 is not closed when str is destroyed.
+           FIXME: Actually I don't know if it is needed, but just
+                  in case... */
+        mu_stream_ioctl (str, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
        if (!tag)
          transport = str;
        else
diff --git a/libmailutils/stream/file_stream.c 
b/libmailutils/stream/file_stream.c
index ffaf5e4..e36f2e5 100644
--- a/libmailutils/stream/file_stream.c
+++ b/libmailutils/stream/file_stream.c
@@ -65,7 +65,7 @@ fd_close (struct _mu_stream *str)
   struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
   if (fstr->fd != -1)
     {
-      if ((str->flags & MU_STREAM_AUTOCLOSE) && close (fstr->fd))
+      if (!(fstr->flags & _MU_FILE_STREAM_FD_BORROWED) && close (fstr->fd))
        return errno;
       fstr->fd = -1;
     }
@@ -119,7 +119,7 @@ fd_open (struct _mu_stream *str)
     str->flags &= ~MU_STREAM_SEEK;
   
   /* Make sure it will be closed */
-  str->flags |= MU_STREAM_AUTOCLOSE;
+  fstr->flags &= ~_MU_FILE_STREAM_FD_BORROWED;
   
   fstr->fd = fd;
   return 0;
@@ -268,7 +268,25 @@ fd_ioctl (struct _mu_stream *str, int code, int opcode, 
void *ptr)
          }
        }
       break;
-      
+
+    case MU_IOCTL_FD:
+      if (!ptr)
+       return EINVAL;
+      switch (opcode)
+       {
+       case MU_IOCTL_FD_GET_BORROW:
+         *(int*) ptr = !!(fstr->flags & _MU_FILE_STREAM_FD_BORROWED);
+         break;
+
+       case MU_IOCTL_FD_SET_BORROW:
+         if (*(int*)ptr)
+           fstr->flags |= _MU_FILE_STREAM_FD_BORROWED;
+         else
+           fstr->flags &= ~_MU_FILE_STREAM_FD_BORROWED;
+         break;
+       }
+      break;
+           
     default:
       return ENOSYS;
     }
@@ -331,7 +349,7 @@ mu_file_stream_create (mu_stream_t *pstream, const char 
*filename, int flags)
   int rc = _mu_file_stream_create (&fstr,
                                   sizeof (struct _mu_file_stream),
                                   filename, -1,
-                                  flags | MU_STREAM_SEEK | 
MU_STREAM_AUTOCLOSE);
+                                  flags | MU_STREAM_SEEK);
   if (rc == 0)
     {
       mu_stream_t stream = (mu_stream_t) fstr;
diff --git a/libmailutils/stream/fltstream.c b/libmailutils/stream/fltstream.c
index 87ce1bb..8160d31 100644
--- a/libmailutils/stream/fltstream.c
+++ b/libmailutils/stream/fltstream.c
@@ -520,8 +520,7 @@ mu_filter_stream_create (mu_stream_t *pflt,
   fs->stream.error_string = filter_error_string;
   fs->stream.flags = flags;
 
-  if (!(flags & MU_STREAM_AUTOCLOSE))
-    mu_stream_ref (str);
+  mu_stream_ref (str);
   fs->transport = str;
   fs->xcode = xcode;
   fs->xdata = xdata;
diff --git a/libmailutils/stream/prog_stream.c 
b/libmailutils/stream/prog_stream.c
index 03a860f..37dc4ed 100644
--- a/libmailutils/stream/prog_stream.c
+++ b/libmailutils/stream/prog_stream.c
@@ -440,7 +440,7 @@ _prog_open (mu_stream_t stream)
   if (REDIRECT_STDOUT_P (flags))
     {
       rc = mu_stdio_stream_create (&fs->in, pfd[0],
-                                  
MU_STREAM_READ|MU_STREAM_AUTOCLOSE|seekable_flag);
+                                  MU_STREAM_READ|seekable_flag);
       if (rc)
        {
          _prog_close (stream);
@@ -451,7 +451,7 @@ _prog_open (mu_stream_t stream)
   if (REDIRECT_STDIN_P (flags))
     {
       rc = mu_stdio_stream_create (&fs->out, pfd[1],
-                                  
MU_STREAM_WRITE|MU_STREAM_AUTOCLOSE|seekable_flag);
+                                  MU_STREAM_WRITE|seekable_flag);
       if (rc)
        {
          _prog_close (stream);
diff --git a/libmailutils/stream/rdcache_stream.c 
b/libmailutils/stream/rdcache_stream.c
index df6e487..4f4cc7a 100644
--- a/libmailutils/stream/rdcache_stream.c
+++ b/libmailutils/stream/rdcache_stream.c
@@ -188,7 +188,7 @@ mu_rdcache_stream_create (mu_stream_t *pstream, mu_stream_t 
transport,
 {
   struct _mu_rdcache_stream *sp;
   int rc;
-  int sflags = MU_STREAM_READ | MU_STREAM_SEEK | (flags & MU_STREAM_AUTOCLOSE);
+  int sflags = MU_STREAM_READ | MU_STREAM_SEEK;
 
   if (flags & ~sflags)
     return EINVAL;
@@ -207,8 +207,7 @@ mu_rdcache_stream_create (mu_stream_t *pstream, mu_stream_t 
transport,
   sp->stream.ctl = rdcache_ioctl;
   sp->stream.wait = rdcache_wait;
 
-  if (!(flags & MU_STREAM_AUTOCLOSE))
-    mu_stream_ref (transport);
+  mu_stream_ref (transport);
   sp->transport = transport;
 
   if ((rc = mu_memory_stream_create (&sp->cache, MU_STREAM_RDWR)))
diff --git a/libmailutils/stream/socket_stream.c 
b/libmailutils/stream/socket_stream.c
index 825a9da..83f208e 100644
--- a/libmailutils/stream/socket_stream.c
+++ b/libmailutils/stream/socket_stream.c
@@ -99,7 +99,7 @@ mu_socket_stream_create (mu_stream_t *pstream, const char 
*filename, int flags)
   /* Create transport stream. */
   rc = _mu_file_stream_create (&fstr, sizeof (*fstr),
                               filename, -1,
-                              (flags | MU_STREAM_AUTOCLOSE) & ~MU_STREAM_SEEK);
+                              flags & ~MU_STREAM_SEEK);
   if (rc)
     return rc;
   fstr->stream.open = _socket_open;
diff --git a/libmailutils/stream/streamref.c b/libmailutils/stream/streamref.c
index 502cbf5..b524e6b 100644
--- a/libmailutils/stream/streamref.c
+++ b/libmailutils/stream/streamref.c
@@ -280,8 +280,7 @@ mu_streamref_create_abridged (mu_stream_t *pref, 
mu_stream_t str,
     return rc;
   mu_stream_get_flags (str, &flags);
   sp = (struct _mu_streamref *)
-         _mu_stream_create (sizeof (*sp), 
-                            (flags & ~MU_STREAM_AUTOCLOSE) | _MU_STR_OPEN);
+         _mu_stream_create (sizeof (*sp), flags | _MU_STR_OPEN);
   if (!sp)
     return ENOMEM;
 
diff --git a/libmailutils/stream/temp_file_stream.c 
b/libmailutils/stream/temp_file_stream.c
index c1dfacd..1cf71f9 100644
--- a/libmailutils/stream/temp_file_stream.c
+++ b/libmailutils/stream/temp_file_stream.c
@@ -67,8 +67,7 @@ mu_temp_file_stream_create (mu_stream_t *pstream,
                               NULL,
                               -1,
                               MU_STREAM_RDWR | MU_STREAM_SEEK |
-                              MU_STREAM_CREAT | 
-                              MU_STREAM_AUTOCLOSE);
+                              MU_STREAM_CREAT);
   if (rc == 0)
     {
       struct _mu_temp_file_stream *tstr = (struct _mu_temp_file_stream *)str;
diff --git a/libmailutils/stream/xscript-stream.c 
b/libmailutils/stream/xscript-stream.c
index 578fbea..ff0d75c 100644
--- a/libmailutils/stream/xscript-stream.c
+++ b/libmailutils/stream/xscript-stream.c
@@ -438,11 +438,9 @@ mu_xscript_stream_create(mu_stream_t *pref, mu_stream_t 
transport,
   sp->stream.event_cb = _xscript_event_cb;
   sp->stream.event_mask = _MU_STR_EVMASK(_MU_STR_EVENT_FILLBUF) |
                           _MU_STR_EVMASK(_MU_STR_EVENT_FLUSHBUF);
-  if (!(flags & MU_STREAM_AUTOCLOSE))
-    {
-      mu_stream_ref (transport);
-      mu_stream_ref (logstr);
-    }
+  mu_stream_ref (transport);
+  mu_stream_ref (logstr);
+
   sp->transport = transport;
   sp->logstr = logstr;
   
diff --git a/libmailutils/tests/fltst.c b/libmailutils/tests/fltst.c
index db73e9d..b639445 100644
--- a/libmailutils/tests/fltst.c
+++ b/libmailutils/tests/fltst.c
@@ -148,8 +148,8 @@ main (int argc, char * argv [])
       MU_ASSERT (mu_filter_create_args (&flt, in, fltname,
                                        argc, (const char **)argv,
                                        mode,
-                                       MU_STREAM_READ|MU_STREAM_SEEK|
-                                       MU_STREAM_AUTOCLOSE));
+                                       MU_STREAM_READ|MU_STREAM_SEEK));
+      mu_stream_unref (in);
       if (shift)
        MU_ASSERT (mu_stream_seek (flt, shift, MU_SEEK_SET, NULL));
       c_copy (out, flt);
diff --git a/libmailutils/tests/tempfile.c b/libmailutils/tests/tempfile.c
index 0df762f..03f4d8d 100644
--- a/libmailutils/tests/tempfile.c
+++ b/libmailutils/tests/tempfile.c
@@ -49,6 +49,7 @@ main (int argc, char **argv)
   char *infile = NULL;
   int verify = 0;
   int verbose = 0;
+  int yes = 1;
 
   progname = argv[0];
   
@@ -139,6 +140,7 @@ main (int argc, char **argv)
        MU_ASSERT (mu_file_stream_create (&in, infile, MU_STREAM_READ));
 
       MU_ASSERT (mu_fd_stream_create (&out, filename, fd, MU_STREAM_WRITE));
+      mu_stream_ioctl (out, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
       MU_ASSERT (mu_stream_copy (out, in, 0, &size));
       if (verbose)
        printf ("copied %lu bytes to the temporary\n", (unsigned long) size);
@@ -154,6 +156,7 @@ main (int argc, char **argv)
       MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
       MU_ASSERT (mu_fd_stream_create (&in, filename, fd,
                                      MU_STREAM_READ|MU_STREAM_SEEK));
+      mu_stream_ioctl (in, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
       MU_ASSERT (mu_stream_copy (out, in, 0, &size));
       if (verbose)
        printf ("dumped %lu bytes\n", (unsigned long) size);
diff --git a/libmu_argp/muinit.c b/libmu_argp/muinit.c
index d85b25c..d014ce9 100644
--- a/libmu_argp/muinit.c
+++ b/libmu_argp/muinit.c
@@ -136,7 +136,9 @@ mu_app_init (struct argp *myargp, const char **capa,
       char *comment;
       char *canonical_name = get_canonical_name ();
       mu_stream_t stream;
+
       mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
+
       mu_asprintf (&comment,
                   "Configuration file structure for %s utility.",
                   mu_program_name);
diff --git a/libmu_auth/ldap.c b/libmu_auth/ldap.c
index 31fa026..859c778 100644
--- a/libmu_auth/ldap.c
+++ b/libmu_auth/ldap.c
@@ -599,8 +599,8 @@ chk_md5 (const char *db_pass, const char *pass)
   mu_md5_finish_ctx (&md5context, md5digest);
 
   mu_static_memory_stream_create (&str, db_pass, strlen (db_pass));
-  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
-                   MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, MU_STREAM_READ);
+  mu_stream_unref (str);
 
   mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
   mu_stream_destroy (&flt);
@@ -621,8 +621,8 @@ chk_smd5 (const char *db_pass, const char *pass)
   
   size = strlen (db_pass);
   mu_static_memory_stream_create (&str, db_pass, size);
-  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
-                   MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, MU_STREAM_READ);
+  mu_stream_unref (str);
 
   d1 = malloc (size);
   if (!d1)
@@ -664,8 +664,8 @@ chk_sha (const char *db_pass, const char *pass)
   mu_sha1_finish_ctx (&sha1context, sha1digest);
 
   mu_static_memory_stream_create (&str, db_pass, strlen (db_pass));
-  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
-                   MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, MU_STREAM_READ);
+  mu_stream_unref (str);
 
   mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
   mu_stream_destroy (&flt);
@@ -686,8 +686,8 @@ chk_ssha (const char *db_pass, const char *pass)
   
   size = strlen (db_pass);
   mu_static_memory_stream_create (&str, db_pass, size);
-  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
-                   MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, MU_STREAM_READ);
+  mu_stream_unref (str);
 
   d1 = malloc (size);
   if (!d1)
diff --git a/libmu_auth/tls.c b/libmu_auth/tls.c
index 80dbf25..288d3f7 100644
--- a/libmu_auth/tls.c
+++ b/libmu_auth/tls.c
@@ -327,8 +327,7 @@ _mu_tls_io_stream_create (mu_stream_t *pstream,
   /* FIXME:
      sp->stream.error_string = _tls_error_string;*/
 
-  if (!(flags & MU_STREAM_AUTOCLOSE))
-    mu_stream_ref (transport);
+  mu_stream_ref (transport);
   sp->transport = transport;
   sp->up = master;
   *pstream = (mu_stream_t) sp;
@@ -642,7 +641,6 @@ _mu_tls_stream_create (mu_stream_t *pstream,
                       mu_stream_t strin, mu_stream_t strout, int flags)
 {
   struct _mu_tls_stream *sp;
-  int autoclose = flags & MU_STREAM_AUTOCLOSE;
   int rc;
   mu_stream_t stream;
   
@@ -664,16 +662,15 @@ _mu_tls_stream_create (mu_stream_t *pstream,
 
   mu_stream_set_buffer (strin, mu_buffer_none, 0);
   mu_stream_set_buffer (strout, mu_buffer_none, 0);
-  rc = _mu_tls_io_stream_create (&sp->transport[0], strin,
-                                MU_STREAM_READ | autoclose, sp);
+  rc = _mu_tls_io_stream_create (&sp->transport[0], strin, MU_STREAM_READ, sp);
   if (rc)
     {
       free (sp);
       return rc;
     }
      
-  rc = _mu_tls_io_stream_create (&sp->transport[1], strout,
-                                MU_STREAM_WRITE | autoclose, sp);
+  rc = _mu_tls_io_stream_create (&sp->transport[1], strout, MU_STREAM_WRITE,
+                                 sp);
   if (rc)
     {
       free (sp);
diff --git a/libmu_sieve/extensions/vacation.c 
b/libmu_sieve/extensions/vacation.c
index ac91fa5..6f63f65 100644
--- a/libmu_sieve/extensions/vacation.c
+++ b/libmu_sieve/extensions/vacation.c
@@ -75,7 +75,8 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, 
mu_mime_t *pmime,
       mu_stream_t fstr;
       rc = mu_filter_create (&fstr, input, "base64",
                             MU_FILTER_ENCODE, 
-                            MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+                            MU_STREAM_READ);
+      mu_stream_unref (input); 
       if (rc == 0) 
        {
          header = "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n"
diff --git a/libproto/pop/pop3_stream.c b/libproto/pop/pop3_stream.c
index 8ceb4e3..e0b9279 100644
--- a/libproto/pop/pop3_stream.c
+++ b/libproto/pop/pop3_stream.c
@@ -73,7 +73,8 @@ mu_pop3_filter_create (mu_stream_t *pstream, mu_stream_t 
stream)
   int rc;
 
   rc = mu_filter_create (pstream, stream, "CRLFDOT", MU_FILTER_DECODE,
-                        MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+                        MU_STREAM_READ);
+  mu_stream_unref (stream);
   if (rc == 0)
     {
       struct mu_pop3_stream *sp = (struct mu_pop3_stream *) stream;
diff --git a/maidag/lmtp.c b/maidag/lmtp.c
index 7ba1377..692ef16 100644
--- a/maidag/lmtp.c
+++ b/maidag/lmtp.c
@@ -579,8 +579,7 @@ lmtp_connection (int fd, struct sockaddr *sa, int salen, 
void *data,
   mu_stream_t str;
   int rc;
   
-  rc = mu_fd_stream_create (&str, NULL, fd,
-                           MU_STREAM_RDWR|MU_STREAM_AUTOCLOSE);
+  rc = mu_fd_stream_create (&str, NULL, fd, MU_STREAM_RDWR);
   if (rc)
     {
       mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
diff --git a/mail/decode.c b/mail/decode.c
index f3497b1..699c464 100644
--- a/mail/decode.c
+++ b/mail/decode.c
@@ -260,8 +260,11 @@ display_submessage (struct mime_descend_closure *closure, 
void *data)
       /* Can we decode.  */
       if (mu_filter_create (&d_stream, b_stream, closure->encoding,
                            MU_FILTER_DECODE, 
-                           MU_STREAM_READ | MU_STREAM_AUTOCLOSE) == 0)
-       stream = d_stream;
+                           MU_STREAM_READ) == 0)
+        {
+          mu_stream_unref (b_stream);
+         stream = d_stream;
+        }
       else
        stream = b_stream;
       
diff --git a/mail/escape.c b/mail/escape.c
index 0f59566..a860c0e 100644
--- a/mail/escape.c
+++ b/mail/escape.c
@@ -444,7 +444,8 @@ quote0 (msgset_t *mspec, mu_message_t mesg, void *data)
   char *prefix = "\t";
   mu_stream_t outstr, flt;
   char *argv[3];
-  
+  int yes = 1;
+
   fprintf (stdout, _("Interpolating: %lu\n"),
           (unsigned long) mspec->msg_part[0]);
 
@@ -457,6 +458,9 @@ quote0 (msgset_t *mspec, mu_message_t mesg, void *data)
       mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create", NULL, rc);
       return rc;
     }
+  /* Set borrow flag to prevent fileno (ofile) from being closed on
+     destroying outstr. */
+  mu_stream_ioctl (outstr, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
   argv[0] = "INLINE-COMMENT";
   argv[1] = prefix;
   argv[2] = NULL;
diff --git a/mh/anno.c b/mh/anno.c
index 4c598d3..c98868a 100644
--- a/mh/anno.c
+++ b/mh/anno.c
@@ -120,24 +120,15 @@ main (int argc, char **argv)
 
   if (!component)
     {
-      mu_stream_t in;
       size_t size = 0;
       char *p;
-      
-      rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
-      if (rc)
-       {
-         mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
-         exit (1);
-       }
 
       if (isatty (0))
        {
-         printf (_("Component name: "));
-         fflush (stdout);
+         mu_printf (_("Component name: "));
+         mu_stream_flush (mu_strout);
        }
-      rc = mu_stream_getline (in, &component, &size, NULL);
-      mu_stream_destroy (&in);
+      rc = mu_stream_getline (mu_strin, &component, &size, NULL);
       if (rc)
        {
          mu_error (_("error reading input stream: %s"), mu_strerror (rc));
diff --git a/mh/mh.h b/mh/mh.h
index 5af2c73..7bfb0af 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -58,6 +58,7 @@
 #include <mailutils/property.h>
 #include <mailutils/prog.h>
 #include <mailutils/mh.h>
+#include <mailutils/stdstream.h>
 
 #include <mu_umaxtostr.h>
 
diff --git a/mh/mh_init.c b/mh/mh_init.c
index 84c1e95..7664b7c 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -53,6 +53,8 @@ void
 mh_init ()
 {
   mh_ensure_stdin ();
+  mu_stdstream_setup ();
+  
   /* Register all mailbox and mailer formats */
   mu_register_all_formats ();
 #ifdef WITH_TLS
@@ -756,16 +758,8 @@ mh_real_install (char *name, int automode)
   char *home = mu_get_homedir ();
   char *mhdir;
   char *ctx;
-  mu_stream_t in;
   int rc;
-  FILE *fp;
-
-  rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
-  if (rc)
-    {
-      mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
-      exit (1);
-    }
+  mu_stream_t profile;
 
   mhdir = mh_safe_make_file_name (home, "Mail");
   
@@ -788,10 +782,11 @@ mh_real_install (char *name, int automode)
             by question mark on output. */
          local = mh_getyn_interactive (_("Do you want a path below your login 
directory"));
          if (local)
-           printf (_("What is the path? "));
+           mu_printf (_("What is the path? "));
          else
-           printf (_("What is the full path? "));
-         if (mu_stream_getline (in, &buf, &size, NULL))
+           mu_printf (_("What is the full path? "));
+         mu_stream_flush (mu_strin);
+         if (mu_stream_getline (mu_strin, &buf, &size, NULL))
            exit (1);
          p = mu_str_stripws (buf);
          if (p > buf)
@@ -811,21 +806,23 @@ mh_real_install (char *name, int automode)
   if (mh_check_folder (mhdir, !automode))
     exit (1);
 
-  fp = fopen (name, "w");
-  if (!fp)
+  rc = mu_file_stream_create (&profile, name,
+                             MU_STREAM_WRITE | MU_STREAM_CREAT);
+  if (rc)
     {
-      mu_error (_("cannot open file %s: %s"), name, mu_strerror (errno));
+      mu_error (_("cannot open file %s: %s"), name, mu_strerror (rc));
       exit (1);
     }
-  fprintf (fp, "Path: %s\n", mhdir);
-  fclose (fp);
+  mu_stream_printf (profile, "Path: %s\n", mhdir);
+  mu_stream_destroy (&profile);
 
   ctx = mh_safe_make_file_name (mhdir, MH_CONTEXT_FILE);
-  fp = fopen (ctx, "w");
-  if (fp)
+  rc = mu_file_stream_create (&profile, ctx,
+                             MU_STREAM_WRITE | MU_STREAM_CREAT);
+  if (rc)
     {
-      fprintf (fp, "Current-Folder: inbox\n");
-      fclose (fp);
+      mu_stream_printf (profile, "Current-Folder: inbox\n");
+      mu_stream_destroy (&profile);
     }
   free (ctx);
   ctx = mh_safe_make_file_name (mhdir, "inbox");
@@ -840,7 +837,7 @@ mh_install (char *name, int automode)
 {
   struct stat st;
   
-  if (stat(name, &st))
+  if (stat (name, &st))
     {
       if (errno == ENOENT)
        {
diff --git a/mh/mh_whatnow.c b/mh/mh_whatnow.c
index dee46b5..c5f4806 100644
--- a/mh/mh_whatnow.c
+++ b/mh/mh_whatnow.c
@@ -331,27 +331,19 @@ static int
 _whatnow (struct mh_whatnow_env *wh, struct action_tab *tab)
 {
   int rc, status = 0;
-  mu_stream_t in;
   char *line = NULL;
   size_t size = 0;
   struct mu_wordsplit ws;
   int wsflags = MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT;
   
-  rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
-  if (rc)
-    {
-      mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
-      exit (1);
-    }
-  
   do
     {
       size_t n;
       handler_fp fun;
       
-      printf ("%s ", wh->prompt);
-      fflush (stdout);
-      rc = mu_stream_getline (in, &line, &size, &n);
+      mu_printf ("%s ", wh->prompt);
+      mu_stream_flush (mu_strout);
+      rc = mu_stream_getline (mu_strin, &line, &size, &n);
       if (rc)
        {
          mu_error (_("cannot read input stream: %s"), mu_strerror (rc));
@@ -443,7 +435,7 @@ quit (struct mh_whatnow_env *wh, int argc, char **argv, int 
*status)
        unlink (wh->draftfile);
       else
        {
-         printf (_("draft left on \"%s\".\n"), wh->draftfile);
+         mu_printf (_("draft left on \"%s\".\n"), wh->draftfile);
          if (strcmp (wh->file, wh->draftfile))
            rename (wh->file, wh->draftfile);
        }
diff --git a/mh/mhl.c b/mh/mhl.c
index c71cabf..7dcd2a6 100644
--- a/mh/mhl.c
+++ b/mh/mhl.c
@@ -151,7 +151,11 @@ open_output ()
   if (moreproc)
     rc = mu_command_stream_create (&output, moreproc, MU_STREAM_WRITE);
   else
-    rc = mu_stdio_stream_create (&output, MU_STDOUT_FD, MU_STREAM_WRITE);
+    {
+      rc = 0;
+      output = mu_strout;
+      mu_stream_ref (output);
+    }
 
   if (rc)
     {
@@ -170,7 +174,11 @@ list_message (char *name, mu_stream_t output)
   mu_message_t msg;
 
   if (!name)
-    rc = mu_stdio_stream_create (&input, MU_STDIN_FD, MU_STREAM_SEEK);
+    {
+      rc = 0;
+      input = mu_strin;
+      mu_stream_ref (input);
+    }
   else
     rc = mu_file_stream_create (&input, name, MU_STREAM_READ);
   if (rc)
diff --git a/mh/mhn.c b/mh/mhn.c
index c3c433f..bceddb5 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -1591,22 +1591,15 @@ show_iterator (mu_mailbox_t mbox, mu_message_t msg, 
size_t num, void *data)
 int
 mhn_show ()
 {
-  int rc;
-  mu_stream_t ostr;
-
-  rc = mu_stdio_stream_create (&ostr, MU_STDOUT_FD, 0);
-  if (rc)
-    {
-      mu_error (_("cannot create output stream: %s"), mu_strerror (rc));
-      exit (1);
-    }
   
+  int rc;
   mhl_format = mhl_format_compile (formfile);
 
   if (message)
-    rc = show_message (message, 0, ostr);
+    rc = show_message (message, 0, mu_strout);
   else
-    rc = mh_iterate (mbox, &msgset, show_iterator, ostr);
+    rc = mh_iterate (mbox, &msgset, show_iterator, mu_strout);
+  mu_stream_flush (mu_strout);
   return rc;
 }
 
@@ -1802,15 +1795,16 @@ store_handler (mu_message_t msg, msg_part_t part, char 
*type, char *encoding,
     case store_to_stdout:
       printf (_("storing msg %s part %s to stdout\n"),
              prefix, partstr);
-      rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, 0);
-      if (rc)
-       mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create", NULL, rc);
+      rc = 0;
+      out = mu_strout;
+      mu_stream_ref (out);
       break;
     }
                           
   if (out)
     {
       show_internal (msg, part, encoding, out);
+      mu_stream_flush (out);
       mu_stream_destroy (&out);
     }
       
@@ -2498,13 +2492,13 @@ edit_mime (char *cmd, struct compose_env *env, 
mu_message_t *msg, int level)
       free (subtype);
     }
 
-  rc = mu_filter_create (&fstr, in, encoding, MU_FILTER_ENCODE,
-                         MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
+  rc = mu_filter_create (&fstr, in, encoding, MU_FILTER_ENCODE, 
MU_STREAM_READ);
   if (rc)
     {
       fstr = in;
-      in = NULL;
+      mu_stream_ref (in);
     }
+  mu_stream_unref (in);
   free (encoding);
 
   mu_message_get_body (*msg, &body);
diff --git a/mimeview/mimetypes.y b/mimeview/mimetypes.y
index 44593c3..e3892e0 100644
--- a/mimeview/mimetypes.y
+++ b/mimeview/mimetypes.y
@@ -299,16 +299,22 @@ static int
 b_ascii (union argument *args)
 {
   int i;
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
+  int rc;
+
+  rc = mu_stream_seek (mimeview_stream, args[0].number, MU_SEEK_SET, NULL);
+  if (rc)
     {
-      mu_error ("fseek: %s", mu_strerror (errno));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", NULL, rc);
       return 0;
     }
 
   for (i = 0; i < args[1].number; i++)
     {
-      int c = getc (mimeview_fp);
-      if (c == EOF)
+      char c;
+      size_t n;
+
+      rc = mu_stream_read (mimeview_stream, &c, 1, &n);
+      if (rc || n == 0)
        break;
       if (!mu_isascii (c))
        return 0;
@@ -329,17 +335,22 @@ static int
 b_printable (union argument *args)
 {
   int i;
+  int rc;
 
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
+  rc = mu_stream_seek (mimeview_stream, args[0].number, MU_SEEK_SET, NULL);
+  if (rc)
     {
-      mu_error ("fseek: %s", mu_strerror (errno));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", NULL, rc);
       return 0;
     }
 
   for (i = 0; i < args[1].number; i++)
     {
-      int c = getc (mimeview_fp);
-      if (c == EOF)
+      char c;
+      size_t n;
+
+      rc = mu_stream_read (mimeview_stream, &c, 1, &n);
+      if (rc || n == 0)
        break;
       if (!ISPRINT ((unsigned)c))
        return 0;
@@ -355,17 +366,22 @@ b_string (union argument *args)
 {
   struct mimetypes_string *str = args[1].string;
   int i;
+  int rc;
   
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
+  rc = mu_stream_seek (mimeview_stream, args[0].number, MU_SEEK_SET, NULL);
+  if (rc)
     {
-      mu_error ("fseek: %s", mu_strerror (errno));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", NULL, rc);
       return 0;
     }
 
   for (i = 0; i < str->len; i++)
     {
-      int c = getc (mimeview_fp);
-      if (c == EOF || (char)c != str->ptr[i])
+      char c;
+      size_t n;
+
+      rc = mu_stream_read (mimeview_stream, &c, 1, &n);
+      if (rc || n == 0 || c != str->ptr[i])
        return 0;
     }
   return 1;
@@ -381,33 +397,60 @@ b_istring (union argument *args)
   int i;
   struct mimetypes_string *str = args[1].string;
   
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
+  int rc;
+
+  rc = mu_stream_seek (mimeview_stream, args[0].number, MU_SEEK_SET, NULL);
+  if (rc)
     {
-      mu_error ("fseek: %s", mu_strerror (errno));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", NULL, rc);
       return 0;
     }
 
   for (i = 0; i < str->len; i++)
     {
-      int c = getc (mimeview_fp);
-      if (c == EOF || mu_tolower (c) != mu_tolower (str->ptr[i]))
+      char c;
+      size_t n;
+
+      rc = mu_stream_read (mimeview_stream, &c, 1, &n);
+      if (rc || n == 0 || mu_tolower (c) != mu_tolower (str->ptr[i]))
        return 0;
     }
   return 1;
 }
 
+int
+compare_bytes (union argument *args, void *sample, void *buf, size_t size)
+{
+  int rc;
+  size_t n;
+  
+  rc = mu_stream_seek (mimeview_stream, args[0].number, MU_SEEK_SET, NULL);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", NULL, rc);
+      return 0;
+    }
+  
+  rc = mu_stream_read (mimeview_stream, buf, sizeof (buf), &n);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_read", NULL, rc);
+      return 0;
+    }
+  else if (n != size)
+    return 0;
+  return memcmp (sample, buf, size) == 0;
+}
+
 /*       char(offset,value)
             True if byte is identical
 */
 static int
 b_char (union argument *args)
 {
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
-    {
-      mu_error ("fseek: %s", mu_strerror (errno));
-      return 0;
-    }
-  return getc (mimeview_fp) == args[1].number;
+  char val = args[1].number;
+  char buf;
+  return compare_bytes (args, &val, &buf, sizeof (buf));
 }
 
 /*        short(offset,value)
@@ -417,24 +460,9 @@ b_char (union argument *args)
 static int
 b_short (union argument *args)
 {
-  unsigned short val;
-  int rc;
-  
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
-    {
-      mu_error ("fseek: %s", mu_strerror (errno));
-      return 0;
-    }
-  rc = fread (&val, sizeof val, 1, mimeview_fp);
-
-  if (rc == -1)
-    {
-      mu_error ("fread: %s", mu_strerror (errno));
-      return 0;
-    }
-  else if (rc == 0)
-    return 0;
-  return val == args[1].number;
+  unsigned short val = args[1].number;
+  unsigned short buf;
+  return compare_bytes (args, &val, &buf, sizeof (buf));
 }
 
 /*        int(offset,value)
@@ -444,23 +472,9 @@ b_short (union argument *args)
 static int
 b_int (union argument *args)
 {
-  unsigned int val;
-  int rc;
-  
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
-    {
-      mu_error ("fseek: %s", mu_strerror (errno));
-      return 0;
-    }
-  rc = fread (&val, sizeof val, 1, mimeview_fp);
-  if (rc == -1)
-    {
-      mu_error ("fread: %s", mu_strerror (errno));
-      return 0;
-    }
-  else if (rc == 0)
-    return 0;
-  return val == args[1].number;
+  unsigned int val = args[1].number;
+  unsigned int buf;
+  return compare_bytes (args, &val, &buf, sizeof (buf));
 }
 
 /*        locale("string")
@@ -479,20 +493,23 @@ b_locale (union argument *args)
 static int
 b_contains (union argument *args)
 {
-  int i, count;
+  size_t i, count;
   char *buf;
   struct mimetypes_string *str = args[2].string;
+  int rc;
 
-  if (fseek (mimeview_fp, args[0].number, SEEK_SET) == -1)
+  rc = mu_stream_seek (mimeview_stream, args[0].number, MU_SEEK_SET, NULL);
+  if (rc)
     {
-      mu_error ("fseek: %s", mu_strerror (errno));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_seek", NULL, rc);
       return 0;
     }
+
   buf = xmalloc (args[1].number);
-  count = fread (buf, 1, args[1].number, mimeview_fp);
-  if (count == -1)
+  rc = mu_stream_read (mimeview_stream, buf, args[1].number, &count);
+  if (count != args[1].number)
     {
-      mu_error ("fread: %s", mu_strerror (errno));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_read", NULL, rc);
     }
   else if (count > str->len)
     for (i = 0; i < count - str->len; i++)
diff --git a/mimeview/mimeview.c b/mimeview/mimeview.c
index 306d748..5f1f2ff 100644
--- a/mimeview/mimeview.c
+++ b/mimeview/mimeview.c
@@ -66,7 +66,7 @@ static char *no_ask_types;  /* List of MIME types for which 
no questions
                               should be asked */
 static int interactive = -1; 
 char *mimeview_file;       /* Name of the file to view */
-FILE *mimeview_fp;     /* Its descriptor */
+mu_stream_t mimeview_stream;    /* The corresponding stream */
 
 static void
 set_debug_flags (const char *arg)
@@ -184,7 +184,9 @@ static const char *capa[] = {
 static int
 open_file (char *name)
 {
+  int rc;
   struct stat st;
+  
   if (stat (name, &st))
     {
       mu_error (_("cannot stat `%s': %s"), name, mu_strerror (errno));
@@ -197,10 +199,10 @@ open_file (char *name)
     }
 
   mimeview_file = name;
-  mimeview_fp = fopen (name, "r");
-  if (mimeview_fp == NULL)
+  rc = mu_file_stream_create (&mimeview_stream, mimeview_file, MU_STREAM_READ);
+  if (rc)
     {
-      mu_error (_("Cannot open `%s': %s"), name, mu_strerror (errno));
+      mu_error (_("Cannot open `%s': %s"), name, mu_strerror (rc));
       return -1;
     }
   return 0;
@@ -209,7 +211,7 @@ open_file (char *name)
 void
 close_file ()
 {
-  fclose (mimeview_fp);
+  mu_stream_close (mimeview_stream);
 }
 
 void
@@ -244,7 +246,6 @@ display_file (const char *type)
     }
   else
     {
-      mu_stream_t stream;
       mu_header_t hdr;
       char *text;
 
@@ -254,17 +255,9 @@ display_file (const char *type)
        mu_error (_("cannot create header: %s"), mu_strerror (status));
       else
        {
-         mu_stdio_stream_create (&stream, fileno (mimeview_fp),
-                                 MU_STREAM_READ|
-                                 MU_STREAM_SEEK);
-         
-         display_stream_mailcap (mimeview_file, stream, hdr,
+         display_stream_mailcap (mimeview_file, mimeview_stream, hdr,
                                  no_ask_types, interactive, dry_run,
                                  debug_level);
-         
-         mu_stream_close (stream);
-         mu_stream_destroy (&stream);
-
          mu_header_destroy (&hdr);
        }
     }
diff --git a/mimeview/mimeview.h b/mimeview/mimeview.h
index a77dc5a..4ad8477 100644
--- a/mimeview/mimeview.h
+++ b/mimeview/mimeview.h
@@ -53,7 +53,7 @@ struct mimetypes_string *mimetypes_string_dup (struct 
mimetypes_string *s);
 const char *get_file_type (void);
 
 extern char *mimeview_file;
-extern FILE *mimeview_fp;    
+extern mu_stream_t mimeview_stream;    
 extern int debug_level;
 
 #define DEBUG(l,f) if (debug_level > (l)) printf f
diff --git a/pop3d/extra.c b/pop3d/extra.c
index 1508e9b..dcbda80 100644
--- a/pop3d/extra.c
+++ b/pop3d/extra.c
@@ -134,13 +134,11 @@ pop3d_setio (int ifd, int ofd)
   if (ofd == -1)
     pop3d_abquit (ERR_NO_OFILE);
 
-  if (mu_stdio_stream_create (&istream, ifd, 
-                              MU_STREAM_READ | MU_STREAM_AUTOCLOSE))
+  if (mu_stdio_stream_create (&istream, ifd, MU_STREAM_READ))
     pop3d_abquit (ERR_NO_IFILE);
   mu_stream_set_buffer (istream, mu_buffer_line, 0);
   
-  if (mu_stdio_stream_create (&ostream, ofd, 
-                              MU_STREAM_WRITE | MU_STREAM_AUTOCLOSE))
+  if (mu_stdio_stream_create (&ostream, ofd, MU_STREAM_WRITE))
     pop3d_abquit (ERR_NO_OFILE);
 
   /* Combine the two streams into an I/O one. */
diff --git a/pop3d/popauth.c b/pop3d/popauth.c
index 4bd172b..1192fcb 100644
--- a/pop3d/popauth.c
+++ b/pop3d/popauth.c
@@ -277,26 +277,26 @@ check_user_perm (int action, struct action_data *ap)
 }
 
 static void
-print_entry (FILE *fp, DBM_DATUM key, DBM_DATUM contents)
+print_entry (mu_stream_t str, DBM_DATUM key, DBM_DATUM contents)
 {
   if (compatibility_option)
-    fprintf (fp, "%.*s: %.*s\n",
-            (int) MU_DATUM_SIZE (key),
-            (char*) MU_DATUM_PTR (key),
-            (int) MU_DATUM_SIZE (contents),
-            (char*) MU_DATUM_PTR (contents));
+    mu_stream_printf (str, "%.*s: %.*s\n",
+                     (int) MU_DATUM_SIZE (key),
+                     (char*) MU_DATUM_PTR (key),
+                     (int) MU_DATUM_SIZE (contents),
+                     (char*) MU_DATUM_PTR (contents));
   else
-    fprintf (fp, "%.*s %.*s\n",
-            (int) MU_DATUM_SIZE (key),
-            (char*) MU_DATUM_PTR (key),
-            (int) MU_DATUM_SIZE (contents),
-            (char*) MU_DATUM_PTR (contents));
+    mu_stream_printf (str, "%.*s %.*s\n",
+                     (int) MU_DATUM_SIZE (key),
+                     (char*) MU_DATUM_PTR (key),
+                     (int) MU_DATUM_SIZE (contents),
+                     (char*) MU_DATUM_PTR (contents));
 }
 
 int
 action_list (struct action_data *ap)
 {
-  FILE *fp;
+  mu_stream_t str;
   DBM_FILE db;
   DBM_DATUM key;
   DBM_DATUM contents;
@@ -311,15 +311,21 @@ action_list (struct action_data *ap)
   
   if (ap->output_name)
     {
-      fp = fopen (ap->output_name, "w");
-      if (!fp)
+      int rc = mu_file_stream_create (&str, ap->output_name,
+                                     MU_STREAM_WRITE|MU_STREAM_CREAT);
+      if (rc)
        {
-         mu_error (_("cannot create file %s: %s"), ap->output_name, 
mu_strerror (errno));
+         mu_error (_("cannot create file %s: %s"),
+                   ap->output_name, mu_strerror (rc));
          return 1;
        }
+      mu_stream_truncate (str, 0);
     }
   else
-    fp = stdout;
+    {
+      str = mu_strout;
+      mu_stream_ref (str);
+    }
 
   if (ap->username)
     {
@@ -333,7 +339,7 @@ action_list (struct action_data *ap)
        }
       else
        {
-         print_entry (fp, key, contents);
+         print_entry (str, key, contents);
          mu_dbm_datum_free (&contents);
        }
     }
@@ -344,13 +350,13 @@ action_list (struct action_data *ap)
        {
          memset (&contents, 0, sizeof contents);
          mu_dbm_fetch (db, key, &contents);
-         print_entry (fp, key, contents);
+         print_entry (str, key, contents);
          mu_dbm_datum_free (&contents);
        }
     }
   
   mu_dbm_close (db);
-  fclose (fp);
+  mu_stream_destroy (&str);
   return 0;
 }
 
@@ -653,7 +659,7 @@ popauth_version (FILE *stream, struct argp_state *state)
 # define FORMAT "Tokyo Cabinet"
 #endif
   mu_program_version_hook (stream, state);
-  printf (_("Database format: %s\n"), FORMAT);
-  printf (_("Database location: %s\n"), APOP_PASSFILE);
+  fprintf (stream, _("Database format: %s\n"), FORMAT);
+  fprintf (stream, _("Database location: %s\n"), APOP_PASSFILE);
   exit (EX_OK);
 }


hooks/post-receive
-- 
GNU Mailutils



reply via email to

[Prev in Thread] Current Thread [Next in Thread]