Added the computation of the number of extracted mails.
authorFrancois Fleuret <francois@fleuret.org>
Sat, 9 Feb 2013 07:51:41 +0000 (08:51 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Sat, 9 Feb 2013 07:51:41 +0000 (08:51 +0100)
mymail.c

index 80b70d3..f9b0148 100644 (file)
--- a/mymail.c
+++ b/mymail.c
@@ -25,7 +25,7 @@
   directories containing mbox files, and create a db file containing
   for each mail a list of fields computed from the header, or (2)
   read such a db file and get all the mails matching regexp-defined
-  conditions on the fields.
+  conditions on the fields, to create a resulting mbox file.
 
   It is low-tech, simple, light and fast.
 
@@ -377,10 +377,10 @@ void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
   fclose(mail_file);
 }
 
-void search_in_db(const char *db_filename,
-                  int nb_search_conditions,
-                  struct search_condition *search_conditions,
-                  FILE *output_file) {
+int search_in_db(const char *db_filename,
+                 int nb_search_conditions,
+                 struct search_condition *search_conditions,
+                 FILE *output_file) {
 
   int hits[MAX_NB_SEARCH_CONDITIONS];
   char raw_db_line[BUFFER_SIZE];
@@ -391,6 +391,9 @@ void search_in_db(const char *db_filename,
   int already_written, m, n;
   int nb_body_conditions, nb_fulfilled_body_conditions;
   FILE *db_file;
+  int nb_extracted_mails;
+
+  nb_extracted_mails = 0;
 
   if(!quiet) {
     printf("Searching in '%s' ... ", db_filename);
@@ -474,6 +477,7 @@ void search_in_db(const char *db_filename,
           }
 
           if(nb_body_conditions == nb_fulfilled_body_conditions) {
+            nb_extracted_mails++;
             extract_mail(current_mail_filename, current_position_in_mail, output_file);
           }
         }
@@ -507,16 +511,19 @@ void search_in_db(const char *db_filename,
     printf("done.\n");
     fflush(stdout);
   }
+
+  return nb_extracted_mails;
 }
 
-void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
-                            int nb_search_conditions,
-                            struct search_condition *search_conditions,
-                            FILE *output_file) {
+int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
+                           int nb_search_conditions,
+                           struct search_condition *search_conditions,
+                           FILE *output_file) {
   DIR *dir;
   struct dirent *dir_e;
   struct stat sb;
   char subname[PATH_MAX + 1];
+  int nb_extracted_mails = 0;
 
   if(lstat(entry_name, &sb) != 0) {
     fprintf(stderr,
@@ -532,9 +539,9 @@ void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
     while((dir_e = readdir(dir))) {
       if(!ignore_entry(dir_e->d_name)) {
         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
-        recursive_search_in_db(subname, db_filename_regexp,
-                               nb_search_conditions, search_conditions,
-                               output_file);
+        nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
+                                                     nb_search_conditions, search_conditions,
+                                                     output_file);
       }
     }
     closedir(dir);
@@ -545,9 +552,12 @@ void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
     while(*s) { if(*s == '/') { filename = s+1; } s++; }
 
     if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
-      search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
+      nb_extracted_mails +=
+        search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
     }
   }
+
+  return nb_extracted_mails;
 }
 
 /*********************************************************************/
@@ -1060,6 +1070,7 @@ int main(int argc, char **argv) {
   }
 
   else {
+    int nb_extracted_mails = 0;
 
     if(nb_search_conditions > 0) {
 
@@ -1076,9 +1087,9 @@ int main(int argc, char **argv) {
           exit(EXIT_FAILURE);
         }
 
-        recursive_search_in_db(db_root_path, &db_filename_regexp,
-                               nb_search_conditions, search_conditions,
-                               output_file);
+        nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
+                                                     nb_search_conditions, search_conditions,
+                                                     output_file);
 
         regfree(&db_filename_regexp);
       }
@@ -1095,7 +1106,8 @@ int main(int argc, char **argv) {
           s = parse_token(db_filename, PATH_MAX + 1, ';', s);
 
           if(db_filename[0]) {
-            search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
+            nb_extracted_mails +=
+              search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
           }
         }
       }
@@ -1103,10 +1115,20 @@ int main(int argc, char **argv) {
       /* Search in all db files listed in the command arguments */
 
       while(optind < argc) {
-        search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
+        nb_extracted_mails +=
+          search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
         optind++;
       }
     }
+
+    if(!quiet) {
+      if(nb_extracted_mails > 0) {
+        printf("Found %d matching mails.\n", nb_extracted_mails);
+      } else {
+        printf("No matching mail found.\n");
+      }
+    }
+
   }
 
   for(n = 0; n < nb_search_conditions; n++) {