+ /* Then parse the said db file */
+
+ current_position_in_mail = 0;
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+
+ nb_body_conditions = 0;
+ need_time = global_discard_mail_from_the_future;
+ mail_time = 0;
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].db_key == ID_BODY) {
+ nb_body_conditions++;
+ }
+ else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
+ need_time = 1;
+ }
+ }
+
+ strcpy(current_mail_filename, "");
+
+ while(nb_extracted_mails < global_nb_mails_max &&
+ fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+
+ /* Removes the CR */
+ char *s = raw_db_line;
+ while(*s && *s != '\n') { s++; }
+ *s = '\0';
+
+ 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] &&
+ (!global_discard_mail_from_the_future || mail_time < global_current_time + 3600 * 24) &&
+ 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++;
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+ db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
+ strncpy(current_mail_filename, db_value, PATH_MAX + 1);
+ current_position_in_mail = atol(position_in_file_string);
+ }
+
+ else {
+ db_key = -1;
+ for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
+ if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
+ db_key = m;
+ }
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ hits[n] |= db_line_match_search(&search_conditions[n],
+ db_key, db_value);
+ }
+
+ if(need_time) {
+ update_time(db_key, db_value, &mail_time);
+ }
+ }
+ }
+
+ /* printf("global_discard_mail_from_the_future = %d\n",
+ global_discard_mail_from_the_future); */
+
+ if(nb_extracted_mails < global_nb_mails_max &&
+ current_mail_filename[0] &&
+ (!global_discard_mail_from_the_future || mail_time < global_current_time + 3600 * 24) &&
+ 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);
+
+ if(!global_quiet) {
+ printf("done.\n");
+ fflush(stdout);
+ }
+
+ return nb_extracted_mails;
+}
+
+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) {
+ DIR *dir;
+ struct dirent *dir_e;
+ struct stat sb;
+ char subname[PATH_MAX + 1];
+
+ if(lstat(entry_name, &sb) != 0) {
+ fprintf(stderr,
+ "mymail: Cannot stat \"%s\": %s\n",
+ entry_name,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ /* printf("recursive_search_in_db %s\n", entry_name); */
+
+ dir = opendir(entry_name);
+
+ if(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_extracted_mails,
+ nb_search_conditions, search_conditions,
+ output_file);
+ }
+ }
+ closedir(dir);
+ }
+
+ else {
+ const char *s = entry_name, *filename = entry_name;
+ 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_extracted_mails,
+ nb_search_conditions, search_conditions, output_file);
+ }
+ }
+
+ return nb_extracted_mails;
+}
+
+/*********************************************************************/
+
+void index_one_mbox_line(unsigned int nb_fields_to_parse,
+ struct parsable_field *fields_to_parse,
+ char *raw_mbox_line, FILE *db_file) {
+ regmatch_t matches;
+ unsigned int f;
+ for(f = 0; f < nb_fields_to_parse; f++) {
+ if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
+ fprintf(db_file, "%s %s\n",
+ field_keys[fields_to_parse[f].id],
+ raw_mbox_line + matches.rm_eo);
+ }
+ }
+}
+
+void index_mbox(const char *mbox_filename,
+ int nb_fields_to_parse, struct parsable_field *fields_to_parse,
+ FILE *db_file) {
+ char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
+ char *end_of_full_line;
+ FILE *file;
+ int in_header, new_header;
+ unsigned long int position_in_file;
+
+ file = safe_fopen(mbox_filename, "r", "mbox for indexing");
+