[Commits] [SCM] claws branch, gtk3, updated. 3.16.0-410-g5ac91d8

ticho at claws-mail.org ticho at claws-mail.org
Tue Oct 16 19:16:53 CEST 2018


The branch, gtk3 has been updated
       via  5ac91d89dca0a8e957165342b652ede11b611d15 (commit)
       via  8d7751e7ee2b6d70361de610a891b7d9a86fc524 (commit)
       via  fc1bfbac75086ae50b770116d16810074fec7b13 (commit)
       via  17451f0fdbf28d4b868d97b1bf85dec2c1c77e90 (commit)
       via  9b1579617a7ae976c5a0085752fa167dc7d6715d (commit)
       via  2485f1fa6128e47829c91a65c80be4fa20972d3f (commit)
       via  9193045b29cc690980f7e1f84311e8e729b66873 (commit)
       via  e2b7ebaa1ae0019f513c6c0c3a0e515ed0fe747c (commit)
       via  34bfb629fcce5df9ad86994fff51f60ee7c0b5c7 (commit)
       via  2f9a45ca063de741f02139ca0c94d0363b0ab608 (commit)
       via  228170113bce5ddf948e5ae7162660175730a9f6 (commit)
       via  b63551d65c05683134e9591fa3863761809c0077 (commit)
       via  69ce23d6739e4713fbb8b86d87e8a26f99ff7fd7 (commit)
       via  68ec07738ae82e1d945b94225687465d406e7069 (commit)
       via  657acd4b1911aa13ad7606279bcc6ee81cdc7238 (commit)
       via  fcc32c9f4370cf74ba7059668a4c18c7865996ff (commit)
       via  429ab1b06c0215cc94ea299cdaa5abcdb7e46b27 (commit)
       via  ebb97540bfbddcf4a4de619086ad8fa43a733eba (commit)
       via  b7e7385fa23d999f95677090c30204ddd6747a0d (commit)
       via  0cfddd54ce8734e793097a6b56bdd07da9de34a9 (commit)
       via  3d72e6abd9e8787265813ff2f632bc9548c5cdc0 (commit)
       via  d59c3d1cb008056725776720e8964c2880371514 (commit)
       via  4ceab3a96a00e52e29f4cf1b7363883b9c036bb2 (commit)
       via  86d559c965d7a7934fd7421f4a4f5adb522ccaa1 (commit)
       via  5708f64867944e0a7eab1f6666828dcc4d34d91e (commit)
       via  dc7dfb44b11da99cee879e25f9075cb816638d4f (commit)
       via  9121905e6511bb40b2c499a2472f54cd784b111f (commit)
       via  15bafe17796fd4429fe2b09abd9e2bab6c2a3ac7 (commit)
       via  7aeffbd66c0b2b8f3d1003894fe39e1491763eeb (commit)
       via  b51f1a104e927913bc03c7476656a0f5a64698d3 (commit)
       via  d8b3d1705d4ad8da8200e9247ca54c79439fc9dc (commit)
      from  1f7e162656b0329c432fe5104a1d01de5e4b8fda (commit)

Summary of changes:
 .gitignore                                         |    1 -
 AUTHORS                                            |    2 +-
 configure.ac                                       |    4 +-
 src/account.c                                      |    9 +-
 src/addrbook.c                                     |   62 +-
 src/addrclip.c                                     |    1 +
 src/addressbook.c                                  |    1 +
 src/addrharvest.c                                  |   14 +-
 src/addrindex.c                                    |   47 +-
 src/addritem.c                                     |    1 +
 src/addrmerge.c                                    |    1 +
 src/advsearch.c                                    |   16 +-
 src/autofaces.c                                    |    9 +-
 src/common/Makefile.am                             |    2 +
 src/common/file-utils.c                            |  864 ++++++++++++++++++++
 src/common/file-utils.h                            |  104 +++
 src/common/log.c                                   |    4 +-
 src/common/plugin.c                                |   11 +-
 src/common/prefs.c                                 |   55 +-
 src/common/ssl_certificate.c                       |   57 +-
 src/common/tags.c                                  |   19 +-
 src/common/template.c                              |   33 +-
 src/common/tests/Makefile.am                       |    4 +-
 src/common/tests/codeconv_test.c                   |    1 +
 .../tests/mock_prefs_common_get_flush_metadata.h   |    5 +
 src/common/tests/xml_test.c                        |    1 +
 src/common/utils.c                                 |  795 +-----------------
 src/common/utils.h                                 |   42 -
 src/common/xml.c                                   |   53 +-
 src/common/xml.h                                   |    1 +
 src/common/xmlprops.c                              |   31 +-
 src/compose.c                                      |  114 +--
 src/crash.c                                        |    6 +-
 src/editaddress.c                                  |    1 +
 src/enriched.c                                     |    8 +-
 src/etpan/imap-thread.c                            |   13 +-
 src/exporthtml.c                                   |   21 +-
 src/exportldif.c                                   |    5 +-
 src/file_checker.c                                 |    1 +
 src/folder.c                                       |    7 +-
 src/gtk/about.c                                    |    9 +-
 src/gtk/authors.h                                  |    2 +-
 src/gtk/gtkcmclist.c                               |    7 +-
 src/gtk/sslcertwindow.c                            |   11 +-
 src/html.c                                         |    8 +-
 src/image_viewer.c                                 |    1 +
 src/imap.c                                         |    7 +-
 src/inc.c                                          |    1 +
 src/jpilot.c                                       |   91 ++-
 src/ldapquery.c                                    |    7 +-
 src/ldif.c                                         |   11 +-
 src/main.c                                         |   39 +-
 src/matcher.c                                      |   88 +-
 src/matcher.h                                      |    5 +-
 src/mbox.c                                         |  136 ++-
 src/messageview.c                                  |   12 +-
 src/mh.c                                           |   25 +-
 src/mimeview.c                                     |   13 +-
 src/msgcache.c                                     |  103 +--
 src/mutt.c                                         |   13 +-
 src/news.c                                         |   33 +-
 src/partial_download.c                             |   92 +--
 src/passwordstore.c                                |   35 +-
 src/pine.c                                         |   13 +-
 src/plugins/acpi_notifier/acpi_notifier.c          |    9 +-
 src/plugins/archive/archiver_gtk.c                 |    1 +
 src/plugins/bogofilter/bogofilter.c                |   19 +-
 src/plugins/clamd/libclamd/clamd-plugin.c          |    9 +-
 src/plugins/dillo/dillo_viewer.c                   |    1 +
 src/plugins/fancy/claws.def                        |    4 +
 src/plugins/fancy/fancy_prefs.c                    |    3 +-
 src/plugins/fancy/fancy_viewer.c                   |   22 +-
 src/plugins/libravatar/claws.def                   |    4 +
 src/plugins/libravatar/libravatar_cache.c          |    1 +
 src/plugins/libravatar/libravatar_image.c          |    9 +-
 src/plugins/libravatar/libravatar_missing.c        |   11 +-
 src/plugins/mailmbox/claws.def                     |    4 +
 src/plugins/mailmbox/mailimf.c                     |    6 +
 src/plugins/mailmbox/mailimf_write.c               |   43 +-
 src/plugins/mailmbox/mailmbox.c                    |    6 +
 src/plugins/mailmbox/mailmbox_folder.c             |   25 +-
 src/plugins/newmail/newmail.c                      |    7 +-
 src/plugins/pdf_viewer/poppler_viewer.c            |   25 +-
 src/plugins/perl/perl_plugin.c                     |   13 +-
 src/plugins/pgpcore/claws.def                      |    4 +
 src/plugins/pgpcore/pgp_utils.c                    |   13 +-
 src/plugins/pgpcore/sgpgme.c                       |    5 +-
 src/plugins/pgpinline/claws.def                    |    4 +
 src/plugins/pgpinline/pgpinline.c                  |   35 +-
 src/plugins/pgpmime/claws.def                      |    4 +
 src/plugins/pgpmime/pgpmime.c                      |   33 +-
 src/plugins/python/python_plugin.c                 |    5 +-
 src/plugins/rssyl/claws.def                        |    4 +
 src/plugins/rssyl/opml_export.c                    |    5 +-
 src/plugins/rssyl/rssyl.c                          |    2 +-
 src/plugins/rssyl/rssyl_add_item.c                 |    5 +-
 src/plugins/rssyl/rssyl_deleted.c                  |    5 +-
 src/plugins/rssyl/rssyl_update_format.c            |    1 +
 src/plugins/smime/claws.def                        |    4 +
 src/plugins/smime/smime.c                          |   53 +-
 src/plugins/spam_report/spam_report.c              |    1 +
 src/plugins/spamassassin/libspamc.c                |    1 +
 src/plugins/spamassassin/spamassassin.c            |    7 +-
 src/plugins/tnef_parse/claws.def                   |    4 +
 src/plugins/tnef_parse/tnef_parse.c                |   17 +-
 src/plugins/vcalendar/claws.def                    |    4 +
 src/plugins/vcalendar/vcal_folder.c                |    1 +
 src/plugins/vcalendar/vcal_manager.c               |    7 +-
 src/plugins/vcalendar/vcal_meeting_gtk.c           |   29 +-
 src/plugins/vcalendar/vcalendar.c                  |   19 +-
 src/pop.c                                          |   53 +-
 src/prefs_account.c                                |    7 +-
 src/prefs_actions.c                                |   15 +-
 src/prefs_common.c                                 |   24 +-
 src/prefs_customheader.c                           |   37 +-
 src/prefs_display_header.c                         |   15 +-
 src/prefs_gtk.c                                    |   68 +-
 src/prefs_themes.c                                 |    7 +-
 src/procheader.c                                   |   33 +-
 src/procheader.h                                   |    2 +
 src/procmime.c                                     |  384 ++++-----
 src/procmsg.c                                      |   93 ++-
 src/quote_fmt_parse.y                              |    1 +
 src/send_message.c                                 |   11 +-
 src/sourcewindow.c                                 |    9 +-
 src/summaryview.c                                  |   13 +-
 src/textview.c                                     |   39 +-
 src/vcard.c                                        |   22 +-
 src/wizard.c                                       |    5 +-
 tools/README                                       |   90 +-
 tools/cm-break.pl                                  |  124 +++
 tools/cm-reparent.pl                               |   53 +-
 tools/textviewer.pl                                |  103 ++-
 133 files changed, 2717 insertions(+), 2173 deletions(-)
 create mode 100644 src/common/file-utils.c
 create mode 100644 src/common/file-utils.h
 create mode 100644 src/common/tests/mock_prefs_common_get_flush_metadata.h
 create mode 100644 tools/cm-break.pl


- Log -----------------------------------------------------------------
commit 5ac91d89dca0a8e957165342b652ede11b611d15
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Tue Oct 16 18:18:40 2018 +0200

    Do not redraw focus of GtkCMCList if the row is unchanged in move_focus_row();

diff --git a/src/gtk/gtkcmclist.c b/src/gtk/gtkcmclist.c
index b1fd3e8..3ee1c98 100644
--- a/src/gtk/gtkcmclist.c
+++ b/src/gtk/gtkcmclist.c
@@ -6967,8 +6967,13 @@ move_focus_row (GtkCMCList      *clist,
     case GTK_SCROLL_JUMP:
       if (position >= 0 && position <= 1)
 	{
+	  gint row = position * (clist->rows - 1);
+
+	  if (row == clist->focus_row)
+	    return;
+
 	  gtk_cmclist_undraw_focus (widget);
-	  clist->focus_row = position * (clist->rows - 1);
+	  clist->focus_row = row;
 	  gtk_cmclist_draw_focus (widget);
 	}
       break;

commit 8d7751e7ee2b6d70361de610a891b7d9a86fc524
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Tue Oct 16 18:16:02 2018 +0200

    In summary_key_pressed(), do not try to reopen the same message if cursor does not change.

diff --git a/src/summaryview.c b/src/summaryview.c
index ee6032e..5befa81 100644
--- a/src/summaryview.c
+++ b/src/summaryview.c
@@ -7007,7 +7007,8 @@ static gboolean summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
 					break;
 			}
 
-			if (next) {
+			if (next != NULL &&
+					next != gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->focus_row)) {
 				gtk_sctree_select_with_state
 					(GTK_SCTREE(ctree), next, (event->state & ~GDK_CONTROL_MASK) );
 

commit fc1bfbac75086ae50b770116d16810074fec7b13
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Tue Oct 16 18:12:05 2018 +0200

    Fix an off-by-one error in summary_key_pressed().

diff --git a/src/summaryview.c b/src/summaryview.c
index e619875..ee6032e 100644
--- a/src/summaryview.c
+++ b/src/summaryview.c
@@ -6989,7 +6989,7 @@ static gboolean summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
 					break;
 				case GDK_KEY_Down: case GDK_KEY_KP_Down:
 					next = gtk_cmctree_node_nth(ctree,
-							MIN(GTK_CMCLIST(ctree)->focus_row + 1, GTK_CMCLIST(ctree)->rows));
+							MIN(GTK_CMCLIST(ctree)->focus_row + 1, GTK_CMCLIST(ctree)->rows - 1));
 					break;
 				case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
 					next = gtk_cmctree_node_nth(ctree,

commit 17451f0fdbf28d4b868d97b1bf85dec2c1c77e90
Author: Paul <paul at claws-mail.org>
Date:   Tue Oct 16 10:10:03 2018 +0100

    fix bug (rfe) 4102, 'Show SHA-256 fingerprint in certificate verification popup'

diff --git a/src/gtk/sslcertwindow.c b/src/gtk/sslcertwindow.c
index 8099579..5fee423 100644
--- a/src/gtk/sslcertwindow.c
+++ b/src/gtk/sslcertwindow.c
@@ -61,7 +61,7 @@ static GtkWidget *cert_presenter(SSLCertificate *cert)
 	char *issuer_commonname, *issuer_location, *issuer_organization;
 	char *subject_commonname, *subject_location, *subject_organization;
 	char *sig_status, *exp_date;
-	char *md5_fingerprint, *sha1_fingerprint, *fingerprint;
+	char *md5_fingerprint, *sha1_fingerprint, *sha256_fingerprint, *fingerprint;
 	size_t n;
 	char buf[100];
 	unsigned char md[128];	
@@ -147,6 +147,10 @@ static GtkWidget *cert_presenter(SSLCertificate *cert)
 	n = 128;
 	gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_SHA1, md, &n);
 	sha1_fingerprint = readable_fingerprint(md, (int)n);
+	n = 128;
+	gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_SHA256, md, &n);
+	sha256_fingerprint = readable_fingerprint(md, (int)n);
+
 
 	/* signature */
 	sig_status = ssl_certificate_check_signer(cert, cert->status);
@@ -216,8 +220,8 @@ static GtkWidget *cert_presenter(SSLCertificate *cert)
 	label = gtk_label_new(_("Fingerprint: \n"));
 	gtk_label_set_xalign(GTK_LABEL(label), 1.0);
 	gtk_table_attach(status_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
-	fingerprint = g_strdup_printf("MD5: %s\nSHA1: %s", 
-			md5_fingerprint, sha1_fingerprint);
+	fingerprint = g_strdup_printf("MD5: %s\nSHA1: %s\nSHA256: %s",
+			md5_fingerprint, sha1_fingerprint, sha256_fingerprint);
 	label = gtk_label_new(fingerprint);
 	g_free(fingerprint);
 	gtk_label_set_selectable(GTK_LABEL(label), TRUE);
@@ -257,6 +261,7 @@ static GtkWidget *cert_presenter(SSLCertificate *cert)
 	g_free(subject_organization);
 	g_free(md5_fingerprint);
 	g_free(sha1_fingerprint);
+	g_free(sha256_fingerprint);
 	g_free(sig_status);
 	g_free(exp_date);
 	return vbox;

commit 9b1579617a7ae976c5a0085752fa167dc7d6715d
Author: Paul <paul at claws-mail.org>
Date:   Tue Oct 16 07:38:19 2018 +0100

    fix invalid cast runtime warning

diff --git a/src/plugins/pdf_viewer/poppler_viewer.c b/src/plugins/pdf_viewer/poppler_viewer.c
index 1e1ce6b..c84a356 100644
--- a/src/plugins/pdf_viewer/poppler_viewer.c
+++ b/src/plugins/pdf_viewer/poppler_viewer.c
@@ -83,7 +83,7 @@ static void pdf_viewer_button_rotate_left_cb(GtkButton *button, PdfViewer *viewe
 static void pdf_viewer_spin_change_page_cb(GtkSpinButton *button, PdfViewer *viewer);
 static void pdf_viewer_spin_zoom_scroll_cb(GtkSpinButton *button, PdfViewer *viewer);
 /* Show/Hide the index pane */
-static void pdf_viewer_show_document_index_cb(GtkButton *button, PdfViewer *viewer);
+static void pdf_viewer_show_document_index_cb(GtkToggleButton *button, PdfViewer *viewer);
 static void pdf_viewer_button_print_cb(GtkButton *button, PdfViewer *viewer);
 static void pdf_viewer_button_document_info_cb(GtkButton *button, PdfViewer *viewer);
 
@@ -1190,7 +1190,7 @@ static void pdf_viewer_button_rotate_left_cb(GtkButton *button, PdfViewer *viewe
 }
 
 /* Show/Hide the index pane */
-static void pdf_viewer_show_document_index_cb(GtkButton *button, PdfViewer *viewer)
+static void pdf_viewer_show_document_index_cb(GtkToggleButton *button, PdfViewer *viewer)
 {
 	if (!viewer->pdf_index) {
 		viewer->pdf_index = poppler_index_iter_new(viewer->pdf_doc);
@@ -1635,6 +1635,13 @@ static void pdf_viewer_scroll_one_line(MimeViewer *_viewer, gboolean up)
 	gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(widget), \
 				col, col+1, 0, 1, 0, 0, BUTTON_H_PADDING, 0); \
 	col++;
+#define ADD_TOGGLE_BUTTON_TO_TABLE(widget, stock_image) \
+	widget = gtk_toggle_button_new(); \
+	img = stock_pixmap_widget(stock_image); \
+	gtk_button_set_image(GTK_BUTTON(widget), img); \
+	gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(widget), \
+				col, col+1, 0, 1, 0, 0, BUTTON_H_PADDING, 0); \
+	col++;
 
 #define ADD_SEP_TO_TABLE \
 	sep = gtk_label_new(""); \
@@ -1811,7 +1818,7 @@ static MimeViewer *pdf_viewer_create(void)
 	ADD_SEP_TO_TABLE
 	ADD_BUTTON_TO_TABLE(viewer->print, STOCK_PIXMAP_PRINTER)
 	ADD_BUTTON_TO_TABLE(viewer->doc_info, STOCK_PIXMAP_DOC_INFO)
-	ADD_BUTTON_TO_TABLE(viewer->doc_index, STOCK_PIXMAP_DOC_INDEX)
+	ADD_TOGGLE_BUTTON_TO_TABLE(viewer->doc_index, STOCK_PIXMAP_DOC_INDEX)
 
 	gtk_scrolled_window_set_policy(
 			GTK_SCROLLED_WINDOW(viewer->scrollwin), 
@@ -2084,6 +2091,7 @@ static MimeViewer *pdf_viewer_create(void)
 }
 
 #undef ADD_BUTTON_TO_TABLE
+#undef ADD_TOGGLE_BUTTON_TO_TABLE
 #undef ADD_SEP_TO_TABLE
 #undef BUTTON_H_PADDING
 #undef SEP_H_PADDING

commit 2485f1fa6128e47829c91a65c80be4fa20972d3f
Author: Colin Leroy <colin at colino.net>
Date:   Mon Oct 15 21:15:05 2018 +0200

    PDF Viewer: handle Ctrl+scroll for zooming

diff --git a/src/plugins/pdf_viewer/poppler_viewer.c b/src/plugins/pdf_viewer/poppler_viewer.c
index e2c3a72..1e1ce6b 100644
--- a/src/plugins/pdf_viewer/poppler_viewer.c
+++ b/src/plugins/pdf_viewer/poppler_viewer.c
@@ -1109,6 +1109,16 @@ static gboolean pdf_viewer_scroll_cb(GtkWidget *widget, GdkEventScroll *event,
 
 	in_scroll_cb = TRUE;
 
+	if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK) {
+		if (event->direction == GDK_SCROLL_UP) {
+			pdf_viewer_button_zoom_in_cb(NULL, viewer);
+		} else {
+			pdf_viewer_button_zoom_out_cb(NULL, viewer);
+		}
+		in_scroll_cb = FALSE;
+		return TRUE;
+	}
+
 	if (event->direction == GDK_SCROLL_UP &&
 	    gtk_adjustment_get_value(adj) == gtk_adjustment_get_lower(adj) &&
 	    cur_p > 1) {

commit 9193045b29cc690980f7e1f84311e8e729b66873
Author: Colin Leroy <colin at colino.net>
Date:   Mon Oct 15 10:39:21 2018 +0200

    Fancy: fix left-click on links not opening in browser.

diff --git a/src/plugins/fancy/fancy_viewer.c b/src/plugins/fancy/fancy_viewer.c
index a10f6bc..3792e84 100644
--- a/src/plugins/fancy/fancy_viewer.c
+++ b/src/plugins/fancy/fancy_viewer.c
@@ -902,17 +902,30 @@ static gboolean press_button_cb (WebKitWebView *view, GdkEvent *ev,
 		FancyViewer *viewer)
 {
 	gint type = 0;
+	gchar *link = NULL;
+
 	WebKitHitTestResult *result =
 		webkit_web_view_get_hit_test_result(view, (GdkEventButton *)ev);
 
 	g_object_get(G_OBJECT(result),
 			"context", &type,
-			"x", &viewer->click_x, "y", &viewer->click_y,
+			"x", &viewer->click_x,
+			"y", &viewer->click_y,
+			"link-uri", &link,
 			NULL);
 
 	if (type & WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION)
 		return FALSE;
 
+	if (viewer->cur_link) {
+		g_free(viewer->cur_link);
+		viewer->cur_link = NULL;
+	}
+	if (link != NULL) {
+		debug_print("press on %s\n", link);
+		viewer->cur_link = link; /* g_context returned a newly-allocated string */
+	}
+
 	viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
 	viewer->window = webkit_dom_document_get_default_view (viewer->doc);
 	viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);

commit e2b7ebaa1ae0019f513c6c0c3a0e515ed0fe747c
Author: Colin Leroy <colin at colino.net>
Date:   Thu Oct 11 16:06:39 2018 +0200

    Make the "Saving sent message..." debug prints more interesting.

diff --git a/src/procmsg.c b/src/procmsg.c
index ee98e09..62f6083 100644
--- a/src/procmsg.c
+++ b/src/procmsg.c
@@ -1132,13 +1132,19 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
 	gint num;
 	MsgInfo *msginfo, *tmp_msginfo;
 	MsgFlags flag = {0, 0};
+	gchar *outbox_path = NULL;
 
-	debug_print("saving sent message...\n");
-
-	if (!outbox)
+	if (!outbox) {
+		debug_print("using default outbox\n");
 		outbox = folder_get_default_outbox();
+	}
+
 	cm_return_val_if_fail(outbox != NULL, -1);
 
+	outbox_path = folder_item_get_path(outbox);
+	debug_print("saving sent message to %s...\n", outbox_path);
+	g_free(outbox_path);
+
 	/* remove queueing headers */
 	if (is_queued) {
 		gchar tmp[MAXPATHLEN + 1];
@@ -1740,13 +1746,21 @@ send_mail:
 
 	/* save message to outbox */
 	if (mailval == 0 && newsval == 0 && savecopyfolder) {
-		debug_print("saving sent message...\n");
+		debug_print("saving sent message to %s...\n", savecopyfolder);
 
 		if (!encrypt || !mailac->save_encrypted_as_clear_text) {
 			outbox = folder_find_item_from_identifier(savecopyfolder);
-			if (!outbox)
+			if (!outbox) {
+				gchar *id;
 				outbox = folder_get_default_outbox();
-
+				if (outbox != NULL) {
+					id = folder_item_get_identifier(outbox);
+					debug_print("%s not found, using %s\n", savecopyfolder, id);
+					g_free(id);
+				} else {
+					debug_print("could not find outbox\n");
+				}
+			}
 			/* Mail was not saved to outbox before encrypting, save it now. */
 			gboolean saved = FALSE;
 			*queued_removed = FALSE;

commit 34bfb629fcce5df9ad86994fff51f60ee7c0b5c7
Author: Colin Leroy <colin at colino.net>
Date:   Wed Oct 10 09:59:10 2018 +0200

    Fix in-memory temporary file length

diff --git a/src/procmime.c b/src/procmime.c
index 970a8e0..73c7967 100644
--- a/src/procmime.c
+++ b/src/procmime.c
@@ -428,8 +428,10 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 					err = TRUE;
 			}
 		}
-		if (tmpfp != outfp)
+		if (tmpfp != outfp) {
+			ftruncate(fileno(tmpfp), ftell(tmpfp));
 			claws_fclose(tmpfp);
+		}
 	} else if (encoding == ENC_X_UUENCODE) {
 		gchar outbuf[BUFFSIZE];
 		gint len;
@@ -465,6 +467,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 			g_warning("write error");
 	}
 
+	ftruncate(fileno(outfp), ftell(outfp));
 	claws_fclose(outfp);
 	claws_fclose(infp);
 
@@ -856,6 +859,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 
 	err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
 
+	ftruncate(fileno(outfp), ftell(outfp));
 	rewind(outfp);
 	if (err == TRUE) {
 		claws_fclose(outfp);
@@ -897,7 +901,7 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 	if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
 		return NULL;
 	}
-
+	ftruncate(fileno(outfp), ftell(outfp));
 	return outfp;
 }
 

commit 2f9a45ca063de741f02139ca0c94d0363b0ab608
Author: Ricardo Mones <ricardo at mones.org>
Date:   Mon Oct 8 15:51:15 2018 +0200

    Update textviewer.pl
    
    Patch by H.Merijn Brand

diff --git a/tools/README b/tools/README
index d562f46..5383def 100644
--- a/tools/README
+++ b/tools/README
@@ -707,18 +707,25 @@ Other tools
   WHAT IT DOES
 	This script tries to recognise an attachment by using the 'file'
 	command and/or the file extension and then uses the available
-	utilities to make an effort to display it as text. For example
-		.doc			antiword -w 72
-		.txt, .diff, .c, .h,
-  		  .ic. .ec, .cc, .sh,
-  		  .sed, .awk		cat
-		.html			txt2htm	or text2html
-		.xls, .sxc, .csv	xlscat -L
-		.rtf			rtf2text
-		.pdf			pdf2text %f -
-		.odt			ooo2txt
-		.pl, .pm		perltidy -st -se or  cat
-		.bz2			bzip2 -d < %f | strings
+	utilities to make an effort to display it as text.
+
+	$ textview.pl --list
+
+	will show available conversion, the top:
+
+	  .awk          cat
+	  .bin          strings
+	  .bz2          bzip2 -d < %f | strings
+	  .c            cat
+	  .cc           cat
+	  .csv          xlscat -L
+
+	If there are multiple alternatives available, they are listed in
+	the ordder they are tried, like for .xls:
+
+	  .xls          xlscat -L
+	  .xls          catdoc -x -dutf-8
+	  .xls          wvText
 
   HOW TO USE IT
   	Go to /Configuration/Message View/External Programs and enter the
@@ -726,7 +733,10 @@ Other tools
 	Now when you right-click an attachment and choose 'Display as text'
 	this script will be invoked.
 
-	
+	xlscat comes with the perl module Spreadsheet::Read, which is a
+	wrapper module over several parsers and supports ods, sxc, csv, xls,
+	xlsx, and sq.  See https://metacpan.org/release/Spreadsheet-Read
+
   Contact: H.Merijn Brand <h.m.brand at xs4all.nl>
 
 
diff --git a/tools/textviewer.pl b/tools/textviewer.pl
index 6851196..e9f04ae 100755
--- a/tools/textviewer.pl
+++ b/tools/textviewer.pl
@@ -1,44 +1,42 @@
 #!/usr/bin/perl
 
 # COPYRIGHT AND LICENSE
-#        Copyright (C) 2005-2006 H.Merijn Brand
+#        Copyright (C) 2005-2018 H.Merijn Brand
 #
 #        This script is free software; you can redistribute it and/or modify it
 #        under the same terms as Perl and/or Claws Mail itself. (GPL)
 
-use strict;
+use 5.14.1;
 use warnings;
 
-sub usage ($;$)
-{
+our $VERSION = "1.01 - 2018-10-08";
+our $CMD = $0 =~ s{.*/}{}r;
+
+sub usage {
     my ($err, $str) = (@_, "");
     $err and select STDERR;
-    print
-	"usage: $0 [--html] [--type=<type>] file\n",
+    say "usage: $CMD [--html] [--type=<type>] file\n",
 	"       --html    Generate HTML (if supported)\n",
-	"       --type=X  X as mimetype (msword => doc)\n";
-    $str and print "$str\n";
+	"       --type=X  X as mimetype (msword => doc)\n",
+	"  $CMD --list will show all implemented conversions";
+    $str and say $str;
     exit $err;
     } # usage
 
- at ARGV == 1 and $ARGV[0] eq "-?" || $ARGV[0] =~ m/^-+help$/ and usage (0);
-
 use Getopt::Long qw(:config bundling nopermute);
 my $opt_v = 0;
-my $opt_t;
 my $opt_h = "text";
 GetOptions (
+    "help|?"		=> sub { usage (0); },
+    "V|version"		=> sub { say "$CMD [$VERSION]"; exit 0; },
+
     "v|verbose:1"	=> \$opt_v,
-    "t|type|mimetype=s"	=> \$opt_t,
+    "t|type|mimetype=s"	=> \my $opt_t,
     "h|html"		=> sub { $opt_h = "html" },
+    "l|list!"		=> \my $opt_l,
     ) or usage (1);
 
-$opt_v and print "$0 @ARGV\n";
-
-my $file = shift or usage (1, "File argument is missing");
--f $file         or usage (1, "File argument is not a plain file");
--r $file         or usage (1, "File argument is not a readable file");
--s $file         or usage (1, "File argument is an empty file");
+$opt_v and say "$0 @ARGV";
 
 # anon-list contains all possible commands to show content
 # plain text is a reference to same type (alias)
@@ -49,40 +47,65 @@ my %fh = (
 
 	txt	=> [ "cat"		], # Plain text
 
-	html	=> [ "txt2htm",
-		     "text2html"	], # HTML
+	html	=> [ "htm2txt",
+		     "html2text"	], # HTML
 
 	msword	=> "doc",
-	doc	=> [ "antiword -w 72"	], # M$ Word
+	doc	=> [ "catdoc -x -dutf-8",
+		     "wvText",
+		     "antiword -w 72"	], # M$ Word
 	"vnd.ms-excel" => "xls",
 	"ms-excel"     => "xls",
-	xls	=> [ "xlscat -L"	], # M$ Excel
+	docx	=> [ "unoconv -f text --stdout"	], # MS Word
+	xlsx	=> "xls",
+	xls	=> [ "xlscat -L",
+		     "catdoc -x -dutf-8",
+		     "wvText"		], # M$ Excel
 #	ppt	=> [ "ppthtml"		], # M$ PowerPoint
 #			ppthtml "$1" | html2text
+	csv	=> "xls",		   # Comma Separated Values
+
+	ics	=> [ "ics2txt"		], # ICS calendar request
 
 	rtf	=> [ "rtf2text",
 		     "unrtf -t text"	], # RTF
 	pdf	=> [ "pdftotext %f -"	], # Adobe PDF
 
+	ods	=> "xls",		   # OpenOffice spreadsheet
 	sxc	=> "xls",		   # OpenOffice spreadsheet
-	odt	=> [ "ooo2txt"		], # OpenOffice writer
-
-	csv	=> "xls",		   # Comma Separated Values
+	odt	=> [ "oo2pod %f | pod2text",
+		     "ooo2txt"		], # OpenOffice writer
+	rtf	=> [ "rtf2text"		], # RTF
 
 	pl	=> [ "perltidy -st -se",
 		     "cat"		], # Perl
 	pm	=> "pl",
 
+	jsn	=> [ "json_pp"		], # JSON
+	json	=> "jsn",
+
+	xml	=> [ "xml_pp"		], # XML
+
 	( map { $_ => "txt" } qw(
-	    diff
+	    patch diff
 	    c h ic ec cc
 	    sh sed awk
 	    plain
+	    yml yaml
 	    )),
 
 	bz2	=> [ "bzip2 -d < %f | strings" ],
 
+	zip	=> [ "unzip -l %f"	], # ZIP
+
 	test	=> [ \&test		], # Internal
+
+	tgz	=> [ "tar tvf"		], # Tar     uncompressed
+	tgz	=> [ "tar tzvf"		], # Tar GZ    compressed
+	tbz	=> [ "tar tjvf"		], # Tar BZip2 compressed
+	txz	=> [ "tar tJvf"		], # Tar XZ    compressed
+
+	rar	=> [ "unrar l"		], # RAR
 	},
 
     html => {
@@ -90,6 +113,21 @@ my %fh = (
 	},
     );
 
+if ($opt_l) {
+    my %tc = %{$fh{text}};
+    foreach my $ext (sort keys %tc) {
+	my $exe = $tc{$ext};
+	ref $exe or $exe = $tc{$exe};
+	printf "  .%-12s %s\n", $ext, $_ for @$exe;
+	}
+    exit 0;
+    }
+
+my $file = shift or usage (1, "File argument is missing");
+-f $file         or usage (1, "File argument is not a plain file");
+-r $file         or usage (1, "File argument is not a readable file");
+-s $file         or usage (1, "File argument is an empty file");
+
 my $ext = $file =~ m/\.(\w+)$/ ? lc $1 : "";
 $opt_t && exists $fh{text}{lc $opt_t} and $ext = lc$opt_t;
 unless (exists $fh{text}{$ext}) {
@@ -111,10 +149,9 @@ exists $fh{$opt_h}{$ext} or $ext   = "txt";
 my          $ref = $fh{$opt_h}{$ext};
 ref $ref or $ref = $fh{$opt_h}{$ref};
 
-$opt_v and print STDERR "[ @$ref ] $file\n";
+$opt_v and warn "[ @$ref ] $file\n";
 
-sub which ($)
-{
+sub which {
     (my $cmd = shift) =~ s/\s.*//; # Only the command. Discard arguments here
     foreach my $path (split m/:+/, $ENV{PATH}) {
 	-x "$path/$cmd" and return "$path/$cmd";
@@ -134,6 +171,8 @@ foreach my $c (@$ref) {
     last;
     }
 
-$cmd =~ s/%f\b/$file/g or $cmd .= " $file";
-$opt_v and print "$cmd\n";
-exec $cmd;
+my @cmd = split m/ +/ => $cmd;
+grep { s/%f\b/$file/ } @cmd or push @cmd, $file;
+#$cmd =~ s/%f\b/$file/g or $cmd .= " $file";
+$opt_v and say "@cmd";
+exec @cmd;

commit 228170113bce5ddf948e5ae7162660175730a9f6
Author: Ricardo Mones <ricardo at mones.org>
Date:   Mon Oct 8 15:49:29 2018 +0200

    Update thread tools
    
    Patch by H.Merijn Brand

diff --git a/tools/README b/tools/README
index 9d81d64..d562f46 100644
--- a/tools/README
+++ b/tools/README
@@ -5,6 +5,7 @@ Contents of the tools directory:
 
 Action scripts:
   cm-reparent.pl                Fix thread parenting for two or more messages
+  cm-break.pl                   Remove thread parenting for one or more messages
   freshmeat_search.pl           Search freshmeat.net for selected text
   google_msgid.pl               Search groups.google.com for selected message-id
   google_search.pl              Search google.com for selected text
@@ -65,6 +66,11 @@ Action scripts
   COMMAND: cm-reparent.pl %F
   Thread the selected messages based on date, old to new
 
+* cm-break.pl
+  WORKS ON: selected messages (one or more)
+  COMMAND: cm-break.pl %F
+  Break thread references for the selected messages
+
 * google_msgid.pl
   WORKS ON: selection
   COMMAND: |google_msgid.pl
@@ -825,3 +831,51 @@ Extra tools
 This file is Copyright 1999-2014 by the Claws Mail team.
 See accompanying COPYING file for license details.
 See each included script for copyright and license details.
+
+* cm-reparent.pl
+
+  WHAT IT DOES
+	This script tries to fix thread parenting for two or more messages
+
+  HOW TO USE IT
+	Define an action as
+
+	  Menu name:  Reparent (fix threading)
+	  Command:    cm-reparent.pl %F
+
+	Then select from the message list all files that should be re-parented
+
+	Then invoke the action
+
+  MORE INFORMATION
+	$ perldoc cm-reparent.pl
+
+  REQUIREMENTS
+	Date::Parse
+	Getopt::Long
+
+  Contact: H.Merijn Brand <h.m.brand at xs4all.nl>
+
+* cm-break.pl
+
+  WHAT IT DOES
+	This script tries to break thread parenting for one or more messages
+
+  HOW TO USE IT
+	Define an action as
+
+	  Menu name:  Unthread (break threading)
+	  Command:    cm-break.pl %F
+
+	Then select from the message list all files that should be un-threaded
+
+	Then invoke the action
+
+  MORE INFORMATION
+	$ perldoc cm-break.pl
+
+  REQUIREMENTS
+	Date::Parse
+	Getopt::Long
+
+  Contact: H.Merijn Brand <h.m.brand at xs4all.nl>
diff --git a/tools/cm-break.pl b/tools/cm-break.pl
new file mode 100644
index 0000000..7553b17
--- /dev/null
+++ b/tools/cm-break.pl
@@ -0,0 +1,124 @@
+#!/usr/bin/perl
+
+use 5.14.1;
+use warnings;
+
+our $VERSION = "1.05 - 2018-10-08";
+our $cmd = $0 =~ s{.*/}{}r;
+
+sub usage {
+    my $err = shift and select STDERR;
+    say "usage: $cmd file ...";
+    exit $err;
+    } # usage
+
+use Date::Parse;
+use Getopt::Long;
+GetOptions (
+    "help|?"	=> sub { usage (0); },
+    "V|version"	=> sub { say "$cmd [$VERSION]"; exit 0; },
+    ) or usage (1);
+
+my %f;
+foreach my $fn (@ARGV) {
+
+    open my $fh, "<", $fn or die "$fn: $!\n";
+    my ($hdr, $body) = split m/(?<=\n)(?=\r?\n)/ => do { local $/; <$fh> }, 2;
+    close $fh;
+
+    $hdr && $hdr =~ m/\b(?:In-Reply-To|References)\b/i or next;
+
+    my ($mid) = $hdr =~ m{^Message-Id:  (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+    my ($dte) = $hdr =~ m{^Date:        (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+    my ($irt) = $hdr =~ m{^In-Reply-To: (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+    my ($ref) = $hdr =~ m{^References:  (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+
+    my $stamp = str2time ($dte) or die $dte;
+    my $date = $stamp ? do {
+	my @d = localtime $stamp;
+	sprintf "%4d-%02d-%02d %02d:%02d:%02d", $d[5] + 1900, ++$d[4], @d[3,2,1,0];
+	} : "-";
+    #printf "%12s %-20s %s\n", $stamp // "-", $date, $rcv;
+
+    $f{$fn} = {
+	msg_id	=> $mid,
+	refs	=> $ref,
+	irt	=> $irt,
+	date	=> $dte,
+	stamp	=> $stamp,
+	sdate	=> $date,
+
+	hdr	=> $hdr,
+	body	=> $body,
+	};
+    }
+
+foreach my $fn (sort keys %f) {
+
+    my $c = 0;
+
+    my $f = $f{$fn};
+    if ($f->{refs}) {
+	$c++;
+	$f->{hdr} =~ s{\nReferences:.*(?:\n\s+.*)*+}{}ig;
+	}
+    if ($f->{irt}) {
+	$c++;
+	$f->{hdr} =~ s{\nIn-Reply-To:.*(?:\n\s+.*)*+}{}ig;
+	}
+
+    $c or next;	# No changes required
+
+    say "$f->{msg_id} => -";
+
+    my @t = stat $fn;
+    open my $fh, ">", $fn or die "$fn: $!\n";
+    print   $fh $f->{hdr}, $f->{body};
+    close   $fh or die "$fn: $!\n";
+    utime $t[8], $t[9], $fn;
+    }
+
+__END__
+
+=head1 NAME
+
+cm-break.pl - remove mail from thread
+
+=head1 SYNOPSIS
+
+ cm-break.pl ~/Mail/inbox/23 ~/Mail/inbox/45 ...
+
+=head1 DESCRIPTION
+
+This script should be called from within Claws-Mail as an action
+
+Define an action as
+
+  Menu name:  Unthread (break threading)
+  Command:    cm-break.pl %F
+
+Then select from the message list all files that should be un-threaded
+
+Then invoke the action
+
+All of those mails will be modified (if needed): their C<In-Reply-To:>
+and C<References:> header tags are removed from the header.
+
+=head1 SEE ALSO
+
+L<Date::Parse>, L<Claws Mail|http://www.claws-mail.org>
+cm-reparent.pl
+
+=head1 AUTHOR
+
+H.Merijn Brand <h.m.brand at xs4all.nl>
+
+=head1 COPYRIGHT AND LICENSE
+
+ Copyright (C) 2018-2018 H.Merijn Brand.  All rights reserved.
+
+This library is free software;  you can redistribute and/or modify it under
+the same terms as Perl itself.
+See the L<Artistic license|http://dev.perl.org/licenses/artistic.html>.
+
+=cut
diff --git a/tools/cm-reparent.pl b/tools/cm-reparent.pl
index 9a16da2..30eb7fd 100755
--- a/tools/cm-reparent.pl
+++ b/tools/cm-reparent.pl
@@ -3,11 +3,12 @@
 use 5.14.1;
 use warnings;
 
-our $VERSION = "1.02 - 2016-06-07";
+our $VERSION = "1.05 - 2018-10-08";
+our $cmd = $0 =~ s{.*/}{}r;
 
 sub usage {
     my $err = shift and select STDERR;
-    say "usage: $0 file ...";
+    say "usage: $cmd file ...";
     exit $err;
     } # usage
 
@@ -15,7 +16,7 @@ use Date::Parse;
 use Getopt::Long;
 GetOptions (
     "help|?"	=> sub { usage (0); },
-    "V|version"	=> sub { say $0 =~ s{.*/}{}r, " [$VERSION]"; exit 0; },
+    "V|version"	=> sub { say "$cmd [$VERSION]"; exit 0; },
     ) or usage (1);
 
 my $p;
@@ -26,35 +27,47 @@ foreach my $fn (@ARGV) {
     my ($hdr, $body) = split m/(?<=\n)(?=\r?\n)/ => do { local $/; <$fh> }, 2;
     close $fh;
 
-    $hdr or next;
-
-    my ($mid) = $hdr =~ m{^Message-Id: (.*)}mi;
-    my ($dte) = $hdr =~ m{^Date: (.*)}mi;
-    my ($irt) = $hdr =~ m{^In-Reply-To: (.*)}mi;
-    my ($ref) = $hdr =~ m{^References: (.*)}mi;
-
-    my $stamp = str2time ($dte) or next;
+    $hdr && $hdr =~ m/\b(?:Date|Received)\b/ or next;
+
+    my ($mid) = $hdr =~ m{^Message-Id:  (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+    my ($dte) = $hdr =~ m{^Date:        (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+    my ($rcv) = $hdr =~ m{\nReceived:   (?:[\x20\t]*\n)?[\x20\t]+ (\S.*(?:\n\s+.*)*+)}xi;
+    my ($irt) = $hdr =~ m{^In-Reply-To: (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+    my ($ref) = $hdr =~ m{^References:  (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
+
+    $rcv ||= $dte;
+    $rcv =~ s/[\s\r\n]+/ /g;
+    $rcv =~ s/\s+$//;
+    $rcv =~ s/.*;\s*//;
+    $rcv =~ s/.* id \S+\s+//i;
+    my $stamp = str2time ($rcv) or die $rcv;
+    my $date = $stamp ? do {
+	my @d = localtime $stamp;
+	sprintf "%4d-%02d-%02d %02d:%02d:%02d", $d[5] + 1900, ++$d[4], @d[3,2,1,0];
+	} : "-";
+    #printf "%12s %-20s %s\n", $stamp // "-", $date, $rcv;
 
     $f{$fn} = {
 	msg_id	=> $mid,
 	refs	=> $ref,
 	irt	=> $irt,
 	date	=> $dte,
+	rcvd	=> $rcv,
 	stamp	=> $stamp,
+	sdate	=> $date,
 
 	hdr	=> $hdr,
 	body	=> $body,
 	};
 
     $p //= $fn;
-
     $stamp < $f{$p}{stamp} and $p = $fn;
     }
 
 # All but the oldest will refer to the oldest as parent
 
 $p or exit 0;
-my $pid = $f{$p}{msg_id};
+my $pid = $f{$p}{msg_id} or die "Parent file $p has no Message-Id\n";
 
 foreach my $fn (sort keys %f) {
 
@@ -86,11 +99,18 @@ foreach my $fn (sort keys %f) {
 
     $c or next;	# No changes required
 
+    unless ($f->{msg_id}) {
+	warn "Child message $fn has no Message-Id, skipped\n";
+	next;
+	}
+
     say "$f->{msg_id} => $pid";
 
+    my @t = stat $fn;
     open my $fh, ">", $fn or die "$fn: $!\n";
-    print $fh $f->{hdr}, $f->{body};
-    close $fh or die "$fn: $!\n";
+    print   $fh $f->{hdr}, $f->{body};
+    close   $fh or die "$fn: $!\n";
+    utime $t[8], $t[9], $fn;
     }
 
 __END__
@@ -149,6 +169,7 @@ C<X-In-Reply-To:> respectively.
 =head1 SEE ALSO
 
 L<Date::Parse>, L<Claws Mail|http://www.claws-mail.org>
+cm-break.pl
 
 =head1 AUTHOR
 
@@ -156,7 +177,7 @@ H.Merijn Brand <h.m.brand at xs4all.nl>
 
 =head1 COPYRIGHT AND LICENSE
 
- Copyright (C) 2016-2016 H.Merijn Brand.  All rights reserved.
+ Copyright (C) 2016-2018 H.Merijn Brand.  All rights reserved.
 
 This library is free software;  you can redistribute and/or modify it under
 the same terms as Perl itself.

commit b63551d65c05683134e9591fa3863761809c0077
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Mon Oct 8 13:31:31 2018 +0200

    ...and one more forgotten NULL check.

diff --git a/src/passwordstore.c b/src/passwordstore.c
index cd56c1b..0564d47 100644
--- a/src/passwordstore.c
+++ b/src/passwordstore.c
@@ -369,7 +369,7 @@ static gint _write_to_file(FILE *fp)
 			continue; /* Just in case. */
 
 		/* Do not save empty blocks. */
-		if (g_hash_table_size(block->entries) == 0)
+		if (block->entries == NULL || g_hash_table_size(block->entries) == 0)
 			continue;
 
 		/* Prepare the section header string and write it out. */

commit 69ce23d6739e4713fbb8b86d87e8a26f99ff7fd7
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Mon Oct 8 13:29:21 2018 +0200

    Added some NULL checks in passwordstore.c to avoid potential crash.

diff --git a/src/passwordstore.c b/src/passwordstore.c
index 4411711..cd56c1b 100644
--- a/src/passwordstore.c
+++ b/src/passwordstore.c
@@ -307,7 +307,7 @@ void passwd_store_reencrypt_all(const gchar *old_mpwd,
 		debug_print("Reencrypting passwords in block (%d/%s).\n",
 				block->block_type, block->block_name);
 
-		if (g_hash_table_size(block->entries) == 0)
+		if (block->entries == NULL || g_hash_table_size(block->entries) == 0)
 			continue;
 
 		keys = g_hash_table_get_keys(block->entries);

commit 68ec07738ae82e1d945b94225687465d406e7069
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Mon Oct 8 10:34:08 2018 +0200

    Fix unit tests building, broken after the file-utils addition.

diff --git a/src/common/tests/Makefile.am b/src/common/tests/Makefile.am
index 7b18f88..e69e4ee 100644
--- a/src/common/tests/Makefile.am
+++ b/src/common/tests/Makefile.am
@@ -12,11 +12,11 @@ AM_CPPFLAGS = \
 
 TEST_PROGS += xml_test
 xml_test_SOURCES = xml_test.c
-xml_test_LDADD = $(common_ldadd) ../xml.o ../stringtable.o ../utils.o ../codeconv.o ../quoted-printable.o ../unmime.o
+xml_test_LDADD = $(common_ldadd) ../xml.o ../stringtable.o ../utils.o ../codeconv.o ../quoted-printable.o ../unmime.o ../file-utils.o
 
 TEST_PROGS += codeconv_test
 codeconv_test_SOURCES = codeconv_test.c
-codeconv_test_LDADD = $(common_ldadd) ../codeconv.o ../utils.o ../quoted-printable.o ../unmime.o
+codeconv_test_LDADD = $(common_ldadd) ../codeconv.o ../utils.o ../quoted-printable.o ../unmime.o ../file-utils.o
 
 TEST_PROGS += md5_test
 md5_test_SOURCES = md5_test.c
diff --git a/src/common/tests/codeconv_test.c b/src/common/tests/codeconv_test.c
index 3b93eae..3cf7437 100644
--- a/src/common/tests/codeconv_test.c
+++ b/src/common/tests/codeconv_test.c
@@ -3,6 +3,7 @@
 #include "codeconv.h"
 
 #include "mock_prefs_common_get_use_shred.h"
+#include "mock_prefs_common_get_flush_metadata.h"
 
 struct td {
 	gchar *pre; /* Input string */
diff --git a/src/common/tests/mock_prefs_common_get_flush_metadata.h b/src/common/tests/mock_prefs_common_get_flush_metadata.h
new file mode 100644
index 0000000..d9f6a12
--- /dev/null
+++ b/src/common/tests/mock_prefs_common_get_flush_metadata.h
@@ -0,0 +1,5 @@
+gboolean prefs_common_get_flush_metadata(void)
+{
+	return FALSE;
+}
+
diff --git a/src/common/tests/xml_test.c b/src/common/tests/xml_test.c
index 1e58d8d..0159712 100644
--- a/src/common/tests/xml_test.c
+++ b/src/common/tests/xml_test.c
@@ -3,6 +3,7 @@
 #include "xml.h"
 
 #include "mock_prefs_common_get_use_shred.h"
+#include "mock_prefs_common_get_flush_metadata.h"
 
 #define DATADIR "data/"
 

commit 657acd4b1911aa13ad7606279bcc6ee81cdc7238
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Mon Oct 8 10:33:26 2018 +0200

    Fix file-utils related compiler warnings in crash.c.

diff --git a/src/crash.c b/src/crash.c
index cbff8c7..3634ee9 100644
--- a/src/crash.c
+++ b/src/crash.c
@@ -48,7 +48,7 @@
 #include "main.h"
 #include "claws.h"
 #include "crash.h"
-#include "utils.h"
+#include "file-utils.h"
 #include "filesel.h"
 #include "version.h"
 #include "prefs_common.h"

commit fcc32c9f4370cf74ba7059668a4c18c7865996ff
Author: Colin Leroy <colin at colino.net>
Date:   Sun Oct 7 21:31:37 2018 +0200

    Fix hang on IMAP - the log file is accessed by multiple threads

diff --git a/src/common/log.c b/src/common/log.c
index 83b023e..344eb97 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -36,12 +36,12 @@
 #include "hooks.h"
 #include "file-utils.h"
 
-#define FWRITE(_b,_s,_n,_f)	if (claws_fwrite(_b,_s,_n,_f) != _n) { \
-					g_message("log claws_fwrite failed!\n"); \
+#define FWRITE(_b,_s,_n,_f)	if (fwrite(_b,_s,_n,_f) != _n) { \
+					g_message("log fwrite failed!\n"); \
 					return; \
 				}
-#define FPUTS(_b,_f)		if (claws_fputs(_b,_f) == EOF) { \
-					g_message("log claws_fputs failed!\n"); \
+#define FPUTS(_b,_f)		if (fputs(_b,_f) == EOF) { \
+					g_message("log fputs failed!\n"); \
 					return; \
 				}
 #define FFLUSH(_f)		if (fflush(_f) != 0) { \
@@ -121,9 +121,9 @@ void set_log_file(LogInstance instance, const gchar *filename)
 		g_free(backupname);
 	}
 
-	log_fp[instance] = claws_fopen(fullname, "wb");
+	log_fp[instance] = fopen(fullname, "wb");
 	if (!log_fp[instance]) {
-		FILE_OP_ERROR(fullname, "claws_fopen");
+		FILE_OP_ERROR(fullname, "fopen");
 		log_filename[instance] = NULL;
 		g_free(fullname);
 		return;
@@ -136,7 +136,7 @@ void set_log_file(LogInstance instance, const gchar *filename)
 void close_log_file(LogInstance instance)
 {
 	if (log_fp[instance]) {
-		claws_safe_fclose(log_fp[instance]);
+		fclose(log_fp[instance]);
 		log_fp[instance] = NULL;
 		log_size[instance] = 0;
 		g_free(log_filename[instance]);

commit 429ab1b06c0215cc94ea299cdaa5abcdb7e46b27
Author: Colin Leroy <colin at colino.net>
Date:   Sun Oct 7 15:40:38 2018 +0200

    Fix performance loss on certain file I/O that doesn't need
    to be safe

diff --git a/src/common/file-utils.c b/src/common/file-utils.c
index 6ade1c6..48d29de 100644
--- a/src/common/file-utils.c
+++ b/src/common/file-utils.c
@@ -513,10 +513,11 @@ gint canonicalize_file_replace(const gchar *file)
 }
 
 
-gint str_write_to_file(const gchar *str, const gchar *file)
+gint str_write_to_file(const gchar *str, const gchar *file, gboolean safe)
 {
 	FILE *fp;
 	size_t len;
+	int r;
 
 	cm_return_val_if_fail(str != NULL, -1);
 	cm_return_val_if_fail(file != NULL, -1);
@@ -539,7 +540,13 @@ gint str_write_to_file(const gchar *str, const gchar *file)
 		return -1;
 	}
 
-	if (claws_safe_fclose(fp) == EOF) {
+	if (safe) {
+		r = claws_safe_fclose(fp);
+	} else {
+		r = claws_fclose(fp);
+	}
+
+	if (r == EOF) {
 		FILE_OP_ERROR(file, "claws_fclose");
 		claws_unlink(file);
 		return -1;
diff --git a/src/common/file-utils.h b/src/common/file-utils.h
index 58eb214..d89b069 100644
--- a/src/common/file-utils.h
+++ b/src/common/file-utils.h
@@ -82,8 +82,6 @@ gint copy_file_part		(FILE		*fp,
 gint canonicalize_file		(const gchar	*src,
 				 const gchar	*dest);
 gint canonicalize_file_replace	(const gchar	*file);
-gint str_write_to_file		(const gchar	*str,
-				 const gchar	*file);
 gchar *file_read_to_str		(const gchar	*file);
 gchar *file_read_to_str_no_recode(const gchar	*file);
 gchar *file_read_stream_to_str	(FILE		*fp);
@@ -100,6 +98,7 @@ FILE *get_tmpfile_in_dir	(const gchar 	*dir,
 				 gchar	       **filename);
 FILE *str_open_as_stream	(const gchar	*str);
 gint str_write_to_file		(const gchar	*str,
-				 const gchar	*file);
+				 const gchar	*file,
+				 gboolean	 safe);
 
 #endif
diff --git a/src/compose.c b/src/compose.c
index 72950ee..2abc6d3 100644
--- a/src/compose.c
+++ b/src/compose.c
@@ -5924,7 +5924,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
 					rewind(fp);
 					content = file_read_stream_to_str(fp);
 
-					str_write_to_file(content, tmp_enc_file);
+					str_write_to_file(content, tmp_enc_file, TRUE);
 					g_free(content);
 
 					/* Now write the unencrypted body. */
@@ -11690,7 +11690,7 @@ static void compose_insert_drag_received_cb (GtkWidget		*widget,
 			/* Assume a list of no files, and data has ://, is a remote link */
 			gchar *tmpdata = g_strstrip(g_strdup(ddata));
 			gchar *tmpfile = get_tmp_file();
-			str_write_to_file(tmpdata, tmpfile);
+			str_write_to_file(tmpdata, tmpfile, TRUE);
 			g_free(tmpdata);  
 			compose_insert_file(compose, tmpfile);
 			claws_unlink(tmpfile);
diff --git a/src/crash.c b/src/crash.c
index a6f10dd..cbff8c7 100644
--- a/src/crash.c
+++ b/src/crash.c
@@ -290,7 +290,7 @@ static void crash_create_debugger_file(void)
 {
 	gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
 	
-	str_write_to_file(DEBUG_SCRIPT, filespec);
+	str_write_to_file(DEBUG_SCRIPT, filespec, TRUE);
 	g_free(filespec);
 }
 
@@ -310,7 +310,7 @@ static void crash_save_crash_log(GtkButton *button, const gchar *text)
 	strftime(buf, sizeof buf, "claws-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
 	if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
 	&&  *filename)
-		str_write_to_file(text, filename);
+		str_write_to_file(text, filename, TRUE);
 	g_free(filename);	
 }
 
diff --git a/src/main.c b/src/main.c
index 65a3ec1..0f17016 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1440,7 +1440,7 @@ int main(int argc, char *argv[])
 		folder_item_update_freeze();
 	}
 	/* make the crash-indicator file */
-	str_write_to_file("foo", get_crashfile_name());
+	str_write_to_file("foo", get_crashfile_name(), FALSE);
 
 	inc_autocheck_timer_init(mainwin);
 
diff --git a/src/news.c b/src/news.c
index 70eb4d6..61120c4 100644
--- a/src/news.c
+++ b/src/news.c
@@ -819,7 +819,7 @@ static gint news_get_article(Folder *folder, gint num, gchar *filename)
 	r = nntp_threaded_article(folder, num, &result, &len);
 	
 	if (r == NEWSNNTP_NO_ERROR) {
-		if (str_write_to_file(result, filename) < 0) {
+		if (str_write_to_file(result, filename, FALSE) < 0) {
 			mmap_string_unref(result);
 			return -1;
 		}
diff --git a/src/plugins/fancy/fancy_prefs.c b/src/plugins/fancy/fancy_prefs.c
index beca04f..f7c53cd 100644
--- a/src/plugins/fancy/fancy_prefs.c
+++ b/src/plugins/fancy/fancy_prefs.c
@@ -365,7 +365,7 @@ static void fancy_prefs_stylesheet_edit_cb(GtkWidget *widget, gpointer data)
 {
 	const gchar *stylesheet = gtk_entry_get_text(GTK_ENTRY(data));
 	if (!is_file_exist(stylesheet))
-		str_write_to_file(stylesheet, "");
+		str_write_to_file(stylesheet, "", TRUE);
 	open_txt_editor(stylesheet, prefs_common_get_ext_editor_cmd());
 }
 
diff --git a/src/plugins/smime/smime.c b/src/plugins/smime/smime.c
index ea999c1..2546343 100644
--- a/src/plugins/smime/smime.c
+++ b/src/plugins/smime/smime.c
@@ -286,7 +286,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
 				gchar *tmp_file = get_tmp_file();
 				MimeInfo *newinfo = NULL, *decinfo = NULL, *parentinfo = NULL;
 
-				str_write_to_file(textstr, tmp_file);
+				str_write_to_file(textstr, tmp_file, TRUE);
 				newinfo = procmime_scan_file(tmp_file);
 				decinfo = g_node_first_child(newinfo->node) != NULL ?
 					g_node_first_child(newinfo->node)->data : NULL;
diff --git a/src/plugins/spamassassin/spamassassin.c b/src/plugins/spamassassin/spamassassin.c
index 0cd725a..e5a39b2 100644
--- a/src/plugins/spamassassin/spamassassin.c
+++ b/src/plugins/spamassassin/spamassassin.c
@@ -389,7 +389,7 @@ gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
 						config.username, config.timeout,
 						config.max_size * 1024, config.compress?"-z":"",
 						spam?"spam":"ham");
-		if (str_write_to_file(contents, fname) < 0) {
+		if (str_write_to_file(contents, fname, TRUE) < 0) {
 			g_free(fname);
 			fname = NULL;
 		}
diff --git a/src/plugins/vcalendar/vcal_manager.c b/src/plugins/vcalendar/vcal_manager.c
index 009cdbd..fe71bf6 100644
--- a/src/plugins/vcalendar/vcal_manager.c
+++ b/src/plugins/vcalendar/vcal_manager.c
@@ -502,7 +502,7 @@ gchar *vcal_manager_event_dump(VCalEvent *event, gboolean is_reply, gboolean is_
 			       "\n"
 			       "%s", headers, icalcomponent_as_ical_string(calendar));
 	
-	if (str_write_to_file(body, tmpfile) < 0) {
+	if (str_write_to_file(body, tmpfile, FALSE) < 0) {
 		g_free(tmpfile);
 		tmpfile = NULL;
 	}
@@ -640,7 +640,7 @@ gchar *vcal_manager_dateevent_dump(const gchar *uid, FolderItem *item)
 			       "\n"
 			       "%s", headers, lines);
 	g_free(lines);
-	if (str_write_to_file(body, tmpfile) < 0) {
+	if (str_write_to_file(body, tmpfile, FALSE) < 0) {
 		g_free(tmpfile);
 		tmpfile = NULL;
 	} else
@@ -754,7 +754,7 @@ gchar *vcal_manager_icalevent_dump(icalcomponent *event, gchar *orga, icalcompon
 			       "\n"
 			       "%s", headers, qpbody);
 	
-	if (str_write_to_file(body, tmpfile) < 0) {
+	if (str_write_to_file(body, tmpfile, FALSE) < 0) {
 		g_free(tmpfile);
 		tmpfile = NULL;
 	} else
diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c
index 11188a4..178ab58 100644
--- a/src/plugins/vcalendar/vcal_meeting_gtk.c
+++ b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -1854,7 +1854,7 @@ void multisync_export(void)
         	    ); 	
 		vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
 		tmp = g_strconcat(path, G_DIR_SEPARATOR_S, file, NULL);
-		str_write_to_file(icalcomponent_as_ical_string(calendar), tmp);
+		str_write_to_file(icalcomponent_as_ical_string(calendar), tmp, TRUE);
 		g_free(tmp);
 		files = g_slist_append(files, file);
 		vcal_manager_free_event(event);
@@ -1914,7 +1914,7 @@ gboolean vcal_meeting_export_calendar(const gchar *path,
 			   	NULL, ALERT_NOTICE);
 			return FALSE;
 		} else {
-			str_write_to_file("", tmpfile);
+			str_write_to_file("", tmpfile, TRUE);
 			goto putfile;
 		}
 	}
@@ -1935,7 +1935,7 @@ gboolean vcal_meeting_export_calendar(const gchar *path,
 		vcal_manager_free_event(event);
 	}
 
-	if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
+	if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file, TRUE) < 0) {
 		g_warning("can't export internal cal");
 	}
 	
@@ -1948,7 +1948,7 @@ gboolean vcal_meeting_export_calendar(const gchar *path,
 	}
 
 	if (vcalprefs.export_enable || path == NULL) {
-		if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
+		if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile, TRUE) < 0) {
 			alertpanel_error(_("Could not export the calendar."));
 			g_free(tmpfile);
 			icalcomponent_free(calendar);
@@ -2097,14 +2097,14 @@ gboolean vcal_meeting_export_freebusy(const gchar *path, const gchar *user,
 
 	icalcomponent_add_component(calendar, vfreebusy);
 	
-	if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
+	if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file, TRUE) < 0) {
 		g_warning("can't export freebusy");
 	}
 	
 	g_free(internal_file);
 
 	if (vcalprefs.export_freebusy_enable) {
-		if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
+		if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile, TRUE) < 0) {
 			alertpanel_error(_("Could not export the freebusy info."));
 			g_free(tmpfile);
 			icalcomponent_free(calendar);
diff --git a/src/prefs_account.c b/src/prefs_account.c
index 8697c33..91a73f0 100644
--- a/src/prefs_account.c
+++ b/src/prefs_account.c
@@ -4554,7 +4554,7 @@ static void prefs_account_signature_edit_cb(GtkWidget *widget, gpointer data)
 {
 	const gchar *sigpath = gtk_entry_get_text(GTK_ENTRY(data));
 	if (!is_file_exist(sigpath))
-		str_write_to_file(sigpath, "");
+		str_write_to_file(sigpath, "", TRUE);
 	open_txt_editor(sigpath, prefs_common_get_ext_editor_cmd());
 }
 
diff --git a/src/wizard.c b/src/wizard.c
index 3c74ba2..6869dea 100644
--- a/src/wizard.c
+++ b/src/wizard.c
@@ -403,7 +403,7 @@ static void wizard_read_defaults(void)
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "accountrc.tmpl", NULL);
 	if (!is_file_exist(rcpath)) {
-		str_write_to_file(accountrc_tmpl, rcpath);
+		str_write_to_file(accountrc_tmpl, rcpath, TRUE);
 	}
 
 	prefs_read_config(template_params, "AccountTemplate", rcpath, NULL);
@@ -591,7 +591,7 @@ static void write_welcome_email(WizardWindow *wizard)
 	msg = g_strconcat(head, body, NULL);
 
 	if (inbox && inbox->total_msgs == 0
-	 && str_write_to_file(msg, file) >= 0) {
+	 && str_write_to_file(msg, file, TRUE) >= 0) {
 		MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
 		folder_item_add_msg(inbox, file, &flags, FALSE);
 	}

commit ebb97540bfbddcf4a4de619086ad8fa43a733eba
Author: Colin Leroy <colin at colino.net>
Date:   Sun Oct 7 20:41:20 2018 +0200

    Make safe_fclose static now that it can be

diff --git a/src/common/file-utils.c b/src/common/file-utils.c
index 8157de5..6ade1c6 100644
--- a/src/common/file-utils.c
+++ b/src/common/file-utils.c
@@ -34,8 +34,7 @@
 gboolean prefs_common_get_flush_metadata(void);
 gboolean prefs_common_get_use_shred(void);
 
-/* FIXME make static once every file I/O is done using claws_* wrappers */
-int safe_fclose(FILE *fp)
+static int safe_fclose(FILE *fp)
 {
 	int r;
 	START_TIMING("");
@@ -53,6 +52,18 @@ int safe_fclose(FILE *fp)
 	return r;
 }
 
+/* Unlock, then safe-close a file pointer
+ * Safe close is done using fflush + fsync
+ * if the according preference says so.
+ */
+int claws_safe_fclose(FILE *fp)
+{
+#if HAVE_FGETS_UNLOCKED
+	funlockfile(fp);
+#endif
+	return safe_fclose(fp);
+}
+
 #if HAVE_FGETS_UNLOCKED
 
 /* Open a file and locks it once
@@ -84,16 +95,7 @@ int claws_fclose(FILE *fp)
 	funlockfile(fp);
 	return fclose(fp);
 }
-
-/* Unlock, then safe-close a file pointer
- * Safe close is done using fflush + fsync
- * if the according preference says so.
- */
-int claws_safe_fclose(FILE *fp)
-{
-	funlockfile(fp);
-	return safe_fclose(fp);
-}
+#endif
 
 #ifdef G_OS_WIN32
 #define WEXITSTATUS(x) (x)
@@ -853,5 +855,3 @@ FILE *str_open_as_stream(const gchar *str)
 	rewind(fp);
 	return fp;
 }
-
-#endif
diff --git a/src/common/file-utils.h b/src/common/file-utils.h
index 6c5ad17..58eb214 100644
--- a/src/common/file-utils.h
+++ b/src/common/file-utils.h
@@ -42,7 +42,6 @@ FILE *claws_fopen		(const char *file,
 FILE *claws_fdopen		(int fd,
 				 const char *mode);
 int claws_fclose		(FILE *fp);
-int claws_safe_fclose		(FILE *fp);
 
 #else
 
@@ -57,10 +56,9 @@ int claws_safe_fclose		(FILE *fp);
 #define claws_fopen g_fopen
 #define claws_fdopen fdopen
 #define claws_fclose fclose
-#define claws_safe_fclose safe_fclose
 #endif
 
-int safe_fclose			(FILE *fp);
+int claws_safe_fclose		(FILE *fp);
 int claws_unlink		(const char	*filename);
 
 gint file_strip_crs		(const gchar	*file);

commit b7e7385fa23d999f95677090c30204ddd6747a0d
Author: Colin Leroy <colin at colino.net>
Date:   Sun Oct 7 14:46:02 2018 +0200

    Rename claws_io to file-utils, and move file-related functions
    from utils.c to file-utils.c

diff --git a/src/account.c b/src/account.c
index 285257b..6f8521e 100644
--- a/src/account.c
+++ b/src/account.c
@@ -55,7 +55,7 @@
 #include "prefs_actions.h"
 #include "hooks.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 enum {
 	ACCOUNT_IS_DEFAULT,
diff --git a/src/addrbook.c b/src/addrbook.c
index 64a3645..8087996 100644
--- a/src/addrbook.c
+++ b/src/addrbook.c
@@ -38,7 +38,7 @@
 #include "addrcache.h"
 #include "addrbook.h"
 #include "adbookbase.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
diff --git a/src/addrclip.c b/src/addrclip.c
index 90180c9..58d5947 100644
--- a/src/addrclip.c
+++ b/src/addrclip.c
@@ -56,6 +56,7 @@
 #include "addrclip.h"
 #include "alertpanel.h"
 #include "defs.h"
+#include "file-utils.h"
 
 /*
 * Create a clipboard.
diff --git a/src/addressbook.c b/src/addressbook.c
index c05871e..85ecf81 100644
--- a/src/addressbook.c
+++ b/src/addressbook.c
@@ -44,6 +44,7 @@
 #include "xml.h"
 #include "prefs_gtk.h"
 #include "procmime.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "codeconv.h"
diff --git a/src/addrharvest.c b/src/addrharvest.c
index 1d94633..d837293 100644
--- a/src/addrharvest.c
+++ b/src/addrharvest.c
@@ -40,7 +40,7 @@
 #ifdef USE_ALT_ADDRBOOK
 	#include "addressbook-dbus.h"
 #endif
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* Mail header names of interest */
 static gchar *_headerFrom_     = HEADER_FROM;
diff --git a/src/addrindex.c b/src/addrindex.c
index bad1fa5..82c2fed 100644
--- a/src/addrindex.c
+++ b/src/addrindex.c
@@ -43,7 +43,7 @@
 #include "utils.h"
 #include "alertpanel.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
diff --git a/src/addritem.c b/src/addritem.c
index 3a81826..cb76257 100644
--- a/src/addritem.c
+++ b/src/addritem.c
@@ -25,6 +25,7 @@
 #include <string.h>
 
 #include "defs.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "addritem.h"
 #include "mgutils.h"
diff --git a/src/addrmerge.c b/src/addrmerge.c
index 363bab7..6aa8315 100644
--- a/src/addrmerge.c
+++ b/src/addrmerge.c
@@ -38,6 +38,7 @@
 #include "addrmerge.h"
 #include "alertpanel.h"
 #include "gtkutils.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "prefs_common.h"
 
diff --git a/src/autofaces.c b/src/autofaces.c
index d12c975..300435c 100644
--- a/src/autofaces.c
+++ b/src/autofaces.c
@@ -27,7 +27,7 @@
 
 #include "utils.h"
 #include "autofaces.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
 {
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index aa307ae..805db4d 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -25,6 +25,7 @@ endif
 
 libclawscommon_la_SOURCES = $(arch_sources) \
 	codeconv.c \
+	file-utils.c \
 	hooks.c \
 	log.c \
 	md5.c \
@@ -43,7 +44,6 @@ libclawscommon_la_SOURCES = $(arch_sources) \
 	string_match.c \
 	stringtable.c \
 	claws.c \
-	claws_io.c \
 	tags.c \
 	template.c \
 	utils.c \
@@ -56,6 +56,7 @@ libclawscommon_la_SOURCES = $(arch_sources) \
 clawscommonincludedir = $(pkgincludedir)/common
 clawscommoninclude_HEADERS = $(arch_headers) \
 	codeconv.h \
+	file-utils.h \
 	defs.h \
 	hooks.h \
 	log.h \
@@ -75,7 +76,6 @@ clawscommoninclude_HEADERS = $(arch_headers) \
 	string_match.h \
 	stringtable.h \
 	claws.h \
-	claws_io.h \
 	tags.h \
 	template.h \
 	timing.h \
diff --git a/src/common/claws_io.c b/src/common/claws_io.c
deleted file mode 100644
index 6268978..0000000
--- a/src/common/claws_io.c
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifdef HAVE_CONFIG_H
-#  include "config.h"
-#include "claws-features.h"
-#endif
-
-#include <stdio.h>
-#include <unistd.h>
-
-#include "timing.h"
-#include "claws_io.h"
-
-gboolean prefs_common_get_flush_metadata(void);
-
-/* FIXME make static once every file I/O is done using claws_* wrappers */
-int safe_fclose(FILE *fp)
-{
-	int r;
-	START_TIMING("");
-
-	if (fflush(fp) != 0) {
-		return EOF;
-	}
-	if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
-		return EOF;
-	}
-
-	r = fclose(fp);
-	END_TIMING();
-
-	return r;
-}
-
-#if HAVE_FGETS_UNLOCKED
-
-/* Open a file and locks it once
- * so subsequent I/O is faster
- */
-FILE *claws_fopen(const char *file, const char *mode)
-{
-	FILE *fp = fopen(file, mode);
-	if (!fp)
-		return NULL;
-	flockfile(fp);
-	return fp;
-}
-
-FILE *claws_fdopen(int fd, const char *mode)
-{
-	FILE *fp = fdopen(fd, mode);
-	if (!fp)
-		return NULL;
-	flockfile(fp);
-	return fp;
-}
-
-/* Unlocks and close a file pointer
- */
-
-int claws_fclose(FILE *fp)
-{
-	funlockfile(fp);
-	return fclose(fp);
-}
-
-/* Unlock, then safe-close a file pointer
- * Safe close is done using fflush + fsync
- * if the according preference says so.
- */
-int claws_safe_fclose(FILE *fp)
-{
-	funlockfile(fp);
-	return safe_fclose(fp);
-}
-
-#endif
diff --git a/src/common/file-utils.c b/src/common/file-utils.c
new file mode 100644
index 0000000..8157de5
--- /dev/null
+++ b/src/common/file-utils.c
@@ -0,0 +1,857 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "defs.h"
+#include "codeconv.h"
+#include "timing.h"
+#include "file-utils.h"
+
+gboolean prefs_common_get_flush_metadata(void);
+gboolean prefs_common_get_use_shred(void);
+
+/* FIXME make static once every file I/O is done using claws_* wrappers */
+int safe_fclose(FILE *fp)
+{
+	int r;
+	START_TIMING("");
+
+	if (fflush(fp) != 0) {
+		return EOF;
+	}
+	if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
+		return EOF;
+	}
+
+	r = fclose(fp);
+	END_TIMING();
+
+	return r;
+}
+
+#if HAVE_FGETS_UNLOCKED
+
+/* Open a file and locks it once
+ * so subsequent I/O is faster
+ */
+FILE *claws_fopen(const char *file, const char *mode)
+{
+	FILE *fp = fopen(file, mode);
+	if (!fp)
+		return NULL;
+	flockfile(fp);
+	return fp;
+}
+
+FILE *claws_fdopen(int fd, const char *mode)
+{
+	FILE *fp = fdopen(fd, mode);
+	if (!fp)
+		return NULL;
+	flockfile(fp);
+	return fp;
+}
+
+/* Unlocks and close a file pointer
+ */
+
+int claws_fclose(FILE *fp)
+{
+	funlockfile(fp);
+	return fclose(fp);
+}
+
+/* Unlock, then safe-close a file pointer
+ * Safe close is done using fflush + fsync
+ * if the according preference says so.
+ */
+int claws_safe_fclose(FILE *fp)
+{
+	funlockfile(fp);
+	return safe_fclose(fp);
+}
+
+#ifdef G_OS_WIN32
+#define WEXITSTATUS(x) (x)
+#endif
+
+int claws_unlink(const char *filename) 
+{
+	GStatBuf s;
+	static int found_shred = -1;
+	static const gchar *args[4];
+
+	if (filename == NULL)
+		return 0;
+
+	if (prefs_common_get_use_shred()) {
+		if (found_shred == -1) {
+			/* init */
+			args[0] = g_find_program_in_path("shred");
+			debug_print("found shred: %s\n", args[0]);
+			found_shred = (args[0] != NULL) ? 1:0;
+			args[1] = "-f";
+			args[3] = NULL;
+		}
+		if (found_shred == 1) {
+			if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
+				if (s.st_nlink == 1) {
+					gint status=0;
+					args[2] = filename;
+					g_spawn_sync(NULL, (gchar **)args, NULL, 0,
+					 NULL, NULL, NULL, NULL, &status, NULL);
+					debug_print("%s %s exited with status %d\n",
+						args[0], filename, WEXITSTATUS(status));
+					if (truncate(filename, 0) < 0)
+						g_warning("couln't truncate: %s", filename);
+				}
+			}
+		}
+	}
+	return g_unlink(filename);
+}
+
+gint file_strip_crs(const gchar *file)
+{
+	FILE *fp = NULL, *outfp = NULL;
+	gchar buf[4096];
+	gchar *out = get_tmp_file();
+	if (file == NULL)
+		goto freeout;
+
+	fp = claws_fopen(file, "rb");
+	if (!fp)
+		goto freeout;
+
+	outfp = claws_fopen(out, "wb");
+	if (!outfp) {
+		claws_fclose(fp);
+		goto freeout;
+	}
+
+	while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
+		strcrchomp(buf);
+		if (claws_fputs(buf, outfp) == EOF) {
+			claws_fclose(fp);
+			claws_fclose(outfp);
+			goto unlinkout;
+		}
+	}
+
+	claws_fclose(fp);
+	if (claws_safe_fclose(outfp) == EOF) {
+		goto unlinkout;
+	}
+	
+	if (move_file(out, file, TRUE) < 0)
+		goto unlinkout;
+	
+	g_free(out);
+	return 0;
+unlinkout:
+	claws_unlink(out);
+freeout:
+	g_free(out);
+	return -1;
+}
+
+/*
+ * Append src file body to the tail of dest file.
+ * Now keep_backup has no effects.
+ */
+gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
+{
+	FILE *src_fp, *dest_fp;
+	gint n_read;
+	gchar buf[BUFSIZ];
+
+	gboolean err = FALSE;
+
+	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+		FILE_OP_ERROR(src, "claws_fopen");
+		return -1;
+	}
+
+	if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		claws_fclose(src_fp);
+		return -1;
+	}
+
+	if (change_file_mode_rw(dest_fp, dest) < 0) {
+		FILE_OP_ERROR(dest, "chmod");
+		g_warning("can't change file mode: %s", dest);
+	}
+
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(src_fp))
+			break;
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
+			g_warning("writing to %s failed.", dest);
+			claws_fclose(dest_fp);
+			claws_fclose(src_fp);
+			claws_unlink(dest);
+			return -1;
+		}
+	}
+
+	if (claws_ferror(src_fp)) {
+		FILE_OP_ERROR(src, "claws_fread");
+		err = TRUE;
+	}
+	claws_fclose(src_fp);
+	if (claws_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
+		err = TRUE;
+	}
+
+	if (err) {
+		claws_unlink(dest);
+		return -1;
+	}
+
+	return 0;
+}
+
+gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
+{
+	FILE *src_fp, *dest_fp;
+	gint n_read;
+	gchar buf[BUFSIZ];
+	gchar *dest_bak = NULL;
+	gboolean err = FALSE;
+
+	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+		FILE_OP_ERROR(src, "claws_fopen");
+		return -1;
+	}
+	if (is_file_exist(dest)) {
+		dest_bak = g_strconcat(dest, ".bak", NULL);
+		if (rename_force(dest, dest_bak) < 0) {
+			FILE_OP_ERROR(dest, "rename");
+			claws_fclose(src_fp);
+			g_free(dest_bak);
+			return -1;
+		}
+	}
+
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		claws_fclose(src_fp);
+		if (dest_bak) {
+			if (rename_force(dest_bak, dest) < 0)
+				FILE_OP_ERROR(dest_bak, "rename");
+			g_free(dest_bak);
+		}
+		return -1;
+	}
+
+	if (change_file_mode_rw(dest_fp, dest) < 0) {
+		FILE_OP_ERROR(dest, "chmod");
+		g_warning("can't change file mode: %s", dest);
+	}
+
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(src_fp))
+			break;
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
+			g_warning("writing to %s failed.", dest);
+			claws_fclose(dest_fp);
+			claws_fclose(src_fp);
+			claws_unlink(dest);
+			if (dest_bak) {
+				if (rename_force(dest_bak, dest) < 0)
+					FILE_OP_ERROR(dest_bak, "rename");
+				g_free(dest_bak);
+			}
+			return -1;
+		}
+	}
+
+	if (claws_ferror(src_fp)) {
+		FILE_OP_ERROR(src, "claws_fread");
+		err = TRUE;
+	}
+	claws_fclose(src_fp);
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
+		err = TRUE;
+	}
+
+	if (err) {
+		claws_unlink(dest);
+		if (dest_bak) {
+			if (rename_force(dest_bak, dest) < 0)
+				FILE_OP_ERROR(dest_bak, "rename");
+			g_free(dest_bak);
+		}
+		return -1;
+	}
+
+	if (keep_backup == FALSE && dest_bak)
+		claws_unlink(dest_bak);
+
+	g_free(dest_bak);
+
+	return 0;
+}
+
+gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
+{
+	if (overwrite == FALSE && is_file_exist(dest)) {
+		g_warning("move_file(): file %s already exists.", dest);
+		return -1;
+	}
+
+	if (rename_force(src, dest) == 0) return 0;
+
+	if (EXDEV != errno) {
+		FILE_OP_ERROR(src, "rename");
+		return -1;
+	}
+
+	if (copy_file(src, dest, FALSE) < 0) return -1;
+
+	claws_unlink(src);
+
+	return 0;
+}
+
+gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
+{
+	gint n_read;
+	gint bytes_left, to_read;
+	gchar buf[BUFSIZ];
+
+	if (fseek(fp, offset, SEEK_SET) < 0) {
+		perror("fseek");
+		return -1;
+	}
+
+	bytes_left = length;
+	to_read = MIN(bytes_left, sizeof(buf));
+
+	while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
+		if (n_read < to_read && claws_ferror(fp))
+			break;
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
+			return -1;
+		}
+		bytes_left -= n_read;
+		if (bytes_left == 0)
+			break;
+		to_read = MIN(bytes_left, sizeof(buf));
+	}
+
+	if (claws_ferror(fp)) {
+		perror("claws_fread");
+		return -1;
+	}
+
+	return 0;
+}
+
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+	FILE *dest_fp;
+	gboolean err = FALSE;
+
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		return -1;
+	}
+
+	if (change_file_mode_rw(dest_fp, dest) < 0) {
+		FILE_OP_ERROR(dest, "chmod");
+		g_warning("can't change file mode: %s", dest);
+	}
+
+	if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
+		err = TRUE;
+
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
+		err = TRUE;
+	}
+
+	if (err) {
+		g_warning("writing to %s failed.", dest);
+		claws_unlink(dest);
+		return -1;
+	}
+
+	return 0;
+}
+
+gint canonicalize_file(const gchar *src, const gchar *dest)
+{
+	FILE *src_fp, *dest_fp;
+	gchar buf[BUFFSIZE];
+	gint len;
+	gboolean err = FALSE;
+	gboolean last_linebreak = FALSE;
+
+	if (src == NULL || dest == NULL)
+		return -1;
+
+	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+		FILE_OP_ERROR(src, "claws_fopen");
+		return -1;
+	}
+
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		claws_fclose(src_fp);
+		return -1;
+	}
+
+	if (change_file_mode_rw(dest_fp, dest) < 0) {
+		FILE_OP_ERROR(dest, "chmod");
+		g_warning("can't change file mode: %s", dest);
+	}
+
+	while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
+		gint r = 0;
+
+		len = strlen(buf);
+		if (len == 0) break;
+		last_linebreak = FALSE;
+
+		if (buf[len - 1] != '\n') {
+			last_linebreak = TRUE;
+			r = claws_fputs(buf, dest_fp);
+		} else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
+			r = claws_fputs(buf, dest_fp);
+		} else {
+			if (len > 1) {
+				r = claws_fwrite(buf, 1, len - 1, dest_fp);
+				if (r != (len -1))
+					r = EOF;
+			}
+			if (r != EOF)
+				r = claws_fputs("\r\n", dest_fp);
+		}
+
+		if (r == EOF) {
+			g_warning("writing to %s failed.", dest);
+			claws_fclose(dest_fp);
+			claws_fclose(src_fp);
+			claws_unlink(dest);
+			return -1;
+		}
+	}
+
+	if (last_linebreak == TRUE) {
+		if (claws_fputs("\r\n", dest_fp) == EOF)
+			err = TRUE;
+	}
+
+	if (claws_ferror(src_fp)) {
+		FILE_OP_ERROR(src, "claws_fgets");
+		err = TRUE;
+	}
+	claws_fclose(src_fp);
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
+		err = TRUE;
+	}
+
+	if (err) {
+		claws_unlink(dest);
+		return -1;
+	}
+
+	return 0;
+}
+
+gint canonicalize_file_replace(const gchar *file)
+{
+	gchar *tmp_file;
+
+	tmp_file = get_tmp_file();
+
+	if (canonicalize_file(file, tmp_file) < 0) {
+		g_free(tmp_file);
+		return -1;
+	}
+
+	if (move_file(tmp_file, file, TRUE) < 0) {
+		g_warning("can't replace file: %s", file);
+		claws_unlink(tmp_file);
+		g_free(tmp_file);
+		return -1;
+	}
+
+	g_free(tmp_file);
+	return 0;
+}
+
+
+gint str_write_to_file(const gchar *str, const gchar *file)
+{
+	FILE *fp;
+	size_t len;
+
+	cm_return_val_if_fail(str != NULL, -1);
+	cm_return_val_if_fail(file != NULL, -1);
+
+	if ((fp = claws_fopen(file, "wb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
+		return -1;
+	}
+
+	len = strlen(str);
+	if (len == 0) {
+		claws_fclose(fp);
+		return 0;
+	}
+
+	if (claws_fwrite(str, 1, len, fp) != len) {
+		FILE_OP_ERROR(file, "claws_fwrite");
+		claws_fclose(fp);
+		claws_unlink(file);
+		return -1;
+	}
+
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(file, "claws_fclose");
+		claws_unlink(file);
+		return -1;
+	}
+
+	return 0;
+}
+
+static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
+{
+	GByteArray *array;
+	guchar buf[BUFSIZ];
+	gint n_read;
+	gchar *str;
+
+	cm_return_val_if_fail(fp != NULL, NULL);
+
+	array = g_byte_array_new();
+
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(fp))
+			break;
+		g_byte_array_append(array, buf, n_read);
+	}
+
+	if (claws_ferror(fp)) {
+		FILE_OP_ERROR("file stream", "claws_fread");
+		g_byte_array_free(array, TRUE);
+		return NULL;
+	}
+
+	buf[0] = '\0';
+	g_byte_array_append(array, buf, 1);
+	str = (gchar *)array->data;
+	g_byte_array_free(array, FALSE);
+
+	if (recode && !g_utf8_validate(str, -1, NULL)) {
+		const gchar *src_codeset, *dest_codeset;
+		gchar *tmp = NULL;
+		src_codeset = conv_get_locale_charset_str();
+		dest_codeset = CS_UTF_8;
+		tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
+		g_free(str);
+		str = tmp;
+	}
+
+	return str;
+}
+
+static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
+{
+	FILE *fp;
+	gchar *str;
+	GStatBuf s;
+#ifndef G_OS_WIN32
+	gint fd, err;
+	struct timeval timeout = {1, 0};
+	fd_set fds;
+	int fflags = 0;
+#endif
+
+	cm_return_val_if_fail(file != NULL, NULL);
+
+	if (g_stat(file, &s) != 0) {
+		FILE_OP_ERROR(file, "stat");
+		return NULL;
+	}
+	if (S_ISDIR(s.st_mode)) {
+		g_warning("%s: is a directory", file);
+		return NULL;
+	}
+
+#ifdef G_OS_WIN32
+	fp = claws_fopen (file, "rb");
+	if (fp == NULL) {
+		FILE_OP_ERROR(file, "open");
+		return NULL;
+	}
+#else	  
+	/* test whether the file is readable without blocking */
+	fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
+	if (fd == -1) {
+		FILE_OP_ERROR(file, "open");
+		return NULL;
+	}
+
+	FD_ZERO(&fds);
+	FD_SET(fd, &fds);
+
+	/* allow for one second */
+	err = select(fd+1, &fds, NULL, NULL, &timeout);
+	if (err <= 0 || !FD_ISSET(fd, &fds)) {
+		if (err < 0) {
+			FILE_OP_ERROR(file, "select");
+		} else {
+			g_warning("%s: doesn't seem readable", file);
+		}
+		close(fd);
+		return NULL;
+	}
+	
+	/* Now clear O_NONBLOCK */
+	if ((fflags = fcntl(fd, F_GETFL)) < 0) {
+		FILE_OP_ERROR(file, "fcntl (F_GETFL)");
+		close(fd);
+		return NULL;
+	}
+	if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
+		FILE_OP_ERROR(file, "fcntl (F_SETFL)");
+		close(fd);
+		return NULL;
+	}
+	
+	/* get the FILE pointer */
+	fp = claws_fdopen(fd, "rb");
+
+	if (fp == NULL) {
+		FILE_OP_ERROR(file, "claws_fdopen");
+		close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
+		return NULL;
+	}
+#endif
+
+	str = file_read_stream_to_str_full(fp, recode);
+
+	claws_fclose(fp);
+
+	return str;
+}
+
+gchar *file_read_to_str(const gchar *file)
+{
+	return file_read_to_str_full(file, TRUE);
+}
+gchar *file_read_stream_to_str(FILE *fp)
+{
+	return file_read_stream_to_str_full(fp, TRUE);
+}
+
+gchar *file_read_to_str_no_recode(const gchar *file)
+{
+	return file_read_to_str_full(file, FALSE);
+}
+gchar *file_read_stream_to_str_no_recode(FILE *fp)
+{
+	return file_read_stream_to_str_full(fp, FALSE);
+}
+
+gint rename_force(const gchar *oldpath, const gchar *newpath)
+{
+#ifndef G_OS_UNIX
+	if (!is_file_entry_exist(oldpath)) {
+		errno = ENOENT;
+		return -1;
+	}
+	if (is_file_exist(newpath)) {
+		if (claws_unlink(newpath) < 0)
+			FILE_OP_ERROR(newpath, "unlink");
+	}
+#endif
+	return g_rename(oldpath, newpath);
+}
+
+gint copy_dir(const gchar *src, const gchar *dst)
+{
+	GDir *dir;
+	const gchar *name;
+
+	if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
+		g_warning("failed to open directory: %s", src);
+		return -1;
+	}
+
+	if (make_dir(dst) < 0)
+		return -1;
+
+	while ((name = g_dir_read_name(dir)) != NULL) {
+		gchar *old_file, *new_file;
+		old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
+		new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
+		debug_print("copying: %s -> %s\n", old_file, new_file);
+		if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
+			gint r = copy_file(old_file, new_file, TRUE);
+			if (r < 0) {
+				g_dir_close(dir);
+				return r;
+			}
+                }
+#ifndef G_OS_WIN32
+                /* Windows has no symlinks.  Or well, Vista seems to
+                   have something like this but the semantics might be
+                   different.  Thus we don't use it under Windows. */
+		 else if (g_file_test(old_file, G_FILE_TEST_IS_SYMLINK)) {
+			GError *error = NULL;
+			gint r = 0;
+			gchar *target = g_file_read_link(old_file, &error);
+			if (target)
+				r = symlink(target, new_file);
+			g_free(target);
+			if (r < 0) {
+				g_dir_close(dir);
+				return r;
+			}
+                 }
+#endif /*G_OS_WIN32*/
+	        else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
+			gint r = copy_dir(old_file, new_file);
+			if (r < 0) {
+				g_dir_close(dir);
+				return r;
+			}
+		}
+	}
+	g_dir_close(dir);
+	return 0;
+}
+
+gint change_file_mode_rw(FILE *fp, const gchar *file)
+{
+#if HAVE_FCHMOD
+	return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
+#else
+	return g_chmod(file, S_IRUSR|S_IWUSR);
+#endif
+}
+
+FILE *my_tmpfile(void)
+{
+	const gchar suffix[] = ".XXXXXX";
+	const gchar *tmpdir;
+	guint tmplen;
+	const gchar *progname;
+	guint proglen;
+	gchar *fname;
+	gint fd;
+	FILE *fp;
+#ifndef G_OS_WIN32
+	gchar buf[2]="\0";
+#endif
+
+	tmpdir = get_tmp_dir();
+	tmplen = strlen(tmpdir);
+	progname = g_get_prgname();
+	if (progname == NULL)
+		progname = "claws-mail";
+	proglen = strlen(progname);
+	Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
+		return tmpfile());
+
+	memcpy(fname, tmpdir, tmplen);
+	fname[tmplen] = G_DIR_SEPARATOR;
+	memcpy(fname + tmplen + 1, progname, proglen);
+	memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
+
+	fd = g_mkstemp(fname);
+	if (fd < 0)
+		return tmpfile();
+
+#ifndef G_OS_WIN32
+	claws_unlink(fname);
+	
+	/* verify that we can write in the file after unlinking */
+	if (write(fd, buf, 1) < 0) {
+		close(fd);
+		return tmpfile();
+	}
+	
+#endif
+
+	fp = claws_fdopen(fd, "w+b");
+	if (!fp)
+		close(fd);
+	else {
+		rewind(fp);
+		return fp;
+	}
+
+	return tmpfile();
+}
+
+FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
+{
+	int fd;
+	*filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
+	fd = g_mkstemp(*filename);
+	if (fd < 0)
+		return NULL;
+	return claws_fdopen(fd, "w+");
+}
+
+FILE *str_open_as_stream(const gchar *str)
+{
+	FILE *fp;
+	size_t len;
+
+	cm_return_val_if_fail(str != NULL, NULL);
+
+	fp = my_tmpfile();
+	if (!fp) {
+		FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
+		return NULL;
+	}
+
+	len = strlen(str);
+	if (len == 0) return fp;
+
+	if (claws_fwrite(str, 1, len, fp) != len) {
+		FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");
+		claws_fclose(fp);
+		return NULL;
+	}
+
+	rewind(fp);
+	return fp;
+}
+
+#endif
diff --git a/src/common/claws_io.h b/src/common/file-utils.h
similarity index 50%
rename from src/common/claws_io.h
rename to src/common/file-utils.h
index 35edae3..6c5ad17 100644
--- a/src/common/claws_io.h
+++ b/src/common/file-utils.h
@@ -25,6 +25,7 @@
 #endif
 
 #include <stdio.h>
+#include <glib.h>
 
 #if HAVE_FGETS_UNLOCKED
 #define claws_fgets fgets_unlocked
@@ -36,10 +37,12 @@
 #define claws_feof feof_unlocked
 #define claws_ferror ferror_unlocked
 
-FILE *claws_fopen(const char *file, const char *mode);
-FILE *claws_fdopen(int fd, const char *mode);
-int claws_fclose(FILE *fp);
-int claws_safe_fclose(FILE *fp);
+FILE *claws_fopen		(const char *file,
+				 const char *mode);
+FILE *claws_fdopen		(int fd,
+				 const char *mode);
+int claws_fclose		(FILE *fp);
+int claws_safe_fclose		(FILE *fp);
 
 #else
 
@@ -57,6 +60,48 @@ int claws_safe_fclose(FILE *fp);
 #define claws_safe_fclose safe_fclose
 #endif
 
-int safe_fclose(FILE *fp);
+int safe_fclose			(FILE *fp);
+int claws_unlink		(const char	*filename);
+
+gint file_strip_crs		(const gchar	*file);
+gint append_file		(const gchar	*src,
+				 const gchar	*dest,
+				 gboolean	 keep_backup);
+gint copy_file			(const gchar	*src,
+				 const gchar	*dest,
+				 gboolean	 keep_backup);
+gint move_file			(const gchar	*src,
+				 const gchar	*dest,
+				 gboolean	 overwrite);
+gint copy_file_part_to_fp	(FILE		*fp,
+				 off_t		 offset,
+				 size_t		 length,
+				 FILE		*dest_fp);
+gint copy_file_part		(FILE		*fp,
+				 off_t		 offset,
+				 size_t		 length,
+				 const gchar	*dest);
+gint canonicalize_file		(const gchar	*src,
+				 const gchar	*dest);
+gint canonicalize_file_replace	(const gchar	*file);
+gint str_write_to_file		(const gchar	*str,
+				 const gchar	*file);
+gchar *file_read_to_str		(const gchar	*file);
+gchar *file_read_to_str_no_recode(const gchar	*file);
+gchar *file_read_stream_to_str	(FILE		*fp);
+gchar *file_read_stream_to_str_no_recode(FILE	*fp);
+
+gint rename_force		(const gchar	*oldpath,
+				 const gchar	*newpath);
+gint copy_dir			(const gchar	*src,
+				 const gchar	*dest);
+gint change_file_mode_rw	(FILE		*fp,
+				 const gchar	*file);
+FILE *my_tmpfile		(void);
+FILE *get_tmpfile_in_dir	(const gchar 	*dir,
+				 gchar	       **filename);
+FILE *str_open_as_stream	(const gchar	*str);
+gint str_write_to_file		(const gchar	*str,
+				 const gchar	*file);
 
 #endif
diff --git a/src/common/log.c b/src/common/log.c
index 15a852b..83b023e 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -34,7 +34,7 @@
 #include "utils.h"
 #include "log.h"
 #include "hooks.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define FWRITE(_b,_s,_n,_f)	if (claws_fwrite(_b,_s,_n,_f) != _n) { \
 					g_message("log claws_fwrite failed!\n"); \
diff --git a/src/common/plugin.c b/src/common/plugin.c
index 963b16d..39679e8 100644
--- a/src/common/plugin.c
+++ b/src/common/plugin.c
@@ -41,7 +41,7 @@
 #include "prefs.h"
 #include "claws.h"
 #include "timing.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef G_OS_WIN32
 #define PLUGINS_BLOCK_PREFIX "PluginsWin32_"
diff --git a/src/common/prefs.c b/src/common/prefs.c
index 971193e..7489507 100644
--- a/src/common/prefs.c
+++ b/src/common/prefs.c
@@ -30,7 +30,7 @@
 
 #include "prefs.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static gboolean prefs_is_readonly	(const gchar 	*path);
 
diff --git a/src/common/ssl_certificate.c b/src/common/ssl_certificate.c
index 8afe4da..6db05f5 100644
--- a/src/common/ssl_certificate.c
+++ b/src/common/ssl_certificate.c
@@ -46,7 +46,7 @@
 #include "socket.h"
 #include "hooks.h"
 #include "defs.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GHashTable *warned_expired = NULL;
 
diff --git a/src/common/tags.c b/src/common/tags.c
index 4086323..7c8bf4d 100644
--- a/src/common/tags.c
+++ b/src/common/tags.c
@@ -40,7 +40,7 @@
 #include "defs.h"
 #include "utils.h"
 #include "tags.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GHashTable *tags_table = NULL;
 static GHashTable *tags_reverse_table = NULL;
diff --git a/src/common/template.c b/src/common/template.c
index 8ae5fde..a7f9ea1 100644
--- a/src/common/template.c
+++ b/src/common/template.c
@@ -29,7 +29,7 @@
 #include "utils.h"
 #include "template.h"
 #include "codeconv.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GSList *template_list;
 
diff --git a/src/common/utils.c b/src/common/utils.c
index aaae7b2..f288888 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -86,7 +86,7 @@
 #include "codeconv.h"
 #include "tlds.h"
 #include "timing.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define BUFFSIZE	8192
 
@@ -314,50 +314,6 @@ gchar *strcrchomp(gchar *str)
 	return str;
 }
 
-gint file_strip_crs(const gchar *file)
-{
-	FILE *fp = NULL, *outfp = NULL;
-	gchar buf[4096];
-	gchar *out = get_tmp_file();
-	if (file == NULL)
-		goto freeout;
-
-	fp = claws_fopen(file, "rb");
-	if (!fp)
-		goto freeout;
-
-	outfp = claws_fopen(out, "wb");
-	if (!outfp) {
-		claws_fclose(fp);
-		goto freeout;
-	}
-
-	while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
-		strcrchomp(buf);
-		if (claws_fputs(buf, outfp) == EOF) {
-			claws_fclose(fp);
-			claws_fclose(outfp);
-			goto unlinkout;
-		}
-	}
-
-	claws_fclose(fp);
-	if (claws_safe_fclose(outfp) == EOF) {
-		goto unlinkout;
-	}
-	
-	if (move_file(out, file, TRUE) < 0)
-		goto unlinkout;
-	
-	g_free(out);
-	return 0;
-unlinkout:
-	claws_unlink(out);
-freeout:
-	g_free(out);
-	return -1;
-}
-
 /* Similar to `strstr' but this function ignores the case of both strings.  */
 gchar *strcasestr(const gchar *haystack, const gchar *needle)
 {
@@ -2392,249 +2348,6 @@ gint remove_dir_recursive(const gchar *dir)
 	return 0;
 }
 
-gint rename_force(const gchar *oldpath, const gchar *newpath)
-{
-#ifndef G_OS_UNIX
-	if (!is_file_entry_exist(oldpath)) {
-		errno = ENOENT;
-		return -1;
-	}
-	if (is_file_exist(newpath)) {
-		if (claws_unlink(newpath) < 0)
-			FILE_OP_ERROR(newpath, "unlink");
-	}
-#endif
-	return g_rename(oldpath, newpath);
-}
-
-/*
- * Append src file body to the tail of dest file.
- * Now keep_backup has no effects.
- */
-gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
-{
-	FILE *src_fp, *dest_fp;
-	gint n_read;
-	gchar buf[BUFSIZ];
-
-	gboolean err = FALSE;
-
-	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
-		FILE_OP_ERROR(src, "claws_fopen");
-		return -1;
-	}
-
-	if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
-		FILE_OP_ERROR(dest, "claws_fopen");
-		claws_fclose(src_fp);
-		return -1;
-	}
-
-	if (change_file_mode_rw(dest_fp, dest) < 0) {
-		FILE_OP_ERROR(dest, "chmod");
-		g_warning("can't change file mode: %s", dest);
-	}
-
-	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
-		if (n_read < sizeof(buf) && claws_ferror(src_fp))
-			break;
-		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
-			g_warning("writing to %s failed.", dest);
-			claws_fclose(dest_fp);
-			claws_fclose(src_fp);
-			claws_unlink(dest);
-			return -1;
-		}
-	}
-
-	if (claws_ferror(src_fp)) {
-		FILE_OP_ERROR(src, "claws_fread");
-		err = TRUE;
-	}
-	claws_fclose(src_fp);
-	if (claws_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "claws_fclose");
-		err = TRUE;
-	}
-
-	if (err) {
-		claws_unlink(dest);
-		return -1;
-	}
-
-	return 0;
-}
-
-gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
-{
-	FILE *src_fp, *dest_fp;
-	gint n_read;
-	gchar buf[BUFSIZ];
-	gchar *dest_bak = NULL;
-	gboolean err = FALSE;
-
-	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
-		FILE_OP_ERROR(src, "claws_fopen");
-		return -1;
-	}
-	if (is_file_exist(dest)) {
-		dest_bak = g_strconcat(dest, ".bak", NULL);
-		if (rename_force(dest, dest_bak) < 0) {
-			FILE_OP_ERROR(dest, "rename");
-			claws_fclose(src_fp);
-			g_free(dest_bak);
-			return -1;
-		}
-	}
-
-	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "claws_fopen");
-		claws_fclose(src_fp);
-		if (dest_bak) {
-			if (rename_force(dest_bak, dest) < 0)
-				FILE_OP_ERROR(dest_bak, "rename");
-			g_free(dest_bak);
-		}
-		return -1;
-	}
-
-	if (change_file_mode_rw(dest_fp, dest) < 0) {
-		FILE_OP_ERROR(dest, "chmod");
-		g_warning("can't change file mode: %s", dest);
-	}
-
-	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
-		if (n_read < sizeof(buf) && claws_ferror(src_fp))
-			break;
-		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
-			g_warning("writing to %s failed.", dest);
-			claws_fclose(dest_fp);
-			claws_fclose(src_fp);
-			claws_unlink(dest);
-			if (dest_bak) {
-				if (rename_force(dest_bak, dest) < 0)
-					FILE_OP_ERROR(dest_bak, "rename");
-				g_free(dest_bak);
-			}
-			return -1;
-		}
-	}
-
-	if (claws_ferror(src_fp)) {
-		FILE_OP_ERROR(src, "claws_fread");
-		err = TRUE;
-	}
-	claws_fclose(src_fp);
-	if (claws_safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "claws_fclose");
-		err = TRUE;
-	}
-
-	if (err) {
-		claws_unlink(dest);
-		if (dest_bak) {
-			if (rename_force(dest_bak, dest) < 0)
-				FILE_OP_ERROR(dest_bak, "rename");
-			g_free(dest_bak);
-		}
-		return -1;
-	}
-
-	if (keep_backup == FALSE && dest_bak)
-		claws_unlink(dest_bak);
-
-	g_free(dest_bak);
-
-	return 0;
-}
-
-gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
-{
-	if (overwrite == FALSE && is_file_exist(dest)) {
-		g_warning("move_file(): file %s already exists.", dest);
-		return -1;
-	}
-
-	if (rename_force(src, dest) == 0) return 0;
-
-	if (EXDEV != errno) {
-		FILE_OP_ERROR(src, "rename");
-		return -1;
-	}
-
-	if (copy_file(src, dest, FALSE) < 0) return -1;
-
-	claws_unlink(src);
-
-	return 0;
-}
-
-gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
-{
-	gint n_read;
-	gint bytes_left, to_read;
-	gchar buf[BUFSIZ];
-
-	if (fseek(fp, offset, SEEK_SET) < 0) {
-		perror("fseek");
-		return -1;
-	}
-
-	bytes_left = length;
-	to_read = MIN(bytes_left, sizeof(buf));
-
-	while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
-		if (n_read < to_read && claws_ferror(fp))
-			break;
-		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
-			return -1;
-		}
-		bytes_left -= n_read;
-		if (bytes_left == 0)
-			break;
-		to_read = MIN(bytes_left, sizeof(buf));
-	}
-
-	if (claws_ferror(fp)) {
-		perror("claws_fread");
-		return -1;
-	}
-
-	return 0;
-}
-
-gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
-{
-	FILE *dest_fp;
-	gboolean err = FALSE;
-
-	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "claws_fopen");
-		return -1;
-	}
-
-	if (change_file_mode_rw(dest_fp, dest) < 0) {
-		FILE_OP_ERROR(dest, "chmod");
-		g_warning("can't change file mode: %s", dest);
-	}
-
-	if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
-		err = TRUE;
-
-	if (claws_safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "claws_fclose");
-		err = TRUE;
-	}
-
-	if (err) {
-		g_warning("writing to %s failed.", dest);
-		claws_unlink(dest);
-		return -1;
-	}
-
-	return 0;
-}
-
 /* convert line endings into CRLF. If the last line doesn't end with
  * linebreak, add it.
  */
@@ -2671,109 +2384,6 @@ gchar *canonicalize_str(const gchar *str)
 	return out;
 }
 
-gint canonicalize_file(const gchar *src, const gchar *dest)
-{
-	FILE *src_fp, *dest_fp;
-	gchar buf[BUFFSIZE];
-	gint len;
-	gboolean err = FALSE;
-	gboolean last_linebreak = FALSE;
-
-	if (src == NULL || dest == NULL)
-		return -1;
-
-	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
-		FILE_OP_ERROR(src, "claws_fopen");
-		return -1;
-	}
-
-	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "claws_fopen");
-		claws_fclose(src_fp);
-		return -1;
-	}
-
-	if (change_file_mode_rw(dest_fp, dest) < 0) {
-		FILE_OP_ERROR(dest, "chmod");
-		g_warning("can't change file mode: %s", dest);
-	}
-
-	while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
-		gint r = 0;
-
-		len = strlen(buf);
-		if (len == 0) break;
-		last_linebreak = FALSE;
-
-		if (buf[len - 1] != '\n') {
-			last_linebreak = TRUE;
-			r = claws_fputs(buf, dest_fp);
-		} else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
-			r = claws_fputs(buf, dest_fp);
-		} else {
-			if (len > 1) {
-				r = claws_fwrite(buf, 1, len - 1, dest_fp);
-				if (r != (len -1))
-					r = EOF;
-			}
-			if (r != EOF)
-				r = claws_fputs("\r\n", dest_fp);
-		}
-
-		if (r == EOF) {
-			g_warning("writing to %s failed.", dest);
-			claws_fclose(dest_fp);
-			claws_fclose(src_fp);
-			claws_unlink(dest);
-			return -1;
-		}
-	}
-
-	if (last_linebreak == TRUE) {
-		if (claws_fputs("\r\n", dest_fp) == EOF)
-			err = TRUE;
-	}
-
-	if (claws_ferror(src_fp)) {
-		FILE_OP_ERROR(src, "claws_fgets");
-		err = TRUE;
-	}
-	claws_fclose(src_fp);
-	if (claws_safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "claws_fclose");
-		err = TRUE;
-	}
-
-	if (err) {
-		claws_unlink(dest);
-		return -1;
-	}
-
-	return 0;
-}
-
-gint canonicalize_file_replace(const gchar *file)
-{
-	gchar *tmp_file;
-
-	tmp_file = get_tmp_file();
-
-	if (canonicalize_file(file, tmp_file) < 0) {
-		g_free(tmp_file);
-		return -1;
-	}
-
-	if (move_file(tmp_file, file, TRUE) < 0) {
-		g_warning("can't replace file: %s", file);
-		claws_unlink(tmp_file);
-		g_free(tmp_file);
-		return -1;
-	}
-
-	g_free(tmp_file);
-	return 0;
-}
-
 gchar *normalize_newlines(const gchar *str)
 {
 	const gchar *p;
@@ -2874,280 +2484,6 @@ gchar *generate_mime_boundary(const gchar *prefix)
 			       buf_uniq);
 }
 
-gint change_file_mode_rw(FILE *fp, const gchar *file)
-{
-#if HAVE_FCHMOD
-	return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
-#else
-	return g_chmod(file, S_IRUSR|S_IWUSR);
-#endif
-}
-
-FILE *my_tmpfile(void)
-{
-	const gchar suffix[] = ".XXXXXX";
-	const gchar *tmpdir;
-	guint tmplen;
-	const gchar *progname;
-	guint proglen;
-	gchar *fname;
-	gint fd;
-	FILE *fp;
-#ifndef G_OS_WIN32
-	gchar buf[2]="\0";
-#endif
-
-	tmpdir = get_tmp_dir();
-	tmplen = strlen(tmpdir);
-	progname = g_get_prgname();
-	if (progname == NULL)
-		progname = "claws-mail";
-	proglen = strlen(progname);
-	Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
-		return tmpfile());
-
-	memcpy(fname, tmpdir, tmplen);
-	fname[tmplen] = G_DIR_SEPARATOR;
-	memcpy(fname + tmplen + 1, progname, proglen);
-	memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
-
-	fd = g_mkstemp(fname);
-	if (fd < 0)
-		return tmpfile();
-
-#ifndef G_OS_WIN32
-	claws_unlink(fname);
-	
-	/* verify that we can write in the file after unlinking */
-	if (write(fd, buf, 1) < 0) {
-		close(fd);
-		return tmpfile();
-	}
-	
-#endif
-
-	fp = claws_fdopen(fd, "w+b");
-	if (!fp)
-		close(fd);
-	else {
-		rewind(fp);
-		return fp;
-	}
-
-	return tmpfile();
-}
-
-FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
-{
-	int fd;
-	*filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
-	fd = g_mkstemp(*filename);
-	if (fd < 0)
-		return NULL;
-	return claws_fdopen(fd, "w+");
-}
-
-FILE *str_open_as_stream(const gchar *str)
-{
-	FILE *fp;
-	size_t len;
-
-	cm_return_val_if_fail(str != NULL, NULL);
-
-	fp = my_tmpfile();
-	if (!fp) {
-		FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
-		return NULL;
-	}
-
-	len = strlen(str);
-	if (len == 0) return fp;
-
-	if (claws_fwrite(str, 1, len, fp) != len) {
-		FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");
-		claws_fclose(fp);
-		return NULL;
-	}
-
-	rewind(fp);
-	return fp;
-}
-
-gint str_write_to_file(const gchar *str, const gchar *file)
-{
-	FILE *fp;
-	size_t len;
-
-	cm_return_val_if_fail(str != NULL, -1);
-	cm_return_val_if_fail(file != NULL, -1);
-
-	if ((fp = claws_fopen(file, "wb")) == NULL) {
-		FILE_OP_ERROR(file, "claws_fopen");
-		return -1;
-	}
-
-	len = strlen(str);
-	if (len == 0) {
-		claws_fclose(fp);
-		return 0;
-	}
-
-	if (claws_fwrite(str, 1, len, fp) != len) {
-		FILE_OP_ERROR(file, "claws_fwrite");
-		claws_fclose(fp);
-		claws_unlink(file);
-		return -1;
-	}
-
-	if (claws_safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(file, "claws_fclose");
-		claws_unlink(file);
-		return -1;
-	}
-
-	return 0;
-}
-
-static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
-{
-	GByteArray *array;
-	guchar buf[BUFSIZ];
-	gint n_read;
-	gchar *str;
-
-	cm_return_val_if_fail(fp != NULL, NULL);
-
-	array = g_byte_array_new();
-
-	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
-		if (n_read < sizeof(buf) && claws_ferror(fp))
-			break;
-		g_byte_array_append(array, buf, n_read);
-	}
-
-	if (claws_ferror(fp)) {
-		FILE_OP_ERROR("file stream", "claws_fread");
-		g_byte_array_free(array, TRUE);
-		return NULL;
-	}
-
-	buf[0] = '\0';
-	g_byte_array_append(array, buf, 1);
-	str = (gchar *)array->data;
-	g_byte_array_free(array, FALSE);
-
-	if (recode && !g_utf8_validate(str, -1, NULL)) {
-		const gchar *src_codeset, *dest_codeset;
-		gchar *tmp = NULL;
-		src_codeset = conv_get_locale_charset_str();
-		dest_codeset = CS_UTF_8;
-		tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
-		g_free(str);
-		str = tmp;
-	}
-
-	return str;
-}
-
-static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
-{
-	FILE *fp;
-	gchar *str;
-	GStatBuf s;
-#ifndef G_OS_WIN32
-	gint fd, err;
-	struct timeval timeout = {1, 0};
-	fd_set fds;
-	int fflags = 0;
-#endif
-
-	cm_return_val_if_fail(file != NULL, NULL);
-
-	if (g_stat(file, &s) != 0) {
-		FILE_OP_ERROR(file, "stat");
-		return NULL;
-	}
-	if (S_ISDIR(s.st_mode)) {
-		g_warning("%s: is a directory", file);
-		return NULL;
-	}
-
-#ifdef G_OS_WIN32
-	fp = claws_fopen (file, "rb");
-	if (fp == NULL) {
-		FILE_OP_ERROR(file, "open");
-		return NULL;
-	}
-#else	  
-	/* test whether the file is readable without blocking */
-	fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
-	if (fd == -1) {
-		FILE_OP_ERROR(file, "open");
-		return NULL;
-	}
-
-	FD_ZERO(&fds);
-	FD_SET(fd, &fds);
-
-	/* allow for one second */
-	err = select(fd+1, &fds, NULL, NULL, &timeout);
-	if (err <= 0 || !FD_ISSET(fd, &fds)) {
-		if (err < 0) {
-			FILE_OP_ERROR(file, "select");
-		} else {
-			g_warning("%s: doesn't seem readable", file);
-		}
-		close(fd);
-		return NULL;
-	}
-	
-	/* Now clear O_NONBLOCK */
-	if ((fflags = fcntl(fd, F_GETFL)) < 0) {
-		FILE_OP_ERROR(file, "fcntl (F_GETFL)");
-		close(fd);
-		return NULL;
-	}
-	if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
-		FILE_OP_ERROR(file, "fcntl (F_SETFL)");
-		close(fd);
-		return NULL;
-	}
-	
-	/* get the FILE pointer */
-	fp = claws_fdopen(fd, "rb");
-
-	if (fp == NULL) {
-		FILE_OP_ERROR(file, "claws_fdopen");
-		close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
-		return NULL;
-	}
-#endif
-
-	str = file_read_stream_to_str_full(fp, recode);
-
-	claws_fclose(fp);
-
-	return str;
-}
-
-gchar *file_read_to_str(const gchar *file)
-{
-	return file_read_to_str_full(file, TRUE);
-}
-gchar *file_read_stream_to_str(FILE *fp)
-{
-	return file_read_stream_to_str_full(fp, TRUE);
-}
-
-gchar *file_read_to_str_no_recode(const gchar *file)
-{
-	return file_read_to_str_full(file, FALSE);
-}
-gchar *file_read_stream_to_str_no_recode(FILE *fp)
-{
-	return file_read_stream_to_str_full(fp, FALSE);
-}
-
 char *fgets_crlf(char *buf, int size, FILE *stream)
 {
 	gboolean is_cr = FALSE;
@@ -4592,60 +3928,6 @@ void mailcap_update_default(const gchar *type, const gchar *command)
 	g_free(outpath);
 }
 
-gint copy_dir(const gchar *src, const gchar *dst)
-{
-	GDir *dir;
-	const gchar *name;
-
-	if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
-		g_warning("failed to open directory: %s", src);
-		return -1;
-	}
-
-	if (make_dir(dst) < 0)
-		return -1;
-
-	while ((name = g_dir_read_name(dir)) != NULL) {
-		gchar *old_file, *new_file;
-		old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
-		new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
-		debug_print("copying: %s -> %s\n", old_file, new_file);
-		if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
-			gint r = copy_file(old_file, new_file, TRUE);
-			if (r < 0) {
-				g_dir_close(dir);
-				return r;
-			}
-                }
-#ifndef G_OS_WIN32
-                /* Windows has no symlinks.  Or well, Vista seems to
-                   have something like this but the semantics might be
-                   different.  Thus we don't use it under Windows. */
-		 else if (g_file_test(old_file, G_FILE_TEST_IS_SYMLINK)) {
-			GError *error = NULL;
-			gint r = 0;
-			gchar *target = g_file_read_link(old_file, &error);
-			if (target)
-				r = symlink(target, new_file);
-			g_free(target);
-			if (r < 0) {
-				g_dir_close(dir);
-				return r;
-			}
-                 }
-#endif /*G_OS_WIN32*/
-	        else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
-			gint r = copy_dir(old_file, new_file);
-			if (r < 0) {
-				g_dir_close(dir);
-				return r;
-			}
-		}
-	}
-	g_dir_close(dir);
-	return 0;
-}
-
 /* crude test to see if a file is an email. */
 gboolean file_is_email (const gchar *filename)
 {
@@ -5046,49 +4328,10 @@ size_t fast_strftime(gchar *buf, gint buflen, const gchar *format, struct tm *lt
 	return total_done;
 }
 
-gboolean prefs_common_get_use_shred(void);
-
-
 #ifdef G_OS_WIN32
 #define WEXITSTATUS(x) (x)
 #endif
 
-int claws_unlink(const gchar *filename) 
-{
-	GStatBuf s;
-	static int found_shred = -1;
-	static const gchar *args[4];
-
-	if (filename == NULL)
-		return 0;
-
-	if (prefs_common_get_use_shred()) {
-		if (found_shred == -1) {
-			/* init */
-			args[0] = g_find_program_in_path("shred");
-			debug_print("found shred: %s\n", args[0]);
-			found_shred = (args[0] != NULL) ? 1:0;
-			args[1] = "-f";
-			args[3] = NULL;
-		}
-		if (found_shred == 1) {
-			if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
-				if (s.st_nlink == 1) {
-					gint status=0;
-					args[2] = filename;
-					g_spawn_sync(NULL, (gchar **)args, NULL, 0,
-					 NULL, NULL, NULL, NULL, &status, NULL);
-					debug_print("%s %s exited with status %d\n",
-						args[0], filename, WEXITSTATUS(status));
-					if (truncate(filename, 0) < 0)
-						g_warning("couln't truncate: %s", filename);
-				}
-			}
-		}
-	}
-	return g_unlink(filename);
-}
-
 GMutex *cm_mutex_new(void) {
 #if GLIB_CHECK_VERSION(2,32,0)
 	GMutex *m = g_new0(GMutex, 1);
diff --git a/src/common/utils.h b/src/common/utils.h
index 96c4dc2..dfb327f 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -281,7 +281,6 @@ gchar *strretchomp	(gchar		*str);
 gchar *strtailchomp	(gchar		*str,
 			 gchar		 tail_char);
 gchar *strcrchomp	(gchar		*str);
-gint file_strip_crs	(const gchar 	*file);
 gchar *strcasestr	(const gchar	*haystack,
 			 const gchar	*needle);
 gchar *strncasestr	(const gchar	*haystack,
@@ -427,50 +426,11 @@ gint remove_numbered_files_not_in_list(const gchar *dir,
 				       GSList *numberlist);
 gint remove_all_numbered_files	(const gchar	*dir);
 gint remove_dir_recursive	(const gchar	*dir);
-gint append_file		(const gchar	*src,
-				 const gchar	*dest,
-				 gboolean	 keep_backup);
-gint rename_force		(const gchar	*oldpath,
-				 const gchar	*newpath);
-gint copy_file			(const gchar	*src,
-				 const gchar	*dest,
-				 gboolean	 keep_backup);
-gint move_file			(const gchar	*src,
-				 const gchar	*dest,
-				 gboolean	 overwrite);
-gint copy_dir			(const gchar	*src,
-				 const gchar	*dest);
-gint copy_file_part_to_fp	(FILE		*fp,
-				 off_t		 offset,
-				 size_t		 length,
-				 FILE		*dest_fp);
-gint copy_file_part		(FILE		*fp,
-				 off_t		 offset,
-				 size_t		 length,
-				 const gchar	*dest);
-
 gchar *canonicalize_str		(const gchar	*str);
-gint canonicalize_file		(const gchar	*src,
-				 const gchar	*dest);
-gint canonicalize_file_replace	(const gchar	*file);
-
 gchar *normalize_newlines	(const gchar	*str);
 
 gchar *get_outgoing_rfc2822_str	(FILE		*fp);
 
-gint change_file_mode_rw	(FILE		*fp,
-				 const gchar	*file);
-FILE *my_tmpfile		(void);
-FILE *get_tmpfile_in_dir	(const gchar 	*dir,
-				 gchar	       **filename);
-FILE *str_open_as_stream	(const gchar	*str);
-gint str_write_to_file		(const gchar	*str,
-				 const gchar	*file);
-gchar *file_read_to_str		(const gchar	*file);
-gchar *file_read_stream_to_str	(FILE		*fp);
-gchar *file_read_to_str_no_recode(const gchar *file);
-gchar *file_read_stream_to_str_no_recode(FILE *fp);
-
 char *fgets_crlf(char *buf, int size, FILE *stream);
 
 /* process execution */
@@ -563,8 +523,6 @@ gboolean file_is_email(const gchar *filename);
 gboolean sc_g_list_bigger(GList *list, gint max);
 gboolean sc_g_slist_bigger(GSList *list, gint max);
 
-int claws_unlink(const gchar *filename);
-
 GMutex *cm_mutex_new(void);
 void cm_mutex_free(GMutex *mutex);
 
diff --git a/src/common/xml.c b/src/common/xml.c
index 50cc9a5..874db1f 100644
--- a/src/common/xml.c
+++ b/src/common/xml.c
@@ -30,7 +30,7 @@
 #include "xml.h"
 #include "utils.h"
 #include "codeconv.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define SPARSE_MEMORY
 /* if this is defined all attr.names and tag.names are stored
diff --git a/src/common/xmlprops.c b/src/common/xmlprops.c
index 1fc1c28..0557bd2 100644
--- a/src/common/xmlprops.c
+++ b/src/common/xmlprops.c
@@ -45,7 +45,7 @@
 #include "mgutils.h"
 #include "xmlprops.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* Element tag names */
 #define XMLS_ELTAG_PROP_LIST     "property-list"
diff --git a/src/compose.c b/src/compose.c
index 3e348d6..72950ee 100644
--- a/src/compose.c
+++ b/src/compose.c
@@ -110,7 +110,7 @@
 #include "autofaces.h"
 #include "spell_entry.h"
 #include "headers.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef USE_LDAP
 #include "password.h"
diff --git a/src/editaddress.c b/src/editaddress.c
index 12095f4..8a448aa 100644
--- a/src/editaddress.c
+++ b/src/editaddress.c
@@ -44,6 +44,7 @@
 #include "prefs_common.h"
 #include "menu.h"
 #include "combobox.h"
+#include "file-utils.h"
 
 /* transient data */
 static struct _PersonEdit_dlg personeditdlg;
diff --git a/src/enriched.c b/src/enriched.c
index b8b0eb3..c29affa 100644
--- a/src/enriched.c
+++ b/src/enriched.c
@@ -29,7 +29,7 @@
 
 #include "enriched.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define ERTFBUFSIZE	8192
 
diff --git a/src/etpan/imap-thread.c b/src/etpan/imap-thread.c
index 4a11f4f..c6dd801 100644
--- a/src/etpan/imap-thread.c
+++ b/src/etpan/imap-thread.c
@@ -44,7 +44,7 @@
 #include "utils.h"
 #include "mainwindow.h"
 #include "proxy.h"
-#include "claws_io.h"
+#include "file-utils.h"
 #include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
diff --git a/src/exporthtml.c b/src/exporthtml.c
index 5dbec5b..86bfdfe 100644
--- a/src/exporthtml.c
+++ b/src/exporthtml.c
@@ -46,7 +46,7 @@
 #include "utils.h"
 #include "exporthtml.h"
 #include "xmlprops.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef MKDIR_TAKES_ONE_ARG
 #undef mkdir
diff --git a/src/exportldif.c b/src/exportldif.c
index 54e6bd0..c35d6de 100644
--- a/src/exportldif.c
+++ b/src/exportldif.c
@@ -38,7 +38,7 @@
 #include "exportldif.h"
 #include "xmlprops.h"
 #include "ldif.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 
 #ifdef MKDIR_TAKES_ONE_ARG
diff --git a/src/file_checker.c b/src/file_checker.c
index d458a83..ccf8d54 100644
--- a/src/file_checker.c
+++ b/src/file_checker.c
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <ctype.h>
 
+#include "file-utils.h"
 #include "utils.h"
 #include "alertpanel.h"
 #include "folder.h"
diff --git a/src/folder.c b/src/folder.c
index 97c5d01..abe8072 100644
--- a/src/folder.c
+++ b/src/folder.c
@@ -63,7 +63,7 @@
 #include "privacy.h"
 #include "prefs_common.h"
 #include "prefs_migration.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* Dependecies to be removed ?! */
 #include "prefs_account.h"
diff --git a/src/gtk/about.c b/src/gtk/about.c
index 261498f..e9ed132 100644
--- a/src/gtk/about.c
+++ b/src/gtk/about.c
@@ -43,7 +43,7 @@
 #include "menu.h"
 #include "textview.h"
 #include "main.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 extern SessionStats session_stats;
 static GtkTextBuffer *stats_text_buffer;
diff --git a/src/html.c b/src/html.c
index 190b467..b702404 100644
--- a/src/html.c
+++ b/src/html.c
@@ -30,7 +30,7 @@
 #include "codeconv.h"
 #include "utils.h"
 #include "entity.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define SC_HTMLBUFSIZE	8192
 #define HR_STR		"────────────────────────────────────────────────"
diff --git a/src/image_viewer.c b/src/image_viewer.c
index 6dd9b30..ef2655d 100644
--- a/src/image_viewer.c
+++ b/src/image_viewer.c
@@ -26,6 +26,7 @@
 #include <gtk/gtk.h>
 
 #include "procmime.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "mimeview.h"
 
diff --git a/src/imap.c b/src/imap.c
index 5871b79..1e98aac 100644
--- a/src/imap.c
+++ b/src/imap.c
@@ -71,7 +71,7 @@
 #include "tags.h"
 #include "main.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 typedef struct _IMAPFolder	IMAPFolder;
 typedef struct _IMAPSession	IMAPSession;
diff --git a/src/inc.c b/src/inc.c
index ed681fb..78fc240 100644
--- a/src/inc.c
+++ b/src/inc.c
@@ -45,6 +45,7 @@
 #include "pop.h"
 #include "recv.h"
 #include "mbox.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "statusbar.h"
diff --git a/src/jpilot.c b/src/jpilot.c
index a29b02b..7b37c37 100644
--- a/src/jpilot.c
+++ b/src/jpilot.c
@@ -56,7 +56,7 @@
 #include "codeconv.h"
 #include "adbookbase.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define JPILOT_DBHOME_DIR   ".jpilot"
 #define JPILOT_DBHOME_FILE  "AddressDB.pdb"
diff --git a/src/ldapquery.c b/src/ldapquery.c
index 16a20d7..2e96831 100644
--- a/src/ldapquery.c
+++ b/src/ldapquery.c
@@ -38,7 +38,7 @@
 #include "ldapctrl.h"
 #include "ldapserver.h"
 #include "mgutils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #include "addritem.h"
 #include "addrcache.h"
diff --git a/src/ldif.c b/src/ldif.c
index a632123..d2b19ef 100644
--- a/src/ldif.c
+++ b/src/ldif.c
@@ -32,7 +32,7 @@
 #include "addrcache.h"
 
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define	LDIF_SEP_TAG    ':'
 #define	LDIF_LANG_TAG   ';'
diff --git a/src/main.c b/src/main.c
index 631e366..65a3ec1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -132,7 +132,7 @@
 #include "advsearch.h"
 #include "avatars.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
diff --git a/src/matcher.c b/src/matcher.c
index c87c897..1186dba 100644
--- a/src/matcher.c
+++ b/src/matcher.c
@@ -49,7 +49,7 @@
 #include "tags.h"
 #include "folder_item_prefs.h"
 #include "procmsg.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /*!
  *\brief	Keyword lookup element
diff --git a/src/mbox.c b/src/mbox.c
index 2f7db0d..9047fbb 100644
--- a/src/mbox.c
+++ b/src/mbox.c
@@ -55,7 +55,7 @@
 #include "filtering.h"
 #include "alertpanel.h"
 #include "statusbar.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define MESSAGEBUFSIZE	8192
 
diff --git a/src/messageview.c b/src/messageview.c
index bbe8370..1725b68 100644
--- a/src/messageview.c
+++ b/src/messageview.c
@@ -67,7 +67,7 @@
 #include "statusbar.h"
 #include "folder_item_prefs.h"
 #include "avatars.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef USE_ALT_ADDRBOOK
 	#include "addressbook.h"
diff --git a/src/mh.c b/src/mh.c
index b0967a8..e7d0d6c 100644
--- a/src/mh.c
+++ b/src/mh.c
@@ -44,7 +44,7 @@
 #include "gtkutils.h"
 #include "timing.h"
 #include "msgcache.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* Define possible missing constants for Windows. */
 #ifdef G_OS_WIN32
diff --git a/src/mimeview.c b/src/mimeview.c
index 7db10d6..60b2793 100644
--- a/src/mimeview.c
+++ b/src/mimeview.c
@@ -55,7 +55,7 @@
 #include "timing.h"
 #include "manage_window.h"
 #include "privacy.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 typedef enum
 {
diff --git a/src/msgcache.c b/src/msgcache.c
index ca9d965..3910255 100644
--- a/src/msgcache.c
+++ b/src/msgcache.c
@@ -46,7 +46,7 @@
 #include "timing.h"
 #include "tags.h"
 #include "prefs_common.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #if G_BYTE_ORDER == G_BIG_ENDIAN
 #define bswap_32(x) \
diff --git a/src/mutt.c b/src/mutt.c
index 8ae8508..c6224c8 100644
--- a/src/mutt.c
+++ b/src/mutt.c
@@ -31,7 +31,7 @@
 #include "mutt.h"
 #include "addritem.h"
 #include "addrcache.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define MUTT_HOME_FILE  ".muttrc"
 #define MUTTBUFSIZE     2048
diff --git a/src/news.c b/src/news.c
index c963ac1..70eb4d6 100644
--- a/src/news.c
+++ b/src/news.c
@@ -62,7 +62,7 @@
 #  include "ssl.h"
 #endif
 #include "main.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define NNTP_PORT	119
 #ifdef USE_GNUTLS
diff --git a/src/partial_download.c b/src/partial_download.c
index 76b25fc..3edb635 100644
--- a/src/partial_download.c
+++ b/src/partial_download.c
@@ -63,7 +63,7 @@
 #include "folder.h"
 #include "procheader.h"
 #include "msgcache.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 int partial_msg_in_uidl_list(MsgInfo *msginfo)
 {
diff --git a/src/passwordstore.c b/src/passwordstore.c
index ba30349..4411711 100644
--- a/src/passwordstore.c
+++ b/src/passwordstore.c
@@ -37,7 +37,7 @@
 #include "prefs_common.h"
 #include "prefs_gtk.h"
 #include "prefs_migration.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GSList *_password_store;
 
diff --git a/src/pine.c b/src/pine.c
index d4b11d1..20944d0 100644
--- a/src/pine.c
+++ b/src/pine.c
@@ -30,7 +30,7 @@
 #include "pine.h"
 #include "addritem.h"
 #include "addrcache.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define PINE_HOME_FILE  ".addressbook"
 #define PINEBUFSIZE     2048
diff --git a/src/plugins/acpi_notifier/acpi_notifier.c b/src/plugins/acpi_notifier/acpi_notifier.c
index 13133e5..aaf1eae 100644
--- a/src/plugins/acpi_notifier/acpi_notifier.c
+++ b/src/plugins/acpi_notifier/acpi_notifier.c
@@ -48,7 +48,7 @@
 #include "folder_item_prefs.h"
 #include "gtk/gtkutils.h"
 #include "gtk/combobox.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define PREFS_BLOCK_NAME "AcpiNotifier"
 #define PLUGIN_NAME _("Acpi Notifier")
diff --git a/src/plugins/archive/archiver_gtk.c b/src/plugins/archive/archiver_gtk.c
index 27ff539..452be92 100644
--- a/src/plugins/archive/archiver_gtk.c
+++ b/src/plugins/archive/archiver_gtk.c
@@ -41,6 +41,7 @@
 #include "common/md5.h"
 #include "plugin.h"
 #include "mainwindow.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "prefs.h"
 #include "folder.h"
diff --git a/src/plugins/bogofilter/bogofilter.c b/src/plugins/bogofilter/bogofilter.c
index 590d392..ffd620a 100644
--- a/src/plugins/bogofilter/bogofilter.c
+++ b/src/plugins/bogofilter/bogofilter.c
@@ -52,7 +52,7 @@
 #include "prefs_common.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef HAVE_SYSEXITS_H
 #include <sysexits.h>
diff --git a/src/plugins/clamd/libclamd/clamd-plugin.c b/src/plugins/clamd/libclamd/clamd-plugin.c
index 831e035..95fa8f3 100644
--- a/src/plugins/clamd/libclamd/clamd-plugin.c
+++ b/src/plugins/clamd/libclamd/clamd-plugin.c
@@ -55,7 +55,7 @@
 #include "statusbar.h"
 #include "alertpanel.h"
 #include "clamd-plugin.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef UNIX_PATH_MAX
 #define UNIX_PATH_MAX 108
diff --git a/src/plugins/dillo/dillo_viewer.c b/src/plugins/dillo/dillo_viewer.c
index 1b4efab..68d813e 100644
--- a/src/plugins/dillo/dillo_viewer.c
+++ b/src/plugins/dillo/dillo_viewer.c
@@ -35,6 +35,7 @@
 #include "common/claws.h"
 #include "common/version.h"
 #include "plugin.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "mimeview.h"
 #include "addr_compl.h"
diff --git a/src/plugins/fancy/fancy_prefs.c b/src/plugins/fancy/fancy_prefs.c
index c5fe369..beca04f 100644
--- a/src/plugins/fancy/fancy_prefs.c
+++ b/src/plugins/fancy/fancy_prefs.c
@@ -30,6 +30,7 @@
 #include "claws.h"
 #include "plugin.h"
 #include "gtkutils.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "prefs.h"
 #include "prefs_common.h"
diff --git a/src/plugins/fancy/fancy_viewer.c b/src/plugins/fancy/fancy_viewer.c
index b4933c3..a10f6bc 100644
--- a/src/plugins/fancy/fancy_viewer.c
+++ b/src/plugins/fancy/fancy_viewer.c
@@ -28,7 +28,7 @@
 #include <fancy_viewer.h>
 #include <fancy_prefs.h>
 #include <alertpanel.h>
-#include <claws_io.h>
+#include <file-utils.h>
 
 #include <printing.h>
 #include <webkit/webkithittestresult.h>
diff --git a/src/plugins/libravatar/libravatar_cache.c b/src/plugins/libravatar/libravatar_cache.c
index 7f01620..22fa9cd 100644
--- a/src/plugins/libravatar/libravatar_cache.c
+++ b/src/plugins/libravatar/libravatar_cache.c
@@ -19,6 +19,7 @@
 #include <sys/stat.h>
 
 #include "libravatar_cache.h"
+#include "file-utils.h"
 #include "utils.h"
 
 gchar *libravatar_cache_init(const char *dirs[], gint start, gint end)
diff --git a/src/plugins/libravatar/libravatar_image.c b/src/plugins/libravatar/libravatar_image.c
index 20bc3b2..d36408e 100644
--- a/src/plugins/libravatar/libravatar_image.c
+++ b/src/plugins/libravatar/libravatar_image.c
@@ -27,7 +27,7 @@
 
 #include <common/claws.h>
 #include <prefs_common.h>
-#include <claws_io.h>
+#include <file-utils.h>
 
 #include "libravatar.h"
 #include "libravatar_prefs.h"
diff --git a/src/plugins/libravatar/libravatar_missing.c b/src/plugins/libravatar/libravatar_missing.c
index dc2d81f..0ff67f0 100644
--- a/src/plugins/libravatar/libravatar_missing.c
+++ b/src/plugins/libravatar/libravatar_missing.c
@@ -21,7 +21,7 @@
 #include "libravatar_missing.h"
 #include "libravatar_prefs.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /**
  * Loads the hash table of md5sum → time from the given filename.
diff --git a/src/plugins/mailmbox/mailimf.c b/src/plugins/mailmbox/mailimf.c
index e932ca7..19b3b29 100644
--- a/src/plugins/mailmbox/mailimf.c
+++ b/src/plugins/mailmbox/mailimf.c
@@ -39,7 +39,7 @@
 #endif
 
 #include "mailimf.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /*
   RFC 2822
diff --git a/src/plugins/mailmbox/mailimf_write.c b/src/plugins/mailmbox/mailimf_write.c
index 27352b5..cb5b953 100644
--- a/src/plugins/mailmbox/mailimf_write.c
+++ b/src/plugins/mailmbox/mailimf_write.c
@@ -43,7 +43,7 @@
 #include <time.h>
 #include <string.h>
 #include <ctype.h>
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define MAX_MAIL_COL 72
 
diff --git a/src/plugins/mailmbox/mailmbox.c b/src/plugins/mailmbox/mailmbox.c
index b32af40..dda0070 100644
--- a/src/plugins/mailmbox/mailmbox.c
+++ b/src/plugins/mailmbox/mailmbox.c
@@ -58,8 +58,8 @@
 #include "mmapstring.h"
 #include "mailmbox_parse.h"
 #include "maillock.h"
+#include "file-utils.h"
 #include "utils.h"
-#include "claws_io.h"
 
 /*
   http://www.qmail.org/qmail-manual-html/man5/mbox.html
diff --git a/src/plugins/mailmbox/mailmbox_folder.c b/src/plugins/mailmbox/mailmbox_folder.c
index 569d9b7..632fa3d 100644
--- a/src/plugins/mailmbox/mailmbox_folder.c
+++ b/src/plugins/mailmbox/mailmbox_folder.c
@@ -56,7 +56,7 @@
 #include "mailmbox.h"
 #include "mailmbox_folder.h"
 #include "mailmbox_parse.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define MAILMBOX_CACHE_DIR           "mailmboxcache"
 
diff --git a/src/plugins/newmail/newmail.c b/src/plugins/newmail/newmail.c
index b8c483c..9a925e7 100644
--- a/src/plugins/newmail/newmail.c
+++ b/src/plugins/newmail/newmail.c
@@ -31,7 +31,7 @@
 #include <inttypes.h>
 
 #include "plugin.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define LOG_NAME	"NewLog"
 #define DEFAULT_DIR	"Mail"
diff --git a/src/plugins/pdf_viewer/poppler_viewer.c b/src/plugins/pdf_viewer/poppler_viewer.c
index 05a139e..e2c3a72 100644
--- a/src/plugins/pdf_viewer/poppler_viewer.c
+++ b/src/plugins/pdf_viewer/poppler_viewer.c
@@ -31,6 +31,7 @@
 #include "gtk/inputdialog.h"
 #include "mimeview.h"
 #include "summaryview.h"
+#include "file-utils.h"
 #ifndef POPPLER_WITH_GDK
 #include "stdbool.h"
 #endif
diff --git a/src/plugins/perl/perl_plugin.c b/src/plugins/perl/perl_plugin.c
index a4bdda0..106387d 100644
--- a/src/plugins/perl/perl_plugin.c
+++ b/src/plugins/perl/perl_plugin.c
@@ -45,7 +45,7 @@
 #include "common/log.h"
 #include "common/plugin.h"
 #include "common/tags.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #include <EXTERN.h>
 #include <perl.h>
diff --git a/src/plugins/pgpcore/pgp_utils.c b/src/plugins/pgpcore/pgp_utils.c
index 8d3e95c..f7b2793 100644
--- a/src/plugins/pgpcore/pgp_utils.c
+++ b/src/plugins/pgpcore/pgp_utils.c
@@ -30,7 +30,7 @@
 
 #include "pgp_utils.h"
 #include "codeconv.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 gchar *fp_read_noconv(FILE *fp)
 {
diff --git a/src/plugins/pgpcore/sgpgme.c b/src/plugins/pgpcore/sgpgme.c
index 6586295..b037ef7 100644
--- a/src/plugins/pgpcore/sgpgme.c
+++ b/src/plugins/pgpcore/sgpgme.c
@@ -59,7 +59,7 @@
 #include "account.h"
 #include "select-keys.h"
 #include "claws.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static void sgpgme_disable_all(void)
 {
diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c
index d4bee07..b284e64 100644
--- a/src/plugins/pgpinline/pgpinline.c
+++ b/src/plugins/pgpinline/pgpinline.c
@@ -28,7 +28,7 @@
 #include <glib/gi18n.h>
 #include <errno.h>
 #include <gpgme.h>
-#include <claws_io.h>
+#include <file-utils.h>
 
 #include "utils.h"
 #include "privacy.h"
diff --git a/src/plugins/pgpmime/pgpmime.c b/src/plugins/pgpmime/pgpmime.c
index d768de4..4ee1898 100644
--- a/src/plugins/pgpmime/pgpmime.c
+++ b/src/plugins/pgpmime/pgpmime.c
@@ -42,7 +42,7 @@
 #include <plugins/pgpcore/pgp_utils.h>
 
 #include "prefs_common.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
diff --git a/src/plugins/python/python_plugin.c b/src/plugins/python/python_plugin.c
index bce0f7b..0a0aa1e 100644
--- a/src/plugins/python/python_plugin.c
+++ b/src/plugins/python/python_plugin.c
@@ -39,7 +39,7 @@
 #include "python-shell.h"
 #include "python-hooks.h"
 #include "clawsmailmodule.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define PYTHON_SCRIPTS_BASE_DIR "python-scripts"
 #define PYTHON_SCRIPTS_MAIN_DIR "main"
diff --git a/src/plugins/rssyl/opml_export.c b/src/plugins/rssyl/opml_export.c
index d87169b..f6202b8 100644
--- a/src/plugins/rssyl/opml_export.c
+++ b/src/plugins/rssyl/opml_export.c
@@ -32,7 +32,7 @@
 #include <log.h>
 #include <folder.h>
 #include <common/utils.h>
-#include <claws_io.h>
+#include <file-utils.h>
 
 /* Local includes */
 #include "libfeed/date.h"
diff --git a/src/plugins/rssyl/rssyl.c b/src/plugins/rssyl/rssyl.c
index c98590e..b246a74 100644
--- a/src/plugins/rssyl/rssyl.c
+++ b/src/plugins/rssyl/rssyl.c
@@ -33,7 +33,6 @@
 #include <folder.h>
 #include <procmsg.h>
 #include <localfolder.h>
-#include <common/utils.h>
 #include <main.h>
 #include <mh.h>
 #include <xml.h>
@@ -41,6 +40,7 @@
 #include <prefs_common.h>
 #include <prefs_toolbar.h>
 #include <utils.h>
+#include <file-utils.h>
 
 /* Local includes */
 #include "libfeed/feeditem.h"
diff --git a/src/plugins/rssyl/rssyl_add_item.c b/src/plugins/rssyl/rssyl_add_item.c
index cc9d8dd..5e74276 100644
--- a/src/plugins/rssyl/rssyl_add_item.c
+++ b/src/plugins/rssyl/rssyl_add_item.c
@@ -34,7 +34,7 @@
 #include <codeconv.h>
 #include <procmsg.h>
 #include <common/utils.h>
-#include <claws_io.h>
+#include <file-utils.h>
 
 /* Local includes */
 #include "libfeed/date.h"
diff --git a/src/plugins/rssyl/rssyl_deleted.c b/src/plugins/rssyl/rssyl_deleted.c
index c8c6395..4a7569a 100644
--- a/src/plugins/rssyl/rssyl_deleted.c
+++ b/src/plugins/rssyl/rssyl_deleted.c
@@ -31,7 +31,7 @@
 /* Claws Mail includes */
 #include <codeconv.h>
 #include <common/utils.h>
-#include <claws_io.h>
+#include <file-utils.h>
 
 /* Local includes */
 #include "rssyl.h"
diff --git a/src/plugins/rssyl/rssyl_update_format.c b/src/plugins/rssyl/rssyl_update_format.c
index 184dee3..4480429 100644
--- a/src/plugins/rssyl/rssyl_update_format.c
+++ b/src/plugins/rssyl/rssyl_update_format.c
@@ -38,6 +38,7 @@
 #include "rssyl.h"
 #include "rssyl_feed.h"
 #include "strutils.h"
+#include "file-utils.h"
 
 struct _RUpdateFormatCtx {
 	FolderItem *o_prev;
diff --git a/src/plugins/smime/smime.c b/src/plugins/smime/smime.c
index 82edd92..ea999c1 100644
--- a/src/plugins/smime/smime.c
+++ b/src/plugins/smime/smime.c
@@ -43,7 +43,7 @@
 #include "prefs_common.h"
 #include "procmime.h"
 #include "plugin.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
diff --git a/src/plugins/spam_report/spam_report.c b/src/plugins/spam_report/spam_report.c
index 826281d..21c96cc 100644
--- a/src/plugins/spam_report/spam_report.c
+++ b/src/plugins/spam_report/spam_report.c
@@ -35,6 +35,7 @@
 #include "password.h"
 #include "plugin.h"
 #include "prefs_common.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "spam_report_prefs.h"
 #include "statusbar.h"
diff --git a/src/plugins/spamassassin/libspamc.c b/src/plugins/spamassassin/libspamc.c
index 09e52b0..5f9836b 100644
--- a/src/plugins/spamassassin/libspamc.c
+++ b/src/plugins/spamassassin/libspamc.c
@@ -70,6 +70,7 @@
 #endif
 
 /* must load *after* errno.h, Bug 6697 */
+#include "file-utils.h"
 #include "utils.h"
 
 /* RedHat 5.2 doesn't define Shutdown 2nd Parameter Constants */
diff --git a/src/plugins/spamassassin/spamassassin.c b/src/plugins/spamassassin/spamassassin.c
index 5c0632c..0cd725a 100644
--- a/src/plugins/spamassassin/spamassassin.c
+++ b/src/plugins/spamassassin/spamassassin.c
@@ -51,7 +51,7 @@
 #include "prefs_common.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef HAVE_SYSEXITS_H
 #include <sysexits.h>
diff --git a/src/plugins/tnef_parse/tnef_parse.c b/src/plugins/tnef_parse/tnef_parse.c
index 93ef632..be8812d 100644
--- a/src/plugins/tnef_parse/tnef_parse.c
+++ b/src/plugins/tnef_parse/tnef_parse.c
@@ -48,7 +48,7 @@
 #include "plugin.h"
 #include "procmime.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #include "tnef_dump.h"
 
diff --git a/src/plugins/vcalendar/vcal_folder.c b/src/plugins/vcalendar/vcal_folder.c
index 4cde1bc..21a9227 100644
--- a/src/plugins/vcalendar/vcal_folder.c
+++ b/src/plugins/vcalendar/vcal_folder.c
@@ -37,6 +37,7 @@
 #include <ctype.h>
 
 #include "account.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "procmsg.h"
 #include "procheader.h"
diff --git a/src/plugins/vcalendar/vcal_manager.c b/src/plugins/vcalendar/vcal_manager.c
index 52bfd81..009cdbd 100644
--- a/src/plugins/vcalendar/vcal_manager.c
+++ b/src/plugins/vcalendar/vcal_manager.c
@@ -49,6 +49,7 @@
 #include <time.h>
 #include "folder.h"
 #include "quoted-printable.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "defs.h"
 
diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c
index 48e00d2..11188a4 100644
--- a/src/plugins/vcalendar/vcal_meeting_gtk.c
+++ b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -54,7 +54,7 @@
 #include "gtkutils.h"
 #include "log.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 struct _VCalMeeting
 {
diff --git a/src/plugins/vcalendar/vcalendar.c b/src/plugins/vcalendar/vcalendar.c
index 3388567..bfe294d 100644
--- a/src/plugins/vcalendar/vcalendar.c
+++ b/src/plugins/vcalendar/vcalendar.c
@@ -36,6 +36,7 @@
 #include "folder.h"
 #include "folder_item_prefs.h"
 #include "mimeview.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "vcalendar.h"
 #include "vcal_manager.h"
@@ -54,7 +55,6 @@
 #include "statusbar.h"
 #include "timing.h"
 #include "inc.h"
-#include "claws_io.h"
 
 MimeViewerFactory vcal_viewer_factory;
 
diff --git a/src/pop.c b/src/pop.c
index 4c5f521..6de10c6 100644
--- a/src/pop.c
+++ b/src/pop.c
@@ -40,7 +40,7 @@
 #include "partial_download.h"
 #include "log.h"
 #include "hooks.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static gint pop3_greeting_recv		(Pop3Session *session,
 					 const gchar *msg);
diff --git a/src/prefs_account.c b/src/prefs_account.c
index 924823f..8697c33 100644
--- a/src/prefs_account.c
+++ b/src/prefs_account.c
@@ -65,7 +65,7 @@
 #include "inputdialog.h"
 #include "ssl_certificate.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static gboolean cancelled;
 static gboolean new_account;
diff --git a/src/prefs_actions.c b/src/prefs_actions.c
index 4f39386..afa3931 100644
--- a/src/prefs_actions.c
+++ b/src/prefs_actions.c
@@ -50,7 +50,7 @@
 #include "prefs_filtering_action.h"
 #include "matcher_parser.h"
 #include "prefs_toolbar.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 enum {
 	PREFS_ACTIONS_STRING,	/*!< string pointer managed by list store, 
diff --git a/src/prefs_common.c b/src/prefs_common.c
index 01747df..a50087d 100644
--- a/src/prefs_common.c
+++ b/src/prefs_common.c
@@ -61,7 +61,7 @@
 #include "prefswindow.h"
 #include "colorlabel.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef USE_ALT_ADDRBOOK
 	#include "addrcustomattr.h"
diff --git a/src/prefs_customheader.c b/src/prefs_customheader.c
index ac119d0..512540e 100644
--- a/src/prefs_customheader.c
+++ b/src/prefs_customheader.c
@@ -47,7 +47,7 @@
 #include "alertpanel.h"
 #include "filesel.h"
 #include "combobox.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 enum {
 	CUSTHDR_STRING,		/*!< display string managed by list store */
diff --git a/src/prefs_display_header.c b/src/prefs_display_header.c
index bd70f4f..3c43ebe 100644
--- a/src/prefs_display_header.c
+++ b/src/prefs_display_header.c
@@ -41,7 +41,7 @@
 #include "displayheader.h"
 #include "utils.h"
 #include "gtkutils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 enum {
 	PREFS_HDR_HEADER,
diff --git a/src/prefs_gtk.c b/src/prefs_gtk.c
index fedffcc..b39bcbd 100644
--- a/src/prefs_gtk.c
+++ b/src/prefs_gtk.c
@@ -41,7 +41,7 @@
 #include "gtkutils.h"
 #include "password.h"
 #include "codeconv.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define CL(x)	(((gulong) (x) >> (gulong) 8) & 0xFFUL)
 #define RGB_FROM_GDK_COLOR(c) \
diff --git a/src/prefs_themes.c b/src/prefs_themes.c
index 60ba49e..24c6356 100644
--- a/src/prefs_themes.c
+++ b/src/prefs_themes.c
@@ -48,7 +48,7 @@
 #include "compose.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define IS_CURRENT_THEME(path)  (strcmp(prefs_common.pixmap_theme_path, path) == 0)
 #define IS_INTERNAL_THEME(path) (strcmp(DEFAULT_PIXMAP_THEME, path) == 0)
diff --git a/src/procheader.c b/src/procheader.c
index 9431841..d384db0 100644
--- a/src/procheader.c
+++ b/src/procheader.c
@@ -41,7 +41,7 @@
 #include "hooks.h"
 #include "utils.h"
 #include "defs.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define BUFFSIZE	8192
 
diff --git a/src/procmime.c b/src/procmime.c
index a9f28a5..970a8e0 100644
--- a/src/procmime.c
+++ b/src/procmime.c
@@ -52,7 +52,7 @@
 #include "timing.h"
 #include "privacy.h"
 #include "account.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GHashTable *procmime_get_mime_type_table	(void);
 static MimeInfo *procmime_scan_file_short(const gchar *filename);
diff --git a/src/procmsg.c b/src/procmsg.c
index 34eff51..ee98e09 100644
--- a/src/procmsg.c
+++ b/src/procmsg.c
@@ -49,7 +49,7 @@
 #include "timing.h"
 #include "inc.h"
 #include "privacy.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 extern SessionStats session_stats;
 
diff --git a/src/quote_fmt_parse.y b/src/quote_fmt_parse.y
index 515d910..7e29f34 100644
--- a/src/quote_fmt_parse.y
+++ b/src/quote_fmt_parse.y
@@ -37,7 +37,7 @@
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
 #include "account.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* decl */
 /*
diff --git a/src/send_message.c b/src/send_message.c
index d0a2160..ce8b125 100644
--- a/src/send_message.c
+++ b/src/send_message.c
@@ -56,7 +56,7 @@
 #include "inc.h"
 #include "log.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 typedef struct _SendProgressDialog	SendProgressDialog;
 
diff --git a/src/sourcewindow.c b/src/sourcewindow.c
index bb3225f..34f52c7 100644
--- a/src/sourcewindow.c
+++ b/src/sourcewindow.c
@@ -30,7 +30,7 @@
 #include "utils.h"
 #include "gtkutils.h"
 #include "prefs_common.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static void source_window_size_alloc_cb	(GtkWidget	*widget,
 					 GtkAllocation	*allocation);
diff --git a/src/summaryview.c b/src/summaryview.c
index 19c03e8..e619875 100644
--- a/src/summaryview.c
+++ b/src/summaryview.c
@@ -46,6 +46,7 @@
 #include "prefs_filtering.h"
 #include "account.h"
 #include "compose.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "stock_pixmap.h"
diff --git a/src/textview.c b/src/textview.c
index 72678f0..af97418 100644
--- a/src/textview.c
+++ b/src/textview.c
@@ -69,7 +69,7 @@
 #include "folder_item_prefs.h"
 #include "hooks.h"
 #include "avatars.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GdkColor quote_colors[3] = {
 	{(gulong)0, (gushort)0, (gushort)0, (gushort)0},
diff --git a/src/vcard.c b/src/vcard.c
index deb083d..df417a8 100644
--- a/src/vcard.c
+++ b/src/vcard.c
@@ -40,7 +40,7 @@
 #include "utils.h"
 #include "codeconv.h"
 #include "quoted-printable.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define GNOMECARD_DIR     ".gnome"
 #define GNOMECARD_FILE    "GnomeCard"
diff --git a/src/wizard.c b/src/wizard.c
index db4a108..3c74ba2 100644
--- a/src/wizard.c
+++ b/src/wizard.c
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <ctype.h>
 
+#include "file-utils.h"
 #include "utils.h"
 #include "gtk/menu.h"
 #include "plugin.h"

commit 0cfddd54ce8734e793097a6b56bdd07da9de34a9
Author: Colin Leroy <colin at colino.net>
Date:   Sun Oct 7 14:12:29 2018 +0200

    Wrap file I/O to claws_* to benefit from custom locking when
    available. Gains about 33% on I/O bound tasks.

diff --git a/configure.ac b/configure.ac
index 0ac7fd6..b5e956b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -180,7 +180,7 @@ if test x"$_gcc_psign" = xyes ; then
 	CFLAGS="$CFLAGS -Wno-pointer-sign"
 fi
 
-CFLAGS="$CFLAGS -Wall"
+CFLAGS="$CFLAGS -Wall -D_GNU_SOURCE"
 
 if test $USE_MAINTAINER_MODE = yes; then
 	CFLAGS="$CFLAGS -g -Wno-pointer-sign -DUSE_MAINTAINER_MODE"
@@ -423,7 +423,7 @@ AC_CHECK_FUNCS(gethostname mkdir mktime socket strstr strchr \
 	       uname flock lockf inet_aton inet_addr \
 	       fchmod mkstemp truncate getuid regcomp)
 
-AC_CHECK_FUNCS(fgets_unlocked fwrite_unlocked fmemopen)
+AC_CHECK_FUNCS(fgets_unlocked fgetc_unlocked fputs_unlocked fputc_unlocked fread_unlocked fwrite_unlocked feof_unlocked ferror_unlocked fmemopen)
 
 dnl *****************
 dnl ** common code **
diff --git a/src/account.c b/src/account.c
index e73a012..285257b 100644
--- a/src/account.c
+++ b/src/account.c
@@ -55,6 +55,7 @@
 #include "prefs_actions.h"
 #include "hooks.h"
 #include "passwordstore.h"
+#include "claws_io.h"
 
 enum {
 	ACCOUNT_IS_DEFAULT,
@@ -194,14 +195,14 @@ void account_read_config_all(void)
 	debug_print("Reading all config for each account...\n");
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
-	if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+	if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
 		g_free(rcpath);
 		return;
 	}
 	g_free(rcpath);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		if (!strncmp(buf, "[Account: ", 10)) {
 			strretchomp(buf);
 			memmove(buf, buf + 1, sizeof(buf) - 1);
@@ -211,7 +212,7 @@ void account_read_config_all(void)
 						       g_strdup(buf));
 		}
 	}
-	fclose(fp);
+	claws_fclose(fp);
 
 	/* read config data from file */
 	cur_account = NULL;
diff --git a/src/addrbook.c b/src/addrbook.c
index 0720c6b..64a3645 100644
--- a/src/addrbook.c
+++ b/src/addrbook.c
@@ -19,6 +19,11 @@
 
 /* General functions for accessing address book files */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -951,11 +956,11 @@ static int addrbook_write_elem_s(FILE *fp, gint lvl, gchar *name)
 {
 	gint i;
 	for (i = 0; i < lvl; i++) 
-		if (fputs("  ", fp) == EOF)
+		if (claws_fputs("  ", fp) == EOF)
 			return -1;
-	if (fputs("<", fp) == EOF)
+	if (claws_fputs("<", fp) == EOF)
 		return -1;
-	if (fputs(name, fp) == EOF)
+	if (claws_fputs(name, fp) == EOF)
 		return -1;
 		
 	return 0;
@@ -971,13 +976,13 @@ static int addrbook_write_elem_e(FILE *fp, gint lvl, gchar *name)
 {
 	gint i;
 	for(i = 0; i < lvl; i++)
-		if (fputs("  ", fp) == EOF)
+		if (claws_fputs("  ", fp) == EOF)
 			return -1;
-	if (fputs("</", fp) == EOF)
+	if (claws_fputs("</", fp) == EOF)
 		return -1;
-	if (fputs(name, fp) == EOF)
+	if (claws_fputs(name, fp) == EOF)
 		return -1;
-	if (fputs(">\n", fp) == EOF)
+	if (claws_fputs(">\n", fp) == EOF)
 		return -1;
 		
 	return 0;
@@ -991,15 +996,15 @@ static int addrbook_write_elem_e(FILE *fp, gint lvl, gchar *name)
  */
 static int addrbook_write_attr(FILE *fp, gchar *name, gchar *value)
 {
-	if (fputs(" ", fp) == EOF)
+	if (claws_fputs(" ", fp) == EOF)
 		return -1;
-	if (fputs(name, fp) == EOF)
+	if (claws_fputs(name, fp) == EOF)
 		return -1;
-	if (fputs("=\"", fp) == EOF)
+	if (claws_fputs("=\"", fp) == EOF)
 		return -1;
 	if (xml_file_put_escape_str(fp, value) < 0)
 		return -1;
-	if (fputs("\"", fp) == EOF)
+	if (claws_fputs("\"", fp) == EOF)
 		return -1;
 	
 	return 0;
@@ -1042,13 +1047,13 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
 				data->error = TRUE;
 			if (addrbook_write_attr(fp, AB_ATTAG_COMMON_NAME, ADDRITEM_NAME(person)) < 0)
 				data->error = TRUE;
-			if (fputs(" >\n", fp) == EOF)
+			if (claws_fputs(" >\n", fp) == EOF)
 				data->error = TRUE;
 
 			/* Output email addresses */
 			if (addrbook_write_elem_s(fp, 2, AB_ELTAG_ADDRESS_LIST) < 0)
 				data->error = TRUE;
-			if (fputs(">\n", fp) == EOF)
+			if (claws_fputs(">\n", fp) == EOF)
 				data->error = TRUE;
 			node = person->listEMail;
 			while (node) {
@@ -1063,7 +1068,7 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
 					data->error = TRUE;
 				if (addrbook_write_attr(fp, AB_ATTAG_REMARKS, email->remarks) < 0)
 					data->error = TRUE;
-				if (fputs(" />\n", fp) == EOF)
+				if (claws_fputs(" />\n", fp) == EOF)
 					data->error = TRUE;
 				node = g_list_next(node);
 			}
@@ -1073,7 +1078,7 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
 			/* Output user attributes */
 			if (addrbook_write_elem_s(fp, 2, AB_ELTAG_ATTRIBUTE_LIST) < 0)
 				data->error = TRUE;
-			if (fputs(">\n", fp) == EOF)
+			if (claws_fputs(">\n", fp) == EOF)
 				data->error = TRUE;
 			node = person->listAttrib;
 			while (node) {
@@ -1084,7 +1089,7 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
 					data->error = TRUE;
 				if (addrbook_write_attr(fp, AB_ATTAG_NAME, attrib->name) < 0)
 					data->error = TRUE;
-				if (fputs(" >", fp) == EOF)
+				if (claws_fputs(" >", fp) == EOF)
 					data->error = TRUE;
 				if (xml_file_put_escape_str(fp, attrib->value) < 0)
 					data->error = TRUE;
@@ -1129,13 +1134,13 @@ static void addrbook_write_item_group_vis(gpointer key, gpointer value,
 				data->error = TRUE;
 			if (addrbook_write_attr(fp, AB_ATTAG_REMARKS, group->remarks) < 0)
 				data->error = TRUE;
-			if (fputs(" >\n", fp) == EOF)
+			if (claws_fputs(" >\n", fp) == EOF)
 				data->error = TRUE;
 
 			/* Output email address links */
 			if (addrbook_write_elem_s(fp, 2, AB_ELTAG_MEMBER_LIST) < 0)
 				data->error = TRUE;
-			if (fputs(">\n", fp) == EOF)
+			if (claws_fputs(">\n", fp) == EOF)
 				data->error = TRUE;
 			node = group->listEMail;
 			while (node) {
@@ -1147,7 +1152,7 @@ static void addrbook_write_item_group_vis(gpointer key, gpointer value,
 					data->error = TRUE;
 				if (addrbook_write_attr(fp, AB_ATTAG_EID, ADDRITEM_ID(email)) < 0)
 					data->error = TRUE;
-				if (fputs(" />\n", fp) == EOF)
+				if (claws_fputs(" />\n", fp) == EOF)
 					data->error = TRUE;
 				node = g_list_next(node);
 			}
@@ -1187,11 +1192,11 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
 				data->error = TRUE;
 			if (addrbook_write_attr(fp, AB_ATTAG_REMARKS, folder->remarks) < 0)
 				data->error = TRUE;
-			if (fputs(" >\n", fp) == EOF)
+			if (claws_fputs(" >\n", fp) == EOF)
 				data->error = TRUE;
 			if (addrbook_write_elem_s(fp, 2, AB_ELTAG_ITEM_LIST) < 0)
 				data->error = TRUE;
-			if (fputs(">\n", fp) == EOF)
+			if (claws_fputs(">\n", fp) == EOF)
 				data->error = TRUE;
 
 			/* Output persons */
@@ -1204,7 +1209,7 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
 					data->error = TRUE;
 				if (addrbook_write_attr(fp, AB_ATTAG_UID, ADDRITEM_ID(item)) < 0)
 					data->error = TRUE;
-				if (fputs(" />\n", fp) == EOF)
+				if (claws_fputs(" />\n", fp) == EOF)
 					data->error = TRUE;
 				node = g_list_next(node);
 			}
@@ -1219,7 +1224,7 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
 					data->error = TRUE;
 				if (addrbook_write_attr(fp, AB_ATTAG_UID, ADDRITEM_ID(item)) < 0)
 					data->error = TRUE;
-				if (fputs(" />\n", fp) == EOF)
+				if (claws_fputs(" />\n", fp) == EOF)
 					data->error = TRUE;
 				node = g_list_next(node);
 			}
@@ -1234,7 +1239,7 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
 					data->error = TRUE;
 				if (addrbook_write_attr(fp, AB_ATTAG_UID, ADDRITEM_ID(item)) < 0)
 					data->error = TRUE;
-				if (fputs(" />\n", fp) == EOF)
+				if (claws_fputs(" />\n", fp) == EOF)
 					data->error = TRUE;
 				node = g_list_next(node);
 			}
@@ -1268,10 +1273,10 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
 
 	book->retVal = MGU_OPEN_FILE;
 #ifdef DEV_STANDALONE
-	fp = g_fopen(fileSpec, "wb");
+	fp = claws_fopen(fileSpec, "wb");
 	g_free(fileSpec);
 	if (fp) {
-		if (fputs("<?xml version=\"1.0\" ?>\n", fp) == EOF) {
+		if (claws_fputs("<?xml version=\"1.0\" ?>\n", fp) == EOF) {
 			book->retVal = MGU_ERROR_WRITE;
 			return book->retVal;
 		}
@@ -1288,7 +1293,7 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
 		if (addrbook_write_attr(fp, AB_ATTAG_NAME,
 				    addrcache_get_name(book->addressCache)) < 0)
 			goto fail;
-		if (fputs(" >\n", fp) == EOF)
+		if (claws_fputs(" >\n", fp) == EOF)
 			goto fail;
 
 		/* Output all persons */
@@ -1319,7 +1324,7 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
 
 		book->retVal = MGU_SUCCESS;
 #ifdef DEV_STANDALONE
-		safe_fclose(fp);
+		claws_safe_fclose(fp);
 #else
 		if (prefs_file_close( pfile ) < 0)
 			book->retVal = MGU_ERROR_WRITE;
diff --git a/src/addrharvest.c b/src/addrharvest.c
index 6d4bf3b..1d94633 100644
--- a/src/addrharvest.c
+++ b/src/addrharvest.c
@@ -21,6 +21,11 @@
  * Functions for an E-Mail address harvester.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <sys/stat.h>
 #include <dirent.h>
 #include <glib.h>
@@ -35,6 +40,7 @@
 #ifdef USE_ALT_ADDRBOOK
 	#include "addressbook-dbus.h"
 #endif
+#include "claws_io.h"
 
 /* Mail header names of interest */
 static gchar *_headerFrom_     = HEADER_FROM;
@@ -644,7 +650,7 @@ static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done
 	list = NULL;
 
 	/* Read line */
-	if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
+	if( claws_fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
 		*done = TRUE;
 		return list;
 	}
@@ -671,7 +677,7 @@ static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done
 		ch = fgetc( fp );
 		if( ch == ' ' || ch == '\t' ) {
 			/* Continuation character - read into buffer */
-			if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
+			if( claws_fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
 				break;
 			}
 		}
@@ -708,7 +714,7 @@ static gint addrharvest_readfile(
 	GSList *list;
 	gboolean done;
 
-	msgFile = g_fopen( fileName, "rb" );
+	msgFile = claws_fopen( fileName, "rb" );
 	if( ! msgFile ) {
 		/* Cannot open file */
 		retVal = MGU_OPEN_FILE;
@@ -747,7 +753,7 @@ static gint addrharvest_readfile(
 		g_free( buf );
 	}
 
-	fclose( msgFile );
+	claws_fclose( msgFile );
 	return MGU_SUCCESS;
 }
 
diff --git a/src/addrindex.c b/src/addrindex.c
index f1adf01..bad1fa5 100644
--- a/src/addrindex.c
+++ b/src/addrindex.c
@@ -937,11 +937,11 @@ static AddressInterface *addrindex_tag_get_datasource(
 static int addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name ) {
 	gint i;
 	for( i = 0; i < lvl; i++ ) 
-		if (fputs( "  ", fp ) == EOF)
+		if (claws_fputs( "  ", fp ) == EOF)
 			return -1;
-	if (fputs( "<", fp ) == EOF)
+	if (claws_fputs( "<", fp ) == EOF)
 		return -1;
-	if (fputs( name, fp ) == EOF)
+	if (claws_fputs( name, fp ) == EOF)
 		return -1;
 	return 0;
 }
@@ -955,13 +955,13 @@ static int addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name )
 static int addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name ) {
 	gint i;
 	for( i = 0; i < lvl; i++ ) 
-		if (fputs( "  ", fp ) == EOF)
+		if (claws_fputs( "  ", fp ) == EOF)
 			return -1;
-	if (fputs( "</", fp ) == EOF)
+	if (claws_fputs( "</", fp ) == EOF)
 		return -1;
-	if (fputs( name, fp ) == EOF)
+	if (claws_fputs( name, fp ) == EOF)
 		return -1;
-	if (fputs( ">\n", fp ) == EOF)
+	if (claws_fputs( ">\n", fp ) == EOF)
 		return -1;
 	return 0;
 }
@@ -973,15 +973,15 @@ static int addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name )
  * \param value Attribute value.
  */
 static int addrindex_write_attr( FILE *fp, const gchar *name, const gchar *value ) {
-	if (fputs( " ", fp ) == EOF)
+	if (claws_fputs( " ", fp ) == EOF)
 		return -1;
-	if (fputs( name, fp ) == EOF)
+	if (claws_fputs( name, fp ) == EOF)
 		return -1;
-	if (fputs( "=\"", fp ) == EOF)
+	if (claws_fputs( "=\"", fp ) == EOF)
 		return -1;
 	if (xml_file_put_escape_str( fp, value ) < 0)
 		return -1;
-	if (fputs( "\"", fp ) == EOF)
+	if (claws_fputs( "\"", fp ) == EOF)
 		return -1;
 	return 0;
 }
@@ -1071,7 +1071,7 @@ static int addrindex_write_fragment(
 			node = g_list_next( node );
 		}
 		if( fragment->children ) {
-			if (fputs(" >\n", fp) == EOF)
+			if (claws_fputs(" >\n", fp) == EOF)
 				return -1;
 
 			/* Output children */
@@ -1088,7 +1088,7 @@ static int addrindex_write_fragment(
 				return -1;
 		}
 		else {
-			if (fputs(" />\n", fp) == EOF)
+			if (claws_fputs(" />\n", fp) == EOF)
 				return -1;
 		}
 	}
@@ -1134,7 +1134,7 @@ static int addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
 			return -1;
 		if (addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName ) < 0)
 			return -1;
-		if (fputs( " />\n", fp ) == EOF)
+		if (claws_fputs( " />\n", fp ) == EOF)
 			return -1;
 	}
 	return 0;
@@ -1172,7 +1172,7 @@ static int addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
 			return -1;
 		if (addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path ) < 0)
 			return -1;
-		if (fputs( " />\n", fp ) == EOF)
+		if (claws_fputs( " />\n", fp ) == EOF)
 			return -1;
 	}
 	return 0;
@@ -1237,7 +1237,7 @@ static int addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
 			ind++;
 			node = g_list_next( node );
 		}
-		if (fputs( " />\n", fp ) == EOF)
+		if (claws_fputs( " />\n", fp ) == EOF)
 			return -1;
 	}
 	return 0;
@@ -1519,7 +1519,7 @@ static int addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
 			ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO ) < 0)
 		return -1;
 
-	if (fputs(" >\n", fp) == EOF)
+	if (claws_fputs(" >\n", fp) == EOF)
 		return -1;
 
 	/* Output attributes */
@@ -1529,7 +1529,7 @@ static int addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
 			return -1;
 		if (addrindex_write_attr( fp, ATTAG_LDAP_ATTR_NAME, node->data ) < 0)
 			return -1;
-		if (fputs(" />\n", fp) == EOF)
+		if (claws_fputs(" />\n", fp) == EOF)
 			return -1;
 		node = g_list_next( node );
 	}
@@ -1708,7 +1708,7 @@ static int addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
 			nodeDS = iface->listSource;
 			if (addrindex_write_elem_s( fp, lvlList, iface->listTag ) < 0)
 				return -1;
-			if (fputs( ">\n", fp ) == EOF)
+			if (claws_fputs( ">\n", fp ) == EOF)
 				return -1;
 			while( nodeDS ) {
 				AddressDataSource *ds = nodeDS->data;
@@ -1759,10 +1759,10 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
 	fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
 	addrIndex->retVal = MGU_OPEN_FILE;
 #ifdef DEV_STANDALONE
-	fp = g_fopen( fileSpec, "wb" );
+	fp = claws_fopen( fileSpec, "wb" );
 	g_free( fileSpec );
 	if( fp ) {
-		fputs( "<?xml version=\"1.0\" ?>\n", fp );
+		claws_fputs( "<?xml version=\"1.0\" ?>\n", fp );
 #else
 	pfile = prefs_write_open( fileSpec );
 	g_free( fileSpec );
@@ -1773,7 +1773,7 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
 #endif
 		if (addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX ) < 0)
 			goto fail;
-		if (fputs( ">\n", fp ) == EOF)
+		if (claws_fputs( ">\n", fp ) == EOF)
 			goto fail;
 
 		if (addrindex_write_index( addrIndex, fp ) < 0)
@@ -1783,7 +1783,7 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
 
 		addrIndex->retVal = MGU_SUCCESS;
 #ifdef DEV_STANDALONE
-		safe_fclose( fp );
+		claws_safe_fclose( fp );
 #else
 		if( prefs_file_close( pfile ) < 0 ) {
 			addrIndex->retVal = MGU_ERROR_WRITE;
diff --git a/src/advsearch.c b/src/advsearch.c
index ccfad6c..85dc3cc 100644
--- a/src/advsearch.c
+++ b/src/advsearch.c
@@ -31,6 +31,7 @@
 #include "matcher_parser.h"
 #include "utils.h"
 #include "prefs_common.h"
+#include "timing.h"
 
 struct _AdvancedSearch {
 	struct {
@@ -502,27 +503,33 @@ static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
 			    FolderItem* folderItem, gboolean recursive)
 {
 	if (recursive) {
-		if (!search_impl(messages, search, folderItem, FALSE))
+		START_TIMING("recursive");
+		if (!search_impl(messages, search, folderItem, FALSE)) {
+			END_TIMING();
 			return FALSE;
-
+		}
 		if (folderItem->node->children != NULL && !search->search_aborted) {
 			GNode *node;
 			for (node = folderItem->node->children; node != NULL; node = node->next) {
 				FolderItem *cur = FOLDER_ITEM(node->data);
 				debug_print("in: %s\n", cur->path);
-				if (!search_impl(messages, search, cur, TRUE))
+				if (!search_impl(messages, search, cur, TRUE)) {
+					END_TIMING();
 					return FALSE;
+				}
 			}
 		}
+		END_TIMING();
 	} else if (!folderItem->no_select) {
 		MsgNumberList *msgnums = NULL;
 		MsgNumberList *cur;
 		MsgInfoList *msgs = NULL;
 		gboolean can_search_on_server = folderItem->folder->klass->supports_server_search;
-
+		START_TIMING("folder");
 		if (!search_filter_folder(&msgnums, search, folderItem,
 					  can_search_on_server)) {
 			g_slist_free(msgnums);
+			END_TIMING();
 			return FALSE;
 		}
 
@@ -542,6 +549,7 @@ static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
 		}
 
 		g_slist_free(msgnums);
+		END_TIMING();
 	}
 
 	return TRUE;
diff --git a/src/autofaces.c b/src/autofaces.c
index 6146f65..d12c975 100644
--- a/src/autofaces.c
+++ b/src/autofaces.c
@@ -27,6 +27,7 @@
 
 #include "utils.h"
 #include "autofaces.h"
+#include "claws_io.h"
 
 static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
 {
@@ -37,20 +38,20 @@ static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint
 	xfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, AUTOFACES_DIR,
 	                    G_DIR_SEPARATOR_S, anyname, NULL);
 	buf[0] = '\0';
-	if ((xfp = g_fopen(xfile, "rb")) == NULL) {
+	if ((xfp = claws_fopen(xfile, "rb")) == NULL) {
 	        g_free(xfile);
 		debug_print("header content file '%s' not found\n", anyname);
 	        return -1;
 	}
-	if (fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
-	        fclose(xfp);
+	if (claws_fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
+	        claws_fclose(xfp);
 	        g_free(xfile);
 		g_warning("header content file '%s' read failure", anyname);
 	        return -2;
 	}
 	lastc = strlen(buf) - 1;        /* remove trailing \n */
 	buf[lastc] = (buf[lastc] == '\n')? '\0': buf[lastc];
-	fclose(xfp);
+	claws_fclose(xfp);
 	g_free(xfile);
 
 	return 0;
diff --git a/src/common/claws_io.c b/src/common/claws_io.c
index 1de8bea..6268978 100644
--- a/src/common/claws_io.c
+++ b/src/common/claws_io.c
@@ -25,9 +25,11 @@
 #include <unistd.h>
 
 #include "timing.h"
+#include "claws_io.h"
 
 gboolean prefs_common_get_flush_metadata(void);
 
+/* FIXME make static once every file I/O is done using claws_* wrappers */
 int safe_fclose(FILE *fp)
 {
 	int r;
@@ -45,3 +47,47 @@ int safe_fclose(FILE *fp)
 
 	return r;
 }
+
+#if HAVE_FGETS_UNLOCKED
+
+/* Open a file and locks it once
+ * so subsequent I/O is faster
+ */
+FILE *claws_fopen(const char *file, const char *mode)
+{
+	FILE *fp = fopen(file, mode);
+	if (!fp)
+		return NULL;
+	flockfile(fp);
+	return fp;
+}
+
+FILE *claws_fdopen(int fd, const char *mode)
+{
+	FILE *fp = fdopen(fd, mode);
+	if (!fp)
+		return NULL;
+	flockfile(fp);
+	return fp;
+}
+
+/* Unlocks and close a file pointer
+ */
+
+int claws_fclose(FILE *fp)
+{
+	funlockfile(fp);
+	return fclose(fp);
+}
+
+/* Unlock, then safe-close a file pointer
+ * Safe close is done using fflush + fsync
+ * if the according preference says so.
+ */
+int claws_safe_fclose(FILE *fp)
+{
+	funlockfile(fp);
+	return safe_fclose(fp);
+}
+
+#endif
diff --git a/src/common/claws_io.h b/src/common/claws_io.h
index e3e0e0c..35edae3 100644
--- a/src/common/claws_io.h
+++ b/src/common/claws_io.h
@@ -19,6 +19,44 @@
 #ifndef __CLAWS_IO_H__
 #define __CLAWS_IO_H__
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
+#include <stdio.h>
+
+#if HAVE_FGETS_UNLOCKED
+#define claws_fgets fgets_unlocked
+#define claws_fgetc fgetc_unlocked
+#define claws_fputs fputs_unlocked
+#define claws_fputc fputc_unlocked
+#define claws_fread fread_unlocked
+#define claws_fwrite fwrite_unlocked
+#define claws_feof feof_unlocked
+#define claws_ferror ferror_unlocked
+
+FILE *claws_fopen(const char *file, const char *mode);
+FILE *claws_fdopen(int fd, const char *mode);
+int claws_fclose(FILE *fp);
+int claws_safe_fclose(FILE *fp);
+
+#else
+
+#define claws_fgets fgets
+#define claws_fgetc fgetc
+#define claws_fputs fputs
+#define claws_fputc fputc
+#define claws_fread fread
+#define claws_fwrite fwrite
+#define claws_feof feof
+#define claws_ferror ferror
+#define claws_fopen g_fopen
+#define claws_fdopen fdopen
+#define claws_fclose fclose
+#define claws_safe_fclose safe_fclose
+#endif
+
 int safe_fclose(FILE *fp);
 
 #endif
diff --git a/src/common/log.c b/src/common/log.c
index 65f284e..15a852b 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -28,7 +28,6 @@
 #  include <w32lib.h>
 #endif
 
-#include <stdio.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 
@@ -37,12 +36,12 @@
 #include "hooks.h"
 #include "claws_io.h"
 
-#define FWRITE(_b,_s,_n,_f)	if (fwrite(_b,_s,_n,_f) != _n) { \
-					g_message("log fwrite failed!\n"); \
+#define FWRITE(_b,_s,_n,_f)	if (claws_fwrite(_b,_s,_n,_f) != _n) { \
+					g_message("log claws_fwrite failed!\n"); \
 					return; \
 				}
-#define FPUTS(_b,_f)		if (fputs(_b,_f) == EOF) { \
-					g_message("log fputs failed!\n"); \
+#define FPUTS(_b,_f)		if (claws_fputs(_b,_f) == EOF) { \
+					g_message("log claws_fputs failed!\n"); \
 					return; \
 				}
 #define FFLUSH(_f)		if (fflush(_f) != 0) { \
@@ -122,9 +121,9 @@ void set_log_file(LogInstance instance, const gchar *filename)
 		g_free(backupname);
 	}
 
-	log_fp[instance] = g_fopen(fullname, "wb");
+	log_fp[instance] = claws_fopen(fullname, "wb");
 	if (!log_fp[instance]) {
-		FILE_OP_ERROR(fullname, "fopen");
+		FILE_OP_ERROR(fullname, "claws_fopen");
 		log_filename[instance] = NULL;
 		g_free(fullname);
 		return;
@@ -137,7 +136,7 @@ void set_log_file(LogInstance instance, const gchar *filename)
 void close_log_file(LogInstance instance)
 {
 	if (log_fp[instance]) {
-		safe_fclose(log_fp[instance]);
+		claws_safe_fclose(log_fp[instance]);
 		log_fp[instance] = NULL;
 		log_size[instance] = 0;
 		g_free(log_filename[instance]);
diff --git a/src/common/plugin.c b/src/common/plugin.c
index 64a6f01..963b16d 100644
--- a/src/common/plugin.c
+++ b/src/common/plugin.c
@@ -41,6 +41,7 @@
 #include "prefs.h"
 #include "claws.h"
 #include "timing.h"
+#include "claws_io.h"
 
 #ifdef G_OS_WIN32
 #define PLUGINS_BLOCK_PREFIX "PluginsWin32_"
@@ -245,13 +246,13 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
 	deps_file = g_strconcat(tmp, ".deps", NULL);
 	g_free(tmp);
 	
-	fp = g_fopen(deps_file, "rb");
+	fp = claws_fopen(deps_file, "rb");
 	g_free(deps_file);
 	
 	if (!fp)
 		return 0;
 	
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		Plugin *dep_plugin = NULL;
 		gchar *path = NULL;
 		buf[strlen(buf)-1]='\0'; /* chop off \n */
@@ -262,7 +263,7 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
 			dep_plugin = plugin_load(path, error);
 			if (dep_plugin == NULL) {
 				g_free(path);
-				fclose(fp);
+				claws_fclose(fp);
 				return -1;
 			}
 			dep_plugin->in_prefix_dir = TRUE;
@@ -278,7 +279,7 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
 					g_strdup(filename));
 		}
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	return 0;
 }
 
@@ -605,7 +606,7 @@ void plugin_load_all(const gchar *type)
 	}
 	g_free(block);
 
-	while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), pfile->fp) != NULL) {
 		if (buf[0] == '[')
 			break;
 
diff --git a/src/common/prefs.c b/src/common/prefs.c
index 77b4213..971193e 100644
--- a/src/common/prefs.c
+++ b/src/common/prefs.c
@@ -22,6 +22,8 @@
 #include "claws-features.h"
 #endif
 
+#include <stdio.h>
+
 #include "defs.h"
 
 #include <glib.h>
@@ -46,8 +48,8 @@ PrefFile *prefs_read_open(const gchar *path)
 
 	cm_return_val_if_fail(path != NULL, NULL);
 
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		FILE_OP_ERROR(path, "claws_fopen");
 		return NULL;
 	}
 
@@ -83,8 +85,8 @@ PrefFile *prefs_write_open(const gchar *path)
 	}
 
 	tmppath = g_strconcat(path, ".tmp", NULL);
-	if ((fp = g_fopen(tmppath, "wb")) == NULL) {
-		FILE_OP_ERROR(tmppath, "fopen");
+	if ((fp = claws_fopen(tmppath, "wb")) == NULL) {
+		FILE_OP_ERROR(tmppath, "claws_fopen");
 		g_free(tmppath);
 		return NULL;
 	}
@@ -128,17 +130,17 @@ gint prefs_file_close(PrefFile *pfile)
 	path = pfile->path;
 
 	if (!pfile->writing) {
-		fclose(fp);
+		claws_fclose(fp);
 		g_free(pfile);
 		g_free(path);
 		return 0;
 	}
 
 	if (orig_fp) {
-    		while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+    		while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
 			/* next block */
 			if (buf[0] == '[') {
-				if (fputs(buf, fp)  == EOF) {
+				if (claws_fputs(buf, fp)  == EOF) {
 					g_warning("failed to write configuration to file");
 					prefs_file_close_revert(pfile);
 				
@@ -148,20 +150,20 @@ gint prefs_file_close(PrefFile *pfile)
 			}
 		}
 		
-		while (fgets(buf, sizeof(buf), orig_fp) != NULL)
-			if (fputs(buf, fp) == EOF) {
+		while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL)
+			if (claws_fputs(buf, fp) == EOF) {
 				g_warning("failed to write configuration to file");
 				prefs_file_close_revert(pfile);			
 				
 				return -1;
 			}
-		fclose(orig_fp);
+		claws_fclose(orig_fp);
 	}
 
 	tmppath = g_strconcat(path, ".tmp", NULL);
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(tmppath, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(tmppath, "claws_fclose");
 		claws_unlink(tmppath);
 		g_free(path);
 		g_free(tmppath);
@@ -214,10 +216,10 @@ gint prefs_file_close_revert(PrefFile *pfile)
 	cm_return_val_if_fail(pfile != NULL, -1);
 
 	if (pfile->orig_fp)
-		fclose(pfile->orig_fp);
+		claws_fclose(pfile->orig_fp);
 	if (pfile->writing)
 		tmppath = g_strconcat(pfile->path, ".tmp", NULL);
-	fclose(pfile->fp);
+	claws_fclose(pfile->fp);
 	if (pfile->writing) {
 		if (claws_unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
 		g_free(tmppath);
@@ -272,7 +274,7 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
 
 	block_label = g_strdup_printf("[%s]", label);
 	if (!pfile->writing) {
-		while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), pfile->fp) != NULL) {
 			gint val;
 			
 			val = strncmp(buf, block_label, strlen(block_label));
@@ -282,10 +284,10 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
 			}
 		}
 	} else {
-		if ((pfile->orig_fp = g_fopen(pfile->path, "rb")) != NULL) {
+		if ((pfile->orig_fp = claws_fopen(pfile->path, "rb")) != NULL) {
 			gboolean block_matched = FALSE;
 
-			while (fgets(buf, sizeof(buf), pfile->orig_fp) != NULL) {
+			while (claws_fgets(buf, sizeof(buf), pfile->orig_fp) != NULL) {
 				gint val;
 
 				val = strncmp(buf, block_label, strlen(block_label));
@@ -294,7 +296,7 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
 					block_matched = TRUE;
 					break;
 				} else {
-					if (fputs(buf, pfile->fp) == EOF) {
+					if (claws_fputs(buf, pfile->fp) == EOF) {
 						g_warning("failed to write configuration to file");
 						prefs_file_close_revert(pfile);
 						g_free(block_label);
@@ -305,13 +307,13 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
 			}
 
 			if (!block_matched) {
-				fclose(pfile->orig_fp);
+				claws_fclose(pfile->orig_fp);
 				pfile->orig_fp = NULL;
 			}
 		}
 
-		if (fputs(block_label, pfile->fp) == EOF ||
-			fputc('\n', pfile->fp) == EOF) {
+		if (claws_fputs(block_label, pfile->fp) == EOF ||
+			claws_fputc('\n', pfile->fp) == EOF) {
 			g_warning("failed to write configuration to file");
 			prefs_file_close_revert(pfile);
 			g_free(block_label);
diff --git a/src/common/ssl_certificate.c b/src/common/ssl_certificate.c
index d098ac2..8afe4da 100644
--- a/src/common/ssl_certificate.c
+++ b/src/common/ssl_certificate.c
@@ -173,7 +173,7 @@ static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_
 	}
 
 	debug_print("writing %zd bytes\n",cert_size);
-	if (fwrite(&output, 1, cert_size, fp) < cert_size) {
+	if (claws_fwrite(&output, 1, cert_size, fp) < cert_size) {
 		g_warning("failed to write cert: %d %s", errno, g_strerror(errno));
 	}
 }
@@ -241,8 +241,8 @@ static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
 	tmp.data = malloc(s.st_size);
 	memset(tmp.data, 0, s.st_size);
 	tmp.size = s.st_size;
-	if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
-		perror("fread");
+	if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+		perror("claws_fread");
 		free(tmp.data);
 		free(crt_list);
 		return -EIO;
@@ -300,8 +300,8 @@ static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_
 	tmp.data = malloc(s.st_size);
 	memset(tmp.data, 0, s.st_size);
 	tmp.size = s.st_size;
-	if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
-		perror("fread");
+	if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+		perror("claws_fread");
 		free(tmp.data);
 		return NULL;
 	}
@@ -331,7 +331,7 @@ static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t f
 	tmp.data = malloc(s.st_size);
 	memset(tmp.data, 0, s.st_size);
 	tmp.size = s.st_size;
-	if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+	if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
 		log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
 				  g_strerror(errno));
 		free(tmp.data);
@@ -367,7 +367,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
 	file = get_certificate_path(cert->host, port, cert->fingerprint);
 
 	g_free(port);
-	fp = g_fopen(file, "wb");
+	fp = claws_fopen(file, "wb");
 	if (fp == NULL) {
 		g_free(file);
 		debug_print("Can't save certificate !\n");
@@ -377,7 +377,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
 	gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
 
 	g_free(file);
-	safe_fclose(fp);
+	claws_safe_fclose(fp);
 
 }
 
@@ -421,14 +421,14 @@ SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gch
 	
 	if (fingerprint != NULL) {
 		file = get_certificate_path(host, buf, fingerprint);
-		fp = g_fopen(file, "rb");
+		fp = claws_fopen(file, "rb");
 	}
 	if (fp == NULL) {
 		/* see if we have the old one */
 		debug_print("didn't get %s\n", file);
 		g_free(file);
 		file = get_certificate_path(host, buf, NULL);
-		fp = g_fopen(file, "rb");
+		fp = claws_fopen(file, "rb");
 
 		if (fp) {
 			debug_print("got %s\n", file);
@@ -449,7 +449,7 @@ SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gch
 		gnutls_x509_crt_deinit(tmp_x509);
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 	g_free(file);
 	
 	if (must_rename) {
@@ -531,7 +531,7 @@ static guint check_cert(SSLCertificate *cert)
 	FILE *fp;
 
 	if (claws_ssl_get_cert_file())
-		fp = g_fopen(claws_ssl_get_cert_file(), "r");
+		fp = claws_fopen(claws_ssl_get_cert_file(), "r");
 	else
 		return (guint)-1;
 
@@ -540,10 +540,10 @@ static guint check_cert(SSLCertificate *cert)
 
 	if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
 		debug_print("CA import failed: %s\n", gnutls_strerror(r));
-		fclose(fp);
+		claws_fclose(fp);
 		return (guint)-1;
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	fp = NULL;
 	
 	buf = g_strdup_printf("%d", cert->port);
@@ -554,20 +554,20 @@ static guint check_cert(SSLCertificate *cert)
 		size_t n = 128;
 		char *fingerprint;
 
-		fp = g_fopen(chain_file, "r");
+		fp = claws_fopen(chain_file, "r");
 		if (fp == NULL) {
-			debug_print("fopen %s failed: %s\n", chain_file, g_strerror(errno));
+			debug_print("claws_fopen %s failed: %s\n", chain_file, g_strerror(errno));
 			g_free(chain_file);
 			return (guint)-1;
 		}
 		if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
 			debug_print("chain import failed: %s\n", gnutls_strerror(r));
-			fclose(fp);
+			claws_fclose(fp);
 			g_free(chain_file);
 			return (guint)-1;
 		}
 		g_free(chain_file);
-		fclose(fp);
+		claws_fclose(fp);
 		fp = NULL;
 
 		gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
@@ -670,7 +670,7 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
 
 			g_free(buf);
 
-			fp = g_fopen(file, "wb");
+			fp = claws_fopen(file, "wb");
 			if (fp == NULL) {
 				g_free(file);
 				debug_print("Can't save certificate !\n");
@@ -683,7 +683,7 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
 
 	}
 	if (fp)
-		safe_fclose(fp);
+		claws_safe_fclose(fp);
 }
 
 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, 
@@ -816,12 +816,12 @@ gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
 	gint status;
 
 	if (claws_ssl_get_cert_file()) {
-		FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
+		FILE *fp = claws_fopen(claws_ssl_get_cert_file(), "rb");
 		int r = -errno;
 
 		if (fp) {
 			r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
-			fclose(fp);
+			claws_fclose(fp);
 		}
 
 		if (r < 0)
@@ -861,10 +861,10 @@ gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
 		return NULL;
 	
 	if (is_file_exist(file)) {
-		FILE *fp = g_fopen(file, "r");
+		FILE *fp = claws_fopen(file, "r");
 		if (fp) {
 			x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
-			fclose(fp);
+			claws_fclose(fp);
 			return x509;
 		} else {
 			log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
@@ -884,10 +884,10 @@ gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
 		return NULL;
 	
 	if (is_file_exist(file)) {
-		FILE *fp = g_fopen(file, "r");
+		FILE *fp = claws_fopen(file, "r");
 		if (fp) {
 			key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
-			fclose(fp);
+			claws_fclose(fp);
 			return key;
 		} else {
 			log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
@@ -1037,10 +1037,10 @@ void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gc
 		return;
 
 	if (is_file_exist(file)) {
-		FILE *fp = g_fopen(file, "r");
+		FILE *fp = claws_fopen(file, "r");
 		if (fp) {
 			p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
-			fclose(fp);
+			claws_fclose(fp);
 			if (!p12) {
 				log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
 			}
diff --git a/src/common/tags.c b/src/common/tags.c
index 264f139..4086323 100644
--- a/src/common/tags.c
+++ b/src/common/tags.c
@@ -53,7 +53,7 @@ void tags_read_tags(void)
 			TAGS_RC, NULL);
 	gchar tmp[255];
 	gint id;
-	FILE *fp = g_fopen(file, "rb");
+	FILE *fp = claws_fopen(file, "rb");
 	
 	g_free(file);
 
@@ -69,10 +69,10 @@ void tags_read_tags(void)
 	if (!fp) 
 		return;
 	if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
-		fclose(fp);
+		claws_fclose(fp);
 		return;
 	}
-	while (fgets(tmp, sizeof(tmp), fp) != NULL) {
+	while (claws_fgets(tmp, sizeof(tmp), fp) != NULL) {
 		gchar *sep = strchr(tmp, '\t');
 		gchar *tag_name = sep?(sep+1):NULL;
 		
@@ -89,7 +89,7 @@ void tags_read_tags(void)
 		}
 	}
 	
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 typedef struct _TagWriteData
@@ -121,10 +121,10 @@ void tags_write_tags(void)
 			TAGS_RC, NULL);
 	TagWriteData data;
 
-	FILE *fp = g_fopen(file, "wb");
+	FILE *fp = claws_fopen(file, "wb");
 			
 	if (!fp) {
-		FILE_OP_ERROR(file, "g_fopen");
+		FILE_OP_ERROR(file, "claws_fopen");
 		g_free(file);
 		g_free(file_new);
 		return;
@@ -141,14 +141,14 @@ void tags_write_tags(void)
 	}
 
 	if (data.error) {
-		fclose(fp);
+		claws_fclose(fp);
 		g_free(file);
 		g_free(file_new);
 		return;
 	}
 	
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(file, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(file, "claws_fclose");
 		g_free(file);
 		g_free(file_new);
 		return;
diff --git a/src/common/template.c b/src/common/template.c
index e8db301..8ae5fde 100644
--- a/src/common/template.c
+++ b/src/common/template.c
@@ -40,8 +40,8 @@ static Template *template_load(gchar *filename)
 	gchar buf[BUFFSIZE];
 	gint bytes_read;
 
-	if ((fp = g_fopen(filename, "rb")) == NULL) {
-		FILE_OP_ERROR(filename, "fopen");
+	if ((fp = claws_fopen(filename, "rb")) == NULL) {
+		FILE_OP_ERROR(filename, "claws_fopen");
 		return NULL;
 	}
 
@@ -56,7 +56,7 @@ static Template *template_load(gchar *filename)
 	tmpl->replyto = NULL;
 	tmpl->value = NULL;
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		if (buf[0] == '\n')
 			break;
 		else if (!g_ascii_strncasecmp(buf, "Name:", 5))
@@ -78,19 +78,19 @@ static Template *template_load(gchar *filename)
 	if (!tmpl->name) {
 		g_warning("wrong template format");
 		template_free(tmpl);
-		fclose(fp);
+		claws_fclose(fp);
 		return NULL;
 	}
 
-	if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
-		if (ferror(fp)) {
-			FILE_OP_ERROR(filename, "fread");
+	if ((bytes_read = claws_fread(buf, 1, sizeof(buf), fp)) == 0) {
+		if (claws_ferror(fp)) {
+			FILE_OP_ERROR(filename, "claws_fread");
 			template_free(tmpl);
-			fclose(fp);
+			claws_fclose(fp);
 			return NULL;
 		}
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	tmpl->value = g_strndup(buf, bytes_read);
 
 	return tmpl;
@@ -203,7 +203,7 @@ GSList *template_read_config(void)
 if (!(func)) \
 { \
 	g_warning("Failed to write template to file"); \
-	if (fp) fclose(fp); \
+	if (fp) claws_fclose(fp); \
 	if (new) claws_unlink(new); \
 	g_free(new); \
 	g_free(filename); \
@@ -256,8 +256,8 @@ static void template_write_config(GSList *tmpl_list)
 			new = g_strconcat(filename, ".new", NULL);
 		}
 
-		if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
-			FILE_OP_ERROR(new?new:filename, "fopen");
+		if ((fp = claws_fopen(new?new:filename, "wb")) == NULL) {
+			FILE_OP_ERROR(new?new:filename, "claws_fopen");
 			g_free(new);
 			g_free(filename);
 			return;
@@ -277,14 +277,14 @@ static void template_write_config(GSList *tmpl_list)
 		if (tmpl->replyto && *tmpl->replyto != '\0')
 			TRY(fprintf(fp, "Reply-To: %s\n", tmpl->replyto) > 0);
 
-		TRY(fputs("\n", fp) != EOF);
+		TRY(claws_fputs("\n", fp) != EOF);
 
 		if (tmpl->value && *tmpl->value != '\0') {
-			TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
+			TRY(claws_fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
 		} else {
-			TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
+			TRY(claws_fwrite("", sizeof(gchar), 1, fp) == 1);
 		}
-		TRY_NO_CLOSE(safe_fclose(fp) != EOF);
+		TRY_NO_CLOSE(claws_safe_fclose(fp) != EOF);
 
 		if (new) {
 			if (rename_force(new, filename) < 0) {
diff --git a/src/common/utils.c b/src/common/utils.c
index 977eda4..aaae7b2 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -322,27 +322,27 @@ gint file_strip_crs(const gchar *file)
 	if (file == NULL)
 		goto freeout;
 
-	fp = g_fopen(file, "rb");
+	fp = claws_fopen(file, "rb");
 	if (!fp)
 		goto freeout;
 
-	outfp = g_fopen(out, "wb");
+	outfp = claws_fopen(out, "wb");
 	if (!outfp) {
-		fclose(fp);
+		claws_fclose(fp);
 		goto freeout;
 	}
 
-	while (fgets(buf, sizeof (buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
 		strcrchomp(buf);
-		if (fputs(buf, outfp) == EOF) {
-			fclose(fp);
-			fclose(outfp);
+		if (claws_fputs(buf, outfp) == EOF) {
+			claws_fclose(fp);
+			claws_fclose(outfp);
 			goto unlinkout;
 		}
 	}
 
-	fclose(fp);
-	if (safe_fclose(outfp) == EOF) {
+	claws_fclose(fp);
+	if (claws_safe_fclose(outfp) == EOF) {
 		goto unlinkout;
 	}
 	
@@ -2419,14 +2419,14 @@ gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 
 	gboolean err = FALSE;
 
-	if ((src_fp = g_fopen(src, "rb")) == NULL) {
-		FILE_OP_ERROR(src, "g_fopen");
+	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+		FILE_OP_ERROR(src, "claws_fopen");
 		return -1;
 	}
 
-	if ((dest_fp = g_fopen(dest, "ab")) == NULL) {
-		FILE_OP_ERROR(dest, "g_fopen");
-		fclose(src_fp);
+	if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		claws_fclose(src_fp);
 		return -1;
 	}
 
@@ -2435,25 +2435,25 @@ gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 		g_warning("can't change file mode: %s", dest);
 	}
 
-	while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
-		if (n_read < sizeof(buf) && ferror(src_fp))
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(src_fp))
 			break;
-		if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
 			g_warning("writing to %s failed.", dest);
-			fclose(dest_fp);
-			fclose(src_fp);
+			claws_fclose(dest_fp);
+			claws_fclose(src_fp);
 			claws_unlink(dest);
 			return -1;
 		}
 	}
 
-	if (ferror(src_fp)) {
-		FILE_OP_ERROR(src, "fread");
+	if (claws_ferror(src_fp)) {
+		FILE_OP_ERROR(src, "claws_fread");
 		err = TRUE;
 	}
-	fclose(src_fp);
-	if (fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "fclose");
+	claws_fclose(src_fp);
+	if (claws_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
 		err = TRUE;
 	}
 
@@ -2473,23 +2473,23 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 	gchar *dest_bak = NULL;
 	gboolean err = FALSE;
 
-	if ((src_fp = g_fopen(src, "rb")) == NULL) {
-		FILE_OP_ERROR(src, "g_fopen");
+	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+		FILE_OP_ERROR(src, "claws_fopen");
 		return -1;
 	}
 	if (is_file_exist(dest)) {
 		dest_bak = g_strconcat(dest, ".bak", NULL);
 		if (rename_force(dest, dest_bak) < 0) {
 			FILE_OP_ERROR(dest, "rename");
-			fclose(src_fp);
+			claws_fclose(src_fp);
 			g_free(dest_bak);
 			return -1;
 		}
 	}
 
-	if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "g_fopen");
-		fclose(src_fp);
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		claws_fclose(src_fp);
 		if (dest_bak) {
 			if (rename_force(dest_bak, dest) < 0)
 				FILE_OP_ERROR(dest_bak, "rename");
@@ -2503,13 +2503,13 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 		g_warning("can't change file mode: %s", dest);
 	}
 
-	while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
-		if (n_read < sizeof(buf) && ferror(src_fp))
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(src_fp))
 			break;
-		if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
 			g_warning("writing to %s failed.", dest);
-			fclose(dest_fp);
-			fclose(src_fp);
+			claws_fclose(dest_fp);
+			claws_fclose(src_fp);
 			claws_unlink(dest);
 			if (dest_bak) {
 				if (rename_force(dest_bak, dest) < 0)
@@ -2520,13 +2520,13 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 		}
 	}
 
-	if (ferror(src_fp)) {
-		FILE_OP_ERROR(src, "fread");
+	if (claws_ferror(src_fp)) {
+		FILE_OP_ERROR(src, "claws_fread");
 		err = TRUE;
 	}
-	fclose(src_fp);
-	if (safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "fclose");
+	claws_fclose(src_fp);
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
 		err = TRUE;
 	}
 
@@ -2583,10 +2583,10 @@ gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
 	bytes_left = length;
 	to_read = MIN(bytes_left, sizeof(buf));
 
-	while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
-		if (n_read < to_read && ferror(fp))
+	while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
+		if (n_read < to_read && claws_ferror(fp))
 			break;
-		if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
 			return -1;
 		}
 		bytes_left -= n_read;
@@ -2595,8 +2595,8 @@ gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
 		to_read = MIN(bytes_left, sizeof(buf));
 	}
 
-	if (ferror(fp)) {
-		perror("fread");
+	if (claws_ferror(fp)) {
+		perror("claws_fread");
 		return -1;
 	}
 
@@ -2608,8 +2608,8 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
 	FILE *dest_fp;
 	gboolean err = FALSE;
 
-	if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "g_fopen");
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
 		return -1;
 	}
 
@@ -2621,8 +2621,8 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
 	if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
 		err = TRUE;
 
-	if (safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "fclose");
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
 		err = TRUE;
 	}
 
@@ -2682,14 +2682,14 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
 	if (src == NULL || dest == NULL)
 		return -1;
 
-	if ((src_fp = g_fopen(src, "rb")) == NULL) {
-		FILE_OP_ERROR(src, "g_fopen");
+	if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+		FILE_OP_ERROR(src, "claws_fopen");
 		return -1;
 	}
 
-	if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "g_fopen");
-		fclose(src_fp);
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
+		claws_fclose(src_fp);
 		return -1;
 	}
 
@@ -2698,7 +2698,7 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
 		g_warning("can't change file mode: %s", dest);
 	}
 
-	while (fgets(buf, sizeof(buf), src_fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
 		gint r = 0;
 
 		len = strlen(buf);
@@ -2707,40 +2707,40 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
 
 		if (buf[len - 1] != '\n') {
 			last_linebreak = TRUE;
-			r = fputs(buf, dest_fp);
+			r = claws_fputs(buf, dest_fp);
 		} else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
-			r = fputs(buf, dest_fp);
+			r = claws_fputs(buf, dest_fp);
 		} else {
 			if (len > 1) {
-				r = fwrite(buf, 1, len - 1, dest_fp);
+				r = claws_fwrite(buf, 1, len - 1, dest_fp);
 				if (r != (len -1))
 					r = EOF;
 			}
 			if (r != EOF)
-				r = fputs("\r\n", dest_fp);
+				r = claws_fputs("\r\n", dest_fp);
 		}
 
 		if (r == EOF) {
 			g_warning("writing to %s failed.", dest);
-			fclose(dest_fp);
-			fclose(src_fp);
+			claws_fclose(dest_fp);
+			claws_fclose(src_fp);
 			claws_unlink(dest);
 			return -1;
 		}
 	}
 
 	if (last_linebreak == TRUE) {
-		if (fputs("\r\n", dest_fp) == EOF)
+		if (claws_fputs("\r\n", dest_fp) == EOF)
 			err = TRUE;
 	}
 
-	if (ferror(src_fp)) {
-		FILE_OP_ERROR(src, "fgets");
+	if (claws_ferror(src_fp)) {
+		FILE_OP_ERROR(src, "claws_fgets");
 		err = TRUE;
 	}
-	fclose(src_fp);
-	if (safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "fclose");
+	claws_fclose(src_fp);
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
 		err = TRUE;
 	}
 
@@ -2802,7 +2802,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
 	str = g_string_new(NULL);
 
 	/* output header part */
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		strretchomp(buf);
 		if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
 			gint next;
@@ -2815,7 +2815,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
 					ungetc(next, fp);
 					break;
 				}
-				if (fgets(buf, sizeof(buf), fp) == NULL)
+				if (claws_fgets(buf, sizeof(buf), fp) == NULL)
 					break;
 			}
 		} else {
@@ -2827,7 +2827,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
 	}
 
 	/* output body part */
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		strretchomp(buf);
 		if (buf[0] == '.')
 			g_string_append_c(str, '.');
@@ -2926,7 +2926,7 @@ FILE *my_tmpfile(void)
 	
 #endif
 
-	fp = fdopen(fd, "w+b");
+	fp = claws_fdopen(fd, "w+b");
 	if (!fp)
 		close(fd);
 	else {
@@ -2944,7 +2944,7 @@ FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
 	fd = g_mkstemp(*filename);
 	if (fd < 0)
 		return NULL;
-	return fdopen(fd, "w+");
+	return claws_fdopen(fd, "w+");
 }
 
 FILE *str_open_as_stream(const gchar *str)
@@ -2963,9 +2963,9 @@ FILE *str_open_as_stream(const gchar *str)
 	len = strlen(str);
 	if (len == 0) return fp;
 
-	if (fwrite(str, 1, len, fp) != len) {
-		FILE_OP_ERROR("str_open_as_stream", "fwrite");
-		fclose(fp);
+	if (claws_fwrite(str, 1, len, fp) != len) {
+		FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");
+		claws_fclose(fp);
 		return NULL;
 	}
 
@@ -2981,26 +2981,26 @@ gint str_write_to_file(const gchar *str, const gchar *file)
 	cm_return_val_if_fail(str != NULL, -1);
 	cm_return_val_if_fail(file != NULL, -1);
 
-	if ((fp = g_fopen(file, "wb")) == NULL) {
-		FILE_OP_ERROR(file, "g_fopen");
+	if ((fp = claws_fopen(file, "wb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return -1;
 	}
 
 	len = strlen(str);
 	if (len == 0) {
-		fclose(fp);
+		claws_fclose(fp);
 		return 0;
 	}
 
-	if (fwrite(str, 1, len, fp) != len) {
-		FILE_OP_ERROR(file, "fwrite");
-		fclose(fp);
+	if (claws_fwrite(str, 1, len, fp) != len) {
+		FILE_OP_ERROR(file, "claws_fwrite");
+		claws_fclose(fp);
 		claws_unlink(file);
 		return -1;
 	}
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(file, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(file, "claws_fclose");
 		claws_unlink(file);
 		return -1;
 	}
@@ -3019,14 +3019,14 @@ static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
 
 	array = g_byte_array_new();
 
-	while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
-		if (n_read < sizeof(buf) && ferror(fp))
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(fp))
 			break;
 		g_byte_array_append(array, buf, n_read);
 	}
 
-	if (ferror(fp)) {
-		FILE_OP_ERROR("file stream", "fread");
+	if (claws_ferror(fp)) {
+		FILE_OP_ERROR("file stream", "claws_fread");
 		g_byte_array_free(array, TRUE);
 		return NULL;
 	}
@@ -3073,7 +3073,7 @@ static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
 	}
 
 #ifdef G_OS_WIN32
-	fp = g_fopen (file, "rb");
+	fp = claws_fopen (file, "rb");
 	if (fp == NULL) {
 		FILE_OP_ERROR(file, "open");
 		return NULL;
@@ -3114,18 +3114,18 @@ static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
 	}
 	
 	/* get the FILE pointer */
-	fp = fdopen(fd, "rb");
+	fp = claws_fdopen(fd, "rb");
 
 	if (fp == NULL) {
-		FILE_OP_ERROR(file, "fdopen");
-		close(fd); /* if fp isn't NULL, we'll use fclose instead! */
+		FILE_OP_ERROR(file, "claws_fdopen");
+		close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
 		return NULL;
 	}
 #endif
 
 	str = file_read_stream_to_str_full(fp, recode);
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	return str;
 }
@@ -4429,12 +4429,12 @@ gchar *make_http_string(const gchar *bp, const gchar *ep)
 
 static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type, const gchar *file_to_open)
 {
-	FILE *fp = g_fopen(path, "rb");
+	FILE *fp = claws_fopen(path, "rb");
 	gchar buf[BUFFSIZE];
 	gchar *result = NULL;
 	if (!fp)
 		return NULL;
-	while (fgets(buf, sizeof (buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
 		gchar **parts = g_strsplit(buf, ";", 3);
 		gchar *trimmed = parts[0];
 		while (trimmed[0] == ' ' || trimmed[0] == '\t')
@@ -4493,7 +4493,7 @@ static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type,
 				trimmed[strlen(trimmed)-1] = '\0';
 			result = g_strdup(trimmed);
 			g_strfreev(parts);
-			fclose(fp);
+			claws_fclose(fp);
 			if (needsterminal) {
 				gchar *tmp = g_strdup_printf("xterm -e %s", result);
 				g_free(result);
@@ -4503,7 +4503,7 @@ static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type,
 		}
 		g_strfreev(parts);
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	return NULL;
 }
 gchar *mailcap_get_command_for_type(const gchar *type, const gchar *file_to_open)
@@ -4526,13 +4526,13 @@ void mailcap_update_default(const gchar *type, const gchar *command)
 	gchar *path = NULL, *outpath = NULL;
 	path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
 	outpath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap.new", NULL);
-	FILE *fp = g_fopen(path, "rb");
+	FILE *fp = claws_fopen(path, "rb");
 	FILE *outfp = NULL;
 	gchar buf[BUFFSIZE];
 	gboolean err = FALSE;
 
 	if (!fp) {
-		fp = g_fopen(path, "a");
+		fp = claws_fopen(path, "a");
 		if (!fp) {
 			g_warning("failed to create file %s", path);
 			g_free(path);
@@ -4548,15 +4548,15 @@ void mailcap_update_default(const gchar *type, const gchar *command)
 		}
 	}
 
-	outfp = g_fopen(outpath, "wb");
+	outfp = claws_fopen(outpath, "wb");
 	if (!outfp) {
 		g_warning("failed to create file %s", outpath);
 		g_free(path);
 		g_free(outpath);
-		fclose(fp);
+		claws_fclose(fp);
 		return;
 	}
-	while (fp && fgets(buf, sizeof (buf), fp) != NULL) {
+	while (fp && claws_fgets(buf, sizeof (buf), fp) != NULL) {
 		gchar **parts = g_strsplit(buf, ";", 3);
 		gchar *trimmed = parts[0];
 		while (trimmed[0] == ' ')
@@ -4569,7 +4569,7 @@ void mailcap_update_default(const gchar *type, const gchar *command)
 			continue;
 		}
 		else {
-			if(fputs(buf, outfp) == EOF) {
+			if(claws_fputs(buf, outfp) == EOF) {
 				err = TRUE;
 				break;
 			}
@@ -4580,9 +4580,9 @@ void mailcap_update_default(const gchar *type, const gchar *command)
 		err = TRUE;
 
 	if (fp)
-		fclose(fp);
+		claws_fclose(fp);
 
-	if (safe_fclose(outfp) == EOF)
+	if (claws_safe_fclose(outfp) == EOF)
 		err = TRUE;
 		
 	if (!err)
@@ -4655,10 +4655,10 @@ gboolean file_is_email (const gchar *filename)
 	gint score = 0;
 	if (filename == NULL)
 		return FALSE;
-	if ((fp = g_fopen(filename, "rb")) == NULL)
+	if ((fp = claws_fopen(filename, "rb")) == NULL)
 		return FALSE;
 	while (i < 60 && score < 3
-	       && fgets(buffer, sizeof (buffer), fp) != NULL) {
+	       && claws_fgets(buffer, sizeof (buffer), fp) != NULL) {
 		if (!strncmp(buffer, "From:", strlen("From:")))
 			score++;
 		else if (!strncmp(buffer, "Date:", strlen("Date:")))
@@ -4669,7 +4669,7 @@ gboolean file_is_email (const gchar *filename)
 			score++;
 		i++;
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	return (score >= 3);
 }
 
diff --git a/src/common/xml.c b/src/common/xml.c
index 45fcd30..50cc9a5 100644
--- a/src/common/xml.c
+++ b/src/common/xml.c
@@ -17,6 +17,11 @@
  * 
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -25,6 +30,7 @@
 #include "xml.h"
 #include "utils.h"
 #include "codeconv.h"
+#include "claws_io.h"
 
 #define SPARSE_MEMORY
 /* if this is defined all attr.names and tag.names are stored
@@ -80,7 +86,7 @@ XMLFile *xml_open_file(const gchar *path)
 
 	newfile = g_new(XMLFile, 1);
 
-	newfile->fp = g_fopen(path, "rb");
+	newfile->fp = claws_fopen(path, "rb");
 	if (!newfile->fp) {
 		g_free(newfile);
 		return NULL;
@@ -97,6 +103,8 @@ XMLFile *xml_open_file(const gchar *path)
 	newfile->level = 0;
 	newfile->is_empty_element = FALSE;
 
+	newfile->path = g_strdup(path);
+
 	return newfile;
 }
 
@@ -104,12 +112,13 @@ void xml_close_file(XMLFile *file)
 {
 	cm_return_if_fail(file != NULL);
 
-	if (file->fp) fclose(file->fp);
+	if (file->fp) claws_fclose(file->fp);
 
 	g_string_free(file->buf, TRUE);
 
 	g_free(file->dtd);
 	g_free(file->encoding);
+	g_free(file->path);
 
 	while (file->tag_stack != NULL)
 		xml_pop_tag(file);
@@ -126,7 +135,7 @@ static GNode *xml_build_tree(XMLFile *file, GNode *parent, guint level)
 	while (xml_parse_next_tag(file) == 0) {
 		if (file->level < level) break;
 		if (file->level == level) {
-			g_warning("xml_build_tree(): Parse error");
+			g_warning("xml_build_tree(): Parse error in %s", file->path);
 			break;
 		}
 
@@ -191,7 +200,7 @@ gint xml_get_dtd(XMLFile *file)
 			file->need_codeconv = FALSE;
 		}
 	} else {
-		g_warning("Can't get XML DTD");
+		g_warning("Can't get XML DTD in %s", file->path);
 		return -1;
 	}
 
@@ -214,7 +223,7 @@ next:
 	}
 
 	if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
-		g_warning("xml_parse_next_tag(): Can't parse next tag");
+		g_warning("xml_parse_next_tag(): Can't parse next tag  in %s", file->path);
 		return -1;
 	}
 
@@ -223,7 +232,7 @@ next:
 	/* end-tag */
 	if (buf[0] == '/') {
 		if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
-			g_warning("xml_parse_next_tag(): Tag name mismatch: %s (%s)", buf, xml_get_current_tag(file)->tag);
+			g_warning("xml_parse_next_tag(): Tag name mismatch in %s : %s (%s)", file->path, buf, xml_get_current_tag(file)->tag);
 			return -1;
 		}
 		xml_pop_tag(file);
@@ -245,7 +254,7 @@ next:
 	}
 	
 	if (strlen(buf) == 0) {
-		g_warning("xml_parse_next_tag(): Tag name is empty");
+		g_warning("xml_parse_next_tag(): Tag name is empty in %s", file->path);
 		return -1;
 	}
 
@@ -287,7 +296,7 @@ next:
 		while (g_ascii_isspace(*bufp)) bufp++;
 		attr_name = bufp;
 		if ((p = strchr(attr_name, '=')) == NULL) {
-			g_warning("xml_parse_next_tag(): Syntax error in tag (a) %s", attr_name);
+			g_warning("xml_parse_next_tag(): Syntax error in %s, tag (a) %s", file->path, attr_name);
 			return -1;
 		}
 		bufp = p;
@@ -295,14 +304,14 @@ next:
 		while (g_ascii_isspace(*bufp)) bufp++;
 
 		if (*bufp != '"' && *bufp != '\'') {
-			g_warning("xml_parse_next_tag(): Syntax error in tag (b) %s", bufp);
+			g_warning("xml_parse_next_tag(): Syntax error in %s, tag (b) %s", file->path, bufp);
 			return -1;
 		}
 		quote = *bufp;
 		bufp++;
 		attr_value = bufp;
 		if ((p = strchr(attr_value, quote)) == NULL) {
-			g_warning("xml_parse_next_tag(): Syntax error in tag (c) %s", attr_value);
+			g_warning("xml_parse_next_tag(): Syntax error in %s, tag (c) %s", file->path, attr_value);
 			return -1;
 		}
 		bufp = p;
@@ -414,7 +423,7 @@ static gint xml_read_line(XMLFile *file)
 	gchar buf[XMLBUFSIZE];
 	gint index;
 
-	if (fgets(buf, sizeof(buf), file->fp) == NULL)
+	if (claws_fgets(buf, sizeof(buf), file->fp) == NULL)
 		return -1;
 
 	index = file->bufp - file->buf->str;
@@ -581,22 +590,22 @@ gint xml_file_put_escape_str(FILE *fp, const gchar *str)
 	for (p = str; *p != '\0'; p++) {
 		switch (*p) {
 		case '<':
-			result = fputs("<", fp);
+			result = claws_fputs("<", fp);
 			break;
 		case '>':
-			result = fputs(">", fp);
+			result = claws_fputs(">", fp);
 			break;
 		case '&':
-			result = fputs("&", fp);
+			result = claws_fputs("&", fp);
 			break;
 		case '\'':
-			result = fputs("'", fp);
+			result = claws_fputs("'", fp);
 			break;
 		case '\"':
-			result = fputs(""", fp);
+			result = claws_fputs(""", fp);
 			break;
 		default:
-			result = fputc(*p, fp);
+			result = claws_fputc(*p, fp);
 		}
 	}
 
@@ -695,7 +704,7 @@ static int xml_write_tree_recursive(GNode *node, FILE *fp)
 
 	depth = g_node_depth(node) - 1;
 	for (i = 0; i < depth; i++)
-		TRY(fputs("    ", fp) != EOF);
+		TRY(claws_fputs("    ", fp) != EOF);
 
 	tag = ((XMLNode *) node->data)->tag;
 
@@ -706,13 +715,13 @@ static int xml_write_tree_recursive(GNode *node, FILE *fp)
 
 		TRY(fprintf(fp, " %s=\"", attr->name) > 0);
 		TRY(xml_file_put_escape_str(fp, attr->value) == 0);
-		TRY(fputs("\"", fp) != EOF);
+		TRY(claws_fputs("\"", fp) != EOF);
 		
 	}
 
 	if (node->children) {
 		GNode *child;
-		TRY(fputs(">\n", fp) != EOF);
+		TRY(claws_fputs(">\n", fp) != EOF);
 
 		child = node->children;
 		while (child) {
@@ -724,10 +733,10 @@ static int xml_write_tree_recursive(GNode *node, FILE *fp)
 		}
 
 		for (i = 0; i < depth; i++)
-			TRY(fputs("    ", fp) != EOF);
+			TRY(claws_fputs("    ", fp) != EOF);
 		TRY(fprintf(fp, "</%s>\n", tag->tag) > 0);
 	} else
-		TRY(fputs(" />\n", fp) != EOF);
+		TRY(claws_fputs(" />\n", fp) != EOF);
 	
 	return 0;
 }
diff --git a/src/common/xml.h b/src/common/xml.h
index 225a12e..6ca9059 100644
--- a/src/common/xml.h
+++ b/src/common/xml.h
@@ -51,6 +51,7 @@ struct _XMLNode
 struct _XMLFile
 {
 	FILE *fp;
+	gchar *path;
 
 	GString *buf;
 	gchar *bufp;
diff --git a/src/common/xmlprops.c b/src/common/xmlprops.c
index 9aadc3b..1fc1c28 100644
--- a/src/common/xmlprops.c
+++ b/src/common/xmlprops.c
@@ -30,6 +30,10 @@
  * ***********************************************************************
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
 
 #include <glib.h>
 #include <stdio.h>
@@ -41,6 +45,7 @@
 #include "mgutils.h"
 #include "xmlprops.h"
 #include "utils.h"
+#include "claws_io.h"
 
 /* Element tag names */
 #define XMLS_ELTAG_PROP_LIST     "property-list"
@@ -126,12 +131,12 @@ void xmlprops_free( XmlProperty *props ) {
 static int xmlprops_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
 	gint i;
 	for( i = 0; i < lvl; i++ ) {
-		if(fputs( "  ", fp ) == EOF)
+		if(claws_fputs( "  ", fp ) == EOF)
 			return -1;
 	}
-	if(fputs( "<", fp ) == EOF)
+	if(claws_fputs( "<", fp ) == EOF)
 		return -1;
-	if(fputs( name, fp ) == EOF)
+	if(claws_fputs( name, fp ) == EOF)
 		return -1;
 	
 	return 0;
@@ -140,29 +145,29 @@ static int xmlprops_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
 static int xmlprops_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
 	gint i;
 	for( i = 0; i < lvl; i++ ) {
-		if(fputs( "  ", fp ) == EOF)
+		if(claws_fputs( "  ", fp ) == EOF)
 			return -1;
 	}
-	if(fputs( "</", fp ) == EOF)
+	if(claws_fputs( "</", fp ) == EOF)
 		return -1;
-	if(fputs( name, fp ) == EOF)
+	if(claws_fputs( name, fp ) == EOF)
 		return -1;
-	if(fputs( ">\n", fp ) == EOF)
+	if(claws_fputs( ">\n", fp ) == EOF)
 		return -1;
 	
 	return 0;
 }
 
 static int xmlprops_write_attr( FILE *fp, gchar *name, gchar *value ) {
-	if(fputs( " ", fp ) == EOF)
+	if(claws_fputs( " ", fp ) == EOF)
 		return -1;
-	if(fputs( name, fp ) == EOF)
+	if(claws_fputs( name, fp ) == EOF)
 		return -1;
-	if(fputs( "=\"", fp ) == EOF)
+	if(claws_fputs( "=\"", fp ) == EOF)
 		return -1;
 	if(xml_file_put_escape_str( fp, value ) < 0)
 		return -1;
-	if(fputs( "\"", fp ) == EOF)
+	if(claws_fputs( "\"", fp ) == EOF)
 		return -1;
 	
 	return 0;
@@ -177,7 +182,7 @@ static void xmlprops_write_vis( gpointer key, gpointer value, gpointer d ) {
 		data->error = 1;
 	if(xmlprops_write_attr( data->fp, XMLS_ATTAG_VALUE, value ) < 0)
 		data->error = 1;
-	if(fputs( " />\n", data->fp ) == EOF)
+	if(claws_fputs( " />\n", data->fp ) == EOF)
 		data->error = 1;
 }
 
@@ -200,7 +205,7 @@ static gint xmlprops_write_to( XmlProperty *props, const gchar *fileSpec ) {
 			goto revert;
 		if(xmlprops_write_elem_s( fp, 0, XMLS_ELTAG_PROP_LIST ) < 0)
 			goto revert;
-		if(fputs( ">\n", fp ) == EOF)
+		if(claws_fputs( ">\n", fp ) == EOF)
 			goto revert;
 
 		/* Output all properties */
diff --git a/src/compose.c b/src/compose.c
index 8e0fd63..3e348d6 100644
--- a/src/compose.c
+++ b/src/compose.c
@@ -2450,12 +2450,12 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
 		gboolean prev_autowrap;
 		GtkTextBuffer *buffer;
 		BLOCK_WRAP();
-		while (fgets(buf, sizeof(buf), fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 			strcrchomp(buf);
 			gtk_text_buffer_insert(textbuf, &iter, buf, -1);
 		}
 		UNBLOCK_WRAP();
-		fclose(fp);
+		claws_fclose(fp);
 	}
 	
 	compose_attach_parts(compose, msginfo);
@@ -2893,7 +2893,7 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
 
 	if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
 	procheader_get_header_fields(fp, hentry);
-	fclose(fp);
+	claws_fclose(fp);
 
 	if (hentry[H_REPLY_TO].body != NULL) {
 		if (hentry[H_REPLY_TO].body[0] != '\0') {
@@ -3018,7 +3018,7 @@ static gint compose_parse_manual_headers(Compose *compose, MsgInfo *msginfo, Hea
 
 	if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
 	procheader_get_header_fields(fp, entries);
-	fclose(fp);
+	claws_fclose(fp);
 
 	he = entries;
 	while (he != NULL && he->name != NULL) {
@@ -3692,8 +3692,8 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
 	}
 
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return COMPOSE_INSERT_READ_ERROR;
 	}
 
@@ -3712,7 +3712,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
 	cur_encoding = conv_get_locale_charset_str_no_utf8();
 
 	file_contents = g_string_new("");
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar *str;
 
 		if (g_utf8_validate(buf, -1, NULL) == TRUE)
@@ -3756,7 +3756,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
 	}
 
 	g_string_free(file_contents, TRUE);
-	fclose(fp);
+	claws_fclose(fp);
 
 	return result;
 }
@@ -3808,11 +3808,11 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
 			return FALSE;
 		}
 	}
-	if ((fp = g_fopen(file, "rb")) == NULL) {
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
 		alertpanel_error(_("Can't read %s."), filename);
 		return FALSE;
 	}
-	fclose(fp);
+	claws_fclose(fp);
 
 	ainfo = g_new0(AttachInfo, 1);
 	auto_ainfo = g_auto_pointer_new_with_free
@@ -5552,7 +5552,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
 		return -1;
 
 	/* separator between header and body */
-	err |= (fputs("\n", fp) == EOF);
+	err |= (claws_fputs("\n", fp) == EOF);
 
 	return (err ? -1:0);
 }
@@ -5585,8 +5585,8 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
 		};
 	gint ret = 0;
 
-	if ((fp = g_fopen(compose->redirect_filename, "rb")) == NULL) {
-		FILE_OP_ERROR(compose->redirect_filename, "fopen");
+	if ((fp = claws_fopen(compose->redirect_filename, "rb")) == NULL) {
+		FILE_OP_ERROR(compose->redirect_filename, "claws_fopen");
 		return -1;
 	}
 
@@ -5604,7 +5604,7 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
 			buf = NULL;
 			continue;
 		}
-		if (fputs(buf, fdest) == -1) {
+		if (claws_fputs(buf, fdest) == -1) {
 			g_free(buf);
 			buf = NULL;
 			goto error;
@@ -5613,7 +5613,7 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
 		if (!prefs_common.redirect_keep_from) {
 			if (g_ascii_strncasecmp(buf, "From:",
 					  strlen("From:")) == 0) {
-				err |= (fputs(" (by way of ", fdest) == EOF);
+				err |= (claws_fputs(" (by way of ", fdest) == EOF);
 				if (compose->account->name
 				    && *compose->account->name) {
 					gchar buffer[BUFFSIZE];
@@ -5629,13 +5629,13 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
 				} else
 					err |= (fprintf(fdest, "%s",
 						compose->account->address) < 0);
-				err |= (fputs(")", fdest) == EOF);
+				err |= (claws_fputs(")", fdest) == EOF);
 			}
 		}
 
 		g_free(buf);
 		buf = NULL;
-		if (fputs("\n", fdest) == -1)
+		if (claws_fputs("\n", fdest) == -1)
 			goto error;
 	}
 
@@ -5645,17 +5645,17 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
 	if (compose_redirect_write_headers(compose, fdest))
 		goto error;
 
-	while ((len = fread(rewrite_buf, sizeof(gchar), sizeof(rewrite_buf), fp)) > 0) {
-		if (fwrite(rewrite_buf, sizeof(gchar), len, fdest) != len)
+	while ((len = claws_fread(rewrite_buf, sizeof(gchar), sizeof(rewrite_buf), fp)) > 0) {
+		if (claws_fwrite(rewrite_buf, sizeof(gchar), len, fdest) != len)
 			goto error;
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	return 0;
 
 error:
-	fclose(fp);
+	claws_fclose(fp);
 
 	return -1;
 }
@@ -5917,7 +5917,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
 				debug_print("saving sent message unencrypted...\n");
 				FILE *tmpfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
 				if (tmpfp) {
-					fclose(tmpfp);
+					claws_fclose(tmpfp);
 
 					/* fp now points to a file with headers written,
 					 * let's make a copy. */
@@ -5928,9 +5928,9 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
 					g_free(content);
 
 					/* Now write the unencrypted body. */
-					if ((tmpfp = g_fopen(tmp_enc_file, "a")) != NULL) {
+					if ((tmpfp = claws_fopen(tmp_enc_file, "a")) != NULL) {
 						procmime_write_mimeinfo(mimemsg, tmpfp);
-						fclose(tmpfp);
+						claws_fclose(tmpfp);
 
 						outbox = folder_find_item_from_identifier(compose_get_save_to(compose));
 						if (!outbox)
@@ -5968,8 +5968,8 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
 	size_t len;
 	gchar *chars, *tmp;
 
-	if ((fp = g_fopen(file, "wb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "wb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return -1;
 	}
 
@@ -5989,24 +5989,24 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
 
 	g_free(tmp);
 	if (!chars) {
-		fclose(fp);
+		claws_fclose(fp);
 		claws_unlink(file);
 		return -1;
 	}
 	/* write body */
 	len = strlen(chars);
-	if (fwrite(chars, sizeof(gchar), len, fp) != len) {
-		FILE_OP_ERROR(file, "fwrite");
+	if (claws_fwrite(chars, sizeof(gchar), len, fp) != len) {
+		FILE_OP_ERROR(file, "claws_fwrite");
 		g_free(chars);
-		fclose(fp);
+		claws_fclose(fp);
 		claws_unlink(file);
 		return -1;
 	}
 
 	g_free(chars);
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(file, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(file, "claws_fclose");
 		claws_unlink(file);
 		return -1;
 	}
@@ -6136,8 +6136,8 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 	tmp = g_strdup_printf("%s%cqueue.%p%08x", get_tmp_dir(),
 			      G_DIR_SEPARATOR, compose, (guint) rand());
 	debug_print("queuing to %s\n", tmp);
-	if ((fp = g_fopen(tmp, "w+b")) == NULL) {
-		FILE_OP_ERROR(tmp, "fopen");
+	if ((fp = claws_fopen(tmp, "w+b")) == NULL) {
+		FILE_OP_ERROR(tmp, "claws_fopen");
 		g_free(tmp);
 		return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
 	}
@@ -6199,7 +6199,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 		err |= (fprintf(fp, "X-Claws-Sign:%d\n", compose->use_signing) < 0);
 		if (compose->use_encryption) {
 			if (!compose_warn_encryption(compose)) {
-				fclose(fp);
+				claws_fclose(fp);
 				claws_unlink(tmp);
 				g_free(tmp);
 				return COMPOSE_QUEUE_ERROR_NO_MSG;
@@ -6224,7 +6224,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 				 * key selection */
 				if (err == TRUE)
 					g_warning("failed to write queue message");
-				fclose(fp);
+				claws_fclose(fp);
 				claws_unlink(tmp);
 				g_free(tmp);
 				return COMPOSE_QUEUE_ERROR_NO_ENCRYPTION_KEY;
@@ -6277,7 +6277,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 
 	if (compose->redirect_filename != NULL) {
 		if (compose_redirect_write_to_file(compose, fp) < 0) {
-			fclose(fp);
+			claws_fclose(fp);
 			claws_unlink(tmp);
 			g_free(tmp);
 			return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
@@ -6285,7 +6285,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 	} else {
 		gint result = 0;
 		if ((result = compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_SEND, TRUE)) < 0) {
-			fclose(fp);
+			claws_fclose(fp);
 			claws_unlink(tmp);
 			g_free(tmp);
 			return result;
@@ -6293,13 +6293,13 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 	}
 	if (err == TRUE) {
 		g_warning("failed to write queue message");
-		fclose(fp);
+		claws_fclose(fp);
 		claws_unlink(tmp);
 		g_free(tmp);
 		return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
 	}
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(tmp, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(tmp, "claws_fclose");
 		claws_unlink(tmp);
 		g_free(tmp);
 		return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
@@ -7070,11 +7070,11 @@ void compose_add_extra_header_entries(GtkListStore *model)
 
 	if (extra_headers == NULL) {
 		exhrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "extraheaderrc", NULL);
-		if ((exh = g_fopen(exhrc, "rb")) == NULL) {
+		if ((exh = claws_fopen(exhrc, "rb")) == NULL) {
 			debug_print("extra headers file not found\n");
 			goto extra_headers_done;
 		}
-		while (fgets(buf, BUFFSIZE, exh) != NULL) {
+		while (claws_fgets(buf, BUFFSIZE, exh) != NULL) {
 			lastc = strlen(buf) - 1;        /* remove trailing control chars */
 			while (lastc >= 0 && buf[lastc] != ':')
 				buf[lastc--] = '\0';
@@ -7092,7 +7092,7 @@ void compose_add_extra_header_entries(GtkListStore *model)
 					g_message("invalid extra header line: %s\n", buf);
 			}
 		}
-		fclose(exh);
+		claws_fclose(exh);
 extra_headers_done:
 		g_free(exhrc);
 		extra_headers = g_slist_prepend(extra_headers, g_strdup("")); /* end of list */
@@ -10276,12 +10276,12 @@ static void compose_register_draft(MsgInfo *info)
 {
 	gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 				      DRAFTED_AT_EXIT, NULL);
-	FILE *fp = g_fopen(filepath, "ab");
+	FILE *fp = claws_fopen(filepath, "ab");
 	
 	if (fp) {
 		fprintf(fp, "%s\t%d\n", folder_item_get_identifier(info->folder), 
 				info->msgnum);
-		fclose(fp);
+		claws_fclose(fp);
 	}
 		
 	g_free(filepath);	
@@ -10322,8 +10322,8 @@ gboolean compose_draft (gpointer data, guint action)
 
 	tmp = g_strdup_printf("%s%cdraft.%p", get_tmp_dir(),
 			      G_DIR_SEPARATOR, compose);
-	if ((fp = g_fopen(tmp, "wb")) == NULL) {
-		FILE_OP_ERROR(tmp, "fopen");
+	if ((fp = claws_fopen(tmp, "wb")) == NULL) {
+		FILE_OP_ERROR(tmp, "claws_fopen");
 		goto warn_err;
 	}
 
@@ -10389,15 +10389,15 @@ gboolean compose_draft (gpointer data, guint action)
 	err |= (fprintf(fp, "X-Claws-End-Special-Headers: 1\n") < 0);
 
 	if (err) {
-		fclose(fp);
+		claws_fclose(fp);
 		goto warn_err;
 	}
 
 	if (compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_STORE, action != COMPOSE_AUTO_SAVE) < 0) {
-		fclose(fp);
+		claws_fclose(fp);
 		goto warn_err;
 	}
-	if (safe_fclose(fp) == EOF) {
+	if (claws_safe_fclose(fp) == EOF) {
 		goto warn_err;
 	}
 	
@@ -10567,11 +10567,11 @@ void compose_reopen_exit_drafts(void)
 {
 	gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 				      DRAFTED_AT_EXIT, NULL);
-	FILE *fp = g_fopen(filepath, "rb");
+	FILE *fp = claws_fopen(filepath, "rb");
 	gchar buf[1024];
 	
 	if (fp) {
-		while (fgets(buf, sizeof(buf), fp)) {
+		while (claws_fgets(buf, sizeof(buf), fp)) {
 			gchar **parts = g_strsplit(buf, "\t", 2);
 			const gchar *folder = parts[0];
 			int msgnum = parts[1] ? atoi(parts[1]):-1;
@@ -10584,7 +10584,7 @@ void compose_reopen_exit_drafts(void)
 			}
 			g_strfreev(parts);
 		}	
-		fclose(fp);
+		claws_fclose(fp);
 	}	
 	g_free(filepath);
 	compose_clear_exit_drafts();
diff --git a/src/enriched.c b/src/enriched.c
index e149047..b8b0eb3 100644
--- a/src/enriched.c
+++ b/src/enriched.c
@@ -17,6 +17,11 @@
  * 
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -24,6 +29,7 @@
 
 #include "enriched.h"
 #include "utils.h"
+#include "claws_io.h"
 
 #define ERTFBUFSIZE	8192
 
@@ -114,7 +120,7 @@ static ERTFState ertf_read_line(ERTFParser *parser)
 	gchar buf2[ERTFBUFSIZE];
 	gint index;
 
-	if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
+	if (claws_fgets(buf, sizeof(buf), parser->fp) == NULL) {
 		parser->state = ERTF_EOF;
 		return ERTF_EOF;
 	}
diff --git a/src/etpan/imap-thread.c b/src/etpan/imap-thread.c
index 48823e1..4a11f4f 100644
--- a/src/etpan/imap-thread.c
+++ b/src/etpan/imap-thread.c
@@ -2626,27 +2626,27 @@ static void fetch_content_run(struct etpan_thread_op * op)
 			goto free;
 		}
 		
-		f = fdopen(fd, "wb");
+		f = claws_fdopen(fd, "wb");
 		if (f == NULL) {
 			result->error = MAILIMAP_ERROR_FETCH;
 			goto close;
 		}
 		
-		r = fwrite(content, 1, content_size, f);
+		r = claws_fwrite(content, 1, content_size, f);
 		if (r < content_size) {
 			result->error = MAILIMAP_ERROR_FETCH;
-			goto fclose;
+			goto do_fclose;
 		}
 		
-		r = safe_fclose(f);
+		r = claws_safe_fclose(f);
 		if (r == EOF) {
 			result->error = MAILIMAP_ERROR_FETCH;
 			goto unlink;
 		}
 		goto free;
 		
-	fclose:
-		fclose(f);
+	do_fclose:
+		claws_fclose(f);
 		goto unlink;
 	close:
 		close(fd);
diff --git a/src/exporthtml.c b/src/exporthtml.c
index d0bfefd..5dbec5b 100644
--- a/src/exporthtml.c
+++ b/src/exporthtml.c
@@ -256,12 +256,12 @@ void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
 	FILE *cssFile;
 
-	cssFile = g_fopen( fileSpec, "rb" );
+	cssFile = claws_fopen( fileSpec, "rb" );
 	if( cssFile ) {
-		fclose( cssFile );
+		claws_fclose( cssFile );
 		return MGU_SUCCESS;
 	}
-	cssFile = g_fopen( fileSpec, "wb" );
+	cssFile = claws_fopen( fileSpec, "wb" );
 	if( ! cssFile ) {
 		return MGU_OPEN_FILE;
 	}
@@ -302,7 +302,7 @@ static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
 	fprintf( cssFile, ".tab-attr {\n" );
 	fprintf( cssFile, "}\n" );
 
-	safe_fclose( cssFile );
+	claws_safe_fclose( cssFile );
 	return MGU_SUCCESS;
 }
 
@@ -314,12 +314,12 @@ static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
 	FILE *cssFile;
 
-	cssFile = g_fopen( fileSpec, "rb" );
+	cssFile = claws_fopen( fileSpec, "rb" );
 	if( cssFile ) {
-		fclose( cssFile );
+		claws_fclose( cssFile );
 		return MGU_SUCCESS;
 	}
-	cssFile = g_fopen( fileSpec, "wb" );
+	cssFile = claws_fopen( fileSpec, "wb" );
 	if( ! cssFile ) {
 		return MGU_OPEN_FILE;
 	}
@@ -366,7 +366,7 @@ static gint exporthtml_create_css_full( const gchar *fileSpec ) {
 	fprintf( cssFile, ".tab-attr {\n" );
 	fprintf( cssFile, "}\n" );
 
-	safe_fclose( cssFile );
+	claws_safe_fclose( cssFile );
 	return MGU_SUCCESS;
 }
 
@@ -964,7 +964,7 @@ void exporthtml_process(
 	static gchar *title;
 	gchar buf[512];
 
-	htmlFile = g_fopen( ctl->path, "wb" );
+	htmlFile = claws_fopen( ctl->path, "wb" );
 	if( ! htmlFile ) {
 		/* Cannot open file */
 		g_print( "Cannot open file for write\n" );
@@ -995,7 +995,7 @@ void exporthtml_process(
 	fprintf( htmlFile, "</body>\n" );
 	fprintf( htmlFile, "</html>\n" );
 
-	safe_fclose( htmlFile );
+	claws_safe_fclose( htmlFile );
 	ctl->retVal = MGU_SUCCESS;
 
 	/* Create stylesheet files */
diff --git a/src/exportldif.c b/src/exportldif.c
index ded6a3e..54e6bd0 100644
--- a/src/exportldif.c
+++ b/src/exportldif.c
@@ -533,7 +533,7 @@ void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
 	ItemFolder *rootFolder;
 	FILE *ldifFile;
 
-	ldifFile = g_fopen( ctl->path, "wb" );
+	ldifFile = claws_fopen( ctl->path, "wb" );
 	if( ! ldifFile ) {
 		/* Cannot open file */
 		ctl->retVal = MGU_OPEN_FILE;
@@ -542,7 +542,7 @@ void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
 
 	rootFolder = cache->rootFolder;
 	exportldif_fmt_folder( ctl, ldifFile, rootFolder );
-	safe_fclose( ldifFile );
+	claws_safe_fclose( ldifFile );
 	ctl->retVal = MGU_SUCCESS;
 }
 
diff --git a/src/folder.c b/src/folder.c
index 503a049..97c5d01 100644
--- a/src/folder.c
+++ b/src/folder.c
@@ -63,6 +63,7 @@
 #include "privacy.h"
 #include "prefs_common.h"
 #include "prefs_migration.h"
+#include "claws_io.h"
 
 /* Dependecies to be removed ?! */
 #include "prefs_account.h"
@@ -2990,12 +2991,12 @@ static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
 	gint msgnum = 0;
 	gchar buf[BUFFSIZE];
 
-	if ((fp = g_fopen(file, "rb")) == NULL)
+	if ((fp = claws_fopen(file, "rb")) == NULL)
 		return 0;
 
 	if ((folder_has_parent_of_type(dest, F_QUEUE)) || 
 	    (folder_has_parent_of_type(dest, F_DRAFT)))
-		while (fgets(buf, sizeof(buf), fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 			/* new way */
 			if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
 				strlen("X-Claws-End-Special-Headers:"))) ||
@@ -3029,7 +3030,7 @@ static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
 	
 	g_free(hentry[0].body);
 	hentry[0].body = NULL;
-	fclose(fp);
+	claws_fclose(fp);
 
 	return msgnum;
 }
diff --git a/src/gtk/about.c b/src/gtk/about.c
index e406e97..261498f 100644
--- a/src/gtk/about.c
+++ b/src/gtk/about.c
@@ -43,6 +43,7 @@
 #include "menu.h"
 #include "textview.h"
 #include "main.h"
+#include "claws_io.h"
 
 extern SessionStats session_stats;
 static GtkTextBuffer *stats_text_buffer;
@@ -643,14 +644,14 @@ static GtkWidget *about_create_child_page_release_notes(void)
 	gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
 
 	path = g_strconcat(DOCDIR, G_DIR_SEPARATOR_S, RELEASE_NOTES_FILE, NULL);
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 		g_free(path);
 		return scrolledwin;
 	}
 	g_free(path);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		const gchar *src_codeset = conv_get_locale_charset_str();
 		const gchar *dest_codeset = CS_UTF_8;
 		gchar *tmp;
@@ -664,7 +665,7 @@ static GtkWidget *about_create_child_page_release_notes(void)
 		gtk_text_buffer_insert(buffer, &iter, tmp, -1);
 		g_free(tmp);
 	}
-	fclose(fp);
+	claws_fclose(fp);
 
 	return scrolledwin;
 }
diff --git a/src/html.c b/src/html.c
index da8625e..190b467 100644
--- a/src/html.c
+++ b/src/html.c
@@ -16,6 +16,11 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -25,6 +30,7 @@
 #include "codeconv.h"
 #include "utils.h"
 #include "entity.h"
+#include "claws_io.h"
 
 #define SC_HTMLBUFSIZE	8192
 #define HR_STR		"────────────────────────────────────────────────"
@@ -136,7 +142,7 @@ static SC_HTMLState sc_html_read_line(SC_HTMLParser *parser)
 	if (parser->fp == NULL)
 		return SC_HTML_EOF;
 
-	n = fread(buf, 1, sizeof(buf) - 1, parser->fp);
+	n = claws_fread(buf, 1, sizeof(buf) - 1, parser->fp);
 	if (n == 0) {
 		parser->state = SC_HTML_EOF;
 		return SC_HTML_EOF;
diff --git a/src/imap.c b/src/imap.c
index 4982b61..5871b79 100644
--- a/src/imap.c
+++ b/src/imap.c
@@ -71,6 +71,7 @@
 #include "tags.h"
 #include "main.h"
 #include "passwordstore.h"
+#include "claws_io.h"
 
 typedef struct _IMAPFolder	IMAPFolder;
 typedef struct _IMAPSession	IMAPSession;
@@ -1377,17 +1378,17 @@ static guint get_file_size_with_crs(const gchar *filename)
 	if (filename == NULL)
 		return -1;
 	
-	fp = g_fopen(filename, "rb");
+	fp = claws_fopen(filename, "rb");
 	if (!fp)
 		return -1;
 	
-	while (fgets(buf, sizeof (buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
 		cnt += strlen(buf);
 		if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
 			cnt++;
 	}
 	
-	fclose(fp);
+	claws_fclose(fp);
 	return cnt;
 }
 
diff --git a/src/jpilot.c b/src/jpilot.c
index fc3927c..a29b02b 100644
--- a/src/jpilot.c
+++ b/src/jpilot.c
@@ -56,6 +56,7 @@
 #include "codeconv.h"
 #include "adbookbase.h"
 #include "utils.h"
+#include "claws_io.h"
 
 #define JPILOT_DBHOME_DIR   ".jpilot"
 #define JPILOT_DBHOME_FILE  "AddressDB.pdb"
@@ -538,10 +539,10 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
 	int r;
 
 	fseek(in, 0, SEEK_SET);
-	r = fread(&rdbh, sizeof(RawDBHeader), 1, in);
+	r = claws_fread(&rdbh, sizeof(RawDBHeader), 1, in);
 	if (r < 1)
 		return MGU_ERROR_READ;
-	if (feof(in)) {
+	if (claws_feof(in)) {
 		return MGU_EOF;
 	}
 
@@ -560,7 +561,7 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
 		return MGU_SUCCESS;
 	}
 
-	r = fread(&rh, sizeof(record_header), 1, in);
+	r = claws_fread(&rh, sizeof(record_header), 1, in);
 	if (r < 1)
 		return MGU_ERROR_READ;
 
@@ -589,7 +590,7 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
 	*buf_size=0;
 
 	if( pilotFile->path ) {
-		in = g_fopen( pilotFile->path, "rb" );
+		in = claws_fopen( pilotFile->path, "rb" );
 		if( !in ) {
 			return MGU_OPEN_FILE;
 		}
@@ -598,15 +599,15 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
 		return MGU_NO_FILE;
 	}
 
-	num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
+	num = claws_fread( &rdbh, sizeof( RawDBHeader ), 1, in );
 	if( num != 1 ) {
-	  	if( ferror(in) ) {
-			fclose(in);
+	  	if( claws_ferror(in) ) {
+			claws_fclose(in);
 			return MGU_ERROR_READ;
 		}
 	}
-	if (feof(in)) {
-		fclose(in);
+	if (claws_feof(in)) {
+		claws_fclose(in);
 		return MGU_EOF;
 	}
 
@@ -615,28 +616,28 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
 
 	num = jpilot_get_info_size(in, &rec_size);
 	if (num) {
-		fclose(in);
+		claws_fclose(in);
 		return MGU_ERROR_READ;
 	}
 
 	if (fseek(in, dbh.app_info_offset, SEEK_SET) < 0) {
-		fclose(in);
+		claws_fclose(in);
 		return MGU_ERROR_READ;
 	}
 	*buf = ( char * ) malloc(rec_size);
 	if (!(*buf)) {
-		fclose(in);
+		claws_fclose(in);
 		return MGU_OO_MEMORY;
 	}
-	num = fread(*buf, rec_size, 1, in);
+	num = claws_fread(*buf, rec_size, 1, in);
 	if (num != 1) {
-		if (ferror(in)) {
-			fclose(in);
+		if (claws_ferror(in)) {
+			claws_fclose(in);
 			free(*buf);
 			return MGU_ERROR_READ;
 		}
 	}
-	fclose(in);
+	claws_fclose(in);
 
 	*buf_size = rec_size;
 
@@ -685,8 +686,8 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
 
 	memset(header, 0, sizeof(PC3RecordHeader));
 
-	num = fread(&l, sizeof(l), 1, pc_in);
-	if (feof(pc_in)) {
+	num = claws_fread(&l, sizeof(l), 1, pc_in);
+	if (claws_feof(pc_in)) {
 		return -1;
 	}
 	if (num!=1) {
@@ -697,8 +698,8 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
 	if (len > 255 || len < sizeof(l)) {
 		return -1;
 	}
-	num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
-	if (feof(pc_in)) {
+	num = claws_fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
+	if (claws_feof(pc_in)) {
 		return -1;
 	}
 	if (num!=1) {
@@ -722,14 +723,14 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
 	int rec_len, num;
 	char *record;
 
-	if( feof( in ) ) {
+	if( claws_feof( in ) ) {
 		return MGU_EOF;
 	}
 	num = read_header( in, &header );
 	if( num < 1 ) {
-		if( ferror( in ) )
+		if( claws_ferror( in ) )
 			return MGU_ERROR_READ;
-		else if( feof( in ) )
+		else if( claws_feof( in ) )
 			return MGU_EOF;
 		else
 			return -1;
@@ -739,9 +740,9 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
 	if( ! record ) {
 		return MGU_OO_MEMORY;
 	}
-	num = fread( record, rec_len, 1, in );
+	num = claws_fread( record, rec_len, 1, in );
 	if( num != 1 ) {
-		if( ferror( in ) ) {
+		if( claws_ferror( in ) ) {
 			free( record );
 			return MGU_ERROR_READ;
 		}
@@ -789,20 +790,20 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
 		return MGU_BAD_ARGS;
 	}
 
-	in = g_fopen( pilotFile->path, "rb" );
+	in = claws_fopen( pilotFile->path, "rb" );
 	if (!in) {
 		return MGU_OPEN_FILE;
 	}
 
 	/* Read the database header */
-	num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
+	num = claws_fread( &rdbh, sizeof( RawDBHeader ), 1, in );
 	if( num != 1 ) {
-		if( ferror( in ) ) {
-			fclose( in );
+		if( claws_ferror( in ) ) {
+			claws_fclose( in );
 			return MGU_ERROR_READ;
 		}
-		if( feof( in ) ) {
-			fclose( in );
+		if( claws_feof( in ) ) {
+			claws_fclose( in );
 			return MGU_EOF;
 		}
 	}
@@ -814,13 +815,13 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
 	prev_offset = 0;
 
 	for( i = 1; i < num_records + 1; i++ ) {
-		num = fread( &rh, sizeof( record_header ), 1, in );
+		num = claws_fread( &rh, sizeof( record_header ), 1, in );
 		if( num != 1 ) {
-			if( ferror( in ) ) {
+			if( claws_ferror( in ) ) {
 				break;
 			}
-			if( feof( in ) ) {
-				fclose( in );
+			if( claws_feof( in ) ) {
+				claws_fclose( in );
 				if (mem_rh)
 					free_mem_rec_header( &mem_rh );
 				return MGU_EOF;
@@ -872,10 +873,10 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
 		}
 		if (fseek( in, next_offset, SEEK_SET ) < 0) {
 			free_mem_rec_header( &mem_rh );
-			fclose(in);
+			claws_fclose(in);
 			return MGU_ERROR_READ;
 		}
-		while( ! feof( in ) ) {
+		while( ! claws_feof( in ) ) {
 			fpos = ftell( in );
 			if( out_of_order ) {
 				find_next_offset(
@@ -895,9 +896,9 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
 			rec_size = next_offset - fpos;
 			buf = malloc( rec_size );
 			if( ! buf ) break;
-			num = fread( buf, rec_size, 1, in );
+			num = claws_fread( buf, rec_size, 1, in );
 			if( ( num != 1 ) ) {
-				if( ferror( in ) ) {
+				if( claws_ferror( in ) ) {
 					free( buf );
 					break;
 				}
@@ -919,20 +920,20 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
 			recs_returned++;
 		}
 	}
-	fclose( in );
+	claws_fclose( in );
 	free_mem_rec_header( &mem_rh );
 
 	/* Read the PC3 file, if present */
 	pcFile = jpilot_get_pc3_file( pilotFile );
 	if( pcFile == NULL ) return MGU_SUCCESS;
-	pc_in = g_fopen( pcFile, "rb");
+	pc_in = claws_fopen( pcFile, "rb");
 	g_free( pcFile );
 
 	if( pc_in == NULL ) {
 		return MGU_SUCCESS;
 	}
 
-	while( ! feof( pc_in ) ) {
+	while( ! claws_feof( pc_in ) ) {
 		gboolean linked;
 
 		temp_br = malloc( sizeof( buf_rec ) );
@@ -982,7 +983,7 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
 			free( temp_br );
 		}
 	}
-	fclose( pc_in );
+	claws_fclose( pc_in );
 
 	return MGU_SUCCESS;
 }
@@ -1616,8 +1617,8 @@ gchar *jpilot_find_pilotdb( void ) {
 	strncat( str, JPILOT_DBHOME_FILE, WORK_BUFLEN - strlen(str) );
 
 	/* Attempt to open */
-	if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
-		fclose( fp );
+	if( ( fp = claws_fopen( str, "rb" ) ) != NULL ) {
+		claws_fclose( fp );
 	}
 	else {
 		/* Truncate filename */
diff --git a/src/ldapquery.c b/src/ldapquery.c
index 5b0750a..16a20d7 100644
--- a/src/ldapquery.c
+++ b/src/ldapquery.c
@@ -424,10 +424,10 @@ static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *att
 				list = g_slist_append( list, g_strndup( vals[0]->bv_val, vals[0]->bv_len ));
 			} else {
 				char *file = get_tmp_file();
-				FILE *fp = g_fopen(file, "wb");
+				FILE *fp = claws_fopen(file, "wb");
 				if (fp) {
-					fwrite(vals[0]->bv_val, 1, vals[0]->bv_len, fp);
-					safe_fclose(fp);
+					claws_fwrite(vals[0]->bv_val, 1, vals[0]->bv_len, fp);
+					claws_safe_fclose(fp);
 				}
 				list = g_slist_append( list, file);
 			}
diff --git a/src/ldif.c b/src/ldif.c
index 93ce800..a632123 100644
--- a/src/ldif.c
+++ b/src/ldif.c
@@ -32,6 +32,7 @@
 #include "addrcache.h"
 
 #include "utils.h"
+#include "claws_io.h"
 
 #define	LDIF_SEP_TAG    ':'
 #define	LDIF_LANG_TAG   ';'
@@ -176,7 +177,7 @@ void ldif_free( LdifFile *ldifFile ) {
 	cm_return_if_fail( ldifFile != NULL );
 
 	/* Close file */
-	if( ldifFile->file ) fclose( ldifFile->file );
+	if( ldifFile->file ) claws_fclose( ldifFile->file );
 
 	/* Free internal stuff */
 	g_free( ldifFile->path );
@@ -207,7 +208,7 @@ void ldif_free( LdifFile *ldifFile ) {
 static gint ldif_open_file( LdifFile* ldifFile ) {
 	/* g_print( "Opening file\n" ); */
 	if( ldifFile->path ) {
-		ldifFile->file = g_fopen( ldifFile->path, "rb" );
+		ldifFile->file = claws_fopen( ldifFile->path, "rb" );
 		if( ! ldifFile->file ) {
 			/* g_print( "can't open %s\n", ldifFile->path ); */
 			ldifFile->retVal = MGU_OPEN_FILE;
@@ -231,7 +232,7 @@ static gint ldif_open_file( LdifFile* ldifFile ) {
  */
 static void ldif_close_file( LdifFile *ldifFile ) {
 	cm_return_if_fail( ldifFile != NULL );
-	if( ldifFile->file ) fclose( ldifFile->file );
+	if( ldifFile->file ) claws_fclose( ldifFile->file );
 	ldifFile->file = NULL;
 }
 
@@ -246,14 +247,14 @@ static gchar *ldif_get_line( LdifFile *ldifFile ) {
 	int i = 0;
 	int cur_alloc = LDIFBUFSIZE;
 
-	if( feof( ldifFile->file ) ) {
+	if( claws_feof( ldifFile->file ) ) {
 		g_free(buf);
 		return NULL;
 	}
 
 	while( i < cur_alloc-1 ) {
 		ch = fgetc( ldifFile->file );
-		if (ferror( ldifFile->file ))
+		if (claws_ferror( ldifFile->file ))
 			ldifFile->retVal = MGU_ERROR_READ;
 		if( ch == '\0' || ch == EOF ) {
 			if( i == 0 ) return NULL;
diff --git a/src/main.c b/src/main.c
index 0bfee71..631e366 100644
--- a/src/main.c
+++ b/src/main.c
@@ -443,12 +443,12 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
 	gchar buf[BUFFSIZE];
 	gboolean err = FALSE;
 
-	oldfp = g_fopen(old_rc, "r");
+	oldfp = claws_fopen(old_rc, "r");
 	if (!oldfp)
 		return -1;
-	newfp = g_fopen(new_rc, "w");
+	newfp = claws_fopen(new_rc, "w");
 	if (!newfp) {
-		fclose(oldfp);
+		claws_fclose(oldfp);
 		return -1;
 	}
 	
@@ -464,21 +464,21 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
 		old_plugin_path = g_strdup(new_plugin_path);
 	}
 	debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
-	while (fgets(buf, sizeof(buf), oldfp)) {
+	while (claws_fgets(buf, sizeof(buf), oldfp)) {
 		if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
-			err |= (fputs(buf, newfp) == EOF);
+			err |= (claws_fputs(buf, newfp) == EOF);
 		} else {
 			debug_print("->replacing %s\n", buf);
 			debug_print("  with %s%s\n", new_plugin_path, buf+strlen(old_plugin_path));
-			err |= (fputs(new_plugin_path, newfp) == EOF);
-			err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
+			err |= (claws_fputs(new_plugin_path, newfp) == EOF);
+			err |= (claws_fputs(buf+strlen(old_plugin_path), newfp) == EOF);
 		}
 	}
 	g_free(plugin_path);
 	g_free(new_plugin_path);
 	g_free(old_plugin_path);
-	fclose(oldfp);
-	if (safe_fclose(newfp) == EOF)
+	claws_fclose(oldfp);
+	if (claws_safe_fclose(newfp) == EOF)
 		err = TRUE;
 	
 	return (err ? -1:0);
@@ -738,7 +738,7 @@ static void win32_open_log(void)
 		if (rename_force(logfile, oldlogfile) < 0)
 			FILE_OP_ERROR(logfile, "rename");
 	}
-	win32_debug_fp = g_fopen(logfile, "w");
+	win32_debug_fp = claws_fopen(logfile, "w");
 	g_free(logfile);
 	g_free(oldlogfile);
 	if (win32_debug_fp)
@@ -761,7 +761,7 @@ static void win32_close_log(void)
 		g_log_remove_handler("", win32_log_handler_app_id);
 		g_log_remove_handler("GLib", win32_log_handler_glib_id);
 		g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
-		fclose(win32_debug_fp);
+		claws_fclose(win32_debug_fp);
 		win32_debug_fp=NULL;
 	}
 }		
@@ -1788,12 +1788,12 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
 	if (isstdin)
 		fp = stdin;
 	else {
-		fp = g_fopen(fn, "r");
+		fp = claws_fopen(fn, "r");
 		if (!fp)
 			G_PRINT_EXIT(_("Cannot open filename for reading\n"));
 	}
 
-	while (fgets(fb, sizeof(fb), fp)) {
+	while (claws_fgets(fb, sizeof(fb), fp)) {
 		gchar *tmp;	
 		strretchomp(fb);
 		if (*fb == '\0')
@@ -1823,11 +1823,11 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
 	g_string_append(body, to);
 	g_free(to);
 	g_string_append(body, "?body=");
-	while (fgets(fb, sizeof(fb), fp)) {
+	while (claws_fgets(fb, sizeof(fb), fp)) {
 		g_string_append_uri_escaped(body, fb, NULL, TRUE);
 	}
 	if (!isstdin)
-		fclose(fp);
+		claws_fclose(fp);
 	/* append the remaining headers */
 	g_string_append(body, headers->str);
 	g_string_free(headers, TRUE);
@@ -2415,7 +2415,7 @@ static gint prohibit_duplicate_launch(void)
  			fd_gets(uxsock, buf, sizeof(buf) - 1);
 			buf[sizeof(buf) - 1] = '\0';
  			if (!strncmp(buf, ".\n", 2)) break;
- 			fputs(buf, stdout);
+ 			claws_fputs(buf, stdout);
  		}
 	} else if (cmd.exit) {
 		fd_write_all(uxsock, "exit\n", 5);
@@ -2426,7 +2426,7 @@ static gint prohibit_duplicate_launch(void)
  			fd_gets(uxsock, buf, sizeof(buf) - 1);
 			buf[sizeof(buf) - 1] = '\0';
  			if (!strncmp(buf, ".\n", 2)) break;
- 			fputs(buf, stdout);
+ 			claws_fputs(buf, stdout);
  		}
 	} else if (cmd.reset_statistics) {
 		fd_write(uxsock, "reset_statistics\n", 17);
@@ -2446,7 +2446,7 @@ static gint prohibit_duplicate_launch(void)
 			fd_gets(uxsock, buf, sizeof(buf) - 1);
 			buf[sizeof(buf) - 1] = '\0';
 			if (!strncmp(buf, ".\n", 2)) break;
-			fputs(buf, stdout);
+			claws_fputs(buf, stdout);
 		}
 	} else {
 #ifndef G_OS_WIN32
diff --git a/src/matcher.c b/src/matcher.c
index 5b67051..c87c897 100644
--- a/src/matcher.c
+++ b/src/matcher.c
@@ -49,6 +49,7 @@
 #include "tags.h"
 #include "folder_item_prefs.h"
 #include "procmsg.h"
+#include "claws_io.h"
 
 /*!
  *\brief	Keyword lookup element
@@ -1623,7 +1624,7 @@ static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo
 	if (!outfp)
 		return FALSE;
 
-	while (fgets(buf, sizeof(buf), outfp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), outfp) != NULL) {
 		strretchomp(buf);
 
 		for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
@@ -1664,14 +1665,14 @@ static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo
 			 * no need to check the others. */
 			if (matcher->result && matcher->done) {
 				if (!matchers->bool_and) {
-					fclose(outfp);
+					claws_fclose(outfp);
 					return TRUE;
 				}
 			}
 		}
 	}
 
-	fclose(outfp);
+	claws_fclose(outfp);
 	return FALSE;
 }
 
@@ -1823,8 +1824,8 @@ static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
 	if (file == NULL)
 		return FALSE;
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		g_free(file);
 		return result;
 	}
@@ -1866,7 +1867,7 @@ static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
 
 	g_free(file);
 
-	fclose(fp);
+	claws_fclose(fp);
 	
 	return result;
 }
@@ -2429,42 +2430,42 @@ static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
 			continue;
 
 		if (prop->enabled) {
-			if (fputs("enabled ", fp) == EOF) {
-				FILE_OP_ERROR("filtering config", "fputs");
+			if (claws_fputs("enabled ", fp) == EOF) {
+				FILE_OP_ERROR("filtering config", "claws_fputs");
 				return -1;
 			}
 		} else {
-			if (fputs("disabled ", fp) == EOF) {
-				FILE_OP_ERROR("filtering config", "fputs");
+			if (claws_fputs("disabled ", fp) == EOF) {
+				FILE_OP_ERROR("filtering config", "claws_fputs");
 				return -1;
 			}
 		}
 
-		if (fputs("rulename \"", fp) == EOF) {
-			FILE_OP_ERROR("filtering config", "fputs");
+		if (claws_fputs("rulename \"", fp) == EOF) {
+			FILE_OP_ERROR("filtering config", "claws_fputs");
 			g_free(filtering_str);
 			return -1;
 		}
 		tmp_name = prop->name;
 		while (tmp_name && *tmp_name != '\0') {
 			if (*tmp_name != '"') {
-				if (fputc(*tmp_name, fp) == EOF) {
-					FILE_OP_ERROR("filtering config", "fputs || fputc");
+				if (claws_fputc(*tmp_name, fp) == EOF) {
+					FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
 					g_free(filtering_str);
 					return -1;
 				}
 			} else if (*tmp_name == '"') {
-				if (fputc('\\', fp) == EOF ||
-				    fputc('"', fp) == EOF) {
-					FILE_OP_ERROR("filtering config", "fputs || fputc");
+				if (claws_fputc('\\', fp) == EOF ||
+				    claws_fputc('"', fp) == EOF) {
+					FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
 					g_free(filtering_str);
 					return -1;
 				}
 			}
 			tmp_name ++;
 		}
-		if (fputs("\" ", fp) == EOF) {
-			FILE_OP_ERROR("filtering config", "fputs");
+		if (claws_fputs("\" ", fp) == EOF) {
+			FILE_OP_ERROR("filtering config", "claws_fputs");
 			g_free(filtering_str);
 			return -1;
 		}
@@ -2473,17 +2474,17 @@ static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
 			gchar *tmp = NULL;
 
 			tmp = g_strdup_printf("account %d ", prop->account_id);
-			if (fputs(tmp, fp) == EOF) {
-				FILE_OP_ERROR("filtering config", "fputs");
+			if (claws_fputs(tmp, fp) == EOF) {
+				FILE_OP_ERROR("filtering config", "claws_fputs");
 				g_free(tmp);
 				return -1;
 			}
 			g_free(tmp);
 		}
 
-		if(fputs(filtering_str, fp) == EOF ||
-		    fputc('\n', fp) == EOF) {
-			FILE_OP_ERROR("filtering config", "fputs || fputc");
+		if(claws_fputs(filtering_str, fp) == EOF ||
+		    claws_fputc('\n', fp) == EOF) {
+			FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
 			g_free(filtering_str);
 			return -1;
 		}
@@ -2531,7 +2532,7 @@ static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
 			data->error = TRUE;
 			goto fail;
 		}
-		if (fputc('\n', data->fp) == EOF) {
+		if (claws_fputc('\n', data->fp) == EOF) {
 			data->error = TRUE;
 			goto fail;
 		}
@@ -2569,19 +2570,19 @@ static int prefs_matcher_save(FILE *fp)
         /* pre global rules */
         if (fprintf(fp, "[preglobal]\n") < 0 ||
             prefs_filtering_write(fp, pre_global_processing) < 0 ||
-            fputc('\n', fp) == EOF)
+            claws_fputc('\n', fp) == EOF)
 		return -1;
 
         /* post global rules */
         if (fprintf(fp, "[postglobal]\n") < 0 ||
             prefs_filtering_write(fp, post_global_processing) < 0 ||
-            fputc('\n', fp) == EOF)
+            claws_fputc('\n', fp) == EOF)
 		return -1;
         
         /* filtering rules */
 	if (fprintf(fp, "[filtering]\n") < 0 ||
             prefs_filtering_write(fp, filtering_rules) < 0 ||
-            fputc('\n', fp) == EOF)
+            claws_fputc('\n', fp) == EOF)
 		return -1;
 
 	return 0;
@@ -2629,11 +2630,11 @@ void prefs_matcher_read_config(void)
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
 
-	f = g_fopen(rcpath, "rb");
+	f = claws_fopen(rcpath, "rb");
 	g_free(rcpath);
 
 	if (f != NULL) {
 		matcher_parser_start_parsing(f);
-		fclose(matcher_parserin);
+		claws_fclose(matcher_parserin);
 	}
 }
diff --git a/src/mbox.c b/src/mbox.c
index 68b3d01..2f7db0d 100644
--- a/src/mbox.c
+++ b/src/mbox.c
@@ -23,7 +23,6 @@
 #endif
 
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #ifdef USE_PTHREAD
@@ -60,23 +59,13 @@
 
 #define MESSAGEBUFSIZE	8192
 
-#ifdef HAVE_FGETS_UNLOCKED
-#define SC_FGETS fgets_unlocked
-#define SC_FPUTS fputs_unlocked
-#define SC_FPUTC fputc_unlocked
-#else
-#define SC_FGETS fgets
-#define SC_FPUTS fputs
-#define SC_FPUTC fputc
-#endif
-
 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
 { \
 	lines++; \
-	if (fputs(s, tmp_fp) == EOF) { \
+	if (claws_fputs(s, tmp_fp) == EOF) { \
 		g_warning("can't write to temporary file"); \
-		fclose(tmp_fp); \
-		fclose(mbox_fp); \
+		claws_fclose(tmp_fp); \
+		claws_fclose(mbox_fp); \
 		claws_unlink(tmp_file); \
 		g_free(tmp_file); \
 		return -1; \
@@ -103,24 +92,24 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 
 	debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
 
-	if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
-		FILE_OP_ERROR(mbox, "fopen");
+	if ((mbox_fp = claws_fopen(mbox, "rb")) == NULL) {
+		FILE_OP_ERROR(mbox, "claws_fopen");
 		alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
 		return -1;
 	}
 
 	/* ignore empty lines on the head */
 	do {
-		if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
+		if (claws_fgets(buf, sizeof(buf), mbox_fp) == NULL) {
 			g_warning("can't read mbox file.");
-			fclose(mbox_fp);
+			claws_fclose(mbox_fp);
 			return -1;
 		}
 	} while (buf[0] == '\n' || buf[0] == '\r');
 
 	if (strncmp(buf, "From ", 5) != 0) {
 		g_warning("invalid mbox format: %s", mbox);
-		fclose(mbox_fp);
+		claws_fclose(mbox_fp);
 		return -1;
 	}
 
@@ -148,10 +137,10 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 			GTK_EVENTS_FLUSH();
 		}
 	
-		if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
-			FILE_OP_ERROR(tmp_file, "fopen");
+		if ((tmp_fp = claws_fopen(tmp_file, "wb")) == NULL) {
+			FILE_OP_ERROR(tmp_file, "claws_fopen");
 			g_warning("can't open temporary file");
-			fclose(mbox_fp);
+			claws_fclose(mbox_fp);
 			g_free(tmp_file);
 			return -1;
 		}
@@ -163,7 +152,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 		lines = 0;
 
 		/* process all lines from mboxrc file */
-		while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), mbox_fp) != NULL) {
 			int offset;
 
 			/* eat empty lines */
@@ -219,22 +208,22 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 		}
 
 		/* more emails to expect? */
-		more = !feof(mbox_fp);
+		more = !claws_feof(mbox_fp);
 
 		/* warn if email part is empty (it's the minimum check 
 		   we can do */
 		if (lines == 0) {
 			g_warning("malformed mbox: %s: message %d is empty", mbox, msgs);
-			fclose(tmp_fp);
-			fclose(mbox_fp);
+			claws_fclose(tmp_fp);
+			claws_fclose(mbox_fp);
 			claws_unlink(tmp_file);
 			return -1;
 		}
 
-		if (safe_fclose(tmp_fp) == EOF) {
-			FILE_OP_ERROR(tmp_file, "fclose");
+		if (claws_safe_fclose(tmp_fp) == EOF) {
+			FILE_OP_ERROR(tmp_file, "claws_fclose");
 			g_warning("can't write to temporary file");
-			fclose(mbox_fp);
+			claws_fclose(mbox_fp);
 			claws_unlink(tmp_file);
 			g_free(tmp_file);
 			return -1;
@@ -242,7 +231,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 
 		if (apply_filter) {
 			if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
-				fclose(mbox_fp);
+				claws_fclose(mbox_fp);
 				claws_unlink(tmp_file);
 				g_free(tmp_file);
 				return -1;
@@ -303,7 +292,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 	folder_item_update_thaw();
 	
 	g_free(tmp_file);
-	fclose(mbox_fp);
+	claws_fclose(mbox_fp);
 	debug_print("%d messages found.\n", msgs);
 
 	return msgs;
@@ -320,8 +309,8 @@ gint lock_mbox(const gchar *base, LockType type)
 		FILE *lockfp;
 
 		lockfile = g_strdup_printf("%s.%d", base, getpid());
-		if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
-			FILE_OP_ERROR(lockfile, "fopen");
+		if ((lockfp = claws_fopen(lockfile, "wb")) == NULL) {
+			FILE_OP_ERROR(lockfile, "claws_fopen");
 			g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible.", lockfile);
 			g_free(lockfile);
 			return -1;
@@ -330,12 +319,12 @@ gint lock_mbox(const gchar *base, LockType type)
 		if (fprintf(lockfp, "%d\n", getpid()) < 0) {
 			FILE_OP_ERROR(lockfile, "fprintf");
 			g_free(lockfile);
-			fclose(lockfp);
+			claws_fclose(lockfp);
 			return -1;
 		}
 
-		if (safe_fclose(lockfp) == EOF) {
-			FILE_OP_ERROR(lockfile, "fclose");
+		if (claws_safe_fclose(lockfp) == EOF) {
+			FILE_OP_ERROR(lockfile, "claws_fclose");
 			g_free(lockfile);
 			return -1;
 		}
@@ -485,8 +474,8 @@ gint copy_mbox(gint srcfd, const gchar *dest)
 		return -1;
 	}
 
-	if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-		FILE_OP_ERROR(dest, "fopen");
+	if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+		FILE_OP_ERROR(dest, "claws_fopen");
 		return -1;
 	}
 
@@ -496,9 +485,9 @@ gint copy_mbox(gint srcfd, const gchar *dest)
 	}
 
 	while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
-		if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+		if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
 			g_warning("writing to %s failed.", dest);
-			fclose(dest_fp);
+			claws_fclose(dest_fp);
 			claws_unlink(dest);
 			return -1;
 		}
@@ -510,8 +499,8 @@ gint copy_mbox(gint srcfd, const gchar *dest)
 		err = TRUE;
 	}
 
-	if (safe_fclose(dest_fp) == EOF) {
-		FILE_OP_ERROR(dest, "fclose");
+	if (claws_safe_fclose(dest_fp) == EOF) {
+		FILE_OP_ERROR(dest, "claws_fclose");
 		err = TRUE;
 	}
 
@@ -527,12 +516,12 @@ void empty_mbox(const gchar *mbox)
 {
 	FILE *fp;
 
-	if ((fp = g_fopen(mbox, "wb")) == NULL) {
-		FILE_OP_ERROR(mbox, "fopen");
+	if ((fp = claws_fopen(mbox, "wb")) == NULL) {
+		FILE_OP_ERROR(mbox, "claws_fopen");
 		g_warning("can't truncate mailbox to zero.");
 		return;
 	}
-	safe_fclose(fp);
+	claws_safe_fclose(fp);
 }
 
 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
@@ -556,16 +545,12 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
 		}
 	}
 
-	if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
-		FILE_OP_ERROR(mbox, "fopen");
+	if ((mbox_fp = claws_fopen(mbox, "wb")) == NULL) {
+		FILE_OP_ERROR(mbox, "claws_fopen");
 		alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
 		return -1;
 	}
 
-#ifdef HAVE_FGETS_UNLOCKED
-	flockfile(mbox_fp);
-#endif
-
 	statusbar_print_all(_("Exporting to mbox..."));
 	for (cur = mlist; cur != NULL; cur = cur->next) {
 		int len;
@@ -577,9 +562,6 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
 			continue;
 		}
 
-#ifdef HAVE_FGETS_UNLOCKED
-		flockfile(msg_fp);
-#endif
 		strncpy2(buf,
 			 msginfo->from ? msginfo->from :
 			 cur_account && cur_account->address ?
@@ -590,17 +572,14 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
 		if (fprintf(mbox_fp, "From %s %s",
 			buf, ctime_r(&msginfo->date_t, buft)) < 0) {
 			err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-			funlockfile(msg_fp);
-#endif
-			fclose(msg_fp);
+			claws_fclose(msg_fp);
 			goto out;
 		}
 
 		buf[0] = '\0';
 		
 		/* write email to mboxrc */
-		while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), msg_fp) != NULL) {
 			/* quote any From, >From, >>From, etc., according to mbox format specs */
 			int offset;
 
@@ -610,21 +589,15 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
 				offset++;
 			}
 			if (!strncmp(buf+offset, "From ", 5)) {
-				if (SC_FPUTC('>', mbox_fp) == EOF) {
+				if (claws_fputc('>', mbox_fp) == EOF) {
 					err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-					funlockfile(msg_fp);
-#endif
-					fclose(msg_fp);
+					claws_fclose(msg_fp);
 					goto out;
 				}
 			}
-			if (SC_FPUTS(buf, mbox_fp) == EOF) {
+			if (claws_fputs(buf, mbox_fp) == EOF) {
 				err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-				funlockfile(msg_fp);
-#endif
-				fclose(msg_fp);
+				claws_fclose(msg_fp);
 				goto out;
 			}
 		}
@@ -634,31 +607,22 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
 		if (len > 0) {
 			len--;
 			if ((buf[len] != '\n') && (buf[len] != '\r')) {
-				if (SC_FPUTC('\n', mbox_fp) == EOF) {
+				if (claws_fputc('\n', mbox_fp) == EOF) {
 					err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-					funlockfile(msg_fp);
-#endif
-					fclose(msg_fp);
+					claws_fclose(msg_fp);
 					goto out;
 				}
 			}
 		}
 
 		/* add a trailing empty line */
-		if (SC_FPUTC('\n', mbox_fp) == EOF) {
+		if (claws_fputc('\n', mbox_fp) == EOF) {
 			err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-			funlockfile(msg_fp);
-#endif
-			fclose(msg_fp);
+			claws_fclose(msg_fp);
 			goto out;
 		}
 
-#ifdef HAVE_FGETS_UNLOCKED
-		funlockfile(msg_fp);
-#endif
-		safe_fclose(msg_fp);
+		claws_safe_fclose(msg_fp);
 		statusbar_progress_all(msgs++,total, 500);
 		if (msgs%500 == 0)
 			GTK_EVENTS_FLUSH();
@@ -668,10 +632,7 @@ out:
 	statusbar_progress_all(0,0,0);
 	statusbar_pop_all();
 
-#ifdef HAVE_FGETS_UNLOCKED
-	funlockfile(mbox_fp);
-#endif
-	safe_fclose(mbox_fp);
+	claws_safe_fclose(mbox_fp);
 
 	return err;
 }
diff --git a/src/messageview.c b/src/messageview.c
index d9c3fa9..bbe8370 100644
--- a/src/messageview.c
+++ b/src/messageview.c
@@ -894,8 +894,8 @@ static gint disposition_notification_send(MsgInfo *msginfo)
 	g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%p",
 		   get_rc_dir(), G_DIR_SEPARATOR, msginfo);
 
-	if ((fp = g_fopen(tmp, "wb")) == NULL) {
-		FILE_OP_ERROR(tmp, "fopen");
+	if ((fp = claws_fopen(tmp, "wb")) == NULL) {
+		FILE_OP_ERROR(tmp, "claws_fopen");
 		return -1;
 	}
 
@@ -1063,8 +1063,8 @@ static gint disposition_notification_send(MsgInfo *msginfo)
 	if (ok < 0)
 		goto FILE_ERROR;	
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(tmp, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(tmp, "claws_fclose");
 		claws_unlink(tmp);
 		return -1;
 	}
@@ -1101,7 +1101,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
 	return ok;
 
 FILE_ERROR:
-	fclose(fp);
+	claws_fclose(fp);
 	claws_unlink(tmp);
 	return -1;
 }
diff --git a/src/mh.c b/src/mh.c
index 69d0eca..b0967a8 100644
--- a/src/mh.c
+++ b/src/mh.c
@@ -1011,8 +1011,8 @@ static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
 	path = folder_item_get_path(new_item);
 	mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
 					    ".mh_sequences", NULL);
-	if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
-		fclose(mh_sequences_file);
+	if ((mh_sequences_file = claws_fopen(mh_sequences_filename, "a+b")) != NULL) {
+		claws_fclose(mh_sequences_file);
 	}
 	g_free(mh_sequences_filename);
 	g_free(path);
@@ -1324,9 +1324,9 @@ static gchar *get_unseen_seq_name(void)
 		gchar *profile_path = g_strconcat(
 			get_home_dir(), G_DIR_SEPARATOR_S,
 			".mh_profile", NULL);
-		FILE *fp = g_fopen(profile_path, "r");
+		FILE *fp = claws_fopen(profile_path, "r");
 		if (fp) {
-			while (fgets(buf, sizeof(buf), fp) != NULL) {
+			while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 				if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
 					gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
 					while (*seq_tmp == ' ')
@@ -1336,7 +1336,7 @@ static gchar *get_unseen_seq_name(void)
 					break;
 				}
 			}
-			fclose(fp);
+			claws_fclose(fp);
 		}
 		if (!seq_name)
 			seq_name = g_strdup("unseen");
@@ -1365,7 +1365,7 @@ static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
 					    ".mh_sequences", NULL);
 	mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
 					    ".mh_sequences.new", NULL);
-	if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
+	if ((mh_sequences_new_fp = claws_fopen(mh_sequences_new, "w+b")) != NULL) {
 		GSList *msglist = folder_item_get_msg_list(item);
 		GSList *cur;
 		MsgInfo *info = NULL;
@@ -1411,18 +1411,18 @@ static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
 					get_unseen_seq_name(), sequence);
 		}
 		/* rewrite the rest of the file */
-		if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
-			while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
+		if ((mh_sequences_old_fp = claws_fopen(mh_sequences_old, "r+b")) != NULL) {
+			while (claws_fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
 				if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
 					if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
 						err = TRUE;
 						break;
 					}
 			}
-			fclose(mh_sequences_old_fp);
+			claws_fclose(mh_sequences_old_fp);
 		}
 		
-		if (safe_fclose(mh_sequences_new_fp) == EOF)
+		if (claws_safe_fclose(mh_sequences_new_fp) == EOF)
 			err = TRUE;
 
 		if (!err) {
diff --git a/src/mimeview.c b/src/mimeview.c
index 82fac6a..7db10d6 100644
--- a/src/mimeview.c
+++ b/src/mimeview.c
@@ -55,6 +55,7 @@
 #include "timing.h"
 #include "manage_window.h"
 #include "privacy.h"
+#include "claws_io.h"
 
 typedef enum
 {
@@ -794,21 +795,21 @@ static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
 	fname = mimeview->file;
 	if (!fname) return;
 
-	if ((fp = g_fopen(fname, "rb")) == NULL) {
-		FILE_OP_ERROR(fname, "fopen");
+	if ((fp = claws_fopen(fname, "rb")) == NULL) {
+		FILE_OP_ERROR(fname, "claws_fopen");
 		return;
 	}
 
 	if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
 		FILE_OP_ERROR(mimeview->file, "fseek");
-		fclose(fp);
+		claws_fclose(fp);
 		return;
 	}
 
 	mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
 	textview_show_part(mimeview->textview, partinfo, fp);
 
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
@@ -1699,7 +1700,7 @@ static void mimeview_drag_data_get(GtkWidget	    *widget,
 		GPtrArray *headers = NULL;
 		FILE *fp;
 
-		fp = g_fopen(partinfo->data.filename, "rb");
+		fp = claws_fopen(partinfo->data.filename, "rb");
 		if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
 			headers = procheader_get_header_array_asis(fp);
 			if (headers) {
@@ -1716,7 +1717,7 @@ static void mimeview_drag_data_get(GtkWidget	    *widget,
 			}
 		}
 		if (fp != NULL)
-			fclose(fp);
+			claws_fclose(fp);
 		if (name)
 			filename = g_path_get_basename(name);
 		g_free(name);
diff --git a/src/msgcache.c b/src/msgcache.c
index 8909ea7..ca9d965 100644
--- a/src/msgcache.c
+++ b/src/msgcache.c
@@ -24,7 +24,6 @@
 
 #include "defs.h"
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #include <glib.h>
@@ -49,12 +48,6 @@
 #include "prefs_common.h"
 #include "claws_io.h"
 
-#ifdef HAVE_FWRITE_UNLOCKED
-#define SC_FWRITE fwrite_unlocked
-#else
-#define SC_FWRITE fwrite
-#endif
-
 #if G_BYTE_ORDER == G_BIG_ENDIAN
 #define bswap_32(x) \
      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
@@ -312,7 +305,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
 	guint32 idata; \
 	size_t ni; \
  \
-	if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
+	if ((ni = claws_fread(&idata, sizeof(idata), 1, fp)) != 1) { \
 		g_warning("read_int: Cache data corrupted, read %zd of %zd at " \
 			  "offset %ld", ni, sizeof(idata), ftell(fp)); \
 		procmsg_msginfo_free(&msginfo); \
@@ -357,7 +350,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
 	guint32 idata;					\
 							\
 	idata = (guint32)bswap_32(n);			\
-	if (SC_FWRITE(&idata, sizeof(idata), 1, fp) != 1)	\
+	if (claws_fwrite(&idata, sizeof(idata), 1, fp) != 1)	\
 		w_err = 1;				\
 	wrote += 4;					\
 }
@@ -381,7 +374,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
 		len = strlen(data);			\
 	WRITE_CACHE_DATA_INT(len, fp);			\
 	if (w_err == 0 && len > 0) {			\
-		if (SC_FWRITE(data, 1, len, fp) != len)	\
+		if (claws_fwrite(data, 1, len, fp) != len)	\
 			w_err = 1;			\
 		wrote += len;				\
 	} \
@@ -413,8 +406,8 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
 
 	if (mode == DATA_WRITE) {
 		int w_err = 0, wrote = 0;
-		if ((fp = g_fopen(file, "wb")) == NULL) {
-			FILE_OP_ERROR(file, "fopen");
+		if ((fp = claws_fopen(file, "wb")) == NULL) {
+			FILE_OP_ERROR(file, "claws_fopen");
 			return NULL;
 		}
 		if (change_file_mode_rw(fp, file) < 0)
@@ -423,23 +416,23 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
 		WRITE_CACHE_DATA_INT(version, fp);
 		if (w_err != 0) {
 			g_warning("failed to write int");
-			fclose(fp);
+			claws_fclose(fp);
 			return NULL;
 		}
 		return fp;
 	}
 
 	/* check version */
-	if ((fp = g_fopen(file, "rb")) == NULL)
+	if ((fp = claws_fopen(file, "rb")) == NULL)
 		debug_print("Mark/Cache file '%s' not found\n", file);
 	else {
 		if (buf && buf_size > 0)
 			setvbuf(fp, buf, _IOFBF, buf_size);
-		if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
+		if (claws_fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
 			 version != bswap_32(data_ver)) {
 			g_message("%s: Mark/Cache version is different (%u != %u).\n",
 				  file, bswap_32(data_ver), version);
-			fclose(fp);
+			claws_fclose(fp);
 			fp = NULL;
 		}
 		data_ver = bswap_32(data_ver);
@@ -450,9 +443,9 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
 
 	if (fp) {
 		/* reopen with append mode */
-		fclose(fp);
-		if ((fp = g_fopen(file, "ab")) == NULL)
-			FILE_OP_ERROR(file, "fopen");
+		claws_fclose(fp);
+		if ((fp = claws_fopen(file, "ab")) == NULL)
+			FILE_OP_ERROR(file, "claws_fopen");
 	} else {
 		/* open with overwrite mode if mark file doesn't exist or
 		   version is different */
@@ -472,7 +465,7 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
 
 	*str = NULL;
 	if (!swapping) {
-		if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
+		if ((ni = claws_fread(&len, sizeof(len), 1, fp) != 1) ||
 		    len > G_MAXINT) {
 			g_warning("read_data_str: Cache data (len) corrupted, read %zd "
 				  "of %zd bytes at offset %ld", ni, sizeof(len),
@@ -480,7 +473,7 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
 			return -1;
 		}
 	} else {
-		if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
+		if ((ni = claws_fread(&len, sizeof(len), 1, fp) != 1) ||
 		    bswap_32(len) > G_MAXINT) {
 			g_warning("read_data_str: Cache data (len) corrupted, read %zd "
 				  "of %zd bytes at offset %ld", ni, sizeof(len),
@@ -499,7 +492,7 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
 		return -1;
 	}
 
-	if ((ni = fread(tmpstr, 1, len, fp)) != len) {
+	if ((ni = claws_fread(tmpstr, 1, len, fp)) != len) {
 		g_warning("read_data_str: Cache data corrupted, read %zd of %u "
 			  "bytes at offset %ld",
 			  ni, len, ftell(fp));
@@ -612,7 +605,7 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
 	}
 
 	if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0) {
-		fclose(fp);
+		claws_fclose(fp);
 		return NULL;
 	}
 	dstcharset = CS_UTF_8;
@@ -718,7 +711,7 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
 				g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
 		}
 	} else {
-		while (fread(&num, sizeof(num), 1, fp) == 1) {
+		while (claws_fread(&num, sizeof(num), 1, fp) == 1) {
 			if (swapping)
 				num = bswap_32(num);
 
@@ -776,7 +769,7 @@ bail_err:
 		munmap(cache_data, map_len);
 #endif
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	if (conv != NULL) {
 		if (conv->free != NULL)
 			conv->free(conv);
@@ -863,10 +856,10 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
 			}
 		}
 	} else {
-		while (fread(&num, sizeof(num), 1, fp) == 1) {
+		while (claws_fread(&num, sizeof(num), 1, fp) == 1) {
 			if (swapping)
 				num = bswap_32(num);
-			if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) {
+			if (claws_fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) {
 				error = TRUE;
 				break;
 			}
@@ -886,7 +879,7 @@ bail_err:
 		munmap(cache_data, map_len);
 #endif
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	if (error) {
 		debug_print("error reading cache mark from %s\n", mark_file);
 	}
@@ -967,7 +960,7 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
 			}
 		}
 	} else {
-		while (fread(&num, sizeof(num), 1, fp) == 1) {
+		while (claws_fread(&num, sizeof(num), 1, fp) == 1) {
 			gint id = -1;
 			if (swapping)
 				num = bswap_32(num);
@@ -976,7 +969,7 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
 				g_slist_free(msginfo->tags);
 				msginfo->tags = NULL;
 				do {
-					if (fread(&id, sizeof(id), 1, fp) != 1) 
+					if (claws_fread(&id, sizeof(id), 1, fp) != 1) 
 						id = -1;
 					if (swapping)
 						id = bswap_32(id);
@@ -998,7 +991,7 @@ bail_err:
 		munmap(cache_data, map_len);
 #endif
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	if (error) {
 		debug_print("error reading cache tags from %s\n", tags_file);
 	}
@@ -1144,7 +1137,7 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
 	if (w_err != 0) {
 		g_warning("failed to write charset");
 		if (write_fps.cache_fp)
-			fclose(write_fps.cache_fp);
+			claws_fclose(write_fps.cache_fp);
 		claws_unlink(new_cache);
 		g_free(new_cache);
 		g_free(new_mark);
@@ -1157,7 +1150,7 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
 			DATA_WRITE, NULL, 0);
 		if (write_fps.mark_fp == NULL) {
 			if (write_fps.cache_fp)
-				fclose(write_fps.cache_fp);
+				claws_fclose(write_fps.cache_fp);
 			claws_unlink(new_cache);
 			g_free(new_cache);
 			g_free(new_mark);
@@ -1173,9 +1166,9 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
 			DATA_WRITE, NULL, 0);
 		if (write_fps.tags_fp == NULL) {
 			if (write_fps.cache_fp)
-				fclose(write_fps.cache_fp);
+				claws_fclose(write_fps.cache_fp);
 			if (write_fps.mark_fp)
-				fclose(write_fps.mark_fp);
+				claws_fclose(write_fps.mark_fp);
 			claws_unlink(new_cache);
 			claws_unlink(new_mark);
 			g_free(new_cache);
@@ -1200,34 +1193,16 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
 	if (write_fps.tags_fp)
 		write_fps.tags_size = ftell(write_fps.tags_fp);
 
-#ifdef HAVE_FWRITE_UNLOCKED
-	/* lock files for write once (instead of once per fwrite) */
-	if (write_fps.cache_fp)
-		flockfile(write_fps.cache_fp);
-	if (write_fps.mark_fp)
-		flockfile(write_fps.mark_fp);
-	if (write_fps.tags_fp)
-		flockfile(write_fps.tags_fp);
-#endif
 	/* write data to the files */
 	g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
-#ifdef HAVE_FWRITE_UNLOCKED
-	/* unlock files */
-	if (write_fps.cache_fp)
-		funlockfile(write_fps.cache_fp);
-	if (write_fps.mark_fp)
-		funlockfile(write_fps.mark_fp);
-	if (write_fps.tags_fp)
-		funlockfile(write_fps.tags_fp);
-#endif
 
 	/* close files */
 	if (write_fps.cache_fp)
-		write_fps.error |= (safe_fclose(write_fps.cache_fp) != 0);
+		write_fps.error |= (claws_safe_fclose(write_fps.cache_fp) != 0);
 	if (write_fps.mark_fp)
-		write_fps.error |= (safe_fclose(write_fps.mark_fp) != 0);
+		write_fps.error |= (claws_safe_fclose(write_fps.mark_fp) != 0);
 	if (write_fps.tags_fp)
-		write_fps.error |= (safe_fclose(write_fps.tags_fp) != 0);
+		write_fps.error |= (claws_safe_fclose(write_fps.tags_fp) != 0);
 
 
 	if (write_fps.error != 0) {
diff --git a/src/mutt.c b/src/mutt.c
index 4564347..8ae8508 100644
--- a/src/mutt.c
+++ b/src/mutt.c
@@ -31,6 +31,7 @@
 #include "mutt.h"
 #include "addritem.h"
 #include "addrcache.h"
+#include "claws_io.h"
 
 #define MUTT_HOME_FILE  ".muttrc"
 #define MUTTBUFSIZE     2048
@@ -74,7 +75,7 @@ void mutt_free( MuttFile *muttFile ) {
 	cm_return_if_fail( muttFile != NULL );
 
 	/* Close file */
-	if( muttFile->file ) fclose( muttFile->file );
+	if( muttFile->file ) claws_fclose( muttFile->file );
 
 	/* Free internal stuff */
 	g_free( muttFile->path );
@@ -100,7 +101,7 @@ void mutt_free( MuttFile *muttFile ) {
 */
 static gint mutt_open_file( MuttFile* muttFile ) {
 	if( muttFile->path ) {
-		muttFile->file = g_fopen( muttFile->path, "rb" );
+		muttFile->file = claws_fopen( muttFile->path, "rb" );
 		if( ! muttFile->file ) {
 			muttFile->retVal = MGU_OPEN_FILE;
 			return muttFile->retVal;
@@ -122,7 +123,7 @@ static gint mutt_open_file( MuttFile* muttFile ) {
 */
 static void mutt_close_file( MuttFile *muttFile ) {
 	cm_return_if_fail( muttFile != NULL );
-	if( muttFile->file ) fclose( muttFile->file );
+	if( muttFile->file ) claws_fclose( muttFile->file );
 	muttFile->file = NULL;
 }
 
@@ -138,7 +139,7 @@ static gchar *mutt_get_line( MuttFile *muttFile, gboolean *flagCont ) {
 	int i = 0, li = 0;
 
 	*flagCont = FALSE;
-	if( feof( muttFile->file ) ) 
+	if( claws_feof( muttFile->file ) ) 
 		return NULL;
 
 	memset(buf, 0, MUTTBUFSIZE);
@@ -549,8 +550,8 @@ gchar *mutt_find_file( void ) {
 	strncat( str, MUTT_HOME_FILE, WORK_BUFLEN - strlen(str) );
 
 	/* Attempt to open */
-	if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
-		fclose( fp );
+	if( ( fp = claws_fopen( str, "rb" ) ) != NULL ) {
+		claws_fclose( fp );
 	}
 	else {
 		/* Truncate filename */
diff --git a/src/news.c b/src/news.c
index 296a4f6..c963ac1 100644
--- a/src/news.c
+++ b/src/news.c
@@ -653,12 +653,12 @@ GSList *news_get_group_list(Folder *folder)
 	filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
 	g_free(path);
 
-	if ((fp = g_fopen(filename, "rb")) == NULL) {
+	if ((fp = claws_fopen(filename, "rb")) == NULL) {
 		NewsSession *session;
 		gint ok;
 		clist *grouplist = NULL;
 		clistiter *cur;
-		fp = g_fopen(filename, "wb");
+		fp = claws_fopen(filename, "wb");
 		
 		if (!fp) {
 			g_free(filename);
@@ -666,7 +666,7 @@ GSList *news_get_group_list(Folder *folder)
 		}
 		session = news_session_get(folder);
 		if (!session) {
-			fclose(fp);
+			claws_fclose(fp);
 			g_free(filename);
 			return NULL;
 		}
@@ -678,7 +678,7 @@ GSList *news_get_group_list(Folder *folder)
 				session_destroy(SESSION(session));
 				REMOTE_FOLDER(folder)->session = NULL;
 			}
-			fclose(fp);
+			claws_fclose(fp);
 			g_free(filename);
 			return NULL;
 		}
@@ -695,7 +695,7 @@ GSList *news_get_group_list(Folder *folder)
 					log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
 					session_destroy(SESSION(session));
 					REMOTE_FOLDER(folder)->session = NULL;
-					fclose(fp);
+					claws_fclose(fp);
 					g_free(filename);
 					newsnntp_list_free(grouplist);
 					return NULL;
@@ -703,7 +703,7 @@ GSList *news_get_group_list(Folder *folder)
 			}
 			newsnntp_list_free(grouplist);
 		}
-		if (safe_fclose(fp) == EOF) {
+		if (claws_safe_fclose(fp) == EOF) {
 			log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
 			session_destroy(SESSION(session));
 			REMOTE_FOLDER(folder)->session = NULL;
@@ -711,14 +711,14 @@ GSList *news_get_group_list(Folder *folder)
 			return NULL;
 		}
 
-		if ((fp = g_fopen(filename, "rb")) == NULL) {
-			FILE_OP_ERROR(filename, "fopen");
+		if ((fp = claws_fopen(filename, "rb")) == NULL) {
+			FILE_OP_ERROR(filename, "claws_fopen");
 			g_free(filename);
 			return NULL;
 		}
 	}
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar *p = buf;
 		gchar *name;
 		gint last_num;
@@ -745,7 +745,7 @@ GSList *news_get_group_list(Folder *folder)
 		}
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 	g_free(filename);
 
 	list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
@@ -949,8 +949,8 @@ gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
 	if (tmp == NULL)
 		return -1;
 
-	if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
-		FILE_OP_ERROR(tmp, "fopen");
+	if ((tmpfp = claws_fopen(tmp, "wb")) == NULL) {
+		FILE_OP_ERROR(tmp, "claws_fopen");
 		return -1;
 	}
 	if (change_file_mode_rw(tmpfp, tmp) < 0) {
@@ -979,14 +979,14 @@ gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
 		       msginfo->from,
 		       date) < 0) {
 		FILE_OP_ERROR(tmp, "fprintf");
-		fclose(tmpfp);
+		claws_fclose(tmpfp);
 		claws_unlink(tmp);
 		g_free(tmp);
 		return -1;
 	}
 
-	if (safe_fclose(tmpfp) == EOF) {
-		FILE_OP_ERROR(tmp, "fclose");
+	if (claws_safe_fclose(tmpfp) == EOF) {
+		FILE_OP_ERROR(tmp, "claws_fclose");
 		claws_unlink(tmp);
 		g_free(tmp);
 		return -1;
diff --git a/src/partial_download.c b/src/partial_download.c
index 9d3c7d2..76b25fc 100644
--- a/src/partial_download.c
+++ b/src/partial_download.c
@@ -90,14 +90,14 @@ int partial_msg_in_uidl_list(MsgInfo *msginfo)
 	path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 			   "uidl", G_DIR_SEPARATOR_S, msginfo->extradata->account_server,
 			   "-", msginfo->extradata->account_login, NULL);
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 		g_free(path);
 		path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 				   "uidl-", msginfo->extradata->account_server,
 				   "-", sanitized_uid, NULL);
-		if ((fp = g_fopen(path, "rb")) == NULL) {
-			if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+		if ((fp = claws_fopen(path, "rb")) == NULL) {
+			if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 			g_free(sanitized_uid);
 			g_free(path);
 			return FALSE;
@@ -108,7 +108,7 @@ int partial_msg_in_uidl_list(MsgInfo *msginfo)
 
 	now = time(NULL);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar tmp[POPBUFSIZE];
 		strretchomp(buf);
 		recv_time = RECV_TIME_NONE;
@@ -122,12 +122,12 @@ int partial_msg_in_uidl_list(MsgInfo *msginfo)
 			}
 		}
 		if (!strcmp(uidl, msginfo->extradata->partial_recv)) {
-			fclose(fp);
+			claws_fclose(fp);
 			return TRUE;
 		}
 	}
 
-	fclose(fp);	
+	claws_fclose(fp);	
 	return FALSE;
 }
 
@@ -171,15 +171,14 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 			   "uidl", G_DIR_SEPARATOR_S, tinfo->extradata->account_server,
 			   "-", sanitized_uid, NULL);
 
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		FILE_OP_ERROR(path, "fopen");
-		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 		g_free(path);
 		path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 				   "uidl-", tinfo->extradata->account_server,
 				   "-", tinfo->extradata->account_login, NULL);
-		if ((fp = g_fopen(path, "rb")) == NULL) {
-			if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+		if ((fp = claws_fopen(path, "rb")) == NULL) {
+			if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 			g_free(path);
 			goto bail;
 		}
@@ -191,16 +190,16 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 	
 	g_free(sanitized_uid);
 
-	if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
-		FILE_OP_ERROR(pathnew, "fopen");
-		fclose(fp);
+	if ((fpnew = claws_fopen(pathnew, "wb")) == NULL) {
+		FILE_OP_ERROR(pathnew, "claws_fopen");
+		claws_fclose(fp);
 		g_free(pathnew);
 		goto bail;
 	}
 	
 	now = time(NULL);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		strretchomp(buf);
 		recv_time = RECV_TIME_NONE;
 		sprintf(partial_recv,"0");
@@ -217,8 +216,8 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 			if (fprintf(fpnew, "%s\t%ld\t%s\n", 
 				uidl, (long int) recv_time, partial_recv) < 0) {
 				FILE_OP_ERROR(pathnew, "fprintf");
-				fclose(fpnew);
-				fclose(fp);
+				claws_fclose(fpnew);
+				claws_fclose(fp);
 				g_free(path);
 				g_free(pathnew);
 				goto bail;
@@ -240,8 +239,8 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 			if (fprintf(fpnew, "%s\t%ld\t%s\n", 
 				uidl, (long int) recv_time, stat) < 0) {
 				FILE_OP_ERROR(pathnew, "fprintf");
-				fclose(fpnew);
-				fclose(fp);
+				claws_fclose(fpnew);
+				claws_fclose(fp);
 				g_free(path);
 				g_free(pathnew);
 				goto bail;
@@ -249,28 +248,28 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 			g_free(stat);
 		}
 	}
-	if (safe_fclose(fpnew) == EOF) {
-		FILE_OP_ERROR(pathnew, "fclose");
-		fclose(fp);
+	if (claws_safe_fclose(fpnew) == EOF) {
+		FILE_OP_ERROR(pathnew, "claws_fclose");
+		claws_fclose(fp);
 		g_free(path);
 		g_free(pathnew);
 		goto bail;
 	}
-	fclose(fp);
+	claws_fclose(fp);
 
 	move_file(pathnew, path, TRUE);
 
 	g_free(path);
 	g_free(pathnew);
 	
-	if ((fp = g_fopen(filename,"rb")) == NULL) {
-		FILE_OP_ERROR(filename, "fopen");
+	if ((fp = claws_fopen(filename,"rb")) == NULL) {
+		FILE_OP_ERROR(filename, "claws_fopen");
 		goto bail;
 	}
 	pathnew = g_strdup_printf("%s.new", filename);
-	if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
-		FILE_OP_ERROR(pathnew, "fopen");
-		fclose(fp);
+	if ((fpnew = claws_fopen(pathnew, "wb")) == NULL) {
+		FILE_OP_ERROR(pathnew, "claws_fopen");
+		claws_fclose(fp);
 		g_free(pathnew);
 		goto bail;
 	}
@@ -278,20 +277,20 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 	if (fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
 			download) < 0) {
 		FILE_OP_ERROR(pathnew, "fprintf");
-		fclose(fpnew);
-		fclose(fp);
+		claws_fclose(fpnew);
+		claws_fclose(fp);
 		g_free(pathnew);
 		goto bail;
 	}
-	while (fgets(buf, sizeof(buf)-1, fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf)-1, fp) != NULL) {
 		if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
 		&& !strncmp(buf, "SC-Marked-For-Download:", 
 		            strlen("SC-Marked-For-Download:"))) {
 			if (fprintf(fpnew, "%s", 
 			 buf+strlen("SC-Marked-For-Download: x\n")) < 0) {
 				FILE_OP_ERROR(pathnew, "fprintf");
-				fclose(fpnew);
-				fclose(fp);
+				claws_fclose(fpnew);
+				claws_fclose(fp);
 				g_free(pathnew);
 				goto bail;
 			}
@@ -303,20 +302,20 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 		}
 		if (fprintf(fpnew, "%s", buf) < 0) {
 			FILE_OP_ERROR(pathnew, "fprintf");
-			fclose(fpnew);
-			fclose(fp);
+			claws_fclose(fpnew);
+			claws_fclose(fp);
 			g_free(pathnew);
 			goto bail;
 		}
 	}
-	if (safe_fclose(fpnew) == EOF) {
-		FILE_OP_ERROR(pathnew, "fclose");
-		fclose(fp);
+	if (claws_safe_fclose(fpnew) == EOF) {
+		FILE_OP_ERROR(pathnew, "claws_fclose");
+		claws_fclose(fp);
 		g_free(pathnew);
 		goto bail;
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 	if (rename_force(pathnew, filename) != 0) {
 		g_free(pathnew);
 		goto bail;
@@ -395,14 +394,14 @@ gchar *partial_get_filename(const gchar *server, const gchar *login,
 	path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 			   "uidl", G_DIR_SEPARATOR_S, 
 			   server, "-", sanitized_uid, NULL);
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 		g_free(path);
 		path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 				   "uidl-", server,
 				   "-", sanitized_uid, NULL);
-		if ((fp = g_fopen(path, "rb")) == NULL) {
-			if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+		if ((fp = claws_fopen(path, "rb")) == NULL) {
+			if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 			g_free(sanitized_uid);
 			g_free(path);
 			return result;
@@ -413,7 +412,7 @@ gchar *partial_get_filename(const gchar *server, const gchar *login,
 
 	now = time(NULL);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar tmp[POPBUFSIZE];
 		strretchomp(buf);
 		recv_time = RECV_TIME_NONE;
@@ -432,7 +431,7 @@ gchar *partial_get_filename(const gchar *server, const gchar *login,
 		}
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 	
 	return result;
 }
diff --git a/src/passwordstore.c b/src/passwordstore.c
index 377e182..ba30349 100644
--- a/src/passwordstore.c
+++ b/src/passwordstore.c
@@ -37,6 +37,7 @@
 #include "prefs_common.h"
 #include "prefs_gtk.h"
 #include "prefs_migration.h"
+#include "claws_io.h"
 
 static GSList *_password_store;
 
@@ -348,16 +349,16 @@ static gint _write_to_file(FILE *fp)
 
 	/* Write out the config_version */
 	line = g_strdup_printf("[config_version:%d]\n", CLAWS_CONFIG_VERSION);
-	if (fputs(line, fp) == EOF) {
-		FILE_OP_ERROR("password store, config_version", "fputs");
+	if (claws_fputs(line, fp) == EOF) {
+		FILE_OP_ERROR("password store, config_version", "claws_fputs");
 		g_free(line);
 		return -1;
 	}
 	g_free(line);
 
 	/* Add a newline if needed */
-	if (_password_store != NULL && fputs("\n", fp) == EOF) {
-		FILE_OP_ERROR("password store", "fputs");
+	if (_password_store != NULL && claws_fputs("\n", fp) == EOF) {
+		FILE_OP_ERROR("password store", "claws_fputs");
 		return -1;
 	}
 
@@ -382,8 +383,8 @@ static gint _write_to_file(FILE *fp)
 		}
 		line = g_strdup_printf("[%s:%s]\n", typestr, block->block_name);
 
-		if (fputs(line, fp) == EOF) {
-			FILE_OP_ERROR("password store", "fputs");
+		if (claws_fputs(line, fp) == EOF) {
+			FILE_OP_ERROR("password store", "claws_fputs");
 			g_free(line);
 			return -1;
 		}
@@ -397,8 +398,8 @@ static gint _write_to_file(FILE *fp)
 				continue;
 
 			line = g_strdup_printf("%s %s\n", key, pwd);
-			if (fputs(line, fp) == EOF) {
-				FILE_OP_ERROR("password store", "fputs");
+			if (claws_fputs(line, fp) == EOF) {
+				FILE_OP_ERROR("password store", "claws_fputs");
 				g_free(line);
 				return -1;
 			}
@@ -407,8 +408,8 @@ static gint _write_to_file(FILE *fp)
 		g_list_free(keys);
 
 		/* Add a separating new line if there is another block remaining */
-		if (item->next != NULL && fputs("\n", fp) == EOF) {
-			FILE_OP_ERROR("password store", "fputs");
+		if (item->next != NULL && claws_fputs("\n", fp) == EOF) {
+			FILE_OP_ERROR("password store", "claws_fputs");
 			return -1;
 		}
 
diff --git a/src/pine.c b/src/pine.c
index 5d29ee5..d4b11d1 100644
--- a/src/pine.c
+++ b/src/pine.c
@@ -30,6 +30,7 @@
 #include "pine.h"
 #include "addritem.h"
 #include "addrcache.h"
+#include "claws_io.h"
 
 #define PINE_HOME_FILE  ".addressbook"
 #define PINEBUFSIZE     2048
@@ -76,7 +77,7 @@ void pine_free( PineFile *pineFile ) {
 	cm_return_if_fail( pineFile != NULL );
 
 	/* Close file */
-	if( pineFile->file ) fclose( pineFile->file );
+	if( pineFile->file ) claws_fclose( pineFile->file );
 
 	/* Free internal stuff */
 	g_free( pineFile->path );
@@ -103,7 +104,7 @@ void pine_free( PineFile *pineFile ) {
  */
 static gint pine_open_file( PineFile* pineFile ) {
 	if( pineFile->path ) {
-		pineFile->file = g_fopen( pineFile->path, "rb" );
+		pineFile->file = claws_fopen( pineFile->path, "rb" );
 		if( ! pineFile->file ) {
 			pineFile->retVal = MGU_OPEN_FILE;
 			return pineFile->retVal;
@@ -126,7 +127,7 @@ static gint pine_open_file( PineFile* pineFile ) {
  */
 static void pine_close_file( PineFile *pineFile ) {
 	cm_return_if_fail( pineFile != NULL );
-	if( pineFile->file ) fclose( pineFile->file );
+	if( pineFile->file ) claws_fclose( pineFile->file );
 	pineFile->file = NULL;
 }
 
@@ -140,7 +141,7 @@ static gchar *pine_read_line( PineFile *pineFile ) {
 	int c, i = 0;
 	gchar ch;
 
-	if( feof( pineFile->file ) ) 
+	if( claws_feof( pineFile->file ) ) 
 		return NULL;
 
 	while( i < PINEBUFSIZE-1 ) {
@@ -651,8 +652,8 @@ gchar *pine_find_file( void ) {
 	strncat( str, PINE_HOME_FILE, WORK_BUFLEN - strlen(str) );
 
 	/* Attempt to open */
-	if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
-		fclose( fp );
+	if( ( fp = claws_fopen( str, "rb" ) ) != NULL ) {
+		claws_fclose( fp );
 	}
 	else {
 		/* Truncate filename */
diff --git a/src/plugins/acpi_notifier/acpi_notifier.c b/src/plugins/acpi_notifier/acpi_notifier.c
index e6e07e0..13133e5 100644
--- a/src/plugins/acpi_notifier/acpi_notifier.c
+++ b/src/plugins/acpi_notifier/acpi_notifier.c
@@ -651,16 +651,16 @@ static void acpi_set(gboolean on)
 	}
 
 	if (!is_program(acpiprefs.file_path)) {
-		fp = fopen(acpiprefs.file_path, "wb");
+		fp = claws_fopen(acpiprefs.file_path, "wb");
 		if (fp == NULL)
 			return;
 
 		if (on) {
-			fwrite(acpiprefs.on_param, 1, strlen(acpiprefs.on_param), fp);
+			claws_fwrite(acpiprefs.on_param, 1, strlen(acpiprefs.on_param), fp);
 		} else {
-			fwrite(acpiprefs.off_param, 1, strlen(acpiprefs.off_param), fp);
+			claws_fwrite(acpiprefs.off_param, 1, strlen(acpiprefs.off_param), fp);
 		}
-		safe_fclose(fp);
+		claws_safe_fclose(fp);
 	} else {
 		gchar *cmd = g_strdup_printf("%s %s", 
 				acpiprefs.file_path,
diff --git a/src/plugins/bogofilter/bogofilter.c b/src/plugins/bogofilter/bogofilter.c
index c6a978d..590d392 100644
--- a/src/plugins/bogofilter/bogofilter.c
+++ b/src/plugins/bogofilter/bogofilter.c
@@ -252,14 +252,14 @@ static void bogofilter_do_filter(BogoFilterData *data)
 					    FOLDER_TYPE(msginfo->folder->folder) == F_MH &&
 					    config.insert_header) {
 						gchar *tmpfile = get_tmp_file();
-						FILE *input = g_fopen(file, "r");
-						FILE *output = g_fopen(tmpfile, "w");
+						FILE *input = claws_fopen(file, "r");
+						FILE *output = claws_fopen(tmpfile, "w");
 						if (strstr(parts[2], "\n"))
 							*(strstr(parts[2], "\n")) = '\0';
 						if (input && !output) 
-							fclose (input);
+							claws_fclose (input);
 						else if (!input && output)
-							fclose (output);
+							claws_fclose (output);
 						else if (input && output) {
 							gchar tmpbuf[BUFFSIZE];
 							gboolean err = FALSE;
@@ -269,18 +269,18 @@ static void bogofilter_do_filter(BogoFilterData *data)
 									"X-Bogosity: %s, spamicity=%s%s\n",
 									bogosity, parts[2],
 									whitelisted?" [whitelisted]":"");
-							if (fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
+							if (claws_fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
 								err = TRUE;
 							} else {
-								while (fgets(tmpbuf, sizeof(buf), input)) {
-									if (fputs(tmpbuf, output) == EOF) {
+								while (claws_fgets(tmpbuf, sizeof(buf), input)) {
+									if (claws_fputs(tmpbuf, output) == EOF) {
 										err = TRUE;
 										break;
 									}
 								}
 							}
-							fclose(input);
-							if (safe_fclose(output) == EOF)
+							claws_fclose(input);
+							if (claws_safe_fclose(output) == EOF)
 								err = TRUE;
 							if (!err)
 								move_file(tmpfile, file, TRUE);
diff --git a/src/plugins/clamd/libclamd/clamd-plugin.c b/src/plugins/clamd/libclamd/clamd-plugin.c
index 15122bb..831e035 100644
--- a/src/plugins/clamd/libclamd/clamd-plugin.c
+++ b/src/plugins/clamd/libclamd/clamd-plugin.c
@@ -55,6 +55,7 @@
 #include "statusbar.h"
 #include "alertpanel.h"
 #include "clamd-plugin.h"
+#include "claws_io.h"
 
 #ifndef UNIX_PATH_MAX
 #define UNIX_PATH_MAX 108
@@ -115,13 +116,13 @@ void clamd_create_config_automatic(const gchar* path) {
 	config->ConfigType = AUTOMATIC;
 	config->automatic.folder = g_strdup(path);
 	debug_print("Opening %s to parse config file\n", path);
-	conf = fopen(path, "r");
+	conf = claws_fopen(path, "r");
 	if (!conf) {
 		/*g_error("%s: Unable to open", path);*/
 		alertpanel_error(_("%s: Unable to open\nclamd will be disabled"), path);
 		return;
 	}
-	while (fgets(buf, sizeof(buf), conf)) {
+	while (claws_fgets(buf, sizeof(buf), conf)) {
 		g_strstrip(buf);
 		if (buf[0] == '#')
 			continue;
@@ -146,7 +147,7 @@ void clamd_create_config_automatic(const gchar* path) {
 						Socket->socket.path = g_strdup(value);
 						g_free(value);
 						value = NULL;
-						fclose(conf);
+						claws_fclose(conf);
 						debug_print("clamctl: %s\n", Socket->socket.path);
 						return;
 					}
@@ -210,7 +211,7 @@ void clamd_create_config_automatic(const gchar* path) {
 			}
 		}
 	}
-	fclose(conf);
+	claws_fclose(conf);
 	if (! (Socket && (Socket->socket.port || Socket->socket.path))) {
 		/*g_error("%s: Not able to find required information", path);*/
 		alertpanel_error(_("%s: Not able to find required information\nclamd will be disabled"), path);
diff --git a/src/plugins/fancy/claws.def b/src/plugins/fancy/claws.def
index a7fa4db..9a6ff50 100644
--- a/src/plugins/fancy/claws.def
+++ b/src/plugins/fancy/claws.def
@@ -5,6 +5,10 @@ alertpanel
 alertpanel_error
 check_plugin_version
 claws_do_idle
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 combobox_get_active_data
 combobox_select_by_data
 combobox_text_new
@@ -85,7 +89,6 @@ procmsg_msginfo_unset_flags
 procmsg_register_spam_learner
 procmsg_spam_set_folder
 procmsg_unregister_spam_learner
-safe_fclose
 settings
 start_address_completion
 statusbar_pop_all
diff --git a/src/plugins/fancy/fancy_viewer.c b/src/plugins/fancy/fancy_viewer.c
index a1b434f..b4933c3 100644
--- a/src/plugins/fancy/fancy_viewer.c
+++ b/src/plugins/fancy/fancy_viewer.c
@@ -645,11 +645,11 @@ static size_t download_file_curl_write_cb(void *buffer, size_t size,
 {
 	FancyViewer *viewer = (FancyViewer *)data;
 	if (!viewer->stream) {
-		viewer->stream = fopen(viewer->curlfile, "wb");
+		viewer->stream = claws_fopen(viewer->curlfile, "wb");
 		if (!viewer->stream)
 			return -1;
 	}
-	return fwrite(buffer, size, nmemb, viewer->stream);
+	return claws_fwrite(buffer, size, nmemb, viewer->stream);
 }
 static void *download_file_curl (void *data)
 {
@@ -671,7 +671,7 @@ static void *download_file_curl (void *data)
 		if (CURLE_OK != res)
 			alertpanel_error(_("An error occurred: %d\n"), res);
 		if (viewer->stream)
-			safe_fclose(viewer->stream);
+			claws_safe_fclose(viewer->stream);
 		curl_global_cleanup();
 	}
 #ifdef USE_PTHREAD
diff --git a/src/plugins/libravatar/claws.def b/src/plugins/libravatar/claws.def
index 924b5d1..3fec5b6 100644
--- a/src/plugins/libravatar/claws.def
+++ b/src/plugins/libravatar/claws.def
@@ -6,6 +6,10 @@ alertpanel_notice
 alertpanel_warning
 auto_configure_service_sync
 claws_unlink
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 extract_address
 get_locale_dir
 check_plugin_version
@@ -36,6 +40,5 @@ prefs_write_param
 prefs_common_get_prefs
 procmsg_msginfo_add_avatar
 procmsg_msginfo_get_avatar
-safe_fclose
 slist_free_strings_full
 to_human_readable
diff --git a/src/plugins/libravatar/libravatar_image.c b/src/plugins/libravatar/libravatar_image.c
index 1482ae1..20bc3b2 100644
--- a/src/plugins/libravatar/libravatar_image.c
+++ b/src/plugins/libravatar/libravatar_image.c
@@ -36,7 +36,7 @@
 
 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
 {
-	size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
+	size_t written = claws_fwrite(ptr, size, nmemb, (FILE *)stream);
 	debug_print("received %zu bytes from avatar server\n", written);
 
 	return written;
@@ -74,7 +74,7 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
 	CURL *curl;
 	long filesize;
 
-	file = fopen(filename, "wb");
+	file = claws_fopen(filename, "wb");
 	if (file == NULL) {
 		g_warning("could not open '%s' for writing", filename);
 		return NULL;
@@ -82,7 +82,7 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
 	curl = curl_easy_init();
 	if (curl == NULL) {
 		g_warning("could not initialize curl to get image from URL");
-		fclose(file);
+		claws_fclose(file);
 		return NULL;
 	}
 
@@ -107,7 +107,7 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
 	debug_print("retrieving URL to file: %s -> %s\n", url, filename);
 	curl_easy_perform(curl);
 	filesize = ftell(file);
-	safe_fclose(file);
+	claws_safe_fclose(file);
 	if (filesize < MIN_PNG_SIZE)
 		debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
 	else
diff --git a/src/plugins/libravatar/libravatar_missing.c b/src/plugins/libravatar/libravatar_missing.c
index a19de48..dc2d81f 100644
--- a/src/plugins/libravatar/libravatar_missing.c
+++ b/src/plugins/libravatar/libravatar_missing.c
@@ -33,7 +33,7 @@
  */
 GHashTable *missing_load_from_file(const gchar *filename)
 {
-	FILE *file = fopen(filename, "r");
+	FILE *file = claws_fopen(filename, "r");
 	time_t t;
 	long long unsigned seen;
 	gchar md5sum[33];
@@ -66,7 +66,7 @@ GHashTable *missing_load_from_file(const gchar *filename)
 	}
 
 close_exit:
-	if (fclose(file) != 0)
+	if (claws_fclose(file) != 0)
 		g_warning("error closing '%s'", filename);
 
 	debug_print("Read %d missing avatar entries, %d obsolete entries discarded\n", a, d);
@@ -84,7 +84,7 @@ static void missing_save_item(gpointer key, gpointer value, gpointer data)
 {
 	FILE *file = (FILE *)data;
 	gchar *line = g_strdup_printf("%s %llu\n", (gchar *)key, *((long long unsigned *)value));
-	if (fputs(line, file) < 0)
+	if (claws_fputs(line, file) < 0)
 		g_warning("error saving missing item");
 	g_free(line);
 }
@@ -99,7 +99,7 @@ static void missing_save_item(gpointer key, gpointer value, gpointer data)
  */
 gint missing_save_to_file(GHashTable *table, const gchar *filename)
 {
-	FILE *file = fopen(filename, "w");
+	FILE *file = claws_fopen(filename, "w");
 
 	if (file == NULL) {
 		g_warning("cannot open '%s' for writing", filename);
@@ -109,7 +109,7 @@ gint missing_save_to_file(GHashTable *table, const gchar *filename)
 	g_hash_table_foreach(table, missing_save_item, (gpointer)file);
 	debug_print("Saved %u missing avatar entries\n", g_hash_table_size(table));
 
-	if (safe_fclose(file) != 0) {
+	if (claws_safe_fclose(file) != 0) {
 		g_warning("error closing '%s'", filename);
 		return -1;
 	}
diff --git a/src/plugins/mailmbox/claws.def b/src/plugins/mailmbox/claws.def
index 0f686d3..0efcc45 100644
--- a/src/plugins/mailmbox/claws.def
+++ b/src/plugins/mailmbox/claws.def
@@ -3,6 +3,10 @@ EXPORTS
 get_locale_dir
 check_plugin_version
 alertpanel
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 conv_codeset_strdup
 conv_get_locale_charset_str_no_utf8
 debug_print_real
@@ -28,4 +32,3 @@ prefs_set_default
 prefs_write_open
 prefs_write_param
 prefs_common_get_prefs
-safe_fclose
\ No newline at end of file
diff --git a/src/plugins/mailmbox/mailimf.c b/src/plugins/mailmbox/mailimf.c
index af49ca4..e932ca7 100644
--- a/src/plugins/mailmbox/mailimf.c
+++ b/src/plugins/mailmbox/mailimf.c
@@ -33,7 +33,13 @@
  * $Id$
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include "mailimf.h"
+#include "claws_io.h"
 
 /*
   RFC 2822
diff --git a/src/plugins/mailmbox/mailimf_write.c b/src/plugins/mailmbox/mailimf_write.c
index c592e8f..27352b5 100644
--- a/src/plugins/mailmbox/mailimf_write.c
+++ b/src/plugins/mailmbox/mailimf_write.c
@@ -33,11 +33,17 @@
  * $Id$
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include "mailimf_write.h"
 
 #include <time.h>
 #include <string.h>
 #include <ctype.h>
+#include "claws_io.h"
 
 #define MAX_MAIL_COL 72
 
@@ -128,23 +134,6 @@ mailimf_resent_msg_id_write(FILE * f, int * col,
 
 /* ************************ */
 
-#if 0
-int mailimf_string_write(FILE * f, int * col,
-			 char * str, size_t length)
-{
-  int r;
-
-  if (length != 0) {
-    r = fwrite(str, sizeof(char), length, f);
-    if (r < 0)
-      return MAILIMF_ERROR_FILE;
-    * col += length;
-  }
-
-  return MAILIMF_NO_ERROR;
-}
-#endif
-
 #define CRLF "\r\n"
 #define HEADER_FOLD "\r\n "
 
@@ -153,7 +142,7 @@ static inline int flush_buf(FILE * f, const char * str, size_t length)
   if (length != 0) {
     int r;
     
-    r = fwrite(str, 1, length, f);
+    r = claws_fwrite(str, 1, length, f);
     if (r == 0)
       return MAILIMF_ERROR_FILE;
   }
@@ -192,7 +181,7 @@ int mailimf_string_write(FILE * f, int * col,
       if (r != MAILIMF_NO_ERROR)
         return r;
       
-      r = fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
+      r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
       if (r == 0)
         return MAILIMF_ERROR_FILE;
       
@@ -208,7 +197,7 @@ int mailimf_string_write(FILE * f, int * col,
       if (r != MAILIMF_NO_ERROR)
         return r;
       
-      r = fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
+      r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
       if (r == 0)
         return MAILIMF_ERROR_FILE;
       
@@ -228,7 +217,7 @@ int mailimf_string_write(FILE * f, int * col,
           if (r != MAILIMF_NO_ERROR)
             return r;
           
-          r = fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
+          r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
           if (r == 0)
             return MAILIMF_ERROR_FILE;
           
@@ -247,7 +236,7 @@ int mailimf_string_write(FILE * f, int * col,
         if (r != MAILIMF_NO_ERROR)
           return r;
         
-        r = fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
+        r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
         if (r == 0)
           return MAILIMF_ERROR_FILE;
         
@@ -1385,29 +1374,29 @@ int mailimf_quoted_string_write(FILE * f, int * col,
   int r;
   size_t i;
 
-  fputc('\"', f);
+  claws_fputc('\"', f);
   for(i = 0 ; i < len ; i ++) {
     switch (string[i]) {
     case '\\':
     case '\"':
-      r = fputc('\\', f);
+      r = claws_fputc('\\', f);
       if (r < 0)
 	return MAILIMF_ERROR_FILE;
-      r = fputc(string[i], f);
+      r = claws_fputc(string[i], f);
       if (r < 0)
 	return MAILIMF_ERROR_FILE;
       (* col) += 2;
       break;
 
     default:
-      r = fputc(string[i], f);
+      r = claws_fputc(string[i], f);
       if (r < 0)
 	return MAILIMF_ERROR_FILE;
       (* col) ++;
       break;
     }
   }
-  fputc('\"', f);
+  claws_fputc('\"', f);
 
   return MAILIMF_NO_ERROR;
 }
diff --git a/src/plugins/mailmbox/mailmbox.c b/src/plugins/mailmbox/mailmbox.c
index feb2453..b32af40 100644
--- a/src/plugins/mailmbox/mailmbox.c
+++ b/src/plugins/mailmbox/mailmbox.c
@@ -33,6 +33,11 @@
  * $Id$
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include "mailmbox.h"
 
 #include <sys/file.h>
@@ -54,6 +59,7 @@
 #include "mailmbox_parse.h"
 #include "maillock.h"
 #include "utils.h"
+#include "claws_io.h"
 
 /*
   http://www.qmail.org/qmail-manual-html/man5/mbox.html
diff --git a/src/plugins/mailmbox/mailmbox_folder.c b/src/plugins/mailmbox/mailmbox_folder.c
index c8a77d8..569d9b7 100644
--- a/src/plugins/mailmbox/mailmbox_folder.c
+++ b/src/plugins/mailmbox/mailmbox_folder.c
@@ -222,17 +222,17 @@ static void read_max_uid_value(FolderItem *item, guint * pmax_uid)
 	file = g_strconcat(path, G_DIR_SEPARATOR_S, MAX_UID_FILE, NULL);
 	g_free(path);
         
-        f = fopen(file, "r");
+        f = claws_fopen(file, "r");
         g_free(file);
         if (f == NULL)
                 return;
-        r = fread(&max_uid, sizeof(max_uid), 1, f);
+        r = claws_fread(&max_uid, sizeof(max_uid), 1, f);
         if (r == 0) {
-                fclose(f);
+                claws_fclose(f);
                 return;
         }
         
-        fclose(f);
+        claws_fclose(f);
         
         * pmax_uid = max_uid;
 }
@@ -248,17 +248,17 @@ static void write_max_uid_value(FolderItem *item, guint max_uid)
 	file = g_strconcat(path, G_DIR_SEPARATOR_S, MAX_UID_FILE, NULL);
 	g_free(path);
         
-        f = fopen(file, "w");
+        f = claws_fopen(file, "w");
         g_free(file);
         if (f == NULL)
                 return;
-        r = fwrite(&max_uid, sizeof(max_uid), 1, f);
+        r = claws_fwrite(&max_uid, sizeof(max_uid), 1, f);
         if (r == 0) {
-                fclose(f);
+                claws_fclose(f);
                 return;
         }
         
-        safe_fclose(f);
+        claws_safe_fclose(f);
 }
 
 static void claws_mailmbox_folder_item_destroy(Folder *folder, FolderItem *_item)
@@ -463,21 +463,21 @@ static gchar *s_claws_mailmbox_fetch_msg(Folder *folder, FolderItem *item, gint
                 goto free;
         
         old_mask = umask(0077);
-        f = fopen(file, "w");
+        f = claws_fopen(file, "w");
         umask(old_mask);
         if (f == NULL)
                 goto free;
         
-        r = fwrite(data, 1, len, f);
+        r = claws_fwrite(data, 1, len, f);
         if (r == 0)
                 goto close;
         
-        safe_fclose(f);
+        claws_safe_fclose(f);
         
 	return file;
         
  close:
-        fclose(f);
+        claws_fclose(f);
         unlink(file);
  free:
         free(file);
diff --git a/src/plugins/newmail/newmail.c b/src/plugins/newmail/newmail.c
index 46bc1bc..b8c483c 100644
--- a/src/plugins/newmail/newmail.c
+++ b/src/plugins/newmail/newmail.c
@@ -31,6 +31,7 @@
 #include <inttypes.h>
 
 #include "plugin.h"
+#include "claws_io.h"
 
 #define LOG_NAME	"NewLog"
 #define DEFAULT_DIR	"Mail"
@@ -83,7 +84,7 @@ gboolean newmail_hook (gpointer source, gpointer data)
 gboolean plugin_done (void)
 {
 	if (NewLog) {
-		(void)fclose (NewLog);
+		(void)claws_fclose (NewLog);
 		NewLog  = NULL;
 	}
 	if (LogName) {
@@ -118,12 +119,12 @@ gint plugin_init (gchar **error)
 			LogName = g_strconcat(g_getenv ("HOME"), G_DIR_SEPARATOR_S, DEFAULT_DIR,
 					G_DIR_SEPARATOR_S, LOG_NAME, NULL);
 		}
-		if (!(NewLog = fopen (LogName, mode))) {
+		if (!(NewLog = claws_fopen (LogName, mode))) {
 			debug_print ("Failed to open default log %s\n", LogName);
 			/* try fallback location */
 			g_free(LogName);
 			LogName = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, LOG_NAME, NULL);
-			if (!(NewLog = fopen (LogName, mode))) {
+			if (!(NewLog = claws_fopen (LogName, mode))) {
 				debug_print ("Failed to open fallback log %s\n", LogName);
 				*error = g_strdup_printf(_("Could not open log file %s: %s\n"),
 						LogName, g_strerror(errno));
diff --git a/src/plugins/perl/perl_plugin.c b/src/plugins/perl/perl_plugin.c
index afeb83c..a4bdda0 100644
--- a/src/plugins/perl/perl_plugin.c
+++ b/src/plugins/perl/perl_plugin.c
@@ -45,6 +45,7 @@
 #include "common/log.h"
 #include "common/plugin.h"
 #include "common/tags.h"
+#include "claws_io.h"
 
 #include <EXTERN.h>
 #include <perl.h>
@@ -661,8 +662,8 @@ static XS(XS_ClawsMail_open_mail_file)
   file = procmsg_get_message_file_path(msginfo);
   if(!file)
     XSRETURN_UNDEF;
-  if((message_file = fopen(file, "rb")) == NULL) {
-    FILE_OP_ERROR(file, "fopen");
+  if((message_file = claws_fopen(file, "rb")) == NULL) {
+    FILE_OP_ERROR(file, "claws_fopen");
     g_warning("Perl Plugin: File open error in ClawsMail::C::open_mail_file");
     g_free(file);
     XSRETURN_UNDEF;
@@ -679,7 +680,7 @@ static XS(XS_ClawsMail_close_mail_file)
     XSRETURN_UNDEF;
   }
   if(message_file != NULL)
-    fclose(message_file);
+    claws_fclose(message_file);
   XSRETURN_YES;
 }
 
@@ -731,7 +732,7 @@ static XS(XS_ClawsMail_get_next_body_line)
     g_warning("Perl Plugin: Message file not open. Use ClawsMail::C::open_message_file first.");
     XSRETURN_UNDEF;
   }
-  if(fgets(buf, sizeof(buf), message_file) != NULL)
+  if(claws_fgets(buf, sizeof(buf), message_file) != NULL)
     XSRETURN_PV(buf);
   else
     XSRETURN_UNDEF;
@@ -2310,7 +2311,7 @@ gint plugin_init(gchar **error)
 
   /* make sure we have at least an empty scriptfile */
   perlfilter = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, PERLFILTER, NULL);
-  if((fp = fopen(perlfilter, "a")) == NULL) {
+  if((fp = claws_fopen(perlfilter, "a")) == NULL) {
     *error = g_strdup("Failed to create blank scriptfile");
     g_free(perlfilter);
     hooks_unregister_hook(MAIL_FILTERING_HOOKLIST,
@@ -2324,7 +2325,7 @@ gint plugin_init(gchar **error)
     FILE_OP_ERROR(perlfilter, "chmod");
     g_warning("Perl Plugin: Can't change file mode");
   }
-  fclose(fp);
+  claws_fclose(fp);
   g_free(perlfilter);
 
   argc = 1;
diff --git a/src/plugins/pgpcore/claws.def b/src/plugins/pgpcore/claws.def
index be6aaf1..87a931d 100644
--- a/src/plugins/pgpcore/claws.def
+++ b/src/plugins/pgpcore/claws.def
@@ -9,6 +9,10 @@ alertpanel_notice
 check_plugin_version
 claws_do_idle
 claws_unlink
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 conv_codeset_strdup
 conv_get_locale_charset_str_no_utf8
 codeconv_set_strict
diff --git a/src/plugins/pgpcore/pgp_utils.c b/src/plugins/pgpcore/pgp_utils.c
index 66f8e9b..8d3e95c 100644
--- a/src/plugins/pgpcore/pgp_utils.c
+++ b/src/plugins/pgpcore/pgp_utils.c
@@ -30,6 +30,7 @@
 
 #include "pgp_utils.h"
 #include "codeconv.h"
+#include "claws_io.h"
 
 gchar *fp_read_noconv(FILE *fp)
 {
@@ -42,14 +43,14 @@ gchar *fp_read_noconv(FILE *fp)
 		return NULL;
 	array = g_byte_array_new();
 
-	while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
-		if (n_read < sizeof(buf) && ferror(fp))
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(fp))
 			break;
 		g_byte_array_append(array, buf, n_read);
 	}
 
-	if (ferror(fp)) {
-		FILE_OP_ERROR("file stream", "fread");
+	if (claws_ferror(fp)) {
+		FILE_OP_ERROR("file stream", "claws_fread");
 		g_byte_array_free(array, TRUE);
 		return NULL;
 	}
@@ -80,14 +81,14 @@ gchar *get_part_as_string(MimeInfo *mimeinfo)
 			g_free(filename);
 			return NULL;
 		}
-		fp = g_fopen(filename,"rb");
+		fp = claws_fopen(filename,"rb");
 		if (!fp) {
 			g_warning("error opening temporary file '%s'", filename);
 			g_free(filename);
 			return NULL;
 		}
 		textdata = fp_read_noconv(fp);
-		fclose(fp);
+		claws_fclose(fp);
 		g_unlink(filename);
 		g_free(filename);
 	}
diff --git a/src/plugins/pgpcore/sgpgme.c b/src/plugins/pgpcore/sgpgme.c
index f79c8b6..6586295 100644
--- a/src/plugins/pgpcore/sgpgme.c
+++ b/src/plugins/pgpcore/sgpgme.c
@@ -59,6 +59,7 @@
 #include "account.h"
 #include "select-keys.h"
 #include "claws.h"
+#include "claws_io.h"
 
 static void sgpgme_disable_all(void)
 {
@@ -453,13 +454,13 @@ gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
 {
 	gpgme_data_t data = NULL;
 	gpgme_error_t err;
-	FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
+	FILE *fp = claws_fopen(mimeinfo->data.filename, "rb");
 
 	if (!fp) 
 		return NULL;
 
 	err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
-	fclose(fp);
+	claws_fclose(fp);
 
 	debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
 	if (err) {
diff --git a/src/plugins/pgpinline/claws.def b/src/plugins/pgpinline/claws.def
index a07c65f..4162a99 100644
--- a/src/plugins/pgpinline/claws.def
+++ b/src/plugins/pgpinline/claws.def
@@ -1,6 +1,10 @@
 LIBRARY CLAWS-MAIL.EXE
 EXPORTS
 claws_unlink
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 check_plugin_version
 codeconv_set_strict
 conv_codeset_strdup
@@ -22,4 +26,3 @@ procmime_scan_file
 procmime_write_mimeinfo
 procmime_get_part
 procmime_get_tmp_file_name
-safe_fclose
\ No newline at end of file
diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c
index 4f8e789..d4bee07 100644
--- a/src/plugins/pgpinline/pgpinline.c
+++ b/src/plugins/pgpinline/pgpinline.c
@@ -341,8 +341,8 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
     	fname = g_strdup_printf("%s%cplaintext.%08x",
 		get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-    	if ((dstfp = g_fopen(fname, "wb")) == NULL) {
-        	FILE_OP_ERROR(fname, "fopen");
+    	if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+        	FILE_OP_ERROR(fname, "claws_fopen");
 		privacy_set_error(_("Couldn't open decrypted file %s"), fname);
         	g_free(fname);
         	gpgme_data_release(plain);
@@ -367,24 +367,24 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
 	/* Store any part before encrypted text */
 	pos = pgp_locate_armor_header(textdata, begin_indicator);
 	if (pos != NULL && (pos - textdata) > 0) {
-	    if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
-        	FILE_OP_ERROR(fname, "fwrite");
+	    if (claws_fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
+        	FILE_OP_ERROR(fname, "claws_fwrite");
 		privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
 		goto FILE_ERROR;
 	    }
 	}
 	
-	if (fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 1,
+	if (claws_fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 1,
 		strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")), 
 		dstfp) < strlen(_("\n--- Start of PGP/Inline encrypted data ---\n"))) {
-        	FILE_OP_ERROR(fname, "fwrite");
+        	FILE_OP_ERROR(fname, "claws_fwrite");
 		privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
 		goto FILE_ERROR;
 	}
 	chars = sgpgme_data_release_and_get_mem(plain, &len);
 	if (len > 0) {
-		if (fwrite(chars, 1, len, dstfp) < len) {
-        		FILE_OP_ERROR(fname, "fwrite");
+		if (claws_fwrite(chars, 1, len, dstfp) < len) {
+        		FILE_OP_ERROR(fname, "claws_fwrite");
 			g_free(chars);
 			privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
 			goto FILE_ERROR;
@@ -392,10 +392,10 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
 	}
 	g_free(chars);
 	/* Store any part after encrypted text */
-	if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
+	if (claws_fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
 		strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
 		dstfp) < strlen(_("--- End of PGP/Inline encrypted data ---\n"))) {
-        		FILE_OP_ERROR(fname, "fwrite");
+        		FILE_OP_ERROR(fname, "claws_fwrite");
 			privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
 			goto FILE_ERROR;
 	}
@@ -403,16 +403,16 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
 	    pos = pgp_locate_armor_header(pos, end_indicator);
 	    if (pos != NULL && *pos != '\0') {
 		pos += strlen(end_indicator);
-		if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
-        		FILE_OP_ERROR(fname, "fwrite");
+		if (claws_fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
+        		FILE_OP_ERROR(fname, "claws_fwrite");
 			privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
 			goto FILE_ERROR;
 		}
 	    }
 	}
 
-	if (safe_fclose(dstfp) == EOF) {
-        	FILE_OP_ERROR(fname, "fclose");
+	if (claws_safe_fclose(dstfp) == EOF) {
+        	FILE_OP_ERROR(fname, "claws_fclose");
 		privacy_set_error(_("Couldn't close decrypted file %s"), fname);
         	g_free(fname);
         	gpgme_data_release(plain);
@@ -463,7 +463,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
 	return decinfo;
 
 FILE_ERROR:
-	fclose(dstfp);
+	claws_fclose(dstfp);
 	g_free(fname);
 	gpgme_data_release(plain);
 	gpgme_release(ctx);
@@ -510,7 +510,7 @@ static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const
 	/* read temporary file into memory */
 	textstr = fp_read_noconv(fp);
 	
-	fclose(fp);
+	claws_fclose(fp);
 		
 	gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
 	gpgme_data_new(&gpgsig);
@@ -704,7 +704,7 @@ static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 	/* read temporary file into memory */
 	textstr = fp_read_noconv(fp);
 	
-	fclose(fp);
+	claws_fclose(fp);
 
 	/* encrypt data */
 	gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
diff --git a/src/plugins/pgpmime/claws.def b/src/plugins/pgpmime/claws.def
index 0d01d49..3855a69 100644
--- a/src/plugins/pgpmime/claws.def
+++ b/src/plugins/pgpmime/claws.def
@@ -1,6 +1,10 @@
 LIBRARY CLAWS-MAIL.EXE
 EXPORTS
 canonicalize_str
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 check_plugin_version
 debug_print_real
 debug_srcname
@@ -18,4 +22,3 @@ procmime_mimeinfo_new
 procmime_mimeinfo_parent
 procmime_scan_file
 procmime_write_mimeinfo
-safe_fclose
\ No newline at end of file
diff --git a/src/plugins/pgpmime/pgpmime.c b/src/plugins/pgpmime/pgpmime.c
index 3827487..d768de4 100644
--- a/src/plugins/pgpmime/pgpmime.c
+++ b/src/plugins/pgpmime/pgpmime.c
@@ -144,12 +144,12 @@ static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
 	gchar buf[BUFFSIZE];
 
 	boundary_len = strlen(boundary);
-	while (fgets(buf, sizeof(buf), fp) != NULL)
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL)
 		if (IS_BOUNDARY(buf, boundary, boundary_len))
 			break;
 
 	textbuffer = g_string_new("");
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar *buf2;
 
 		if (IS_BOUNDARY(buf, boundary, boundary_len))
@@ -196,13 +196,13 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
 	}
 	parent = procmime_mimeinfo_parent(mimeinfo);
 
-	fp = g_fopen(parent->data.filename, "rb");
+	fp = claws_fopen(parent->data.filename, "rb");
 	cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
 	
 	boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
 	if (!boundary) {
 		privacy_set_error(_("Signature boundary not found."));
-		fclose(fp);
+		claws_fclose(fp);
 		return 0;
 	}
 	textstr = get_canonical_content(fp, boundary);
@@ -231,7 +231,7 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
 	gpgme_data_release(sigdata);
 	gpgme_data_release(textdata);
 	g_free(textstr);
-	fclose(fp);
+	claws_fclose(fp);
 	
 	return 0;
 }
@@ -348,8 +348,8 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
     	fname = g_strdup_printf("%s%cplaintext.%08x",
 		get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-    	if ((dstfp = g_fopen(fname, "wb")) == NULL) {
-        	FILE_OP_ERROR(fname, "fopen");
+    	if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+        	FILE_OP_ERROR(fname, "claws_fopen");
 		privacy_set_error(_("Couldn't open decrypted file %s"), fname);
         	g_free(fname);
         	gpgme_data_release(plain);
@@ -360,7 +360,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 
 	if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
         	FILE_OP_ERROR(fname, "fprintf");
-		fclose(dstfp);
+		claws_fclose(dstfp);
 		privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
         	g_free(fname);
         	gpgme_data_release(plain);
@@ -371,10 +371,10 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 
 	chars = sgpgme_data_release_and_get_mem(plain, &len);
 	if (len > 0) {
-		if (fwrite(chars, 1, len, dstfp) < len) {
-        		FILE_OP_ERROR(fname, "fwrite");
+		if (claws_fwrite(chars, 1, len, dstfp) < len) {
+        		FILE_OP_ERROR(fname, "claws_fwrite");
 			g_free(chars);
-			fclose(dstfp);
+			claws_fclose(dstfp);
 			privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
         		g_free(fname);
         		gpgme_data_release(plain);
@@ -385,8 +385,8 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 	}
 	g_free(chars);
 
-	if (safe_fclose(dstfp) == EOF) {
-        	FILE_OP_ERROR(fname, "fclose");
+	if (claws_safe_fclose(dstfp) == EOF) {
+        	FILE_OP_ERROR(fname, "claws_fclose");
 		privacy_set_error(_("Couldn't close decrypted file %s"), fname);
         	g_free(fname);
         	gpgme_data_release(plain);
@@ -462,7 +462,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
 
 	/* read temporary file into memory */
 	test_msg = file_read_stream_to_str(fp);
-	fclose(fp);
+	claws_fclose(fp);
 	
 	memset (&info, 0, sizeof info);
 
@@ -503,7 +503,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
 	textstr = get_canonical_content(fp, boundary);
 
 	g_free(boundary);
-	fclose(fp);
+	claws_fclose(fp);
 
 	gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
 	gpgme_data_new(&gpgsig);
@@ -703,7 +703,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 	textstr = get_canonical_content(fp, boundary);
 
 	g_free(boundary);
-	fclose(fp);
+	claws_fclose(fp);
 
 	/* encrypt data */
 	gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
diff --git a/src/plugins/python/python_plugin.c b/src/plugins/python/python_plugin.c
index dac6298..bce0f7b 100644
--- a/src/plugins/python/python_plugin.c
+++ b/src/plugins/python/python_plugin.c
@@ -39,6 +39,7 @@
 #include "python-shell.h"
 #include "python-hooks.h"
 #include "clawsmailmodule.h"
+#include "claws_io.h"
 
 #define PYTHON_SCRIPTS_BASE_DIR "python-scripts"
 #define PYTHON_SCRIPTS_MAIN_DIR "main"
@@ -158,7 +159,7 @@ static gchar* extract_filename(const gchar *str)
 static void run_script_file(const gchar *filename, Compose *compose)
 {
   FILE *fp;
-  fp = fopen(filename, "r");
+  fp = claws_fopen(filename, "r");
   if(!fp) {
     debug_print("Error: Could not open file '%s'\n", filename);
     return;
@@ -166,7 +167,7 @@ static void run_script_file(const gchar *filename, Compose *compose)
   put_composewindow_into_module(compose);
   if(PyRun_SimpleFile(fp, filename) == 0)
     debug_print("Problem running script file '%s'\n", filename);
-  fclose(fp);
+  claws_fclose(fp);
 }
 
 static void run_auto_script_file_if_it_exists(const gchar *autofilename, Compose *compose)
diff --git a/src/plugins/rssyl/claws.def b/src/plugins/rssyl/claws.def
index 6fc560f..85de45e 100644
--- a/src/plugins/rssyl/claws.def
+++ b/src/plugins/rssyl/claws.def
@@ -9,6 +9,10 @@ claws_is_exiting
 claws_is_starting
 claws_ssl_get_cert_file
 claws_unlink
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 cm_menu_set_sensitive_full
 conv_encode_header_full
 conv_unmime_header
@@ -116,7 +120,6 @@ procheader_parse_file
 procmsg_get_message_file
 procmsg_msginfo_unset_flags
 remove_dir_recursive
-safe_fclose
 slist_free_strings_full
 strtailchomp
 subst_for_shellsafe_filename
diff --git a/src/plugins/rssyl/opml_export.c b/src/plugins/rssyl/opml_export.c
index 20649db..d87169b 100644
--- a/src/plugins/rssyl/opml_export.c
+++ b/src/plugins/rssyl/opml_export.c
@@ -137,7 +137,7 @@ void rssyl_opml_export(void)
 		}
 	}
 	
-	if( (f = g_fopen(opmlfile, "w")) == NULL ) {
+	if( (f = claws_fopen(opmlfile, "w")) == NULL ) {
 		log_warning(LOG_PROTOCOL,
 				_("RSSyl: Couldn't open file '%s' for feed list exporting: %s\n"),
 				opmlfile, g_strerror(errno));
@@ -186,7 +186,7 @@ void rssyl_opml_export(void)
 
 	debug_print("RSSyl: Feed export finished.\n");
 
-	safe_fclose(f);
+	claws_safe_fclose(f);
 	g_free(opmlfile);
 	g_free(ctx);
 }
diff --git a/src/plugins/rssyl/rssyl_add_item.c b/src/plugins/rssyl/rssyl_add_item.c
index a7906fd..cc9d8dd 100644
--- a/src/plugins/rssyl/rssyl_add_item.c
+++ b/src/plugins/rssyl/rssyl_add_item.c
@@ -408,7 +408,7 @@ void rssyl_add_item(RFolderItem *ritem, FeedItem *feed_item)
 		return;
 	}
 
-	f = fdopen(fd, "w");
+	f = claws_fdopen(fd, "w");
 	if (f == NULL) {
 		g_warning("Couldn't open file '%s', not adding message!", template);
 		g_free(dirname);
@@ -547,7 +547,7 @@ void rssyl_add_item(RFolderItem *ritem, FeedItem *feed_item)
 				feed_item_enclosure_get_size(enc) );
 
 	fprintf(f, "</body></html>\n");
-	safe_fclose(f);
+	claws_safe_fclose(f);
 
 	g_return_if_fail(template != NULL);
 
diff --git a/src/plugins/rssyl/rssyl_deleted.c b/src/plugins/rssyl/rssyl_deleted.c
index 11e2ed1..c8c6395 100644
--- a/src/plugins/rssyl/rssyl_deleted.c
+++ b/src/plugins/rssyl/rssyl_deleted.c
@@ -181,7 +181,7 @@ static void rssyl_deleted_store_internal(GSList *deleted_items, const gchar *del
 	if (g_slist_length(deleted_items) == 0)
 		return;
 
-	if ((f = g_fopen(deleted_file, "w")) == NULL) {
+	if ((f = claws_fopen(deleted_file, "w")) == NULL) {
 		debug_print("RSSyl: Couldn't open '%s', bailing out.\n", deleted_file);
 		return;
 	}
@@ -189,7 +189,7 @@ static void rssyl_deleted_store_internal(GSList *deleted_items, const gchar *del
 	g_slist_foreach(deleted_items, (GFunc)_store_one_deleted_item,
 			(gpointer)f);
 
-	safe_fclose(f);
+	claws_safe_fclose(f);
 	debug_print("RSSyl: written and closed deletion file\n");
 }
 
diff --git a/src/plugins/smime/claws.def b/src/plugins/smime/claws.def
index 09f67d1..a3eeed5 100644
--- a/src/plugins/smime/claws.def
+++ b/src/plugins/smime/claws.def
@@ -3,6 +3,10 @@ EXPORTS
 alertpanel_error
 canonicalize_file_replace
 canonicalize_str
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 check_plugin_version
 claws_unlink
 debug_print_real
@@ -26,5 +30,4 @@ procmime_mimeinfo_parent
 procmime_scan_file
 procmime_write_mime_header
 procmime_write_mimeinfo
-safe_fclose
 str_write_to_file
diff --git a/src/plugins/smime/smime.c b/src/plugins/smime/smime.c
index abc49c8..82edd92 100644
--- a/src/plugins/smime/smime.c
+++ b/src/plugins/smime/smime.c
@@ -171,13 +171,13 @@ static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
 
 	if (boundary) {
 		boundary_len = strlen(boundary);
-		while (fgets(buf, sizeof(buf), fp) != NULL)
+		while (claws_fgets(buf, sizeof(buf), fp) != NULL)
 			if (IS_BOUNDARY(buf, boundary, boundary_len))
 				break;
 	}
 	
 	textbuffer = g_string_new("");
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar *buf2;
 
 		if (boundary && IS_BOUNDARY(buf, boundary, boundary_len))
@@ -230,7 +230,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
 	}
 	parent = procmime_mimeinfo_parent(mimeinfo);
 
-	fp = g_fopen(parent->data.filename, "rb");
+	fp = claws_fopen(parent->data.filename, "rb");
 	cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
 	
 	boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
@@ -278,7 +278,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
 			g_free(textstr);
 			cm_gpgme_data_rewind(cipher);
 			textstr = sgpgme_data_release_and_get_mem(cipher, &len);
-			fclose(fp);
+			claws_fclose(fp);
 			if (textstr && len > 0)
 				textstr[len-1]='\0';
 
@@ -343,7 +343,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
 	gpgme_data_release(sigdata);
 	gpgme_data_release(textdata);
 	g_free(textstr);
-	fclose(fp);
+	claws_fclose(fp);
 	
 	return 0;
 }
@@ -445,8 +445,8 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
     	fname = g_strdup_printf("%s%cplaintext.%08x",
 		get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-    	if ((dstfp = g_fopen(fname, "wb")) == NULL) {
-        	FILE_OP_ERROR(fname, "g_fopen");
+    	if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+        	FILE_OP_ERROR(fname, "claws_fopen");
         	g_free(fname);
         	gpgme_data_release(plain);
 		gpgme_release(ctx);
@@ -458,7 +458,7 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
 	if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
         	FILE_OP_ERROR(fname, "fprintf");
         	g_free(fname);
-		fclose(dstfp);
+		claws_fclose(dstfp);
         	gpgme_data_release(plain);
 		gpgme_release(ctx);
 		debug_print("can't close!\n");
@@ -469,9 +469,9 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
 	chars = sgpgme_data_release_and_get_mem(plain, &len);
 
 	if (len > 0) {
-		if (fwrite(chars, 1, len, dstfp) < len) {
-        		FILE_OP_ERROR(fname, "fwrite");
-        		fclose(dstfp);
+		if (claws_fwrite(chars, 1, len, dstfp) < len) {
+        		FILE_OP_ERROR(fname, "claws_fwrite");
+        		claws_fclose(dstfp);
         		g_free(fname);
         		g_free(chars);
         		gpgme_data_release(plain);
@@ -481,8 +481,8 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
 			return NULL;
 		}
 	}
-	if (safe_fclose(dstfp) == EOF) {
-        	FILE_OP_ERROR(fname, "fclose");
+	if (claws_safe_fclose(dstfp) == EOF) {
+        	FILE_OP_ERROR(fname, "claws_fclose");
         	g_free(fname);
        		g_free(chars);
         	gpgme_data_release(plain);
@@ -564,7 +564,7 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
 
 	/* read temporary file into memory */
 	test_msg = file_read_stream_to_str(fp);
-	fclose(fp);
+	claws_fclose(fp);
 	
 	memset (&info, 0, sizeof info);
 
@@ -606,7 +606,7 @@ gboolean smime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from
 
 	g_free(boundary);
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
 	gpgme_data_new(&gpgsig);
@@ -796,7 +796,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
 	/* write message content to temporary file */
 	tmpfile = get_tmp_file();
-	fp = g_fopen(tmpfile, "wb");
+	fp = claws_fopen(tmpfile, "wb");
 	if (fp == NULL) {
 		FILE_OP_ERROR(tmpfile, "create");
 		g_free(kset);
@@ -805,9 +805,9 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 	procmime_decode_content(msgcontent);
 	procmime_write_mime_header(msgcontent, fp);
 	procmime_write_mimeinfo(msgcontent, fp);
-	safe_fclose(fp);
+	claws_safe_fclose(fp);
 	canonicalize_file_replace(tmpfile);
-	fp = g_fopen(tmpfile, "rb");
+	fp = claws_fopen(tmpfile, "rb");
 	if (fp == NULL) {
 		FILE_OP_ERROR(tmpfile, "open");
 		g_free(kset);
@@ -818,7 +818,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 	/* read temporary file into memory */
 	textstr = fp_read_noconv(fp);
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	/* encrypt data */
 	gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
@@ -837,18 +837,18 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 	}
 
 	tmpfile = get_tmp_file();
-	fp = g_fopen(tmpfile, "wb");
+	fp = claws_fopen(tmpfile, "wb");
 	if (fp) {
-		if (fwrite(enccontent, 1, len, fp) < len) {
-			FILE_OP_ERROR(tmpfile, "fwrite");
-			fclose(fp);
+		if (claws_fwrite(enccontent, 1, len, fp) < len) {
+			FILE_OP_ERROR(tmpfile, "claws_fwrite");
+			claws_fclose(fp);
 			claws_unlink(tmpfile);
 			g_free(tmpfile);
 			g_free(enccontent);
 			return FALSE;
 		}
-		if (safe_fclose(fp) == EOF) {
-			FILE_OP_ERROR(tmpfile, "fclose");
+		if (claws_safe_fclose(fp) == EOF) {
+			FILE_OP_ERROR(tmpfile, "claws_fclose");
 			claws_unlink(tmpfile);
 			g_free(tmpfile);
 			g_free(enccontent);
diff --git a/src/plugins/spamassassin/spamassassin.c b/src/plugins/spamassassin/spamassassin.c
index e664adb..5c0632c 100644
--- a/src/plugins/spamassassin/spamassassin.c
+++ b/src/plugins/spamassassin/spamassassin.c
@@ -51,6 +51,7 @@
 #include "prefs_common.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
+#include "claws_io.h"
 
 #ifdef HAVE_SYSEXITS_H
 #include <sysexits.h>
@@ -253,7 +254,7 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
 		
 		if (whitelisted) {
 			debug_print("message is ham (whitelisted)\n");
-			fclose(fp);
+			claws_fclose(fp);
 			return FALSE;
 		}
 	}
@@ -291,7 +292,7 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
 			g_main_context_iteration(NULL, TRUE);
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	if (is_spam) {
 		debug_print("message is spam\n");
diff --git a/src/plugins/tnef_parse/claws.def b/src/plugins/tnef_parse/claws.def
index 28eadc1..b55dd9c 100644
--- a/src/plugins/tnef_parse/claws.def
+++ b/src/plugins/tnef_parse/claws.def
@@ -1,5 +1,9 @@
 LIBRARY CLAWS-MAIL.EXE
 EXPORTS
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 get_locale_dir
 check_plugin_version
 claws_unlink
diff --git a/src/plugins/tnef_parse/tnef_parse.c b/src/plugins/tnef_parse/tnef_parse.c
index 6f0f86f..93ef632 100644
--- a/src/plugins/tnef_parse/tnef_parse.c
+++ b/src/plugins/tnef_parse/tnef_parse.c
@@ -48,6 +48,7 @@
 #include "plugin.h"
 #include "procmime.h"
 #include "utils.h"
+#include "claws_io.h"
 
 #include "tnef_dump.h"
 
@@ -74,7 +75,7 @@ static MimeInfo *tnef_broken_mimeinfo(const gchar *reason)
 			"Claws Mail TNEF parser:\n\n"
 			"%s\n"), reason?reason:_("Unknown error"));
 
-	fclose(fp);
+	claws_fclose(fp);
 	if (g_stat(tmpfilename, &statbuf) < 0) {
 		claws_unlink(tmpfilename);
 		procmime_mimeinfo_free_all(&sub_info);
@@ -122,14 +123,14 @@ static MimeInfo *tnef_dump_file(const gchar *filename, char *data, size_t size)
 		}
 	} 
 
-	if (fwrite(data, 1, size, fp) < size) {
-		FILE_OP_ERROR(tmpfilename, "fwrite");
-		fclose(fp);
+	if (claws_fwrite(data, 1, size, fp) < size) {
+		FILE_OP_ERROR(tmpfilename, "claws_fwrite");
+		claws_fclose(fp);
 		claws_unlink(tmpfilename);
 		procmime_mimeinfo_free_all(&sub_info);
 		return tnef_broken_mimeinfo(_("Failed to write the part data."));
 	}
-	fclose(fp);
+	claws_fclose(fp);
 
 	if (g_stat(tmpfilename, &statbuf) < 0) {
 		claws_unlink(tmpfilename);
@@ -166,7 +167,7 @@ MimeInfo *tnef_parse_vcal(TNEFStruct *tnef)
 
 	result = SaveVCalendar(fp, tnef);
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	if (g_stat(tmpfilename, &statbuf) < 0) {
 		result = FALSE;
@@ -206,7 +207,7 @@ MimeInfo *tnef_parse_vtask(TNEFStruct *tnef)
 
 	result = SaveVTask(fp, tnef);
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	if (g_stat(tmpfilename, &statbuf) < 0) {
 		result = FALSE;
@@ -260,7 +261,7 @@ MimeInfo *tnef_parse_vcard(TNEFStruct *tnef)
 	
 	result = SaveVCard(fp, tnef);
 	
-	fclose(fp);
+	claws_fclose(fp);
 
 	ret = g_stat(tmpfilename, &statbuf);
 	if (ret == -1) {
diff --git a/src/plugins/vcalendar/claws.def b/src/plugins/vcalendar/claws.def
index f6d4208..6bfa523 100644
--- a/src/plugins/vcalendar/claws.def
+++ b/src/plugins/vcalendar/claws.def
@@ -17,6 +17,10 @@ alertpanel_full
 alertpanel_notice
 claws_do_idle
 claws_unlink
+claws_fopen
+claws_fdopen
+claws_fclose
+claws_safe_fclose
 cm_menu_create_action_group_full
 cm_menu_set_sensitive_full
 cm_toggle_menu_set_active_full
@@ -144,7 +148,6 @@ procmsg_msginfo_get_full_info
 procmsg_send_message_queue_with_lock
 qp_encode_line
 remove_dir_recursive
-safe_fclose
 slist_free_strings
 slist_free_strings_full
 statusbar_progress_all
diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c
index df05f4a..48e00d2 100644
--- a/src/plugins/vcalendar/vcal_meeting_gtk.c
+++ b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -1864,7 +1864,7 @@ void multisync_export(void)
 	g_slist_free(list);
 	
 	file = g_strconcat(path, G_DIR_SEPARATOR_S, "backup_entries", NULL);
-	fp = g_fopen(file, "wb");
+	fp = claws_fopen(file, "wb");
 	g_free(file);
 	if (fp) {
 		for (cur = files; cur; cur = cur->next) {
@@ -1873,10 +1873,10 @@ void multisync_export(void)
 				FILE_OP_ERROR(file, "fprintf");
 			g_free(file);
 		}
-		if (safe_fclose(fp) == EOF)
-			FILE_OP_ERROR(file, "fclose");
+		if (claws_safe_fclose(fp) == EOF)
+			FILE_OP_ERROR(file, "claws_fclose");
 	} else {
-		FILE_OP_ERROR(file, "fopen");
+		FILE_OP_ERROR(file, "claws_fopen");
 	}
 	g_free(path);
 	g_slist_free(files);
@@ -1996,7 +1996,7 @@ putfile:
 		g_free(afile);
 		g_free(file);
 	} else if (file) {
-		FILE *fp = g_fopen(tmpfile, "rb");
+		FILE *fp = claws_fopen(tmpfile, "rb");
 		if (!strncmp(file, "webcal", 6)) {
 			gchar *tmp = g_strdup_printf("http%s", file+6);
 			g_free(file);
@@ -2004,7 +2004,7 @@ putfile:
 		}
 		if (fp) {
 			res = vcal_curl_put(file, fp, filesize, user, (pass != NULL ? pass : ""));
-			fclose(fp);
+			claws_fclose(fp);
 		}
 		g_free(file);
 	}
@@ -2144,7 +2144,7 @@ gboolean vcal_meeting_export_freebusy(const gchar *path, const gchar *user,
 		g_free(afile);
 		g_free(file);
 	} else if (file) {
-		FILE *fp = g_fopen(tmpfile, "rb");
+		FILE *fp = claws_fopen(tmpfile, "rb");
 		if (!strncmp(file, "webcal", 6)) {
 			gchar *tmp = g_strdup_printf("http%s", file+6);
 			g_free(file);
@@ -2152,7 +2152,7 @@ gboolean vcal_meeting_export_freebusy(const gchar *path, const gchar *user,
 		}
 		if (fp) {
 			res = vcal_curl_put(file, fp, filesize, user, (pass != NULL ? pass : ""));
-			fclose(fp);
+			claws_fclose(fp);
 		}
 		g_free(file);
 	}
diff --git a/src/plugins/vcalendar/vcalendar.c b/src/plugins/vcalendar/vcalendar.c
index 4f06765..3388567 100644
--- a/src/plugins/vcalendar/vcalendar.c
+++ b/src/plugins/vcalendar/vcalendar.c
@@ -54,6 +54,7 @@
 #include "statusbar.h"
 #include "timing.h"
 #include "inc.h"
+#include "claws_io.h"
 
 MimeViewerFactory vcal_viewer_factory;
 
@@ -166,7 +167,7 @@ static void create_meeting_from_message_cb_ui(GtkAction *action, gpointer data)
 			gint sequence = 1;
 			PrefsAccount *account = NULL;
 			
-			fclose(fp);
+			claws_fclose(fp);
 
 			if (item && item->prefs && item->prefs->enable_default_account)
 				account = account_find_from_id(item->prefs->default_account);
@@ -276,23 +277,23 @@ static VCalEvent *vcalviewer_get_component(const gchar *file, const gchar *chars
 
 	g_return_val_if_fail(file != NULL, NULL);
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "g_fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return NULL;
 	}
 
 	array = g_byte_array_new();
 
-	while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
-		if (n_read < sizeof(buf) && ferror(fp))
+	while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+		if (n_read < sizeof(buf) && claws_ferror(fp))
 			break;
 		g_byte_array_append(array, (guchar *)buf, n_read);
 	}
 
-	if (ferror(fp)) {
-		FILE_OP_ERROR("file stream", "fread");
+	if (claws_ferror(fp)) {
+		FILE_OP_ERROR("file stream", "claws_fread");
 		g_byte_array_free(array, TRUE);
-		fclose(fp);
+		claws_fclose(fp);
 		return NULL;
 	}
 
@@ -301,7 +302,7 @@ static VCalEvent *vcalviewer_get_component(const gchar *file, const gchar *chars
 	compstr = (gchar *)array->data;
 	g_byte_array_free(array, FALSE);
 
-	fclose(fp);	
+	claws_fclose(fp);	
 
 	if (compstr) {
 		event = vcal_get_event_from_ical(compstr, charset);
diff --git a/src/pop.c b/src/pop.c
index 9a60c95..4c5f521 100644
--- a/src/pop.c
+++ b/src/pop.c
@@ -603,14 +603,14 @@ static void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
 			   "-", sanitized_uid, NULL);
 			   
 	g_free(sanitized_uid);
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 		g_free(path);
 		path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 				   "uidl-", ac_prefs->recv_server,
 				   "-", ac_prefs->userid, NULL);
-		if ((fp = g_fopen(path, "rb")) == NULL) {
-			if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+		if ((fp = claws_fopen(path, "rb")) == NULL) {
+			if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 			g_free(path);
 			session->uidl_table = table;
 			session->partial_recv_table = partial_recv_table;
@@ -621,7 +621,7 @@ static void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
 
 	now = time(NULL);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gchar tmp[POPBUFSIZE];
 		strretchomp(buf);
 		recv_time = RECV_TIME_NONE;
@@ -653,7 +653,7 @@ static void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
 				    GINT_TO_POINTER(partial_recv));
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 	session->uidl_table = table;
 	session->partial_recv_table = partial_recv_table;
 	
@@ -690,8 +690,8 @@ gint pop3_write_uidl_list(Pop3Session *session)
 
 	g_free(sanitized_uid);
 
-	if ((fp = g_fopen(tmp_path, "wb")) == NULL) {
-		FILE_OP_ERROR(tmp_path, "fopen");
+	if ((fp = claws_fopen(tmp_path, "wb")) == NULL) {
+		FILE_OP_ERROR(tmp_path, "claws_fopen");
 		goto err_write;
 	}
 
@@ -706,8 +706,8 @@ gint pop3_write_uidl_list(Pop3Session *session)
 			    > 0);
 	}
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(tmp_path, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(tmp_path, "claws_fclose");
 		fp = NULL;
 		goto err_write;
 	}
@@ -724,7 +724,7 @@ gint pop3_write_uidl_list(Pop3Session *session)
 	return 0;
 err_write:
 	if (fp)
-		fclose(fp);
+		claws_fclose(fp);
 	g_free(path);
 	g_free(tmp_path);
 	return -1;
@@ -740,8 +740,8 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
 
 	cm_return_val_if_fail(file != NULL, -1);
 
-	if ((fp = g_fopen(file, "wb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "wb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return -1;
 	}
 
@@ -751,7 +751,7 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
 	if (prefix != NULL) {
 		if (fprintf(fp, "%s\n", prefix) < 0) {
 			FILE_OP_ERROR(file, "fprintf");
-			fclose(fp);
+			claws_fclose(fp);
 			claws_unlink(file);
 			return -1;
 		}
@@ -763,11 +763,11 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
 	prev = data;
 	while ((cur = (gchar *)my_memmem(prev, len - (prev - data), "\r\n", 2))
 	       != NULL) {
-		if ((cur > prev && fwrite(prev, 1, cur - prev, fp) < 1) ||
-		    fputc('\n', fp) == EOF) {
-			FILE_OP_ERROR(file, "fwrite");
+		if ((cur > prev && claws_fwrite(prev, 1, cur - prev, fp) < 1) ||
+		    claws_fputc('\n', fp) == EOF) {
+			FILE_OP_ERROR(file, "claws_fwrite");
 			g_warning("can't write to file: %s", file);
-			fclose(fp);
+			claws_fclose(fp);
 			claws_unlink(file);
 			return -1;
 		}
@@ -790,25 +790,25 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
 	}
 
 	if (prev - data < len &&
-	    fwrite(prev, 1, len - (prev - data), fp) < 1) {
-		FILE_OP_ERROR(file, "fwrite");
+	    claws_fwrite(prev, 1, len - (prev - data), fp) < 1) {
+		FILE_OP_ERROR(file, "claws_fwrite");
 		g_warning("can't write to file: %s", file);
-		fclose(fp);
+		claws_fclose(fp);
 		claws_unlink(file);
 		return -1;
 	}
 	if (data[len - 1] != '\r' && data[len - 1] != '\n') {
-		if (fputc('\n', fp) == EOF) {
-			FILE_OP_ERROR(file, "fputc");
+		if (claws_fputc('\n', fp) == EOF) {
+			FILE_OP_ERROR(file, "claws_fputc");
 			g_warning("can't write to file: %s", file);
-			fclose(fp);
+			claws_fclose(fp);
 			claws_unlink(file);
 			return -1;
 		}
 	}
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(file, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(file, "claws_fclose");
 		claws_unlink(file);
 		return -1;
 	}
diff --git a/src/prefs_account.c b/src/prefs_account.c
index ccb0796..924823f 100644
--- a/src/prefs_account.c
+++ b/src/prefs_account.c
@@ -65,6 +65,7 @@
 #include "inputdialog.h"
 #include "ssl_certificate.h"
 #include "passwordstore.h"
+#include "claws_io.h"
 
 static gboolean cancelled;
 static gboolean new_account;
@@ -4173,8 +4174,8 @@ void prefs_account_write_config_all(GList *account_list)
 		privacy_prefs = NULL;
 
 		if (cur->next) {
-			if (fputc('\n', pfile->fp) == EOF) {
-				FILE_OP_ERROR(rcpath, "fputc");
+			if (claws_fputc('\n', pfile->fp) == EOF) {
+				FILE_OP_ERROR(rcpath, "claws_fputc");
 				prefs_file_close_revert(pfile);
 				return;
 			}
diff --git a/src/prefs_actions.c b/src/prefs_actions.c
index 515bf26..4f39386 100644
--- a/src/prefs_actions.c
+++ b/src/prefs_actions.c
@@ -50,6 +50,7 @@
 #include "prefs_filtering_action.h"
 #include "matcher_parser.h"
 #include "prefs_toolbar.h"
+#include "claws_io.h"
 
 enum {
 	PREFS_ACTIONS_STRING,	/*!< string pointer managed by list store, 
@@ -442,8 +443,8 @@ void prefs_actions_read_config(void)
 	debug_print("Reading actions configurations...\n");
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
-	if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+	if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
 		g_free(rcpath);
 		return;
 	}
@@ -456,7 +457,7 @@ void prefs_actions_read_config(void)
 		g_free(act);
 	}
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		const gchar *src_codeset = conv_get_locale_charset_str();
 		const gchar *dest_codeset = CS_UTF_8;
 		gchar *tmp;
@@ -477,7 +478,7 @@ void prefs_actions_read_config(void)
 		else
 			g_free(tmp);
 	}
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 void prefs_actions_write_config(void)
@@ -507,9 +508,9 @@ void prefs_actions_write_config(void)
 			act = g_strdup(act);
 		}
 
-		if (fputs(act, pfile->fp) == EOF ||
-		    fputc('\n', pfile->fp) == EOF) {
-			FILE_OP_ERROR(rcpath, "fputs || fputc");
+		if (claws_fputs(act, pfile->fp) == EOF ||
+		    claws_fputc('\n', pfile->fp) == EOF) {
+			FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
 			prefs_file_close_revert(pfile);
 			g_free(act);
 			g_free(rcpath);
diff --git a/src/prefs_common.c b/src/prefs_common.c
index 54bbf68..01747df 100644
--- a/src/prefs_common.c
+++ b/src/prefs_common.c
@@ -1289,19 +1289,19 @@ GList *prefs_common_read_history_from_dir_with_defaults(const gchar *dirname, co
 	path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, history,
 			   NULL);
 	}
-	if ((fp = g_fopen(path, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+	if ((fp = claws_fopen(path, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
 		g_free(path);
 		/* returns default list if set, otherwise NULL */
 		return default_list;
 	}
 	g_free(path);
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		g_strstrip(buf);
 		if (buf[0] == '\0') continue;
 		tmp = add_history(tmp, buf);
 	}
-	fclose(fp);
+	claws_fclose(fp);
 
 	tmp = g_list_reverse(tmp);
 
@@ -1391,18 +1391,18 @@ static void prefs_common_save_history_to_dir(const gchar *dirname, const gchar *
 	}
 	tmp_path = g_strconcat(path, ".tmp", NULL);
 
-	if ((fp = g_fopen(tmp_path, "wb")) == NULL) {
-		FILE_OP_ERROR(tmp_path, "fopen");
+	if ((fp = claws_fopen(tmp_path, "wb")) == NULL) {
+		FILE_OP_ERROR(tmp_path, "claws_fopen");
 		goto out;
 	}
 
 	for (cur = list; cur != NULL; cur = cur->next) {
-		TRY(fputs((gchar *)cur->data, fp) != EOF &&
-		    fputc('\n', fp) != EOF);
+		TRY(claws_fputs((gchar *)cur->data, fp) != EOF &&
+		    claws_fputc('\n', fp) != EOF);
 	}
 
-	if (safe_fclose(fp) == EOF) {
-		FILE_OP_ERROR(tmp_path, "fclose");
+	if (claws_safe_fclose(fp) == EOF) {
+		FILE_OP_ERROR(tmp_path, "claws_fclose");
 		fp = NULL;
 		goto out;
 	}
@@ -1417,7 +1417,7 @@ static void prefs_common_save_history_to_dir(const gchar *dirname, const gchar *
 
 out:
 	if (fp)
-		safe_fclose(fp);
+		claws_safe_fclose(fp);
 	g_free(tmp_path);
 	g_free(path);
 }
diff --git a/src/prefs_customheader.c b/src/prefs_customheader.c
index b6deece..ac119d0 100644
--- a/src/prefs_customheader.c
+++ b/src/prefs_customheader.c
@@ -47,6 +47,7 @@
 #include "alertpanel.h"
 #include "filesel.h"
 #include "combobox.h"
+#include "claws_io.h"
 
 enum {
 	CUSTHDR_STRING,		/*!< display string managed by list store */
@@ -328,8 +329,8 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 			     CUSTOM_HEADER_RC, NULL);
-	if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+	if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
 		g_free(rcpath);
 		ac->customhdr_list = NULL;
 		return;
@@ -343,7 +344,7 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
 		custom_header_free(ch);
 	}
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		ch = custom_header_read_str(buf);
 		if (ch) {
 			if (ch->account_id == ac->account_id) {
@@ -354,7 +355,7 @@ void prefs_custom_header_read_config(PrefsAccount *ac)
 		}
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 static void prefs_custom_header_write_config(PrefsAccount *ac)
@@ -373,12 +374,12 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 			     CUSTOM_HEADER_RC, NULL);
 
-	if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+	if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
 	} else {
 		all_hdrs = NULL;
 
-		while (fgets(buf, sizeof(buf), fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 			ch = custom_header_read_str(buf);
 			if (ch) {
 				if (ch->account_id != ac->account_id)
@@ -389,7 +390,7 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
 			}
 		}
 
-		fclose(fp);
+		claws_fclose(fp);
 	}
 
 	if ((pfile = prefs_write_open(rcpath)) == NULL) {
@@ -403,9 +404,9 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
 		gchar *chstr;
 
 		chstr = custom_header_get_str(hdr);
-		if (fputs(chstr, pfile->fp) == EOF ||
-		    fputc('\n', pfile->fp) == EOF) {
-			FILE_OP_ERROR(rcpath, "fputs || fputc");
+		if (claws_fputs(chstr, pfile->fp) == EOF ||
+		    claws_fputc('\n', pfile->fp) == EOF) {
+			FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
 			prefs_file_close_revert(pfile);
 			g_free(rcpath);
 			g_free(chstr);
@@ -419,9 +420,9 @@ static void prefs_custom_header_write_config(PrefsAccount *ac)
 		gchar *chstr;
 
 		chstr = custom_header_get_str(hdr);
-		if (fputs(chstr, pfile->fp) == EOF ||
-		    fputc('\n', pfile->fp) == EOF) {
-			FILE_OP_ERROR(rcpath, "fputs || fputc");
+		if (claws_fputs(chstr, pfile->fp) == EOF ||
+		    claws_fputc('\n', pfile->fp) == EOF) {
+			FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
 			prefs_file_close_revert(pfile);
 			g_free(rcpath);
 			g_free(chstr);
@@ -634,13 +635,13 @@ static void prefs_custom_header_val_from_file_cb(void)
 				goto settext;
 			}
 
-			fp = g_fopen(filename, "rb");
+			fp = claws_fopen(filename, "rb");
 			if (!fp) {
 				g_free(filename);
 				return;	
 			}
 
-			while ((len = fread(inbuf, sizeof(gchar),
+			while ((len = claws_fread(inbuf, sizeof(gchar),
 					    B64_LINE_SIZE, fp))
 			       == B64_LINE_SIZE) {
 				outbuf = g_base64_encode(inbuf, B64_LINE_SIZE);
@@ -650,14 +651,14 @@ static void prefs_custom_header_val_from_file_cb(void)
 				g_free(outbuf);
 				g_free(tmp);
 			}
-			if (len > 0 && feof(fp)) {
+			if (len > 0 && claws_feof(fp)) {
 				tmp = contents;
 				outbuf = g_base64_encode(inbuf, len);
 				contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
 				g_free(outbuf);
 				g_free(tmp);
 			}
-			fclose(fp);
+			claws_fclose(fp);
 		}
 	} else {
 		if (!filename)
diff --git a/src/prefs_display_header.c b/src/prefs_display_header.c
index ab1ec19..bd70f4f 100644
--- a/src/prefs_display_header.c
+++ b/src/prefs_display_header.c
@@ -41,6 +41,7 @@
 #include "displayheader.h"
 #include "utils.h"
 #include "gtkutils.h"
+#include "claws_io.h"
 
 enum {
 	PREFS_HDR_HEADER,
@@ -424,8 +425,8 @@ void prefs_display_header_read_config(void)
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
 			     DISPLAY_HEADER_RC, NULL);
-	if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+	if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
 		g_free(rcpath);
 		prefs_common.disphdr_list = NULL;
 		prefs_display_header_set_default();
@@ -441,7 +442,7 @@ void prefs_display_header_read_config(void)
 			g_slist_remove(prefs_common.disphdr_list, dp);
 	}
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		g_strdelimit(buf, "\r\n", '\0');
 		dp = display_header_prop_read_str(buf);
 		if (dp)
@@ -449,7 +450,7 @@ void prefs_display_header_read_config(void)
 				g_slist_append(prefs_common.disphdr_list, dp);
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 static void prefs_display_header_write_config(void)
@@ -475,9 +476,9 @@ static void prefs_display_header_write_config(void)
 		gchar *dpstr;
 
 		dpstr = display_header_prop_get_str(dp);
-		if (fputs(dpstr, pfile->fp) == EOF ||
-		    fputc('\n', pfile->fp) == EOF) {
-			FILE_OP_ERROR(rcpath, "fputs || fputc");
+		if (claws_fputs(dpstr, pfile->fp) == EOF ||
+		    claws_fputc('\n', pfile->fp) == EOF) {
+			FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
 			prefs_file_close_revert(pfile);
 			g_free(rcpath);
 			g_free(dpstr);
diff --git a/src/prefs_gtk.c b/src/prefs_gtk.c
index 8b00dc1..fedffcc 100644
--- a/src/prefs_gtk.c
+++ b/src/prefs_gtk.c
@@ -22,7 +22,6 @@
 #include "claws-features.h"
 #endif
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #include <glib.h>
@@ -42,6 +41,7 @@
 #include "gtkutils.h"
 #include "password.h"
 #include "codeconv.h"
+#include "claws_io.h"
 
 #define CL(x)	(((gulong) (x) >> (gulong) 8) & 0xFFUL)
 #define RGB_FROM_GDK_COLOR(c) \
@@ -49,12 +49,6 @@
 	 (CL(c.green) << (gulong)  8) | \
 	 (CL(c.blue)))
 
-#ifdef HAVE_FGETS_UNLOCKED
-#define SC_FGETS fgets_unlocked
-#else
-#define SC_FGETS fgets
-#endif
-
 typedef enum
 {
 	DUMMY_PARAM
@@ -91,19 +85,15 @@ void prefs_read_config(PrefParam *param, const gchar *label,
 			return;
 	}
 
-	if ((fp = g_fopen(rcfile, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
+	if ((fp = claws_fopen(rcfile, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcfile, "claws_fopen");
 		return;
 	}
 
 	block_label = g_strdup_printf("[%s]", label);
 
-#ifdef HAVE_FGETS_UNLOCKED
-	flockfile(fp);
-#endif
-
 	/* search aiming block */
-	while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		gint val;
 
 		if (encoding) {
@@ -125,7 +115,7 @@ void prefs_read_config(PrefParam *param, const gchar *label,
 	}
 	g_free(block_label);
 
-	while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		strretchomp(buf);
 		/* reached next block */
 		if (buf[0] == '[') break;
@@ -145,10 +135,7 @@ void prefs_read_config(PrefParam *param, const gchar *label,
 	}
 
 	debug_print("Finished reading configuration.\n");
-#ifdef HAVE_FGETS_UNLOCKED
-	funlockfile(fp);
-#endif
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
@@ -227,7 +214,7 @@ static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
 if (!(func)) \
 { \
 	g_warning("Failed to write configuration to file"); \
-	if (orig_fp) fclose(orig_fp); \
+	if (orig_fp) claws_fclose(orig_fp); \
 	prefs_file_close_revert(pfile); \
 	g_free(rcpath); \
 	g_free(block_label); \
@@ -249,13 +236,13 @@ void prefs_write_config(PrefParam *param, const gchar *label,
 	cm_return_if_fail(rcfile != NULL);
 
 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
-	if ((orig_fp = g_fopen(rcpath, "rb")) == NULL) {
-		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+	if ((orig_fp = claws_fopen(rcpath, "rb")) == NULL) {
+		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
 	}
 
 	if ((pfile = prefs_write_open(rcpath)) == NULL) {
 		g_warning("Failed to write configuration to file");
-		if (orig_fp) fclose(orig_fp);
+		if (orig_fp) claws_fclose(orig_fp);
 		g_free(rcpath);
 		return;
 	}
@@ -264,7 +251,7 @@ void prefs_write_config(PrefParam *param, const gchar *label,
 
 	/* search aiming block */
 	if (orig_fp) {
-		while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
 			gint val;
 
 			val = strncmp(buf, block_label, strlen(block_label));
@@ -273,7 +260,7 @@ void prefs_write_config(PrefParam *param, const gchar *label,
 				block_matched = TRUE;
 				break;
 			} else
-				TRY(fputs(buf, pfile->fp) != EOF);
+				TRY(claws_fputs(buf, pfile->fp) != EOF);
 		}
 	}
 
@@ -284,15 +271,15 @@ void prefs_write_config(PrefParam *param, const gchar *label,
 
 	if (block_matched) {
 		gboolean in_dup_block = FALSE;
-		while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
 			/* next block */
 			if (buf[0] == '[') {
-				TRY(fputc('\n', pfile->fp) != EOF &&
-				    fputs(buf, pfile->fp)  != EOF);
+				TRY(claws_fputc('\n', pfile->fp) != EOF &&
+				    claws_fputs(buf, pfile->fp)  != EOF);
 				break;
 			}
 		}
-		while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
 			if (buf[0] == '[') {
 				if (!strncmp(buf, block_label,
 						strlen(block_label)))
@@ -301,14 +288,14 @@ void prefs_write_config(PrefParam *param, const gchar *label,
 					in_dup_block = FALSE;
 			}
 			if (!in_dup_block)
-				TRY(fputs(buf, pfile->fp) != EOF);
+				TRY(claws_fputs(buf, pfile->fp) != EOF);
 		}
 	}
 
 	g_free(block_label);
 	block_label = NULL;
 
-	if (orig_fp) fclose(orig_fp);
+	if (orig_fp) claws_fclose(orig_fp);
 	if (prefs_file_close(pfile) < 0)
 		g_warning("Failed to write configuration to file");
 	g_free(rcpath);
@@ -375,8 +362,8 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
 		}
 
 		if (buf[0] != '\0') {
-			if (fputs(buf, fp) == EOF) {
-				perror("fputs");
+			if (claws_fputs(buf, fp) == EOF) {
+				perror("claws_fputs");
 				return -1;
 			}
 		}
@@ -939,17 +926,13 @@ static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
 	GHashTable *section_cache = NULL;
 
 	if (rcfile)
-		fp = g_fopen(rcfile, "rb");
+		fp = claws_fopen(rcfile, "rb");
 	if (!fp) {
 		debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
 		return -1;
 	}
-	
-#ifdef HAVE_FGETS_UNLOCKED
-	flockfile(fp);
-#endif
-	
-	while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+		
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		strretchomp(buf);
 		if (buf[0] == '\0')
 			continue;
@@ -991,10 +974,7 @@ static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
 			}
 		}
 	}
-#ifdef HAVE_FGETS_UNLOCKED
-	funlockfile(fp);
-#endif
-	fclose(fp);
+	claws_fclose(fp);
 	return 0;
 }
 
diff --git a/src/prefs_themes.c b/src/prefs_themes.c
index 0d4adc6..60ba49e 100644
--- a/src/prefs_themes.c
+++ b/src/prefs_themes.c
@@ -48,6 +48,7 @@
 #include "compose.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
+#include "claws_io.h"
 
 #define IS_CURRENT_THEME(path)  (strcmp(prefs_common.pixmap_theme_path, path) == 0)
 #define IS_INTERNAL_THEME(path) (strcmp(DEFAULT_PIXMAP_THEME, path) == 0)
@@ -811,7 +812,7 @@ static void prefs_themes_display_global_stats(const ThemesData *tdata)
 
 #define FGETS_INFOFILE_LINE() \
 	line[0] = '\0'; \
-	if (fgets(line, INFOFILE_LINE_LEN, finfo) != NULL && (len = strlen(line)) > 0) { \
+	if (claws_fgets(line, INFOFILE_LINE_LEN, finfo) != NULL && (len = strlen(line)) > 0) { \
 		if (line[len - 1] == '\n') line[len - 1] = '\0'; \
 	} \
 	else { \
@@ -844,7 +845,7 @@ static void prefs_themes_get_theme_info(ThemesData *tdata)
 	}
 	else {
 		sinfo = g_strconcat(path, G_DIR_SEPARATOR_S, THEMEINFO_FILE, NULL);
-		finfo = g_fopen(sinfo, "r");
+		finfo = claws_fopen(sinfo, "r");
 		if (finfo == NULL) {
 			info->name = g_strdup(_("No info file available for this theme"));
 			info->author = g_strdup(_("Unknown"));
@@ -858,7 +859,7 @@ static void prefs_themes_get_theme_info(ThemesData *tdata)
 			FGETS_INFOFILE_LINE()
 			info->url = g_strdup(line);
 		
-			fclose(finfo);
+			claws_fclose(finfo);
 		}
 		g_free(sinfo);
 
diff --git a/src/procheader.c b/src/procheader.c
index 6a98f12..9431841 100644
--- a/src/procheader.c
+++ b/src/procheader.c
@@ -41,6 +41,7 @@
 #include "hooks.h"
 #include "utils.h"
 #include "defs.h"
+#include "claws_io.h"
 
 #define BUFFSIZE	8192
 
@@ -459,13 +460,13 @@ MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
 		return NULL;
 #endif
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return NULL;
 	}
 
 	msginfo = procheader_parse_stream(fp, flags, full, decrypted);
-	fclose(fp);
+	claws_fclose(fp);
 
 	if (msginfo) {
 #ifdef G_OS_WIN32
@@ -1209,8 +1210,8 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *he
 	hentry[0].name = header;
 
 	file = procmsg_get_message_file_path(msginfo);
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		g_free(file);
 		g_free(*buf);
 		*buf = NULL;
@@ -1218,8 +1219,8 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *he
 	}
 	val = procheader_get_one_field(buf, fp, hentry);
 
-	if (fclose(fp) == EOF) {
-		FILE_OP_ERROR(file, "fclose");
+	if (claws_fclose(fp) == EOF) {
+		FILE_OP_ERROR(file, "claws_fclose");
 		claws_unlink(file);
 		g_free(file);
 		g_free(*buf);
diff --git a/src/procmime.c b/src/procmime.c
index 02c49a0..a9f28a5 100644
--- a/src/procmime.c
+++ b/src/procmime.c
@@ -21,7 +21,6 @@
 #include "claws-features.h"
 #endif
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #include "defs.h"
@@ -53,6 +52,7 @@
 #include "timing.h"
 #include "privacy.h"
 #include "account.h"
+#include "claws_io.h"
 
 static GHashTable *procmime_get_mime_type_table	(void);
 static MimeInfo *procmime_scan_file_short(const gchar *filename);
@@ -257,40 +257,6 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
 	return value;
 }
 
-#ifdef HAVE_FGETS_UNLOCKED
-#define SC_FGETS fgets_unlocked
-#define SC_FPUTS fputs_unlocked
-#define SC_FPUTC fputc_unlocked
-#define SC_FREAD fread_unlocked
-#define SC_FWRITE fwrite_unlocked
-#define SC_FEOF feof_unlocked
-#define SC_FERROR ferror_unlocked
-
-static FILE *procmime_fopen(const gchar *file, const gchar *mode)
-{
-	FILE *fp = g_fopen(file, mode);
-	if (!fp)
-		return NULL;
-	flockfile(fp);
-	return fp;
-}
-static int procmime_fclose(FILE *fp)
-{
-	funlockfile(fp);
-	return fclose(fp);
-}
-#else
-#define SC_FGETS fgets
-#define SC_FPUTS fputs
-#define SC_FPUTC fputc
-#define SC_FREAD fread
-#define SC_FWRITE fwrite
-#define SC_FEOF feof
-#define SC_FERROR ferror
-#define procmime_fopen g_fopen
-#define procmime_fclose fclose
-#endif
-
 #define FLUSH_LASTLINE() {							\
 	if (*lastline != '\0') {						\
 		gint llen = 0;							\
@@ -301,12 +267,12 @@ static int procmime_fclose(FILE *fp)
 			/* this is flowed */					\
 			if (delsp)						\
 				lastline[llen-1] = '\0';			\
-			if (SC_FPUTS(lastline, outfp) == EOF)			\
+			if (claws_fputs(lastline, outfp) == EOF)			\
 				err = TRUE;					\
 		} else {							\
-			if (SC_FPUTS(lastline, outfp) == EOF)			\
+			if (claws_fputs(lastline, outfp) == EOF)			\
 				err = TRUE;					\
-			if (SC_FPUTS("\n", outfp) == EOF)				\
+			if (claws_fputs("\n", outfp) == EOF)				\
 				err = TRUE;					\
 		}								\
 	} 									\
@@ -361,26 +327,24 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 	if (mimeinfo->data.filename == NULL)
 		return FALSE;
 
-	infp = procmime_fopen(mimeinfo->data.filename, "rb");
+	infp = claws_fopen(mimeinfo->data.filename, "rb");
 	if (!infp) {
-		FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 		return FALSE;
 	}
 	if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
 		FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-		procmime_fclose(infp);
+		claws_fclose(infp);
 		return FALSE;
 	}
 
 	outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
 	if (!outfp) {
 		perror("tmpfile");
-		procmime_fclose(infp);
+		claws_fclose(infp);
 		return FALSE;
 	}
-#ifdef HAVE_FGETS_UNLOCKED
-	flockfile(outfp);
-#endif
+
 	tmp_file = TRUE;
 	readend = mimeinfo->offset + mimeinfo->length;
 
@@ -388,12 +352,12 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 
 	*buf = '\0';
 	if (encoding == ENC_QUOTED_PRINTABLE) {
-		while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
+		while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
 			gint len;
 			len = qp_decode_line(buf);
 			buf[len] = '\0';
 			if (!flowed) {
-				if (SC_FWRITE(buf, 1, len, outfp) < len)
+				if (claws_fwrite(buf, 1, len, outfp) < len)
 					err = TRUE;
 			} else {
 				FLUSH_LASTLINE();
@@ -417,18 +381,15 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 			if (!tmpfp) {
 				perror("my_tmpfile");
 				if (tmp_file) 
-					procmime_fclose(outfp);
-				procmime_fclose(infp);
+					claws_fclose(outfp);
+				claws_fclose(infp);
 				return FALSE;
 			}
-#ifdef HAVE_FGETS_UNLOCKED
-			flockfile(tmpfp);
-#endif
 		} else
 			tmpfp = outfp;
 
 		while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
-			inread = SC_FREAD(buf, 1, inlen, infp);
+			inread = claws_fread(buf, 1, inlen, infp);
 			len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
 			if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
 				uncanonicalize = FALSE;
@@ -437,7 +398,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 			starting = FALSE;
 			if (((inread != inlen) || len < 0) && !got_error) {
 				g_warning("Bad BASE64 content.");
-				if (SC_FWRITE(_("[Error decoding BASE64]\n"),
+				if (claws_fwrite(_("[Error decoding BASE64]\n"),
 					sizeof(gchar),
 					strlen(_("[Error decoding BASE64]\n")),
 					tmpfp) < strlen(_("[Error decoding BASE64]\n")))
@@ -449,10 +410,10 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 				 * per block */
 				if (null_bytes) {
 					/* we won't uncanonicalize, output to outfp directly */
-					if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
+					if (claws_fwrite(outbuf, sizeof(gchar), len, outfp) < len)
 						err = TRUE;
 				} else {
-					if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
+					if (claws_fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
 						err = TRUE;
 				}
 				got_error = FALSE;
@@ -461,20 +422,20 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 
 		if (uncanonicalize) {
 			rewind(tmpfp);
-			while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
+			while (claws_fgets(buf, sizeof(buf), tmpfp) != NULL) {
 				strcrchomp(buf);
-				if (SC_FPUTS(buf, outfp) == EOF)
+				if (claws_fputs(buf, outfp) == EOF)
 					err = TRUE;
 			}
 		}
 		if (tmpfp != outfp)
-			procmime_fclose(tmpfp);
+			claws_fclose(tmpfp);
 	} else if (encoding == ENC_X_UUENCODE) {
 		gchar outbuf[BUFFSIZE];
 		gint len;
 		gboolean flag = FALSE;
 
-		while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
+		while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
 			if (!flag && strncmp(buf,"begin ", 6)) continue;
 
 			if (flag) {
@@ -484,15 +445,15 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 						g_warning("Bad UUENCODE content (%d)", len);
 					break;
 				}
-				if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
+				if (claws_fwrite(outbuf, sizeof(gchar), len, outfp) < len)
 					err = TRUE;
 			} else
 				flag = TRUE;
 		}
 	} else {
-		while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
+		while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
 			if (!flowed) {
-				if (SC_FPUTS(buf, outfp) == EOF)
+				if (claws_fputs(buf, outfp) == EOF)
 					err = TRUE;
 			} else {
 				FLUSH_LASTLINE();
@@ -504,8 +465,8 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 			g_warning("write error");
 	}
 
-	procmime_fclose(outfp);
-	procmime_fclose(infp);
+	claws_fclose(outfp);
+	claws_fclose(infp);
 
 	account_signatures_matchlist_delete();
 
@@ -556,27 +517,21 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 		perror("tmpfile");
 		return FALSE;
 	}
-#ifdef HAVE_FGETS_UNLOCKED
-	flockfile(outfp);
-#endif
 
 	if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
-		if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+		if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
 			g_warning("Can't open file %s", mimeinfo->data.filename);
-			procmime_fclose(outfp);
+			claws_fclose(outfp);
 			return FALSE;
 		}
 	} else if (mimeinfo->content == MIMECONTENT_MEM) {
 		infp = str_open_as_stream(mimeinfo->data.mem);
 		if (infp == NULL) {
-			procmime_fclose(outfp);
+			claws_fclose(outfp);
 			return FALSE;
 		}
-#ifdef HAVE_FGETS_UNLOCKED
-		flockfile(infp);
-#endif
 	} else {
-		procmime_fclose(outfp);
+		claws_fclose(outfp);
 		g_warning("Unknown mimeinfo");
 		return FALSE;
 	}
@@ -592,62 +547,59 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 				tmp_file = get_tmp_file();
 				if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
 					g_free(tmp_file);
-					procmime_fclose(infp);
-					procmime_fclose(outfp);
+					claws_fclose(infp);
+					claws_fclose(outfp);
 					return FALSE;
 				}
-				if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
-					FILE_OP_ERROR(tmp_file, "fopen");
+				if ((tmp_fp = claws_fopen(tmp_file, "rb")) == NULL) {
+					FILE_OP_ERROR(tmp_file, "claws_fopen");
 					claws_unlink(tmp_file);
 					g_free(tmp_file);
-					procmime_fclose(infp);
-					procmime_fclose(outfp);
+					claws_fclose(infp);
+					claws_fclose(outfp);
 					return FALSE;
 				}
 			} else {
 				gchar *out = canonicalize_str(mimeinfo->data.mem);
-				procmime_fclose(infp);
+				claws_fclose(infp);
 				infp = str_open_as_stream(out);
 				tmp_fp = infp;
 				g_free(out);
 				if (infp == NULL) {
-					procmime_fclose(outfp);
+					claws_fclose(outfp);
 					return FALSE;
 				}
-#ifdef HAVE_FGETS_UNLOCKED
-				flockfile(infp);
-#endif
 			}
 		}
 
-		while ((len = SC_FREAD(inbuf, sizeof(gchar),
+		while ((len = claws_fread(inbuf, sizeof(gchar),
 				    B64_LINE_SIZE, tmp_fp))
 		       == B64_LINE_SIZE) {
 			out = g_base64_encode(inbuf, B64_LINE_SIZE);
-			if (SC_FPUTS(out, outfp) == EOF)
+			if (claws_fputs(out, outfp) == EOF)
 				err = TRUE;
 			g_free(out);
-			if (SC_FPUTC('\n', outfp) == EOF)
+			if (claws_fputc('\n', outfp) == EOF)
 				err = TRUE;
 		}
-		if (len > 0 && SC_FEOF(tmp_fp)) {
+		if (len > 0 && claws_feof(tmp_fp)) {
 			out = g_base64_encode(inbuf, len);
-			if (SC_FPUTS(out, outfp) == EOF)
+			if (claws_fputs(out, outfp) == EOF)
 				err = TRUE;
 			g_free(out);
-			if (SC_FPUTC('\n', outfp) == EOF)
+			if (claws_fputc('\n', outfp) == EOF)
 				err = TRUE;
 		}
 
 		if (tmp_file) {
-			procmime_fclose(tmp_fp);
+			claws_fclose(tmp_fp);
 			claws_unlink(tmp_file);
 			g_free(tmp_file);
 		}
 	} else if (encoding == ENC_QUOTED_PRINTABLE) {
 		gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
 
-		while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
+		while (claws_fgets(inbuf, sizeof(inbuf), infp) != NULL) {
 			qp_encode_line(outbuf, inbuf);
 
 			if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
@@ -655,27 +607,27 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 				
 				tmpbuf += sizeof("From ")-1;
 				
-				if (SC_FPUTS("=46rom ", outfp) == EOF)
+				if (claws_fputs("=46rom ", outfp) == EOF)
 					err = TRUE;
-				if (SC_FPUTS(tmpbuf, outfp) == EOF)
+				if (claws_fputs(tmpbuf, outfp) == EOF)
 					err = TRUE;
 			} else {
-				if (SC_FPUTS(outbuf, outfp) == EOF)
+				if (claws_fputs(outbuf, outfp) == EOF)
 					err = TRUE;
 			}
 		}
 	} else {
 		gchar buf[BUFFSIZE];
 
-		while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), infp) != NULL) {
 			strcrchomp(buf);
-			if (SC_FPUTS(buf, outfp) == EOF)
+			if (claws_fputs(buf, outfp) == EOF)
 				err = TRUE;
 		}
 	}
 
-	procmime_fclose(outfp);
-	procmime_fclose(infp);
+	claws_fclose(outfp);
+	claws_fclose(infp);
 
 	if (err == TRUE)
 		return FALSE;
@@ -716,30 +668,30 @@ static gint procmime_get_part_to_stream(FILE *outfp, MimeInfo *mimeinfo)
 	if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
 		return -EINVAL;
 
-	if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+	if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
 		saved_errno = errno;
-		FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 		return -(saved_errno);
 	}
 	if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
 		saved_errno = errno;
 		FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-		procmime_fclose(infp);
+		claws_fclose(infp);
 		return -(saved_errno);
 	}
 
 	restlength = mimeinfo->length;
 
-	while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
-		if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
+	while ((restlength > 0) && ((readlength = claws_fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
+		if (claws_fwrite(buf, 1, readlength, outfp) != readlength) {
 			saved_errno = errno;
-			procmime_fclose(infp);
+			claws_fclose(infp);
 			return -(saved_errno);
 		}
 		restlength -= readlength;
 	}
 
-	procmime_fclose(infp);
+	claws_fclose(infp);
 	rewind(outfp);
 
 	return 0;
@@ -753,17 +705,17 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
 
 	cm_return_val_if_fail(outfile != NULL, -1);
 
-	if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
+	if ((outfp = claws_fopen(outfile, "wb")) == NULL) {
 		saved_errno = errno;
-		FILE_OP_ERROR(outfile, "fopen");
+		FILE_OP_ERROR(outfile, "claws_fopen");
 		return -(saved_errno);
 	}
 
 	result = procmime_get_part_to_stream(outfp, mimeinfo);
 
-	if (procmime_fclose(outfp) == EOF) {
+	if (claws_fclose(outfp) == EOF) {
 		saved_errno = errno;
-		FILE_OP_ERROR(outfile, "fclose");
+		FILE_OP_ERROR(outfile, "claws_fclose");
 		claws_unlink(outfile);
 		return -(saved_errno);
 	}
@@ -799,7 +751,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 		return TRUE;
 	}
 
-	tmpfp = procmime_fopen(tmpfile, "w+");
+	tmpfp = claws_fopen(tmpfile, "w+");
 #endif
 
 	if (tmpfp == NULL) {
@@ -855,7 +807,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 		ertf_parser_destroy(parser);
 		conv_code_converter_destroy(conv);
 	} else if (mimeinfo->type == MIMETYPE_TEXT && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
-		while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), tmpfp) != NULL) {
 			str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
 			if (str) {
 				if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
@@ -874,7 +826,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 	if (conv_fail)
 		g_warning("procmime_get_text_content(): Code conversion failed.");
 
-	procmime_fclose(tmpfp);
+	claws_fclose(tmpfp);
 
 #if !HAVE_FMEMOPEN
 	claws_unlink(tmpfile);
@@ -886,7 +838,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 
 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
 {
-	if (SC_FPUTS(str, (FILE *)fp) == EOF)
+	if (claws_fputs(str, (FILE *)fp) == EOF)
 		return TRUE;
 	
 	return FALSE;
@@ -901,20 +853,14 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 		perror("my_tmpfile");
 		return NULL;
 	}
-#ifdef HAVE_FGETS_UNLOCKED
-	flockfile(outfp);
-#endif
 
 	err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
 
 	rewind(outfp);
 	if (err == TRUE) {
-		procmime_fclose(outfp);
+		claws_fclose(outfp);
 		return NULL;
 	}
-#ifdef HAVE_FGETS_UNLOCKED
-	funlockfile(outfp);
-#endif
 	return outfp;
 
 }
@@ -922,7 +868,9 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 {
 	FILE *outfp;
+#if !HAVE_FMEMOPEN
 	gchar *tmpfile = NULL;
+#endif
 
 	cm_return_val_if_fail(mimeinfo != NULL, NULL);
 
@@ -938,19 +886,18 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 		return TRUE;
 	}
 
-	outfp = procmime_fopen(tmpfile, "w+");
+	outfp = claws_fopen(tmpfile, "w+");
 
-	g_unlink(tmpfile);
-	g_free(tmpfile);
+	if (tmpfile != NULL) {
+		g_unlink(tmpfile);
+		g_free(tmpfile);
+	}
 #endif
 
 	if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
 		return NULL;
 	}
 
-#ifdef HAVE_FGETS_UNLOCKED
-	funlockfile(outfp);
-#endif
 	return outfp;
 }
 
@@ -996,7 +943,6 @@ scan_again:
 	}
 	procmime_mimeinfo_free_all(&mimeinfo);
 
-	/* outfp already unlocked at this time */
 	return outfp;
 }
 
@@ -1059,7 +1005,6 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
 
 	procmime_mimeinfo_free_all(&mimeinfo);
 
-	/* outfp already unlocked at this time */
 	return outfp;
 }
 
@@ -1238,23 +1183,23 @@ GList *procmime_get_mime_type_list(void)
 		return mime_type_list;
 	
 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
-	if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
+	if ((fp = claws_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
 #else
-	if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL) 
+	if ((fp = claws_fopen("/usr/share/mime/globs", "rb")) == NULL) 
 #endif
 	{
 		fp_is_glob_file = FALSE;
-		if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
-			if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb")) 
+		if ((fp = claws_fopen("/etc/mime.types", "rb")) == NULL) {
+			if ((fp = claws_fopen(SYSCONFDIR "/mime.types", "rb")) 
 				== NULL) {
 				FILE_OP_ERROR(SYSCONFDIR "/mime.types", 
-					"fopen");
+					"claws_fopen");
 				return NULL;
 			}
 		}
 	}
 
-	while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		p = strchr(buf, '#');
 		if (p) *p = '\0';
 		g_strstrip(buf);
@@ -1293,7 +1238,7 @@ GList *procmime_get_mime_type_list(void)
 		list = g_list_append(list, mime_type);
 	}
 
-	procmime_fclose(fp);
+	claws_fclose(fp);
 
 	if (!list)
 		g_warning("Can't read mime.types");
@@ -1332,12 +1277,12 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
 	gfloat octet_percentage;
 	gboolean force_b64 = FALSE;
 
-	if ((fp = procmime_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return ENC_UNKNOWN;
 	}
 
-	while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
+	while ((len = claws_fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
 		guchar *p;
 		gint i;
 
@@ -1352,7 +1297,7 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
 		total_len += len;
 	}
 
-	procmime_fclose(fp);
+	claws_fclose(fp);
 	
 	if (total_len > 0)
 		octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
@@ -1498,14 +1443,14 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
 
 	procmime_decode_content(mimeinfo);
 
-	fp = procmime_fopen(mimeinfo->data.filename, "rb");
+	fp = claws_fopen(mimeinfo->data.filename, "rb");
 	if (fp == NULL) {
-		FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 		return;
 	}
 	if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
 		FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-		procmime_fclose(fp);
+		claws_fclose(fp);
 		return;
 	}
 	procheader_get_header_fields(fp, hentry);
@@ -1541,7 +1486,7 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
         }
   
 	content_start = ftell(fp);
-	procmime_fclose(fp);
+	claws_fclose(fp);
 	
 	len = mimeinfo->length - (content_start - mimeinfo->offset);
 	if (len < 0)
@@ -1575,14 +1520,14 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
 	procmime_decode_content(mimeinfo);
 
 	debug_print("parse disposition notification\n");
-	fp = procmime_fopen(mimeinfo->data.filename, "rb");
+	fp = claws_fopen(mimeinfo->data.filename, "rb");
 	if (fp == NULL) {
-		FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 		return;
 	}
 	if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
 		FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-		procmime_fclose(fp);
+		claws_fclose(fp);
 		return;
 	}
 
@@ -1593,7 +1538,7 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
 		procheader_get_header_fields(fp, hentry);
 	}
     
-        procmime_fclose(fp);
+        claws_fclose(fp);
 
     	if (!hentry[0].body || !hentry[1].body) {
 		debug_print("MsgId %s, Disp %s\n",
@@ -1704,19 +1649,19 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 
 	procmime_decode_content(mimeinfo);
 
-	fp = procmime_fopen(mimeinfo->data.filename, "rb");
+	fp = claws_fopen(mimeinfo->data.filename, "rb");
 	if (fp == NULL) {
-		FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 		return;
 	}
 
 	if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
 		FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-		procmime_fclose(fp);
+		claws_fclose(fp);
 		return;
 	}
 
-	while (SC_FGETS(buf, sizeof(buf), fp) != NULL && result == 0) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL && result == 0) {
 		if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
 			break;
 
@@ -1772,7 +1717,7 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 		g_free(hentry[i].body);
 		hentry[i].body = NULL;
 	}
-	procmime_fclose(fp);
+	claws_fclose(fp);
 }
 
 static void parse_parameters(const gchar *parameters, GHashTable *table)
@@ -2262,10 +2207,10 @@ static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean s
 	cm_return_val_if_fail(filename != NULL, NULL);
 
 	/* Open file */
-	if ((fp = procmime_fopen(filename, "rb")) == NULL)
+	if ((fp = claws_fopen(filename, "rb")) == NULL)
 		return NULL;
 	/* Skip queue header */
-	while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		/* new way */
 		if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
 			strlen("X-Claws-End-Special-Headers:"))) ||
@@ -2284,7 +2229,7 @@ static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean s
 		}
 	}
 	offset = ftell(fp);
-	procmime_fclose(fp);
+	claws_fclose(fp);
 
 	mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
 
@@ -2535,16 +2480,16 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
 	/* write header */
 	switch (mimeinfo->content) {
 	case MIMECONTENT_FILE:
-		if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-			FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+			FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 			return -1;
 		}
 		if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
 			FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-			procmime_fclose(infp);
+			claws_fclose(infp);
 			return -1;
 		}
-		while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
+		while (claws_fgets(buf, sizeof(buf), infp) == buf) {
 			strcrchomp(buf);
 			if (buf[0] == '\n' && buf[1] == '\0')
 				break;
@@ -2561,19 +2506,19 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
 				continue;
 			}
 			len = strlen(buf);
-			if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
+			if (claws_fwrite(buf, sizeof(gchar), len, fp) < len) {
 				g_warning("failed to dump %zd bytes from file", len);
-				procmime_fclose(infp);
+				claws_fclose(infp);
 				return -1;
 			}
 			skip = FALSE;
 		}
-		procmime_fclose(infp);
+		claws_fclose(infp);
 		break;
 
 	case MIMECONTENT_MEM:
 		len = strlen(mimeinfo->data.mem);
-		if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
+		if (claws_fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
 			g_warning("failed to dump %zd bytes from mem", len);
 			return -1;
 		}
@@ -2612,26 +2557,26 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 
 	switch (mimeinfo->content) {
 	case MIMECONTENT_FILE:
-		if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-			FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+		if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+			FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 			return -1;
 		}
 		if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
 			FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-			procmime_fclose(infp);
+			claws_fclose(infp);
 			return -1;
 		}
-		while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
+		while (claws_fgets(buf, sizeof(buf), infp) == buf) {
 			if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
 				break;
 			len = strlen(buf);
-			if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
+			if (claws_fwrite(buf, sizeof(gchar), len, fp) < len) {
 				g_warning("failed to write %zd", len);
-				procmime_fclose(infp);
+				claws_fclose(infp);
 				return -1;
 			}
 		}
-		procmime_fclose(infp);
+		claws_fclose(infp);
 		break;
 
 	case MIMECONTENT_MEM:
@@ -2640,7 +2585,7 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 		    (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
 			*(str2 - 2) = '\0';
 		len = strlen(str);
-		if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
+		if (claws_fwrite(str, sizeof(gchar), len, fp) < len) {
 			g_warning("failed to write %zd from mem", len);
 			g_free(str);
 			return -1;
@@ -2685,17 +2630,17 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
 	if (G_NODE_IS_LEAF(mimeinfo->node)) {
 		switch (mimeinfo->content) {
 		case MIMECONTENT_FILE:
-			if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-				FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+			if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+				FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 				return -1;
 			}
 			copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
-			procmime_fclose(infp);
+			claws_fclose(infp);
 			return 0;
 
 		case MIMECONTENT_MEM:
 			len = strlen(mimeinfo->data.mem);
-			if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
+			if (claws_fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
 				return -1;
 			return 0;
 
diff --git a/src/procmsg.c b/src/procmsg.c
index 73cd7c0..34eff51 100644
--- a/src/procmsg.c
+++ b/src/procmsg.c
@@ -496,8 +496,8 @@ FILE *procmsg_open_message(MsgInfo *msginfo)
 			return NULL;
 	}
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		g_free(file);
 		return NULL;
 	}
@@ -507,7 +507,7 @@ FILE *procmsg_open_message(MsgInfo *msginfo)
 	if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
 		gchar buf[BUFFSIZE];
 
-		while (fgets(buf, sizeof(buf), fp) != NULL) {
+		while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 			/* new way */
 			if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
 				strlen("X-Claws-End-Special-Headers:"))) ||
@@ -581,7 +581,7 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
 		if ((fp = procmsg_open_message(msginfo)) == NULL)
 			return;
 		procheader_get_header_fields(fp, hentry);
-		fclose(fp);
+		claws_fclose(fp);
 
 #define SET_FILTER_KEY(hstr, idx)	\
 {					\
@@ -743,8 +743,8 @@ static PrefsAccount *procmsg_get_account_from_file(const gchar *file)
 	
 	cm_return_val_if_fail(file != NULL, NULL);
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return NULL;
 	}
 
@@ -758,7 +758,7 @@ static PrefsAccount *procmsg_get_account_from_file(const gchar *file)
 		g_free(buf);
 		buf = NULL;
 	}
-	fclose(fp);
+	claws_fclose(fp);
 	return mailac;
 }
 
@@ -1086,16 +1086,16 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
 	FILE *fp, *outfp;
 	gchar buf[BUFFSIZE];
 	
-	if ((fp = g_fopen(in, "rb")) == NULL) {
-		FILE_OP_ERROR(in, "fopen");
+	if ((fp = claws_fopen(in, "rb")) == NULL) {
+		FILE_OP_ERROR(in, "claws_fopen");
 		return -1;
 	}
-	if ((outfp = g_fopen(out, "wb")) == NULL) {
-		FILE_OP_ERROR(out, "fopen");
-		fclose(fp);
+	if ((outfp = claws_fopen(out, "wb")) == NULL) {
+		FILE_OP_ERROR(out, "claws_fopen");
+		claws_fclose(fp);
 		return -1;
 	}
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		/* new way */
 		if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
 			strlen("X-Claws-End-Special-Headers:"))) ||
@@ -1113,16 +1113,16 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
 			break;
 		}
 	}
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
-		if (fputs(buf, outfp) == EOF) {
-			FILE_OP_ERROR(out, "fputs");
-			fclose(outfp);
-			fclose(fp);
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
+		if (claws_fputs(buf, outfp) == EOF) {
+			FILE_OP_ERROR(out, "claws_fputs");
+			claws_fclose(outfp);
+			claws_fclose(fp);
 			return -1;
 		}
 	}
-	safe_fclose(outfp);
-	fclose(fp);
+	claws_safe_fclose(outfp);
+	claws_fclose(fp);
 	return 0;
 }
 
@@ -1549,8 +1549,8 @@ static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_ses
 
 	cm_return_val_if_fail(file != NULL, -1);
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		if (errstr) {
 			if (*errstr) g_free(*errstr);
 			*errstr = g_strdup_printf(_("Couldn't open file %s."), file);
@@ -1686,8 +1686,8 @@ send_mail:
     		/* write to temporary file */
     		tmp = g_strdup_printf("%s%cnntp%p", get_tmp_dir(),
                     	    G_DIR_SEPARATOR, file);
-    		if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
-            		FILE_OP_ERROR(tmp, "fopen");
+    		if ((tmpfp = claws_fopen(tmp, "wb")) == NULL) {
+            		FILE_OP_ERROR(tmp, "claws_fopen");
             		newsval = -1;
 			alertpanel_error(_("Couldn't create temporary file for news sending."));
     		} else {
@@ -1696,9 +1696,9 @@ send_mail:
 				g_warning("can't change file mode");
     			}
 
-			while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
-				if (fputs(buf, tmpfp) == EOF) {
-					FILE_OP_ERROR(tmp, "fputs");
+			while ((newsval == 0) && claws_fgets(buf, sizeof(buf), fp) != NULL) {
+				if (claws_fputs(buf, tmpfp) == EOF) {
+					FILE_OP_ERROR(tmp, "claws_fputs");
 					newsval = -1;
 					if (errstr) {
 						if (*errstr) g_free(*errstr);
@@ -1706,7 +1706,7 @@ send_mail:
 					}
 				}
 			}
-			safe_fclose(tmpfp);
+			claws_safe_fclose(tmpfp);
 
 			if (newsval == 0) {
 				debug_print("Sending message by news\n");
@@ -1725,7 +1725,7 @@ send_mail:
 		g_free(tmp);
 	}
 
-	fclose(fp);
+	claws_fclose(fp);
 
 	/* update session statistics */
 	if (mailval == 0 && newsval == 0) {
@@ -2328,26 +2328,26 @@ MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimei
 	MsgInfo *tmp_msginfo = NULL;
 	MsgFlags flags = {0, 0};
 	gchar *tmpfile = get_tmp_file();
-	FILE *fp = g_fopen(tmpfile, "wb");
+	FILE *fp = claws_fopen(tmpfile, "wb");
 	
 	if (!mimeinfo || mimeinfo->type != MIMETYPE_MESSAGE ||
 	    g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
 		g_warning("procmsg_msginfo_new_from_mimeinfo(): unsuitable mimeinfo");
 		if (fp) 
-			fclose(fp);
+			claws_fclose(fp);
 		g_free(tmpfile);
 		return NULL;
 	}
 	
 	if (fp && procmime_write_mimeinfo(mimeinfo, fp) >= 0) {
-		safe_fclose(fp);
+		claws_safe_fclose(fp);
 		fp = NULL;
 		tmp_msginfo = procheader_parse_file(
 			tmpfile, flags, 
 			TRUE, FALSE);
 	}
 	if (fp)
-		safe_fclose(fp);
+		claws_safe_fclose(fp);
 
 	if (tmp_msginfo != NULL) {
 		if (src_msginfo)
diff --git a/src/quote_fmt_parse.y b/src/quote_fmt_parse.y
index 92e300e..515d910 100644
--- a/src/quote_fmt_parse.y
+++ b/src/quote_fmt_parse.y
@@ -37,6 +37,7 @@
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
 #include "account.h"
+#include "claws_io.h"
 
 /* decl */
 /*
diff --git a/src/send_message.c b/src/send_message.c
index 497fe4d..d0a2160 100644
--- a/src/send_message.c
+++ b/src/send_message.c
@@ -56,6 +56,7 @@
 #include "inc.h"
 #include "log.h"
 #include "passwordstore.h"
+#include "claws_io.h"
 
 typedef struct _SendProgressDialog	SendProgressDialog;
 
@@ -110,8 +111,8 @@ gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
 	cm_return_val_if_fail(ac_prefs != NULL, -1);
 	cm_return_val_if_fail(to_list != NULL, -1);
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		return -1;
 	}
 
@@ -119,13 +120,13 @@ gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
 	if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
 	    (*ac_prefs->mail_command)) {
 		val = send_message_local(ac_prefs->mail_command, fp);
-		fclose(fp);
+		claws_fclose(fp);
 		inc_unlock();
 		return val;
 	} else {
 		val = send_message_smtp(ac_prefs, to_list, fp);
 		
-		fclose(fp);
+		claws_fclose(fp);
 		inc_unlock();
 		return val;
 	}
@@ -172,7 +173,7 @@ gint send_message_local(const gchar *command, FILE *fp)
 	}
 	g_strfreev(argv);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
 		strretchomp(buf);
 		if (buf[0] == '.' && buf[1] == '\0') {
 			if (fd_write_all(child_stdin, ".", 1) < 0) {
diff --git a/src/sourcewindow.c b/src/sourcewindow.c
index e170512..bb3225f 100644
--- a/src/sourcewindow.c
+++ b/src/sourcewindow.c
@@ -30,6 +30,7 @@
 #include "utils.h"
 #include "gtkutils.h"
 #include "prefs_common.h"
+#include "claws_io.h"
 
 static void source_window_size_alloc_cb	(GtkWidget	*widget,
 					 GtkAllocation	*allocation);
@@ -148,8 +149,8 @@ void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
 
 	cm_return_if_fail(file != NULL);
 
-	if ((fp = g_fopen(file, "rb")) == NULL) {
-		FILE_OP_ERROR(file, "fopen");
+	if ((fp = claws_fopen(file, "rb")) == NULL) {
+		FILE_OP_ERROR(file, "claws_fopen");
 		g_free(file);
 		return;
 	}
@@ -161,10 +162,10 @@ void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
 	g_free(title);
 	g_free(file);
 
-	while (fgets(buf, sizeof(buf), fp) != NULL)
+	while (claws_fgets(buf, sizeof(buf), fp) != NULL)
 		source_window_append(sourcewin, buf);
 
-	fclose(fp);
+	claws_fclose(fp);
 }
 
 static void source_window_append(SourceWindow *sourcewin, const gchar *str)
diff --git a/src/summaryview.c b/src/summaryview.c
index f036f8b..19c03e8 100644
--- a/src/summaryview.c
+++ b/src/summaryview.c
@@ -1148,7 +1148,7 @@ static void summaryview_quicksearch_recurse(SummaryView *summaryview)
 		|| summaryview->folder_item == NULL) {
 		return;
 	}
-
+	START_TIMING("");
 	main_window_cursor_wait(summaryview->mainwin);
 
 	summaryview_reset_recursive_folder_match(summaryview);
@@ -1157,6 +1157,7 @@ static void summaryview_quicksearch_recurse(SummaryView *summaryview)
 	summaryview_quicksearch_search_subfolders(summaryview, summaryview->folder_item);
 	
 	main_window_cursor_normal(summaryview->mainwin);
+	END_TIMING();
 }
 
 static gboolean summary_check_consistency(FolderItem *item, GSList *mlist)
@@ -8449,12 +8450,8 @@ gboolean summary_is_opened_message_selected(SummaryView *summaryview)
 
 gboolean summary_has_opened_message(SummaryView *summaryview)
 {
-	GList *sel = NULL;
-
 	cm_return_val_if_fail(summaryview != NULL, FALSE);
 
-	sel = GTK_CMCLIST(summaryview->ctree)->selection;
-
 	return (summaryview->displayed != NULL);
 }
 
diff --git a/src/textview.c b/src/textview.c
index 82718d8..72678f0 100644
--- a/src/textview.c
+++ b/src/textview.c
@@ -69,6 +69,7 @@
 #include "folder_item_prefs.h"
 #include "hooks.h"
 #include "avatars.h"
+#include "claws_io.h"
 
 static GdkColor quote_colors[3] = {
 	{(gulong)0, (gushort)0, (gushort)0, (gushort)0},
@@ -689,15 +690,15 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 		if (mimeinfo->content == MIMECONTENT_MEM)
 			fp = str_open_as_stream(mimeinfo->data.mem);
 		else
-			fp = g_fopen(mimeinfo->data.filename, "rb");
+			fp = claws_fopen(mimeinfo->data.filename, "rb");
 		if (!fp) {
-			FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+			FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 			END_TIMING();
 			return;
 		}
 		if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
 			FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-			fclose(fp);
+			claws_fclose(fp);
 			END_TIMING();
 			return;
 		}
@@ -711,7 +712,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 			textview_show_header(textview, headers);
 			procheader_header_array_destroy(headers);
 		}
-		fclose(fp);
+		claws_fclose(fp);
 		END_TIMING();
 		return;
 	}
@@ -1090,10 +1091,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 		
 		filename = procmime_get_tmp_file_name(mimeinfo);
 		if (procmime_get_part(filename, mimeinfo) == 0) {
-			tmpfp = g_fopen(filename, "rb");
+			tmpfp = claws_fopen(filename, "rb");
 			if (tmpfp) {
 				textview_show_html(textview, tmpfp, conv);
-				fclose(tmpfp);
+				claws_fclose(tmpfp);
 			}
 			claws_unlink(filename);
 		}
@@ -1103,10 +1104,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 		
 		filename = procmime_get_tmp_file_name(mimeinfo);
 		if (procmime_get_part(filename, mimeinfo) == 0) {
-			tmpfp = g_fopen(filename, "rb");
+			tmpfp = claws_fopen(filename, "rb");
 			if (tmpfp) {
 				textview_show_ertf(textview, tmpfp, conv);
-				fclose(tmpfp);
+				claws_fclose(tmpfp);
 			}
 			claws_unlink(filename);
 		}
@@ -1157,12 +1158,12 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 			exit(255);
 		}
 		close(pfd[1]);
-		tmpfp = fdopen(pfd[0], "rb");
-		while (fgets(buf, sizeof(buf), tmpfp)) {
+		tmpfp = claws_fdopen(pfd[0], "rb");
+		while (claws_fgets(buf, sizeof(buf), tmpfp)) {
 			textview_write_line(textview, buf, conv, TRUE);
 			
 			if (textview->stop_loading) {
-				fclose(tmpfp);
+				claws_fclose(tmpfp);
 				waitpid(pid, pfd, 0);
 				g_unlink(fname);
 				account_signatures_matchlist_delete();
@@ -1170,7 +1171,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 			}
 		}
 
-		fclose(tmpfp);
+		claws_fclose(tmpfp);
 		waitpid(pid, pfd, 0);
 		g_unlink(fname);
 #endif
@@ -1192,25 +1193,25 @@ textview_default:
 		if (mimeinfo->content == MIMECONTENT_MEM)
 			tmpfp = str_open_as_stream(mimeinfo->data.mem);
 		else
-			tmpfp = g_fopen(mimeinfo->data.filename, "rb");
+			tmpfp = claws_fopen(mimeinfo->data.filename, "rb");
 		if (!tmpfp) {
-			FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+			FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
 			account_signatures_matchlist_delete();
 			return;
 		}
 		if (fseek(tmpfp, mimeinfo->offset, SEEK_SET) < 0) {
 			FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-			fclose(tmpfp);
+			claws_fclose(tmpfp);
 			account_signatures_matchlist_delete();
 			return;
 		}
 		debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
 		while (((i = ftell(tmpfp)) < mimeinfo->offset + mimeinfo->length) &&
-		       (fgets(buf, sizeof(buf), tmpfp) != NULL)
+		       (claws_fgets(buf, sizeof(buf), tmpfp) != NULL)
 		       && continue_write) {
 			textview_write_line(textview, buf, conv, TRUE);
 			if (textview->stop_loading) {
-				fclose(tmpfp);
+				claws_fclose(tmpfp);
 				account_signatures_matchlist_delete();
 				return;
 			}
@@ -1221,7 +1222,7 @@ textview_default:
 				continue_write = FALSE;
 			}
 		}
-		fclose(tmpfp);
+		claws_fclose(tmpfp);
 	}
 
 	account_signatures_matchlist_delete();
@@ -1955,7 +1956,7 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
 	}
 
 	if (!prefs_common.display_header) {
-		while (fgets(buf, sizeof(buf), fp) != NULL)
+		while (claws_fgets(buf, sizeof(buf), fp) != NULL)
 			if (buf[0] == '\r' || buf[0] == '\n') break;
 		return NULL;
 	}
diff --git a/src/vcard.c b/src/vcard.c
index 514ab4f..deb083d 100644
--- a/src/vcard.c
+++ b/src/vcard.c
@@ -23,6 +23,11 @@
  * RFC2426 for more information.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <sys/stat.h>
 #include <string.h>
@@ -35,6 +40,7 @@
 #include "utils.h"
 #include "codeconv.h"
 #include "quoted-printable.h"
+#include "claws_io.h"
 
 #define GNOMECARD_DIR     ".gnome"
 #define GNOMECARD_FILE    "GnomeCard"
@@ -136,7 +142,7 @@ void vcard_free( VCardFile *cardFile ) {
 	cm_return_if_fail( cardFile != NULL );
 
 	/* Close file */
-	if( cardFile->file ) fclose( cardFile->file );
+	if( cardFile->file ) claws_fclose( cardFile->file );
 
 	/* Clear cache */
 	addrcache_clear( cardFile->addressCache );
@@ -168,7 +174,7 @@ static gint vcard_open_file( VCardFile* cardFile ) {
 	/* g_print( "Opening file\n" ); */
 	cardFile->addressCache->dataRead = FALSE;
 	if( cardFile->path ) {
-		cardFile->file = g_fopen( cardFile->path, "rb" );
+		cardFile->file = claws_fopen( cardFile->path, "rb" );
 		if( ! cardFile->file ) {
 			/* g_printerr( "can't open %s\n", cardFile->path ); */
 			cardFile->retVal = MGU_OPEN_FILE;
@@ -193,7 +199,7 @@ static gint vcard_open_file( VCardFile* cardFile ) {
 */
 static void vcard_close_file( VCardFile *cardFile ) {
 	cm_return_if_fail( cardFile != NULL );
-	if( cardFile->file ) fclose( cardFile->file );
+	if( cardFile->file ) claws_fclose( cardFile->file );
 	cardFile->file = NULL;
 }
 
@@ -203,7 +209,7 @@ static void vcard_close_file( VCardFile *cardFile ) {
 */
 static gchar *vcard_read_line( VCardFile *cardFile ) {
 	while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) {
-		if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
+		if( claws_fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
 			return NULL;
 		g_strstrip( cardFile->buffer );
 		cardFile->bufptr = cardFile->buffer;
@@ -584,16 +590,16 @@ gchar *vcard_find_gnomecard( void ) {
 	strncat( str, GNOMECARD_FILE, WORK_BUFLEN - strlen(str) );
 
 	fileSpec = NULL;
-	if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
+	if( ( fp = claws_fopen( str, "rb" ) ) != NULL ) {
 		/* Read configuration file */
 		lenlbl = strlen( GNOMECARD_SECTION );
-		while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
+		while( claws_fgets( buf, sizeof( buf ), fp ) != NULL ) {
 			if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
 				break;
 			}
 		}
 
-		while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
+		while( claws_fgets( buf, sizeof( buf ), fp ) != NULL ) {
 			g_strchomp( buf );
 			if( buf[0] == '[' ) break;
 			for( i = 0; i < lenlbl; i++ ) {
@@ -605,7 +611,7 @@ gchar *vcard_find_gnomecard( void ) {
 				}
 			}
 		}
-		fclose( fp );
+		claws_fclose( fp );
 	}
 
 	if( fileSpec == NULL ) {

commit 3d72e6abd9e8787265813ff2f632bc9548c5cdc0
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Sun Oct 7 19:16:15 2018 +0200

    Improve debug output in passwd_store_set().

diff --git a/src/passwordstore.c b/src/passwordstore.c
index 97f44d3..377e182 100644
--- a/src/passwordstore.c
+++ b/src/passwordstore.c
@@ -121,11 +121,6 @@ gboolean passwd_store_set(PasswordBlockType block_type,
 	else
 		p = password;
 
-	debug_print("%s password '%s' in block (%d/%s)%s\n",
-			(p == NULL ? "Deleting" : "Storing"),
-			password_id, block_type, block_name,
-			(encrypted ? ", already encrypted" : "") );
-
 	/* find correct block (create if needed) */
 	if ((block = _get_block(block_type, block_name)) == NULL) {
 		/* If caller wants to delete a password, and even its block
@@ -140,6 +135,11 @@ gboolean passwd_store_set(PasswordBlockType block_type,
 		}
 	}
 
+	debug_print("%s password for '%s' in block (%d/%s)%s\n",
+			(p == NULL ? "Deleting" : "Storing"),
+			password_id, block_type, block_name,
+			(encrypted ? ", already encrypted" : "") );
+
 	if (p == NULL) {
 		/* NULL password was passed to us, so delete the entry with
 		 * corresponding id */

commit d59c3d1cb008056725776720e8964c2880371514
Author: Ricardo Mones <ricardo at mones.org>
Date:   Sun Oct 7 13:27:30 2018 +0200

    Fix CID 1439996 and remove unnecessary comparison

diff --git a/src/procmime.c b/src/procmime.c
index c47c7b6..02c49a0 100644
--- a/src/procmime.c
+++ b/src/procmime.c
@@ -939,12 +939,10 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 	}
 
 	outfp = procmime_fopen(tmpfile, "w+");
-#endif
 
-	if (tmpfile != NULL) {
-		g_unlink(tmpfile);
-		g_free(tmpfile);
-	}
+	g_unlink(tmpfile);
+	g_free(tmpfile);
+#endif
 
 	if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
 		return NULL;

commit 4ceab3a96a00e52e29f4cf1b7363883b9c036bb2
Author: Colin Leroy <colin at colino.net>
Date:   Sun Oct 7 11:11:06 2018 +0200

    Hey I'm married :)

diff --git a/AUTHORS b/AUTHORS
index 22e1370..2b04e2d 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -5,7 +5,7 @@ Claws Mail
 	Andrej Kacian		<ticho at claws-mail.org> 
 	Darko Koruga		<darko at users.sourceforge.net>
 	Charles Lehner		<charles at claws-mail.org>
-	Colin Leroy		<colin at colino.net>
+	Colin Leroy-Mira	<colin at colino.net>
 	Paul Mangan		<paul at claws-mail.org>
 	Ricardo Mones Lastra 	<ricardo at mones.org>
 	Salvatore De Paolis	<iwkse at claws-mail.org>
diff --git a/src/gtk/authors.h b/src/gtk/authors.h
index 2d4a706..20634f7 100644
--- a/src/gtk/authors.h
+++ b/src/gtk/authors.h
@@ -4,7 +4,7 @@ static char *TEAM_LIST[] = {
 "Andrej Kacian <andrej at kacian.sk>",
 "Darko Koruga <darko at users.sf.net>",
 "Charles Lehner <charles at claws-mail.org>",
-"Colin Leroy <colin at colino.net>",
+"Colin Leroy-Mira <colin at colino.net>",
 "Paul Mangan <paul at claws-mail.org>",
 "Ricardo Mones Lastra <ricardo at mones.org>",
 "Salvatore De Paolis <iwkse at claws-mail.org>",

commit 86d559c965d7a7934fd7421f4a4f5adb522ccaa1
Author: Ricardo Mones <ricardo at mones.org>
Date:   Sat Oct 6 16:38:23 2018 +0200

    Don't ignore required file

diff --git a/.gitignore b/.gitignore
index ba5856d..4c43aaa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -90,7 +90,6 @@ Makefile.in
 /po/POTFILES
 /po/quot.sed
 /po/remove-potcdate.sed
-/po/remove-potcdate.sin
 /po/Rules-quot
 /.project
 /.settings

commit 5708f64867944e0a7eab1f6666828dcc4d34d91e
Author: Colin Leroy <colin at colino.net>
Date:   Sat Oct 6 13:04:16 2018 +0200

    Fix indent, sorry.

diff --git a/src/matcher.c b/src/matcher.c
index c58c559..5b67051 100644
--- a/src/matcher.c
+++ b/src/matcher.c
@@ -487,10 +487,10 @@ static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
 	if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
 	    prop->matchtype == MATCHTYPE_MATCHCASE) {
 		str1 = g_utf8_casefold(str, -1);
-	if (!prop->casefold_expr) {
-		prop->casefold_expr = g_utf8_casefold(prop->expr, -1);
-	}
-	down_expr = prop->casefold_expr;
+		if (!prop->casefold_expr) {
+			prop->casefold_expr = g_utf8_casefold(prop->expr, -1);
+		}
+		down_expr = prop->casefold_expr;
 
 		should_free = TRUE;
 	} else {

commit dc7dfb44b11da99cee879e25f9075cb816638d4f
Author: Colin Leroy <colin at colino.net>
Date:   Sat Oct 6 13:00:00 2018 +0200

    Fix build

diff --git a/src/Makefile.am b/src/Makefile.am
index b19eccd..9844f30 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -228,7 +228,6 @@ claws_mail_SOURCES = \
 	quote_fmt_parse.y \
 	recv.c \
 	remotefolder.c \
-	safe_fclose.c \
 	send_message.c \
 	setup.c \
 	sourcewindow.c \
@@ -349,7 +348,6 @@ claws_mailinclude_HEADERS = \
 	quote_fmt_parse.h \
 	recv.h \
 	remotefolder.h \
-	safe_fclose.h \
 	send_message.h \
 	setup.h \
 	sourcewindow.h \
diff --git a/src/addrbook.c b/src/addrbook.c
index c519eb7..0720c6b 100644
--- a/src/addrbook.c
+++ b/src/addrbook.c
@@ -33,7 +33,7 @@
 #include "addrcache.h"
 #include "addrbook.h"
 #include "adbookbase.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
diff --git a/src/addrindex.c b/src/addrindex.c
index fda0bcd..f1adf01 100644
--- a/src/addrindex.c
+++ b/src/addrindex.c
@@ -43,7 +43,7 @@
 #include "utils.h"
 #include "alertpanel.h"
 #include "passwordstore.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index d1fd799..aa307ae 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -43,6 +43,7 @@ libclawscommon_la_SOURCES = $(arch_sources) \
 	string_match.c \
 	stringtable.c \
 	claws.c \
+	claws_io.c \
 	tags.c \
 	template.c \
 	utils.c \
@@ -74,6 +75,7 @@ clawscommoninclude_HEADERS = $(arch_headers) \
 	string_match.h \
 	stringtable.h \
 	claws.h \
+	claws_io.h \
 	tags.h \
 	template.h \
 	timing.h \
@@ -87,7 +89,6 @@ clawscommoninclude_HEADERS = $(arch_headers) \
 	unmime.h
 
 AM_CPPFLAGS = \
-	-I$(srcdir)/.. \
 	-I$(top_srcdir)/intl \
 	$(GLIB_CFLAGS) \
 	$(VALGRIND_CFLAGS) \
diff --git a/src/safe_fclose.c b/src/common/claws_io.c
similarity index 88%
rename from src/safe_fclose.c
rename to src/common/claws_io.c
index bec77fd..1de8bea 100644
--- a/src/safe_fclose.c
+++ b/src/common/claws_io.c
@@ -24,8 +24,9 @@
 #include <stdio.h>
 #include <unistd.h>
 
-#include "prefs_common.h"
-#include "common/timing.h"
+#include "timing.h"
+
+gboolean prefs_common_get_flush_metadata(void);
 
 int safe_fclose(FILE *fp)
 {
@@ -35,7 +36,7 @@ int safe_fclose(FILE *fp)
 	if (fflush(fp) != 0) {
 		return EOF;
 	}
-	if (prefs_common_get_prefs()->flush_metadata && fsync(fileno(fp)) != 0) {
+	if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
 		return EOF;
 	}
 
diff --git a/src/safe_fclose.h b/src/common/claws_io.h
similarity index 94%
rename from src/safe_fclose.h
rename to src/common/claws_io.h
index f0153a5..e3e0e0c 100644
--- a/src/safe_fclose.h
+++ b/src/common/claws_io.h
@@ -16,8 +16,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef __SAFE_FCLOSE_H__
-#define __SAFE_FCLOSE_H__
+#ifndef __CLAWS_IO_H__
+#define __CLAWS_IO_H__
 
 int safe_fclose(FILE *fp);
 
diff --git a/src/common/log.c b/src/common/log.c
index 1003d51..65f284e 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -35,7 +35,7 @@
 #include "utils.h"
 #include "log.h"
 #include "hooks.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #define FWRITE(_b,_s,_n,_f)	if (fwrite(_b,_s,_n,_f) != _n) { \
 					g_message("log fwrite failed!\n"); \
diff --git a/src/common/prefs.c b/src/common/prefs.c
index 362cbb9..77b4213 100644
--- a/src/common/prefs.c
+++ b/src/common/prefs.c
@@ -28,7 +28,7 @@
 
 #include "prefs.h"
 #include "utils.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 static gboolean prefs_is_readonly	(const gchar 	*path);
 
diff --git a/src/common/ssl_certificate.c b/src/common/ssl_certificate.c
index 1f71662..d098ac2 100644
--- a/src/common/ssl_certificate.c
+++ b/src/common/ssl_certificate.c
@@ -46,7 +46,7 @@
 #include "socket.h"
 #include "hooks.h"
 #include "defs.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 static GHashTable *warned_expired = NULL;
 
diff --git a/src/common/tags.c b/src/common/tags.c
index e0f020a..264f139 100644
--- a/src/common/tags.c
+++ b/src/common/tags.c
@@ -40,7 +40,7 @@
 #include "defs.h"
 #include "utils.h"
 #include "tags.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 static GHashTable *tags_table = NULL;
 static GHashTable *tags_reverse_table = NULL;
diff --git a/src/common/template.c b/src/common/template.c
index dfd9603..e8db301 100644
--- a/src/common/template.c
+++ b/src/common/template.c
@@ -29,7 +29,7 @@
 #include "utils.h"
 #include "template.h"
 #include "codeconv.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 static GSList *template_list;
 
diff --git a/src/common/utils.c b/src/common/utils.c
index e3ba80c..977eda4 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -86,7 +86,7 @@
 #include "codeconv.h"
 #include "tlds.h"
 #include "timing.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #define BUFFSIZE	8192
 
diff --git a/src/compose.c b/src/compose.c
index 88ff254..8e0fd63 100644
--- a/src/compose.c
+++ b/src/compose.c
@@ -110,7 +110,7 @@
 #include "autofaces.h"
 #include "spell_entry.h"
 #include "headers.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifdef USE_LDAP
 #include "password.h"
diff --git a/src/etpan/imap-thread.c b/src/etpan/imap-thread.c
index 29d0380..48823e1 100644
--- a/src/etpan/imap-thread.c
+++ b/src/etpan/imap-thread.c
@@ -44,7 +44,7 @@
 #include "utils.h"
 #include "mainwindow.h"
 #include "proxy.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 #include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
diff --git a/src/exporthtml.c b/src/exporthtml.c
index 7ed6fc8..d0bfefd 100644
--- a/src/exporthtml.c
+++ b/src/exporthtml.c
@@ -46,7 +46,7 @@
 #include "utils.h"
 #include "exporthtml.h"
 #include "xmlprops.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifdef MKDIR_TAKES_ONE_ARG
 #undef mkdir
diff --git a/src/exportldif.c b/src/exportldif.c
index eba8737..ded6a3e 100644
--- a/src/exportldif.c
+++ b/src/exportldif.c
@@ -38,7 +38,7 @@
 #include "exportldif.h"
 #include "xmlprops.h"
 #include "ldif.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 
 #ifdef MKDIR_TAKES_ONE_ARG
diff --git a/src/ldapquery.c b/src/ldapquery.c
index 257ae19..5b0750a 100644
--- a/src/ldapquery.c
+++ b/src/ldapquery.c
@@ -38,7 +38,7 @@
 #include "ldapctrl.h"
 #include "ldapserver.h"
 #include "mgutils.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #include "addritem.h"
 #include "addrcache.h"
diff --git a/src/main.c b/src/main.c
index 8a0ce23..0bfee71 100644
--- a/src/main.c
+++ b/src/main.c
@@ -132,7 +132,7 @@
 #include "advsearch.h"
 #include "avatars.h"
 #include "passwordstore.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
diff --git a/src/mbox.c b/src/mbox.c
index ea1138a..68b3d01 100644
--- a/src/mbox.c
+++ b/src/mbox.c
@@ -56,7 +56,7 @@
 #include "filtering.h"
 #include "alertpanel.h"
 #include "statusbar.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #define MESSAGEBUFSIZE	8192
 
diff --git a/src/messageview.c b/src/messageview.c
index 0882cc4..d9c3fa9 100644
--- a/src/messageview.c
+++ b/src/messageview.c
@@ -67,7 +67,7 @@
 #include "statusbar.h"
 #include "folder_item_prefs.h"
 #include "avatars.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifndef USE_ALT_ADDRBOOK
 	#include "addressbook.h"
diff --git a/src/mh.c b/src/mh.c
index 713ce0d..69d0eca 100644
--- a/src/mh.c
+++ b/src/mh.c
@@ -44,7 +44,7 @@
 #include "gtkutils.h"
 #include "timing.h"
 #include "msgcache.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 /* Define possible missing constants for Windows. */
 #ifdef G_OS_WIN32
diff --git a/src/msgcache.c b/src/msgcache.c
index 871c04a..8909ea7 100644
--- a/src/msgcache.c
+++ b/src/msgcache.c
@@ -47,7 +47,7 @@
 #include "timing.h"
 #include "tags.h"
 #include "prefs_common.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifdef HAVE_FWRITE_UNLOCKED
 #define SC_FWRITE fwrite_unlocked
diff --git a/src/news.c b/src/news.c
index 6c29cfb..296a4f6 100644
--- a/src/news.c
+++ b/src/news.c
@@ -62,7 +62,7 @@
 #  include "ssl.h"
 #endif
 #include "main.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #define NNTP_PORT	119
 #ifdef USE_GNUTLS
diff --git a/src/partial_download.c b/src/partial_download.c
index 384393e..9d3c7d2 100644
--- a/src/partial_download.c
+++ b/src/partial_download.c
@@ -63,7 +63,7 @@
 #include "folder.h"
 #include "procheader.h"
 #include "msgcache.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 int partial_msg_in_uidl_list(MsgInfo *msginfo)
 {
diff --git a/src/plugins/acpi_notifier/acpi_notifier.c b/src/plugins/acpi_notifier/acpi_notifier.c
index ee59b2e..e6e07e0 100644
--- a/src/plugins/acpi_notifier/acpi_notifier.c
+++ b/src/plugins/acpi_notifier/acpi_notifier.c
@@ -48,7 +48,7 @@
 #include "folder_item_prefs.h"
 #include "gtk/gtkutils.h"
 #include "gtk/combobox.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #define PREFS_BLOCK_NAME "AcpiNotifier"
 #define PLUGIN_NAME _("Acpi Notifier")
diff --git a/src/plugins/bogofilter/bogofilter.c b/src/plugins/bogofilter/bogofilter.c
index 446b652..c6a978d 100644
--- a/src/plugins/bogofilter/bogofilter.c
+++ b/src/plugins/bogofilter/bogofilter.c
@@ -52,7 +52,7 @@
 #include "prefs_common.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifdef HAVE_SYSEXITS_H
 #include <sysexits.h>
diff --git a/src/plugins/fancy/fancy_viewer.c b/src/plugins/fancy/fancy_viewer.c
index 1823fd4..a1b434f 100644
--- a/src/plugins/fancy/fancy_viewer.c
+++ b/src/plugins/fancy/fancy_viewer.c
@@ -28,7 +28,7 @@
 #include <fancy_viewer.h>
 #include <fancy_prefs.h>
 #include <alertpanel.h>
-#include <safe_fclose.h>
+#include <claws_io.h>
 
 #include <printing.h>
 #include <webkit/webkithittestresult.h>
diff --git a/src/plugins/libravatar/libravatar_image.c b/src/plugins/libravatar/libravatar_image.c
index 66a0e1d..1482ae1 100644
--- a/src/plugins/libravatar/libravatar_image.c
+++ b/src/plugins/libravatar/libravatar_image.c
@@ -27,7 +27,7 @@
 
 #include <common/claws.h>
 #include <prefs_common.h>
-#include <safe_fclose.h>
+#include <claws_io.h>
 
 #include "libravatar.h"
 #include "libravatar_prefs.h"
diff --git a/src/plugins/libravatar/libravatar_missing.c b/src/plugins/libravatar/libravatar_missing.c
index 6a01911..a19de48 100644
--- a/src/plugins/libravatar/libravatar_missing.c
+++ b/src/plugins/libravatar/libravatar_missing.c
@@ -21,7 +21,7 @@
 #include "libravatar_missing.h"
 #include "libravatar_prefs.h"
 #include "utils.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 /**
  * Loads the hash table of md5sum → time from the given filename.
diff --git a/src/plugins/mailmbox/mailmbox_folder.c b/src/plugins/mailmbox/mailmbox_folder.c
index 1db007b..c8a77d8 100644
--- a/src/plugins/mailmbox/mailmbox_folder.c
+++ b/src/plugins/mailmbox/mailmbox_folder.c
@@ -56,7 +56,7 @@
 #include "mailmbox.h"
 #include "mailmbox_folder.h"
 #include "mailmbox_parse.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #define MAILMBOX_CACHE_DIR           "mailmboxcache"
 
diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c
index bd2f026..4f8e789 100644
--- a/src/plugins/pgpinline/pgpinline.c
+++ b/src/plugins/pgpinline/pgpinline.c
@@ -28,7 +28,7 @@
 #include <glib/gi18n.h>
 #include <errno.h>
 #include <gpgme.h>
-#include <safe_fclose.h>
+#include <claws_io.h>
 
 #include "utils.h"
 #include "privacy.h"
diff --git a/src/plugins/pgpmime/pgpmime.c b/src/plugins/pgpmime/pgpmime.c
index ff01c93..3827487 100644
--- a/src/plugins/pgpmime/pgpmime.c
+++ b/src/plugins/pgpmime/pgpmime.c
@@ -42,7 +42,7 @@
 #include <plugins/pgpcore/pgp_utils.h>
 
 #include "prefs_common.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
diff --git a/src/plugins/rssyl/opml_export.c b/src/plugins/rssyl/opml_export.c
index 884f72c..20649db 100644
--- a/src/plugins/rssyl/opml_export.c
+++ b/src/plugins/rssyl/opml_export.c
@@ -32,7 +32,7 @@
 #include <log.h>
 #include <folder.h>
 #include <common/utils.h>
-#include <safe_fclose.h>
+#include <claws_io.h>
 
 /* Local includes */
 #include "libfeed/date.h"
diff --git a/src/plugins/rssyl/rssyl_add_item.c b/src/plugins/rssyl/rssyl_add_item.c
index 048eb04..a7906fd 100644
--- a/src/plugins/rssyl/rssyl_add_item.c
+++ b/src/plugins/rssyl/rssyl_add_item.c
@@ -34,7 +34,7 @@
 #include <codeconv.h>
 #include <procmsg.h>
 #include <common/utils.h>
-#include <safe_fclose.h>
+#include <claws_io.h>
 
 /* Local includes */
 #include "libfeed/date.h"
diff --git a/src/plugins/rssyl/rssyl_deleted.c b/src/plugins/rssyl/rssyl_deleted.c
index f6e6a76..11e2ed1 100644
--- a/src/plugins/rssyl/rssyl_deleted.c
+++ b/src/plugins/rssyl/rssyl_deleted.c
@@ -31,7 +31,7 @@
 /* Claws Mail includes */
 #include <codeconv.h>
 #include <common/utils.h>
-#include <safe_fclose.h>
+#include <claws_io.h>
 
 /* Local includes */
 #include "rssyl.h"
diff --git a/src/plugins/smime/smime.c b/src/plugins/smime/smime.c
index 08d8dc2..abc49c8 100644
--- a/src/plugins/smime/smime.c
+++ b/src/plugins/smime/smime.c
@@ -43,7 +43,7 @@
 #include "prefs_common.h"
 #include "procmime.h"
 #include "plugin.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c
index 68d2069..df05f4a 100644
--- a/src/plugins/vcalendar/vcal_meeting_gtk.c
+++ b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -54,7 +54,7 @@
 #include "gtkutils.h"
 #include "log.h"
 #include "utils.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 struct _VCalMeeting
 {
diff --git a/src/pop.c b/src/pop.c
index f89ddc3..9a60c95 100644
--- a/src/pop.c
+++ b/src/pop.c
@@ -40,7 +40,7 @@
 #include "partial_download.h"
 #include "log.h"
 #include "hooks.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 static gint pop3_greeting_recv		(Pop3Session *session,
 					 const gchar *msg);
diff --git a/src/prefs_common.c b/src/prefs_common.c
index e1b9fde..54bbf68 100644
--- a/src/prefs_common.c
+++ b/src/prefs_common.c
@@ -61,7 +61,7 @@
 #include "prefswindow.h"
 #include "colorlabel.h"
 #include "passwordstore.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 #ifndef USE_ALT_ADDRBOOK
 	#include "addrcustomattr.h"
diff --git a/src/procmsg.c b/src/procmsg.c
index 5c776b4..73cd7c0 100644
--- a/src/procmsg.c
+++ b/src/procmsg.c
@@ -49,7 +49,7 @@
 #include "timing.h"
 #include "inc.h"
 #include "privacy.h"
-#include "safe_fclose.h"
+#include "claws_io.h"
 
 extern SessionStats session_stats;
 

commit 9121905e6511bb40b2c499a2472f54cd784b111f
Author: Colin Leroy <colin at colino.net>
Date:   Sat Oct 6 12:48:01 2018 +0200

    Use a memory-based FILE pointer when we can; speeds up search.

diff --git a/configure.ac b/configure.ac
index fbe9f7b..0ac7fd6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -423,7 +423,7 @@ AC_CHECK_FUNCS(gethostname mkdir mktime socket strstr strchr \
 	       uname flock lockf inet_aton inet_addr \
 	       fchmod mkstemp truncate getuid regcomp)
 
-AC_CHECK_FUNCS(fgets_unlocked fwrite_unlocked)
+AC_CHECK_FUNCS(fgets_unlocked fwrite_unlocked fmemopen)
 
 dnl *****************
 dnl ** common code **
diff --git a/src/procmime.c b/src/procmime.c
index da81590..c47c7b6 100644
--- a/src/procmime.c
+++ b/src/procmime.c
@@ -64,6 +64,7 @@ MimeInfo *procmime_mimeinfo_new(void)
 	MimeInfo *mimeinfo;
 
 	mimeinfo = g_new0(MimeInfo, 1);
+
 	mimeinfo->content	 = MIMECONTENT_EMPTY;
 	mimeinfo->data.filename	 = NULL;
 
@@ -702,14 +703,14 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 	return TRUE;
 }
 
-gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
+static gint procmime_get_part_to_stream(FILE *outfp, MimeInfo *mimeinfo)
 {
-	FILE *infp, *outfp;
+	FILE *infp;
 	gchar buf[BUFFSIZE];
 	gint restlength, readlength;
 	gint saved_errno = 0;
 
-	cm_return_val_if_fail(outfile != NULL, -1);
+	cm_return_val_if_fail(outfp != NULL, -1);
 	cm_return_val_if_fail(mimeinfo != NULL, -1);
 
 	if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
@@ -726,12 +727,6 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
 		procmime_fclose(infp);
 		return -(saved_errno);
 	}
-	if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
-		saved_errno = errno;
-		FILE_OP_ERROR(outfile, "fopen");
-		procmime_fclose(infp);
-		return -(saved_errno);
-	}
 
 	restlength = mimeinfo->length;
 
@@ -739,13 +734,33 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
 		if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
 			saved_errno = errno;
 			procmime_fclose(infp);
-			procmime_fclose(outfp);
 			return -(saved_errno);
 		}
 		restlength -= readlength;
 	}
 
 	procmime_fclose(infp);
+	rewind(outfp);
+
+	return 0;
+}
+
+gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
+{
+	FILE *outfp;
+	gint result;
+	gint saved_errno = 0;
+
+	cm_return_val_if_fail(outfile != NULL, -1);
+
+	if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
+		saved_errno = errno;
+		FILE_OP_ERROR(outfile, "fopen");
+		return -(saved_errno);
+	}
+
+	result = procmime_get_part_to_stream(outfp, mimeinfo);
+
 	if (procmime_fclose(outfp) == EOF) {
 		saved_errno = errno;
 		FILE_OP_ERROR(outfile, "fclose");
@@ -753,7 +768,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
 		return -(saved_errno);
 	}
 
-	return 0;
+	return result;
 }
 
 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
@@ -765,8 +780,9 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 	gboolean conv_fail = FALSE;
 	gchar buf[BUFFSIZE];
 	gchar *str;
-	gchar *tmpfile;
 	gboolean scan_ret = FALSE;
+	gchar *tmpfile = NULL;
+	int r;
 
 	cm_return_val_if_fail(mimeinfo != NULL, TRUE);
 	cm_return_val_if_fail(scan_callback != NULL, TRUE);
@@ -774,17 +790,26 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 	if (!procmime_decode_content(mimeinfo))
 		return TRUE;
 
+#if HAVE_FMEMOPEN
+	tmpfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
+#else
 	tmpfile = procmime_get_tmp_file_name(mimeinfo);
-	if (tmpfile == NULL)
+	if (tmpfile == NULL) {
+		g_warning("no filename\n");
 		return TRUE;
+	}
+
+	tmpfp = procmime_fopen(tmpfile, "w+");
+#endif
 
-	if (procmime_get_part(tmpfile, mimeinfo) < 0) {
+	if (tmpfp == NULL) {
 		g_free(tmpfile);
+		FILE_OP_ERROR(tmpfile, "open");
 		return TRUE;
 	}
 
-	tmpfp = procmime_fopen(tmpfile, "rb");
-	if (tmpfp == NULL) {
+	if ((r = procmime_get_part_to_stream(tmpfp, mimeinfo)) < 0) {
+		g_warning("procmime_get_part_to_stream error %d\n", r);
 		g_free(tmpfile);
 		return TRUE;
 	}
@@ -850,8 +875,11 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 		g_warning("procmime_get_text_content(): Code conversion failed.");
 
 	procmime_fclose(tmpfp);
+
+#if !HAVE_FMEMOPEN
 	claws_unlink(tmpfile);
 	g_free(tmpfile);
+#endif
 
 	return scan_ret;
 }
@@ -894,31 +922,33 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 {
 	FILE *outfp;
-	gchar *tmpfile;
+	gchar *tmpfile = NULL;
 
 	cm_return_val_if_fail(mimeinfo != NULL, NULL);
 
 	if (!procmime_decode_content(mimeinfo))
 		return NULL;
 
+#if HAVE_FMEMOPEN
+	outfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
+#else
 	tmpfile = procmime_get_tmp_file_name(mimeinfo);
-	if (tmpfile == NULL)
-		return NULL;
-
-	if (procmime_get_part(tmpfile, mimeinfo) < 0) {
-		g_free(tmpfile);
-		return NULL;
+	if (tmpfile == NULL) {
+		g_warning("no filename\n");
+		return TRUE;
 	}
 
-	outfp = procmime_fopen(tmpfile, "rb");
-	if (outfp == NULL) {
+	outfp = procmime_fopen(tmpfile, "w+");
+#endif
+
+	if (tmpfile != NULL) {
 		g_unlink(tmpfile);
 		g_free(tmpfile);
-		return NULL;
 	}
 
-	g_unlink(tmpfile);
-	g_free(tmpfile);
+	if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
+		return NULL;
+	}
 
 #ifdef HAVE_FGETS_UNLOCKED
 	funlockfile(outfp);

commit 15bafe17796fd4429fe2b09abd9e2bab6c2a3ac7
Author: Colin Leroy <colin at colino.net>
Date:   Sat Oct 6 12:46:48 2018 +0200

    Don't bother parsing headers when we want to skip them
    Speeds up body-only search

diff --git a/src/matcher.c b/src/matcher.c
index 4a0392d..c58c559 100644
--- a/src/matcher.c
+++ b/src/matcher.c
@@ -1330,19 +1330,6 @@ void matcherlist_free(MatcherList *cond)
 }
 
 /*!
- *\brief	Skip all headers in a message file
- *
- *\param	fp Message file
- */
-static void matcherlist_skip_headers(FILE *fp)
-{
-	gchar *buf = NULL;
-
-	while (procheader_get_one_field(&buf, fp, NULL) != -1)
-		g_free(buf);
-}
-
-/*!
  *\brief	Check if a header matches a matcher condition
  *
  *\param	matcher Matcher structure to check header for
@@ -1848,7 +1835,7 @@ static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
 		if (matcherlist_match_headers(matchers, fp))
 			read_body = FALSE;
 	} else {
-		matcherlist_skip_headers(fp);
+		procheader_skip_headers(fp);
 	}
 
 	/* read the body */
diff --git a/src/procheader.c b/src/procheader.c
index 174e283..6a98f12 100644
--- a/src/procheader.c
+++ b/src/procheader.c
@@ -83,6 +83,24 @@ static gint string_get_one_field(gchar **buf, char **str,
 				     TRUE);
 }
 
+gboolean procheader_skip_headers(FILE *fp)
+{
+	gchar *buf = g_malloc(BUFFSIZE);
+	do {
+		if (fgets_crlf(buf, BUFFSIZE - 1, fp) == NULL) {
+			g_free(buf);
+			return FALSE;
+		}
+		if (buf[0] == '\r' || buf[0] == '\n') {
+			break;
+		}
+	} while (TRUE);
+	g_free(buf);
+
+	return TRUE;
+}
+
+
 static char *string_getline(char *buf, size_t len, char **str)
 {
 	gboolean is_cr = FALSE;
diff --git a/src/procheader.h b/src/procheader.h
index 0588f30..50e6c55 100644
--- a/src/procheader.h
+++ b/src/procheader.h
@@ -51,6 +51,8 @@ GPtrArray *procheader_get_header_array_asis	(FILE		*fp);
 void procheader_header_array_destroy		(GPtrArray	*harray);
 void procheader_header_free			(Header		*header);
 
+gboolean procheader_skip_headers(FILE *fp);
+
 void procheader_get_header_fields	(FILE		*fp,
 					 HeaderEntry	 hentry[]);
 MsgInfo *procheader_parse_file		(const gchar	*file,

commit 7aeffbd66c0b2b8f3d1003894fe39e1491763eeb
Author: Colin Leroy <colin at colino.net>
Date:   Sat Oct 6 12:43:31 2018 +0200

    Fix big leak on mimeinfo freeing

diff --git a/src/procmime.c b/src/procmime.c
index f53e530..da81590 100644
--- a/src/procmime.c
+++ b/src/procmime.c
@@ -138,6 +138,8 @@ static gboolean free_func(GNode *node, gpointer data)
 	if (mimeinfo->privacy)
 		privacy_free_privacydata(mimeinfo->privacy);
 
+	g_free(mimeinfo);
+
 	return FALSE;
 }
 
@@ -154,7 +156,6 @@ void procmime_mimeinfo_free_all(MimeInfo **mimeinfo_ptr)
 
 	g_node_destroy(node);
 
-	g_free(mimeinfo);
 	*mimeinfo_ptr = NULL;
 }
 

commit b51f1a104e927913bc03c7476656a0f5a64698d3
Author: Colin Leroy <colin at colino.net>
Date:   Sat Oct 6 11:26:53 2018 +0200

    Halve the time spent manipulating case in case-insensitive searches

diff --git a/src/matcher.c b/src/matcher.c
index 29581e9..4a0392d 100644
--- a/src/matcher.c
+++ b/src/matcher.c
@@ -324,6 +324,7 @@ MatcherProp *matcherprop_new(gint criteria, const gchar *header,
 #ifndef G_OS_WIN32
 	prop->preg = NULL;
 #endif
+	prop->casefold_expr = NULL;
 	prop->value = value;
 	prop->error = 0;
 
@@ -345,6 +346,7 @@ void matcherprop_free(MatcherProp *prop)
 		regfree(prop->preg);
 		g_free(prop->preg);
 	}
+	g_free(prop->casefold_expr);
 #endif
 	g_free(prop);
 }
@@ -368,6 +370,7 @@ MatcherProp *matcherprop_copy(const MatcherProp *src)
 #ifndef G_OS_WIN32
 	prop->preg = NULL; /* will be re-evaluated */
 #endif
+	prop->casefold_expr = src->casefold_expr ? g_strdup(src->casefold_expr) : NULL;
 	prop->value = src->value;
 	prop->error = src->error;	
 	return prop;		
@@ -475,7 +478,7 @@ static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
 					 const gchar *debug_context)
 {
 	gchar *str1;
-	gchar *down_expr;
+	const gchar *down_expr;
 	gboolean ret = FALSE;
 	gboolean should_free = FALSE;
 	if (str == NULL)
@@ -484,7 +487,11 @@ static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
 	if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
 	    prop->matchtype == MATCHTYPE_MATCHCASE) {
 		str1 = g_utf8_casefold(str, -1);
-		down_expr = g_utf8_casefold(prop->expr, -1);
+	if (!prop->casefold_expr) {
+		prop->casefold_expr = g_utf8_casefold(prop->expr, -1);
+	}
+	down_expr = prop->casefold_expr;
+
 		should_free = TRUE;
 	} else {
 		str1 = (gchar *)str;
@@ -569,7 +576,6 @@ static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
 free_strs:
 	if (should_free) {
 		g_free(str1);
-		g_free(down_expr);
 	}
 	return ret;
 }
diff --git a/src/matcher.h b/src/matcher.h
index 392924d..6b6fbbe 100644
--- a/src/matcher.h
+++ b/src/matcher.h
@@ -36,10 +36,13 @@ struct _MatcherProp {
 	gchar *header;
 	gchar *expr;
 	int value;
-	regex_t *preg;
 	int error;
 	gboolean result;
 	gboolean done;
+	/* Allows recompiling expr each time */
+	regex_t *preg;
+	/* Allows casefolding expr each time */
+	gchar *casefold_expr;
 };
 
 struct _MatcherList {

commit d8b3d1705d4ad8da8200e9247ca54c79439fc9dc
Author: Colin Leroy <colin at colino.net>
Date:   Fri Oct 5 23:01:46 2018 +0200

    Implement safe_fclose() so we can respect the "Metadata handling"
    preference for every data file we write into.

diff --git a/src/Makefile.am b/src/Makefile.am
index 9844f30..b19eccd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -228,6 +228,7 @@ claws_mail_SOURCES = \
 	quote_fmt_parse.y \
 	recv.c \
 	remotefolder.c \
+	safe_fclose.c \
 	send_message.c \
 	setup.c \
 	sourcewindow.c \
@@ -348,6 +349,7 @@ claws_mailinclude_HEADERS = \
 	quote_fmt_parse.h \
 	recv.h \
 	remotefolder.h \
+	safe_fclose.h \
 	send_message.h \
 	setup.h \
 	sourcewindow.h \
diff --git a/src/addrbook.c b/src/addrbook.c
index 274d4ac..c519eb7 100644
--- a/src/addrbook.c
+++ b/src/addrbook.c
@@ -33,6 +33,7 @@
 #include "addrcache.h"
 #include "addrbook.h"
 #include "adbookbase.h"
+#include "safe_fclose.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
@@ -1318,7 +1319,7 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
 
 		book->retVal = MGU_SUCCESS;
 #ifdef DEV_STANDALONE
-		fclose(fp);
+		safe_fclose(fp);
 #else
 		if (prefs_file_close( pfile ) < 0)
 			book->retVal = MGU_ERROR_WRITE;
diff --git a/src/addrindex.c b/src/addrindex.c
index 8012996..fda0bcd 100644
--- a/src/addrindex.c
+++ b/src/addrindex.c
@@ -43,6 +43,7 @@
 #include "utils.h"
 #include "alertpanel.h"
 #include "passwordstore.h"
+#include "safe_fclose.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
@@ -1782,7 +1783,7 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
 
 		addrIndex->retVal = MGU_SUCCESS;
 #ifdef DEV_STANDALONE
-		fclose( fp );
+		safe_fclose( fp );
 #else
 		if( prefs_file_close( pfile ) < 0 ) {
 			addrIndex->retVal = MGU_ERROR_WRITE;
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 5c4f249..d1fd799 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -87,6 +87,7 @@ clawscommoninclude_HEADERS = $(arch_headers) \
 	unmime.h
 
 AM_CPPFLAGS = \
+	-I$(srcdir)/.. \
 	-I$(top_srcdir)/intl \
 	$(GLIB_CFLAGS) \
 	$(VALGRIND_CFLAGS) \
diff --git a/src/common/log.c b/src/common/log.c
index 286b836..1003d51 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -35,6 +35,7 @@
 #include "utils.h"
 #include "log.h"
 #include "hooks.h"
+#include "safe_fclose.h"
 
 #define FWRITE(_b,_s,_n,_f)	if (fwrite(_b,_s,_n,_f) != _n) { \
 					g_message("log fwrite failed!\n"); \
@@ -136,7 +137,7 @@ void set_log_file(LogInstance instance, const gchar *filename)
 void close_log_file(LogInstance instance)
 {
 	if (log_fp[instance]) {
-		fclose(log_fp[instance]);
+		safe_fclose(log_fp[instance]);
 		log_fp[instance] = NULL;
 		log_size[instance] = 0;
 		g_free(log_filename[instance]);
diff --git a/src/common/prefs.c b/src/common/prefs.c
index 23bcc22..362cbb9 100644
--- a/src/common/prefs.c
+++ b/src/common/prefs.c
@@ -28,6 +28,7 @@
 
 #include "prefs.h"
 #include "utils.h"
+#include "safe_fclose.h"
 
 static gboolean prefs_is_readonly	(const gchar 	*path);
 
@@ -159,17 +160,7 @@ gint prefs_file_close(PrefFile *pfile)
 
 	tmppath = g_strconcat(path, ".tmp", NULL);
 
-	
-	if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) < 0) {
-		FILE_OP_ERROR(tmppath, "fsync");
-		fclose(fp);
-		claws_unlink(tmppath);
-		g_free(path);
-		g_free(tmppath);
-		return -1;
-	}
-
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(tmppath, "fclose");
 		claws_unlink(tmppath);
 		g_free(path);
diff --git a/src/common/ssl_certificate.c b/src/common/ssl_certificate.c
index 75b61e6..1f71662 100644
--- a/src/common/ssl_certificate.c
+++ b/src/common/ssl_certificate.c
@@ -46,6 +46,7 @@
 #include "socket.h"
 #include "hooks.h"
 #include "defs.h"
+#include "safe_fclose.h"
 
 static GHashTable *warned_expired = NULL;
 
@@ -376,7 +377,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
 	gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
 
 	g_free(file);
-	fclose(fp);
+	safe_fclose(fp);
 
 }
 
@@ -682,7 +683,7 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
 
 	}
 	if (fp)
-		fclose(fp);
+		safe_fclose(fp);
 }
 
 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, 
diff --git a/src/common/tags.c b/src/common/tags.c
index e68ee64..e0f020a 100644
--- a/src/common/tags.c
+++ b/src/common/tags.c
@@ -40,6 +40,7 @@
 #include "defs.h"
 #include "utils.h"
 #include "tags.h"
+#include "safe_fclose.h"
 
 static GHashTable *tags_table = NULL;
 static GHashTable *tags_reverse_table = NULL;
@@ -146,7 +147,7 @@ void tags_write_tags(void)
 		return;
 	}
 	
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(file, "fclose");
 		g_free(file);
 		g_free(file_new);
diff --git a/src/common/template.c b/src/common/template.c
index 9d53d88..dfd9603 100644
--- a/src/common/template.c
+++ b/src/common/template.c
@@ -29,6 +29,7 @@
 #include "utils.h"
 #include "template.h"
 #include "codeconv.h"
+#include "safe_fclose.h"
 
 static GSList *template_list;
 
@@ -283,7 +284,7 @@ static void template_write_config(GSList *tmpl_list)
 		} else {
 			TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
 		}
-		TRY_NO_CLOSE(fclose(fp) != EOF);
+		TRY_NO_CLOSE(safe_fclose(fp) != EOF);
 
 		if (new) {
 			if (rename_force(new, filename) < 0) {
diff --git a/src/common/utils.c b/src/common/utils.c
index 3833545..e3ba80c 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -85,6 +85,8 @@
 #include "socket.h"
 #include "codeconv.h"
 #include "tlds.h"
+#include "timing.h"
+#include "safe_fclose.h"
 
 #define BUFFSIZE	8192
 
@@ -340,7 +342,7 @@ gint file_strip_crs(const gchar *file)
 	}
 
 	fclose(fp);
-	if (fclose(outfp) == EOF) {
+	if (safe_fclose(outfp) == EOF) {
 		goto unlinkout;
 	}
 	
@@ -2523,7 +2525,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 		err = TRUE;
 	}
 	fclose(src_fp);
-	if (fclose(dest_fp) == EOF) {
+	if (safe_fclose(dest_fp) == EOF) {
 		FILE_OP_ERROR(dest, "fclose");
 		err = TRUE;
 	}
@@ -2619,7 +2621,7 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
 	if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
 		err = TRUE;
 
-	if (fclose(dest_fp) == EOF) {
+	if (safe_fclose(dest_fp) == EOF) {
 		FILE_OP_ERROR(dest, "fclose");
 		err = TRUE;
 	}
@@ -2737,7 +2739,7 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
 		err = TRUE;
 	}
 	fclose(src_fp);
-	if (fclose(dest_fp) == EOF) {
+	if (safe_fclose(dest_fp) == EOF) {
 		FILE_OP_ERROR(dest, "fclose");
 		err = TRUE;
 	}
@@ -2997,7 +2999,7 @@ gint str_write_to_file(const gchar *str, const gchar *file)
 		return -1;
 	}
 
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(file, "fclose");
 		claws_unlink(file);
 		return -1;
@@ -4580,7 +4582,7 @@ void mailcap_update_default(const gchar *type, const gchar *command)
 	if (fp)
 		fclose(fp);
 
-	if (fclose(outfp) == EOF)
+	if (safe_fclose(outfp) == EOF)
 		err = TRUE;
 		
 	if (!err)
diff --git a/src/compose.c b/src/compose.c
index 6694348..88ff254 100644
--- a/src/compose.c
+++ b/src/compose.c
@@ -110,6 +110,8 @@
 #include "autofaces.h"
 #include "spell_entry.h"
 #include "headers.h"
+#include "safe_fclose.h"
+
 #ifdef USE_LDAP
 #include "password.h"
 #include "ldapserver.h"
@@ -6003,7 +6005,7 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
 
 	g_free(chars);
 
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(file, "fclose");
 		claws_unlink(file);
 		return -1;
@@ -6296,7 +6298,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 		g_free(tmp);
 		return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
 	}
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(tmp, "fclose");
 		claws_unlink(tmp);
 		g_free(tmp);
@@ -10395,7 +10397,7 @@ gboolean compose_draft (gpointer data, guint action)
 		fclose(fp);
 		goto warn_err;
 	}
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		goto warn_err;
 	}
 	
diff --git a/src/etpan/imap-thread.c b/src/etpan/imap-thread.c
index 80279d2..29d0380 100644
--- a/src/etpan/imap-thread.c
+++ b/src/etpan/imap-thread.c
@@ -44,6 +44,7 @@
 #include "utils.h"
 #include "mainwindow.h"
 #include "proxy.h"
+#include "safe_fclose.h"
 #include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
@@ -2637,7 +2638,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
 			goto fclose;
 		}
 		
-		r = fclose(f);
+		r = safe_fclose(f);
 		if (r == EOF) {
 			result->error = MAILIMAP_ERROR_FETCH;
 			goto unlink;
diff --git a/src/exporthtml.c b/src/exporthtml.c
index 675576f..7ed6fc8 100644
--- a/src/exporthtml.c
+++ b/src/exporthtml.c
@@ -46,6 +46,7 @@
 #include "utils.h"
 #include "exporthtml.h"
 #include "xmlprops.h"
+#include "safe_fclose.h"
 
 #ifdef MKDIR_TAKES_ONE_ARG
 #undef mkdir
@@ -301,7 +302,7 @@ static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
 	fprintf( cssFile, ".tab-attr {\n" );
 	fprintf( cssFile, "}\n" );
 
-	fclose( cssFile );
+	safe_fclose( cssFile );
 	return MGU_SUCCESS;
 }
 
@@ -365,7 +366,7 @@ static gint exporthtml_create_css_full( const gchar *fileSpec ) {
 	fprintf( cssFile, ".tab-attr {\n" );
 	fprintf( cssFile, "}\n" );
 
-	fclose( cssFile );
+	safe_fclose( cssFile );
 	return MGU_SUCCESS;
 }
 
@@ -994,7 +995,7 @@ void exporthtml_process(
 	fprintf( htmlFile, "</body>\n" );
 	fprintf( htmlFile, "</html>\n" );
 
-	fclose( htmlFile );
+	safe_fclose( htmlFile );
 	ctl->retVal = MGU_SUCCESS;
 
 	/* Create stylesheet files */
diff --git a/src/exportldif.c b/src/exportldif.c
index d9c1b49..eba8737 100644
--- a/src/exportldif.c
+++ b/src/exportldif.c
@@ -38,6 +38,7 @@
 #include "exportldif.h"
 #include "xmlprops.h"
 #include "ldif.h"
+#include "safe_fclose.h"
 
 
 #ifdef MKDIR_TAKES_ONE_ARG
@@ -541,7 +542,7 @@ void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
 
 	rootFolder = cache->rootFolder;
 	exportldif_fmt_folder( ctl, ldifFile, rootFolder );
-	fclose( ldifFile );
+	safe_fclose( ldifFile );
 	ctl->retVal = MGU_SUCCESS;
 }
 
diff --git a/src/ldapquery.c b/src/ldapquery.c
index b132989..257ae19 100644
--- a/src/ldapquery.c
+++ b/src/ldapquery.c
@@ -38,6 +38,7 @@
 #include "ldapctrl.h"
 #include "ldapserver.h"
 #include "mgutils.h"
+#include "safe_fclose.h"
 
 #include "addritem.h"
 #include "addrcache.h"
@@ -426,7 +427,7 @@ static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *att
 				FILE *fp = g_fopen(file, "wb");
 				if (fp) {
 					fwrite(vals[0]->bv_val, 1, vals[0]->bv_len, fp);
-					fclose(fp);
+					safe_fclose(fp);
 				}
 				list = g_slist_append( list, file);
 			}
diff --git a/src/main.c b/src/main.c
index 398dadd..8a0ce23 100644
--- a/src/main.c
+++ b/src/main.c
@@ -132,6 +132,7 @@
 #include "advsearch.h"
 #include "avatars.h"
 #include "passwordstore.h"
+#include "safe_fclose.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
@@ -477,7 +478,7 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
 	g_free(new_plugin_path);
 	g_free(old_plugin_path);
 	fclose(oldfp);
-	if (fclose(newfp) == EOF)
+	if (safe_fclose(newfp) == EOF)
 		err = TRUE;
 	
 	return (err ? -1:0);
diff --git a/src/mbox.c b/src/mbox.c
index 9929a29..ea1138a 100644
--- a/src/mbox.c
+++ b/src/mbox.c
@@ -56,6 +56,7 @@
 #include "filtering.h"
 #include "alertpanel.h"
 #include "statusbar.h"
+#include "safe_fclose.h"
 
 #define MESSAGEBUFSIZE	8192
 
@@ -230,7 +231,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 			return -1;
 		}
 
-		if (fclose(tmp_fp) == EOF) {
+		if (safe_fclose(tmp_fp) == EOF) {
 			FILE_OP_ERROR(tmp_file, "fclose");
 			g_warning("can't write to temporary file");
 			fclose(mbox_fp);
@@ -333,7 +334,7 @@ gint lock_mbox(const gchar *base, LockType type)
 			return -1;
 		}
 
-		if (fclose(lockfp) == EOF) {
+		if (safe_fclose(lockfp) == EOF) {
 			FILE_OP_ERROR(lockfile, "fclose");
 			g_free(lockfile);
 			return -1;
@@ -509,7 +510,7 @@ gint copy_mbox(gint srcfd, const gchar *dest)
 		err = TRUE;
 	}
 
-	if (fclose(dest_fp) == EOF) {
+	if (safe_fclose(dest_fp) == EOF) {
 		FILE_OP_ERROR(dest, "fclose");
 		err = TRUE;
 	}
@@ -531,7 +532,7 @@ void empty_mbox(const gchar *mbox)
 		g_warning("can't truncate mailbox to zero.");
 		return;
 	}
-	fclose(fp);
+	safe_fclose(fp);
 }
 
 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
@@ -657,7 +658,7 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
 #ifdef HAVE_FGETS_UNLOCKED
 		funlockfile(msg_fp);
 #endif
-		fclose(msg_fp);
+		safe_fclose(msg_fp);
 		statusbar_progress_all(msgs++,total, 500);
 		if (msgs%500 == 0)
 			GTK_EVENTS_FLUSH();
@@ -670,7 +671,7 @@ out:
 #ifdef HAVE_FGETS_UNLOCKED
 	funlockfile(mbox_fp);
 #endif
-	fclose(mbox_fp);
+	safe_fclose(mbox_fp);
 
 	return err;
 }
diff --git a/src/messageview.c b/src/messageview.c
index b696691..0882cc4 100644
--- a/src/messageview.c
+++ b/src/messageview.c
@@ -67,6 +67,8 @@
 #include "statusbar.h"
 #include "folder_item_prefs.h"
 #include "avatars.h"
+#include "safe_fclose.h"
+
 #ifndef USE_ALT_ADDRBOOK
 	#include "addressbook.h"
 #else
@@ -1061,7 +1063,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
 	if (ok < 0)
 		goto FILE_ERROR;	
 
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(tmp, "fclose");
 		claws_unlink(tmp);
 		return -1;
diff --git a/src/mh.c b/src/mh.c
index 0bf0783..713ce0d 100644
--- a/src/mh.c
+++ b/src/mh.c
@@ -44,6 +44,7 @@
 #include "gtkutils.h"
 #include "timing.h"
 #include "msgcache.h"
+#include "safe_fclose.h"
 
 /* Define possible missing constants for Windows. */
 #ifdef G_OS_WIN32
@@ -1421,11 +1422,7 @@ static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
 			fclose(mh_sequences_old_fp);
 		}
 		
-		fflush(mh_sequences_new_fp);
-#if 0
-		fsync(fileno(mh_sequences_new_fp));
-#endif
-		if (fclose(mh_sequences_new_fp) == EOF)
+		if (safe_fclose(mh_sequences_new_fp) == EOF)
 			err = TRUE;
 
 		if (!err) {
diff --git a/src/msgcache.c b/src/msgcache.c
index edaa092..871c04a 100644
--- a/src/msgcache.c
+++ b/src/msgcache.c
@@ -47,6 +47,7 @@
 #include "timing.h"
 #include "tags.h"
 #include "prefs_common.h"
+#include "safe_fclose.h"
 
 #ifdef HAVE_FWRITE_UNLOCKED
 #define SC_FWRITE fwrite_unlocked
@@ -1219,29 +1220,14 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
 	if (write_fps.tags_fp)
 		funlockfile(write_fps.tags_fp);
 #endif
-	/* flush buffers */
-	if (write_fps.cache_fp)
-		write_fps.error |= (fflush(write_fps.cache_fp) != 0);
-	if (write_fps.mark_fp)
-		write_fps.error |= (fflush(write_fps.mark_fp) != 0);
-	if (write_fps.tags_fp)
-		write_fps.error |= (fflush(write_fps.tags_fp) != 0);
-
-	/* sync to filesystem */
-	if (prefs_common.flush_metadata && write_fps.cache_fp)
-		write_fps.error |= (fsync(fileno(write_fps.cache_fp)) != 0);
-	if (prefs_common.flush_metadata && write_fps.mark_fp)
-		write_fps.error |= (fsync(fileno(write_fps.mark_fp)) != 0);
-	if (prefs_common.flush_metadata && write_fps.tags_fp)
-		write_fps.error |= (fsync(fileno(write_fps.tags_fp)) != 0);
 
 	/* close files */
 	if (write_fps.cache_fp)
-		write_fps.error |= (fclose(write_fps.cache_fp) != 0);
+		write_fps.error |= (safe_fclose(write_fps.cache_fp) != 0);
 	if (write_fps.mark_fp)
-		write_fps.error |= (fclose(write_fps.mark_fp) != 0);
+		write_fps.error |= (safe_fclose(write_fps.mark_fp) != 0);
 	if (write_fps.tags_fp)
-		write_fps.error |= (fclose(write_fps.tags_fp) != 0);
+		write_fps.error |= (safe_fclose(write_fps.tags_fp) != 0);
 
 
 	if (write_fps.error != 0) {
diff --git a/src/news.c b/src/news.c
index 7d5fbfa..6c29cfb 100644
--- a/src/news.c
+++ b/src/news.c
@@ -62,6 +62,7 @@
 #  include "ssl.h"
 #endif
 #include "main.h"
+#include "safe_fclose.h"
 
 #define NNTP_PORT	119
 #ifdef USE_GNUTLS
@@ -702,7 +703,7 @@ GSList *news_get_group_list(Folder *folder)
 			}
 			newsnntp_list_free(grouplist);
 		}
-		if (fclose(fp) == EOF) {
+		if (safe_fclose(fp) == EOF) {
 			log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
 			session_destroy(SESSION(session));
 			REMOTE_FOLDER(folder)->session = NULL;
@@ -984,7 +985,7 @@ gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
 		return -1;
 	}
 
-	if (fclose(tmpfp) == EOF) {
+	if (safe_fclose(tmpfp) == EOF) {
 		FILE_OP_ERROR(tmp, "fclose");
 		claws_unlink(tmp);
 		g_free(tmp);
diff --git a/src/partial_download.c b/src/partial_download.c
index e0283cc..384393e 100644
--- a/src/partial_download.c
+++ b/src/partial_download.c
@@ -63,6 +63,7 @@
 #include "folder.h"
 #include "procheader.h"
 #include "msgcache.h"
+#include "safe_fclose.h"
 
 int partial_msg_in_uidl_list(MsgInfo *msginfo)
 {
@@ -248,7 +249,7 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 			g_free(stat);
 		}
 	}
-	if (fclose(fpnew) == EOF) {
+	if (safe_fclose(fpnew) == EOF) {
 		FILE_OP_ERROR(pathnew, "fclose");
 		fclose(fp);
 		g_free(path);
@@ -308,7 +309,7 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
 			goto bail;
 		}
 	}
-	if (fclose(fpnew) == EOF) {
+	if (safe_fclose(fpnew) == EOF) {
 		FILE_OP_ERROR(pathnew, "fclose");
 		fclose(fp);
 		g_free(pathnew);
diff --git a/src/plugins/acpi_notifier/acpi_notifier.c b/src/plugins/acpi_notifier/acpi_notifier.c
index 50430c6..ee59b2e 100644
--- a/src/plugins/acpi_notifier/acpi_notifier.c
+++ b/src/plugins/acpi_notifier/acpi_notifier.c
@@ -48,6 +48,7 @@
 #include "folder_item_prefs.h"
 #include "gtk/gtkutils.h"
 #include "gtk/combobox.h"
+#include "safe_fclose.h"
 
 #define PREFS_BLOCK_NAME "AcpiNotifier"
 #define PLUGIN_NAME _("Acpi Notifier")
@@ -659,7 +660,7 @@ static void acpi_set(gboolean on)
 		} else {
 			fwrite(acpiprefs.off_param, 1, strlen(acpiprefs.off_param), fp);
 		}
-		fclose(fp);
+		safe_fclose(fp);
 	} else {
 		gchar *cmd = g_strdup_printf("%s %s", 
 				acpiprefs.file_path,
diff --git a/src/plugins/bogofilter/bogofilter.c b/src/plugins/bogofilter/bogofilter.c
index a0823e9..446b652 100644
--- a/src/plugins/bogofilter/bogofilter.c
+++ b/src/plugins/bogofilter/bogofilter.c
@@ -52,6 +52,7 @@
 #include "prefs_common.h"
 #include "alertpanel.h"
 #include "addr_compl.h"
+#include "safe_fclose.h"
 
 #ifdef HAVE_SYSEXITS_H
 #include <sysexits.h>
@@ -279,7 +280,7 @@ static void bogofilter_do_filter(BogoFilterData *data)
 								}
 							}
 							fclose(input);
-							if (fclose(output) == EOF)
+							if (safe_fclose(output) == EOF)
 								err = TRUE;
 							if (!err)
 								move_file(tmpfile, file, TRUE);
diff --git a/src/plugins/fancy/claws.def b/src/plugins/fancy/claws.def
index d264aed..a7fa4db 100644
--- a/src/plugins/fancy/claws.def
+++ b/src/plugins/fancy/claws.def
@@ -85,6 +85,7 @@ procmsg_msginfo_unset_flags
 procmsg_register_spam_learner
 procmsg_spam_set_folder
 procmsg_unregister_spam_learner
+safe_fclose
 settings
 start_address_completion
 statusbar_pop_all
diff --git a/src/plugins/fancy/fancy_viewer.c b/src/plugins/fancy/fancy_viewer.c
index 7f5e0f6..1823fd4 100644
--- a/src/plugins/fancy/fancy_viewer.c
+++ b/src/plugins/fancy/fancy_viewer.c
@@ -28,6 +28,7 @@
 #include <fancy_viewer.h>
 #include <fancy_prefs.h>
 #include <alertpanel.h>
+#include <safe_fclose.h>
 
 #include <printing.h>
 #include <webkit/webkithittestresult.h>
@@ -670,7 +671,7 @@ static void *download_file_curl (void *data)
 		if (CURLE_OK != res)
 			alertpanel_error(_("An error occurred: %d\n"), res);
 		if (viewer->stream)
-			fclose(viewer->stream);
+			safe_fclose(viewer->stream);
 		curl_global_cleanup();
 	}
 #ifdef USE_PTHREAD
diff --git a/src/plugins/libravatar/claws.def b/src/plugins/libravatar/claws.def
index faf52cc..924b5d1 100644
--- a/src/plugins/libravatar/claws.def
+++ b/src/plugins/libravatar/claws.def
@@ -36,5 +36,6 @@ prefs_write_param
 prefs_common_get_prefs
 procmsg_msginfo_add_avatar
 procmsg_msginfo_get_avatar
+safe_fclose
 slist_free_strings_full
 to_human_readable
diff --git a/src/plugins/libravatar/libravatar_image.c b/src/plugins/libravatar/libravatar_image.c
index 02c7e4b..66a0e1d 100644
--- a/src/plugins/libravatar/libravatar_image.c
+++ b/src/plugins/libravatar/libravatar_image.c
@@ -27,6 +27,7 @@
 
 #include <common/claws.h>
 #include <prefs_common.h>
+#include <safe_fclose.h>
 
 #include "libravatar.h"
 #include "libravatar_prefs.h"
@@ -106,7 +107,7 @@ static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gcha
 	debug_print("retrieving URL to file: %s -> %s\n", url, filename);
 	curl_easy_perform(curl);
 	filesize = ftell(file);
-	fclose(file);
+	safe_fclose(file);
 	if (filesize < MIN_PNG_SIZE)
 		debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
 	else
diff --git a/src/plugins/libravatar/libravatar_missing.c b/src/plugins/libravatar/libravatar_missing.c
index 5ea99a7..6a01911 100644
--- a/src/plugins/libravatar/libravatar_missing.c
+++ b/src/plugins/libravatar/libravatar_missing.c
@@ -21,6 +21,7 @@
 #include "libravatar_missing.h"
 #include "libravatar_prefs.h"
 #include "utils.h"
+#include "safe_fclose.h"
 
 /**
  * Loads the hash table of md5sum → time from the given filename.
@@ -108,7 +109,7 @@ gint missing_save_to_file(GHashTable *table, const gchar *filename)
 	g_hash_table_foreach(table, missing_save_item, (gpointer)file);
 	debug_print("Saved %u missing avatar entries\n", g_hash_table_size(table));
 
-	if (fclose(file) != 0) {
+	if (safe_fclose(file) != 0) {
 		g_warning("error closing '%s'", filename);
 		return -1;
 	}
diff --git a/src/plugins/mailmbox/claws.def b/src/plugins/mailmbox/claws.def
index 5d8ff75..0f686d3 100644
--- a/src/plugins/mailmbox/claws.def
+++ b/src/plugins/mailmbox/claws.def
@@ -28,3 +28,4 @@ prefs_set_default
 prefs_write_open
 prefs_write_param
 prefs_common_get_prefs
+safe_fclose
\ No newline at end of file
diff --git a/src/plugins/mailmbox/mailmbox_folder.c b/src/plugins/mailmbox/mailmbox_folder.c
index 8b601fc..1db007b 100644
--- a/src/plugins/mailmbox/mailmbox_folder.c
+++ b/src/plugins/mailmbox/mailmbox_folder.c
@@ -56,6 +56,7 @@
 #include "mailmbox.h"
 #include "mailmbox_folder.h"
 #include "mailmbox_parse.h"
+#include "safe_fclose.h"
 
 #define MAILMBOX_CACHE_DIR           "mailmboxcache"
 
@@ -257,7 +258,7 @@ static void write_max_uid_value(FolderItem *item, guint max_uid)
                 return;
         }
         
-        fclose(f);
+        safe_fclose(f);
 }
 
 static void claws_mailmbox_folder_item_destroy(Folder *folder, FolderItem *_item)
@@ -471,7 +472,7 @@ static gchar *s_claws_mailmbox_fetch_msg(Folder *folder, FolderItem *item, gint
         if (r == 0)
                 goto close;
         
-        fclose(f);
+        safe_fclose(f);
         
 	return file;
         
diff --git a/src/plugins/pgpinline/claws.def b/src/plugins/pgpinline/claws.def
index c9f0ecd..a07c65f 100644
--- a/src/plugins/pgpinline/claws.def
+++ b/src/plugins/pgpinline/claws.def
@@ -22,3 +22,4 @@ procmime_scan_file
 procmime_write_mimeinfo
 procmime_get_part
 procmime_get_tmp_file_name
+safe_fclose
\ No newline at end of file
diff --git a/src/plugins/pgpinline/pgpinline.c b/src/plugins/pgpinline/pgpinline.c
index 6ebb883..bd2f026 100644
--- a/src/plugins/pgpinline/pgpinline.c
+++ b/src/plugins/pgpinline/pgpinline.c
@@ -28,6 +28,7 @@
 #include <glib/gi18n.h>
 #include <errno.h>
 #include <gpgme.h>
+#include <safe_fclose.h>
 
 #include "utils.h"
 #include "privacy.h"
@@ -410,7 +411,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
 	    }
 	}
 
-	if (fclose(dstfp) == EOF) {
+	if (safe_fclose(dstfp) == EOF) {
         	FILE_OP_ERROR(fname, "fclose");
 		privacy_set_error(_("Couldn't close decrypted file %s"), fname);
         	g_free(fname);
diff --git a/src/plugins/pgpmime/claws.def b/src/plugins/pgpmime/claws.def
index fe03c78..0d01d49 100644
--- a/src/plugins/pgpmime/claws.def
+++ b/src/plugins/pgpmime/claws.def
@@ -18,3 +18,4 @@ procmime_mimeinfo_new
 procmime_mimeinfo_parent
 procmime_scan_file
 procmime_write_mimeinfo
+safe_fclose
\ No newline at end of file
diff --git a/src/plugins/pgpmime/pgpmime.c b/src/plugins/pgpmime/pgpmime.c
index 8dc761d..ff01c93 100644
--- a/src/plugins/pgpmime/pgpmime.c
+++ b/src/plugins/pgpmime/pgpmime.c
@@ -42,6 +42,7 @@
 #include <plugins/pgpcore/pgp_utils.h>
 
 #include "prefs_common.h"
+#include "safe_fclose.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -384,7 +385,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 	}
 	g_free(chars);
 
-	if (fclose(dstfp) == EOF) {
+	if (safe_fclose(dstfp) == EOF) {
         	FILE_OP_ERROR(fname, "fclose");
 		privacy_set_error(_("Couldn't close decrypted file %s"), fname);
         	g_free(fname);
diff --git a/src/plugins/rssyl/claws.def b/src/plugins/rssyl/claws.def
index 5f257e5..6fc560f 100644
--- a/src/plugins/rssyl/claws.def
+++ b/src/plugins/rssyl/claws.def
@@ -116,6 +116,7 @@ procheader_parse_file
 procmsg_get_message_file
 procmsg_msginfo_unset_flags
 remove_dir_recursive
+safe_fclose
 slist_free_strings_full
 strtailchomp
 subst_for_shellsafe_filename
diff --git a/src/plugins/rssyl/opml_export.c b/src/plugins/rssyl/opml_export.c
index dfc7c90..884f72c 100644
--- a/src/plugins/rssyl/opml_export.c
+++ b/src/plugins/rssyl/opml_export.c
@@ -32,6 +32,7 @@
 #include <log.h>
 #include <folder.h>
 #include <common/utils.h>
+#include <safe_fclose.h>
 
 /* Local includes */
 #include "libfeed/date.h"
@@ -185,7 +186,7 @@ void rssyl_opml_export(void)
 
 	debug_print("RSSyl: Feed export finished.\n");
 
-	fclose(f);
+	safe_fclose(f);
 	g_free(opmlfile);
 	g_free(ctx);
 }
diff --git a/src/plugins/rssyl/rssyl_add_item.c b/src/plugins/rssyl/rssyl_add_item.c
index 685d75b..048eb04 100644
--- a/src/plugins/rssyl/rssyl_add_item.c
+++ b/src/plugins/rssyl/rssyl_add_item.c
@@ -34,6 +34,7 @@
 #include <codeconv.h>
 #include <procmsg.h>
 #include <common/utils.h>
+#include <safe_fclose.h>
 
 /* Local includes */
 #include "libfeed/date.h"
@@ -546,7 +547,7 @@ void rssyl_add_item(RFolderItem *ritem, FeedItem *feed_item)
 				feed_item_enclosure_get_size(enc) );
 
 	fprintf(f, "</body></html>\n");
-	fclose(f);
+	safe_fclose(f);
 
 	g_return_if_fail(template != NULL);
 
diff --git a/src/plugins/rssyl/rssyl_deleted.c b/src/plugins/rssyl/rssyl_deleted.c
index 497ed34..f6e6a76 100644
--- a/src/plugins/rssyl/rssyl_deleted.c
+++ b/src/plugins/rssyl/rssyl_deleted.c
@@ -31,6 +31,7 @@
 /* Claws Mail includes */
 #include <codeconv.h>
 #include <common/utils.h>
+#include <safe_fclose.h>
 
 /* Local includes */
 #include "rssyl.h"
@@ -188,7 +189,7 @@ static void rssyl_deleted_store_internal(GSList *deleted_items, const gchar *del
 	g_slist_foreach(deleted_items, (GFunc)_store_one_deleted_item,
 			(gpointer)f);
 
-	fclose(f);
+	safe_fclose(f);
 	debug_print("RSSyl: written and closed deletion file\n");
 }
 
diff --git a/src/plugins/smime/claws.def b/src/plugins/smime/claws.def
index eec9eb5..09f67d1 100644
--- a/src/plugins/smime/claws.def
+++ b/src/plugins/smime/claws.def
@@ -26,4 +26,5 @@ procmime_mimeinfo_parent
 procmime_scan_file
 procmime_write_mime_header
 procmime_write_mimeinfo
+safe_fclose
 str_write_to_file
diff --git a/src/plugins/smime/smime.c b/src/plugins/smime/smime.c
index 93268a3..08d8dc2 100644
--- a/src/plugins/smime/smime.c
+++ b/src/plugins/smime/smime.c
@@ -43,6 +43,7 @@
 #include "prefs_common.h"
 #include "procmime.h"
 #include "plugin.h"
+#include "safe_fclose.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -480,7 +481,7 @@ static MimeInfo *smime_decrypt(MimeInfo *mimeinfo)
 			return NULL;
 		}
 	}
-	if (fclose(dstfp) == EOF) {
+	if (safe_fclose(dstfp) == EOF) {
         	FILE_OP_ERROR(fname, "fclose");
         	g_free(fname);
        		g_free(chars);
@@ -804,7 +805,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 	procmime_decode_content(msgcontent);
 	procmime_write_mime_header(msgcontent, fp);
 	procmime_write_mimeinfo(msgcontent, fp);
-	fclose(fp);
+	safe_fclose(fp);
 	canonicalize_file_replace(tmpfile);
 	fp = g_fopen(tmpfile, "rb");
 	if (fp == NULL) {
@@ -846,7 +847,7 @@ gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 			g_free(enccontent);
 			return FALSE;
 		}
-		if (fclose(fp) == EOF) {
+		if (safe_fclose(fp) == EOF) {
 			FILE_OP_ERROR(tmpfile, "fclose");
 			claws_unlink(tmpfile);
 			g_free(tmpfile);
diff --git a/src/plugins/vcalendar/claws.def b/src/plugins/vcalendar/claws.def
index a0add79..f6d4208 100644
--- a/src/plugins/vcalendar/claws.def
+++ b/src/plugins/vcalendar/claws.def
@@ -144,6 +144,7 @@ procmsg_msginfo_get_full_info
 procmsg_send_message_queue_with_lock
 qp_encode_line
 remove_dir_recursive
+safe_fclose
 slist_free_strings
 slist_free_strings_full
 statusbar_progress_all
diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c
index 299e033..68d2069 100644
--- a/src/plugins/vcalendar/vcal_meeting_gtk.c
+++ b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -54,6 +54,7 @@
 #include "gtkutils.h"
 #include "log.h"
 #include "utils.h"
+#include "safe_fclose.h"
 
 struct _VCalMeeting
 {
@@ -1872,7 +1873,7 @@ void multisync_export(void)
 				FILE_OP_ERROR(file, "fprintf");
 			g_free(file);
 		}
-		if (fclose(fp) == EOF)
+		if (safe_fclose(fp) == EOF)
 			FILE_OP_ERROR(file, "fclose");
 	} else {
 		FILE_OP_ERROR(file, "fopen");
diff --git a/src/pop.c b/src/pop.c
index e8831a0..f89ddc3 100644
--- a/src/pop.c
+++ b/src/pop.c
@@ -40,6 +40,7 @@
 #include "partial_download.h"
 #include "log.h"
 #include "hooks.h"
+#include "safe_fclose.h"
 
 static gint pop3_greeting_recv		(Pop3Session *session,
 					 const gchar *msg);
@@ -705,7 +706,7 @@ gint pop3_write_uidl_list(Pop3Session *session)
 			    > 0);
 	}
 
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(tmp_path, "fclose");
 		fp = NULL;
 		goto err_write;
@@ -806,7 +807,7 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
 		}
 	}
 
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(file, "fclose");
 		claws_unlink(file);
 		return -1;
diff --git a/src/prefs_common.c b/src/prefs_common.c
index 3c74b05..e1b9fde 100644
--- a/src/prefs_common.c
+++ b/src/prefs_common.c
@@ -61,6 +61,8 @@
 #include "prefswindow.h"
 #include "colorlabel.h"
 #include "passwordstore.h"
+#include "safe_fclose.h"
+
 #ifndef USE_ALT_ADDRBOOK
 	#include "addrcustomattr.h"
 #endif
@@ -1399,7 +1401,7 @@ static void prefs_common_save_history_to_dir(const gchar *dirname, const gchar *
 		    fputc('\n', fp) != EOF);
 	}
 
-	if (fclose(fp) == EOF) {
+	if (safe_fclose(fp) == EOF) {
 		FILE_OP_ERROR(tmp_path, "fclose");
 		fp = NULL;
 		goto out;
@@ -1415,7 +1417,7 @@ static void prefs_common_save_history_to_dir(const gchar *dirname, const gchar *
 
 out:
 	if (fp)
-		fclose(fp);
+		safe_fclose(fp);
 	g_free(tmp_path);
 	g_free(path);
 }
diff --git a/src/procmsg.c b/src/procmsg.c
index df05e9b..5c776b4 100644
--- a/src/procmsg.c
+++ b/src/procmsg.c
@@ -49,6 +49,7 @@
 #include "timing.h"
 #include "inc.h"
 #include "privacy.h"
+#include "safe_fclose.h"
 
 extern SessionStats session_stats;
 
@@ -1120,7 +1121,7 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
 			return -1;
 		}
 	}
-	fclose(outfp);
+	safe_fclose(outfp);
 	fclose(fp);
 	return 0;
 }
@@ -1705,7 +1706,7 @@ send_mail:
 					}
 				}
 			}
-			fclose(tmpfp);
+			safe_fclose(tmpfp);
 
 			if (newsval == 0) {
 				debug_print("Sending message by news\n");
@@ -2339,14 +2340,14 @@ MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimei
 	}
 	
 	if (fp && procmime_write_mimeinfo(mimeinfo, fp) >= 0) {
-		fclose(fp);
+		safe_fclose(fp);
 		fp = NULL;
 		tmp_msginfo = procheader_parse_file(
 			tmpfile, flags, 
 			TRUE, FALSE);
 	}
 	if (fp)
-		fclose(fp);
+		safe_fclose(fp);
 
 	if (tmp_msginfo != NULL) {
 		if (src_msginfo)
diff --git a/src/safe_fclose.c b/src/safe_fclose.c
new file mode 100644
index 0000000..bec77fd
--- /dev/null
+++ b/src/safe_fclose.c
@@ -0,0 +1,46 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "prefs_common.h"
+#include "common/timing.h"
+
+int safe_fclose(FILE *fp)
+{
+	int r;
+	START_TIMING("");
+
+	if (fflush(fp) != 0) {
+		return EOF;
+	}
+	if (prefs_common_get_prefs()->flush_metadata && fsync(fileno(fp)) != 0) {
+		return EOF;
+	}
+
+	r = fclose(fp);
+	END_TIMING();
+
+	return r;
+}
diff --git a/src/safe_fclose.h b/src/safe_fclose.h
new file mode 100644
index 0000000..f0153a5
--- /dev/null
+++ b/src/safe_fclose.h
@@ -0,0 +1,24 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __SAFE_FCLOSE_H__
+#define __SAFE_FCLOSE_H__
+
+int safe_fclose(FILE *fp);
+
+#endif

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


hooks/post-receive
-- 
Claws Mail


More information about the Commits mailing list