From 1ed39d135312a0c698adf82d81a1723a9141bca3 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Sat, 8 Jun 2013 22:29:47 +0200 Subject: [PATCH] Added the -n option to set the maximum number of mails to extract. --- mymail.1 | 5 +++- mymail.c | 80 ++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/mymail.1 b/mymail.1 index 217a817..7e48dcc 100644 --- a/mymail.1 +++ b/mymail.1 @@ -1,4 +1,4 @@ -.TH "MYMAIL" "0.9.8" "April 2013" "Francois Fleuret" "User Commands" +.TH "MYMAIL" "0.9.9" "June 2013" "Francois Fleuret" "User Commands" \" This man page was written by Francois Fleuret \" and is distributed under a Creative Commons Attribution-Share Alike @@ -61,6 +61,9 @@ directories following the options on the command lines \fB-o \fR, \fB--output \fR set the result mbox filename. If it is not set, the standard output is used .TP +\fB-n \fR, \fB--nb-mails-max \fR set the +maximum number of mails to extract (default is 250) +.TP \fB-a \fR, \fB--default-search \fR set the default search key. If a search request is not understood, it is interpreted as the regexp with this default search key diff --git a/mymail.c b/mymail.c index 80864d2..c1b09be 100644 --- a/mymail.c +++ b/mymail.c @@ -46,7 +46,7 @@ #include #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file" -#define MYMAIL_VERSION "0.9.8" +#define MYMAIL_VERSION "0.9.9" #define MYMAIL_DB_FORMAT_VERSION 1 @@ -68,6 +68,7 @@ struct alias_node { int global_quiet; int global_use_leading_time; +int global_nb_mails_max; regex_t global_leading_from_line_regexp; struct alias_node *global_alias_list; @@ -254,6 +255,8 @@ void print_usage(FILE *out) { fprintf(out, " index mails\n"); fprintf(out, " -o , --output \n"); fprintf(out, " set the result file, use stdout if unset\n"); + fprintf(out, " -n , --nb-mails-max \n"); + fprintf(out, " set the maximum number of mails to extract\n"); fprintf(out, " -a , --default-search \n"); fprintf(out, " set the default search field\n"); } @@ -437,6 +440,7 @@ void update_time(int db_key, const char *db_value, time_t *t) { } int search_in_db(const char *db_filename, + int nb_extracted_mails, int nb_search_conditions, struct search_condition *search_conditions, FILE *output_file) { @@ -451,13 +455,10 @@ int search_in_db(const char *db_filename, int db_key; int hits[MAX_NB_SEARCH_CONDITIONS]; int nb_body_conditions, need_time; - int nb_extracted_mails; time_t mail_time; int m, n; - nb_extracted_mails = 0; - if(!global_quiet) { printf("Searching in '%s' ... ", db_filename); fflush(stdout); @@ -502,7 +503,8 @@ int search_in_db(const char *db_filename, strcpy(current_mail_filename, ""); - while(fgets(raw_db_line, BUFFER_SIZE, db_file)) { + while(nb_extracted_mails < global_nb_mails_max && + fgets(raw_db_line, BUFFER_SIZE, db_file)) { db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line); if(strcmp("mail", db_key_string) == 0) { @@ -541,14 +543,14 @@ int search_in_db(const char *db_filename, } } - if(current_mail_filename[0]) { - if(check_full_mail_match(current_mail_filename, - mail_time, - nb_search_conditions, search_conditions, - nb_body_conditions, hits, current_position_in_mail)) { - extract_mail(current_mail_filename, current_position_in_mail, output_file); - nb_extracted_mails++; - } + if(nb_extracted_mails < global_nb_mails_max && + current_mail_filename[0] && + check_full_mail_match(current_mail_filename, + mail_time, + nb_search_conditions, search_conditions, + nb_body_conditions, hits, current_position_in_mail)) { + extract_mail(current_mail_filename, current_position_in_mail, output_file); + nb_extracted_mails++; } fclose(db_file); @@ -562,6 +564,7 @@ int search_in_db(const char *db_filename, } int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp, + int nb_extracted_mails, int nb_search_conditions, struct search_condition *search_conditions, FILE *output_file) { @@ -569,7 +572,6 @@ int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp, 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, @@ -584,12 +586,14 @@ int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp, dir = opendir(entry_name); if(dir) { - while((dir_e = readdir(dir))) { + while((dir_e = readdir(dir)) && + nb_extracted_mails < global_nb_mails_max) { if(!ignore_entry(dir_e->d_name)) { snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name); - nb_extracted_mails += 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_extracted_mails, + nb_search_conditions, search_conditions, + output_file); } } closedir(dir); @@ -600,8 +604,10 @@ int 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) { - nb_extracted_mails += - search_in_db(entry_name, nb_search_conditions, search_conditions, output_file); + nb_extracted_mails = + search_in_db(entry_name, + nb_extracted_mails, + nb_search_conditions, search_conditions, output_file); } } @@ -769,6 +775,7 @@ static struct option long_options[] = { { "index", 0, 0, 'i' }, { "output", 1, 0, 'o' }, { "default-search", 1, 0, 'a' }, + { "nb-mails-max", 1, 0, 'n' }, { 0, 0, 0, 0 } }; @@ -1023,6 +1030,8 @@ int main(int argc, char **argv) { global_alias_list = 0; global_quiet = 0; global_use_leading_time = 0; + global_nb_mails_max = 250; + default_search_field = 0; strncpy(output_filename, "", PATH_MAX); @@ -1031,13 +1040,13 @@ int main(int argc, char **argv) { } /* - { -#warning Test code added on 2013 May 02 11:17:01 + { + #warning Test code added on 2013 May 02 11:17:01 struct alias_node *a; for(a = global_alias_list; a; a = a->next) { - printf ("ALIAS [%s] [%s]\n", a->alias, a->value); + printf ("ALIAS [%s] [%s]\n", a->alias, a->value); + } } - } */ setlocale(LC_ALL, ""); @@ -1126,6 +1135,10 @@ int main(int argc, char **argv) { default_search_field = optarg; break; + case 'n': + global_nb_mails_max = atoi(optarg); + break; + default: error = 1; break; @@ -1256,9 +1269,10 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - nb_extracted_mails += 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_extracted_mails, + nb_search_conditions, search_conditions, + output_file); regfree(&db_filename_regexp); } @@ -1275,8 +1289,10 @@ int main(int argc, char **argv) { s = parse_token(db_filename, PATH_MAX + 1, ';', s); if(db_filename[0]) { - nb_extracted_mails += - search_in_db(db_filename, nb_search_conditions, search_conditions, output_file); + nb_extracted_mails = + search_in_db(db_filename, + nb_extracted_mails, + nb_search_conditions, search_conditions, output_file); } } } @@ -1284,8 +1300,10 @@ int main(int argc, char **argv) { /* Search in all db files listed in the command arguments */ while(optind < argc) { - nb_extracted_mails += - search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file); + nb_extracted_mails = + search_in_db(argv[optind], + nb_extracted_mails, + nb_search_conditions, search_conditions, output_file); optind++; } } -- 2.20.1