+ /* Then parse the said db file */
+
+ current_position_in_mail = 0;
+ already_written = 0;
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+
+ nb_body_conditions = 0;
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].field_id == ID_BODY) {
+ nb_body_conditions++;
+ }
+ }
+
+ strcpy(current_mail_filename, "");
+
+ while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+ mbox_value = parse_token(mbox_key, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
+
+ if(strcmp("mail", mbox_key) == 0) {
+ char position_in_file_string[TOKEN_BUFFER_SIZE];
+ if(current_mail_filename[0] &&
+ check_full_mail_match(current_mail_filename,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions, hits, current_position_in_mail)) {
+ nb_extracted_mails++;
+ extract_mail(current_mail_filename, current_position_in_mail, output_file);
+ }
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+ mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
+ mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
+ current_position_in_mail = atol(position_in_file_string);
+ already_written = 0;
+ }
+
+ else {
+ mbox_id = -1;
+ for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
+ if(strncmp(field_keys[m], mbox_key, strlen(mbox_key)) == 0) {
+ mbox_id = m;
+ }
+ }
+ for(n = 0; n < nb_search_conditions; n++) {
+ hits[n] |= mbox_line_match_search(&search_conditions[n],
+ mbox_id, mbox_value);
+ }
+ }
+ }
+
+ if(current_mail_filename[0] &&
+ check_full_mail_match(current_mail_filename,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions, hits, current_position_in_mail)) {
+ nb_extracted_mails++;
+ extract_mail(current_mail_filename, current_position_in_mail, output_file);
+ }
+
+ 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_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,
+ "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))) {
+ 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);
+ }
+ }
+ 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_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");
+