diff --git a/DataStr/List.cpp b/DataStr/List.cpp
index 884a9554269ffe787541bf2758561b18f41a8c7a..368908319982a15118fec476c7fb84dba0a1bcf9 100644
--- a/DataStr/List.cpp
+++ b/DataStr/List.cpp
@@ -1,4 +1,4 @@
-// $Id: List.cpp,v 1.23 2003-01-23 20:19:17 geuzaine Exp $
+// $Id: List.cpp,v 1.24 2003-02-10 18:57:02 geuzaine Exp $
 //
 // Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle
 //
@@ -27,6 +27,7 @@
 #include "Malloc.h"
 #include "List.h"
 #include "Message.h"
+#include "SafeIO.h"
 
 static char *startptr;
 
@@ -450,7 +451,7 @@ void List_WriteToFile(List_T *liste, FILE *file, int format){
       Msg(GERROR, "Bad type of data to write list to file (size = %d)", liste->size);
     break;
   case LIST_FORMAT_BINARY :
-    fwrite(liste->array, liste->size, n, file);
+    safe_fwrite(liste->array, liste->size, n, file);
     break;
   default :
     Msg(GERROR, "Unknown list format");
diff --git a/DataStr/Makefile b/DataStr/Makefile
index 7f399fa3671f408b50a19fff580048e5e9505507..9c948f17e71bb5188437e979c57549e9a387f1a8 100644
--- a/DataStr/Makefile
+++ b/DataStr/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.16 2002-05-18 16:31:16 geuzaine Exp $
+# $Id: Makefile,v 1.17 2003-02-10 18:57:02 geuzaine Exp $
 #
 # Makefile for "libGmshDataStr.a"
 #
@@ -20,6 +20,7 @@ CFLAGS  = $(OPT_FLAGS) $(OS_FLAGS) $(VERSION_FLAGS) $(INCLUDE)
 
 SRC = List.cpp \
       Malloc.cpp \
+      SafeIO.cpp \
       Tree.cpp \
       avl.cpp \
       Tools.cpp
@@ -47,8 +48,9 @@ depend:
 	$(RM) Makefile.new
 
 # DO NOT DELETE THIS LINE
-List.o: List.cpp Malloc.h List.h ../Common/Message.h
+List.o: List.cpp Malloc.h List.h SafeIO.h ../Common/Message.h
 Malloc.o: Malloc.cpp Malloc.h ../Common/Message.h
+SafeIO.o: SafeIO.cpp SafeIO.h ../Common/Message.h
 Tree.o: Tree.cpp Malloc.h Tree.h avl.h ../Common/Message.h
 avl.o: avl.cpp avl.h Malloc.h
 Tools.o: Tools.cpp Tools.h List.h Tree.h avl.h
diff --git a/DataStr/SafeIO.cpp b/DataStr/SafeIO.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f49a3cdb3b42e8f5991b3945b69532631baa8e70
--- /dev/null
+++ b/DataStr/SafeIO.cpp
@@ -0,0 +1,81 @@
+// $Id: SafeIO.cpp,v 1.1 2003-02-10 18:57:02 geuzaine Exp $
+//
+// Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+// 
+// Please report all bugs and problems to "gmsh@geuz.org".
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "SafeIO.h"
+#include "Message.h"
+
+/*
+  Safe fprintf routine
+
+  In a perfect world, one should use this routine. Unfortunately, it
+  is very, very slow.
+
+  We should use a macro, i.e.
+
+    #define gprintf(...) { fprintf(__VA_ARGS__); ... }
+
+  but but var args don't work with all C preprocessors.
+
+  Bottom line: don't use safe_fprintf.
+*/
+
+int safe_fprintf(FILE *stream, char *fmt, ...){
+  va_list  args;
+
+  va_start (args, fmt);
+  vfprintf(stream, fmt, args); 
+  va_end (args);
+
+  if(ferror(stream)){
+    Msg(GERROR, strerror(errno));
+    clearerr(stream);
+    return 1;
+  }
+    
+  return 0;
+}
+
+/* Safe fwrite routine */
+
+int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
+  size_t result = fwrite(ptr, size, nmemb, stream);
+
+  if(result < nmemb){
+    if(result >= 0) /* Partial write */
+      Msg(GERROR, "Disk full");
+    else
+      Msg(GERROR, strerror(errno));
+    Msg(GERROR, "Truncating output file");
+    if(fflush(stream) < 0)
+      Msg(GERROR, "EOF reached");
+    if(ftruncate(fileno(stream), 0) < 0)
+      Msg(GERROR, strerror(errno));
+    if(fclose(stream) < 0)
+      Msg(GERROR, strerror(errno));
+    return 1;
+  }
+  return 0;
+}
diff --git a/DataStr/SafeIO.h b/DataStr/SafeIO.h
new file mode 100644
index 0000000000000000000000000000000000000000..278126f0097f7a96a619c7e4f871e6766e112d54
--- /dev/null
+++ b/DataStr/SafeIO.h
@@ -0,0 +1,28 @@
+#ifndef _SAFE_IO_H_
+
+// Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+// 
+// Please report all bugs and problems to "gmsh@geuz.org".
+
+#include <stdarg.h>
+#include <stdio.h>
+
+int safe_fprintf(FILE *stream, char *fmt, ...);
+int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
+#endif