#include <time.h>
#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
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;
fprintf(out, " index mails\n");
fprintf(out, " -o <output filename>, --output <output filename>\n");
fprintf(out, " set the result file, use stdout if unset\n");
+ fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
+ fprintf(out, " set the maximum number of mails to extract\n");
fprintf(out, " -a <search field>, --default-search <search field>\n");
fprintf(out, " set the default search field\n");
}
}
int search_in_db(const char *db_filename,
+ int nb_extracted_mails,
int nb_search_conditions,
struct search_condition *search_conditions,
FILE *output_file) {
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);
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) {
}
}
- 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);
}
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) {
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,
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);
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);
}
}
{ "index", 0, 0, 'i' },
{ "output", 1, 0, 'o' },
{ "default-search", 1, 0, 'a' },
+ { "nb-mails-max", 1, 0, 'n' },
{ 0, 0, 0, 0 }
};
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);
}
/*
- {
-#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, "");
default_search_field = optarg;
break;
+ case 'n':
+ global_nb_mails_max = atoi(optarg);
+ break;
+
default:
error = 1;
break;
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);
}
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);
}
}
}
/* 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++;
}
}