+ fclose(mail_file);
+}
+
+int check_full_mail_match(char *current_mail_filename,
+ int nb_search_conditions,
+ struct search_condition *search_conditions,
+ int nb_body_conditions,
+ int *hits,
+ int current_position_in_mail) {
+ int n, nb_fulfilled_body_conditions;
+
+ /* We first check all conditions but the body ones */
+
+ for(n = 0; n < nb_search_conditions &&
+ ((search_conditions[n].field_id == ID_BODY) ||
+ xor(hits[n], search_conditions[n].negation)); n++);
+
+ if(n == nb_search_conditions) {
+
+ /* Now check the body ones */
+
+ nb_fulfilled_body_conditions = 0;
+
+ if(nb_body_conditions > 0) {
+ update_body_hits(current_mail_filename, current_position_in_mail,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions,
+ hits);
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].field_id == ID_BODY &&
+ xor(hits[n], search_conditions[n].negation)) {
+ nb_fulfilled_body_conditions++;
+ }
+ }
+ }
+ return nb_body_conditions == nb_fulfilled_body_conditions;
+ } else {
+ return 0;
+ }
+}
+
+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];
+ char current_mail_filename[PATH_MAX + 1];
+ unsigned long int current_position_in_mail;
+ char mbox_key[TOKEN_BUFFER_SIZE];
+ const char *mbox_value;
+ int mbox_id;
+ int already_written, m, n;
+ int nb_body_conditions;
+ FILE *db_file;
+ int nb_extracted_mails;
+
+ nb_extracted_mails = 0;
+
+ if(!global_quiet) {
+ printf("Searching in '%s' ... ", db_filename);
+ fflush(stdout);
+ }
+
+ db_file = safe_fopen(db_filename, "r", "index file for search");
+
+ /* First, check the db file leading line integrity */
+
+ if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+ if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
+ fprintf(stderr,
+ "mymail: Header line in '%s' does not match the mymail db format.\n",
+ db_filename);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ fprintf(stderr,
+ "mymail: Cannot read the header line in '%s'.\n",
+ db_filename);
+ exit(EXIT_FAILURE);
+ }
+
+ /* 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));