}
 }
 
-/********************************************************************/
-
-/* malloc with error checking.  */
-
-void *safe_malloc(size_t n) {
-  void *p = malloc(n);
-  if(!p && n != 0) {
+FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
+  FILE *result = fopen(path, mode);
+  if(result) {
+    return result;
+  } else {
     fprintf(stderr,
-            "mymail: cannot allocate memory: %s\n", strerror(errno));
+            "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
+            path, comment, mode);
     exit(EXIT_FAILURE);
   }
-  return p;
 }
 
 /*********************************************************************/
   nb_body_hits = 0;
 
   header = 1;
-  mail_file = fopen(mail_filename, "r");
-
-  if(!mail_file) {
-    fprintf(stderr,
-            "mymail: Cannot open mbox '%s' for body scan.\n",
-            mail_filename);
-    exit(EXIT_FAILURE);
-  }
+  mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
 
   fseek(mail_file, position_in_mail, SEEK_SET);
 
   char raw_mbox_line[BUFFER_SIZE];
   FILE *mail_file;
 
-  mail_file = fopen(mail_filename, "r");
-
-  if(!mail_file) {
-    fprintf(stderr,
-            "mymail: Cannot open mbox '%s' for mail extraction.\n",
-            mail_filename);
-    exit(EXIT_FAILURE);
-  }
-
+  mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
   fseek(mail_file, position_in_mail, SEEK_SET);
 
   if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
     fflush(stdout);
   }
 
-  db_file = fopen(db_filename, "r");
-
-  if(!db_file) {
-    fprintf(stderr,
-            "mymail: Cannot open \"%s\" for reading: %s\n",
-            db_filename,
-            strerror(errno));
-    exit(EXIT_FAILURE);
-  }
+  db_file = safe_fopen(db_filename, "r", "index file for search");
 
   /* First, check the db file leading line integrity */
 
   int in_header, new_header;
   unsigned long int position_in_file;
 
-  file = fopen(mbox_filename, "r");
-
-  if(!file) {
-    fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
-    if(paranoid) { exit(EXIT_FAILURE); }
-    return;
-  }
+  file = safe_fopen(mbox_filename, "r", "mbox for indexing");
 
   in_header = 0;
   new_header = 0;
       mbox_filename_regexp = 0;
     }
 
-    db_file = fopen(db_filename, "w");
-
-    if(!db_file) {
-      fprintf(stderr,
-              "mymail: Cannot open \"%s\" for writing: %s\n",
-              db_filename,
-              strerror(errno));
-      exit(EXIT_FAILURE);
-    }
+    db_file = safe_fopen(db_filename, "w", "index file for indexing");
 
     for(f = 0; f < nb_fields_to_parse; f++) {
       if(regcomp(&fields_to_parse[f].regexp,
     int nb_extracted_mails = 0;
 
     if(output_filename[0]) {
-      output_file = fopen(output_filename, "w");
-
-      if(!output_file) {
-        fprintf(stderr,
-                "mymail: Cannot open result file \"%s\" for writing: %s\n",
-                output_filename,
-                strerror(errno));
-        exit(EXIT_FAILURE);
-      }
+      output_file = safe_fopen(output_filename, "w", "result mbox");
     } else {
       output_file = stdout;
       quiet = 1;