+ fclose(mail_file);
+}
+
+int check_full_mail_match(char *current_mail_filename,
+ time_t mail_time,
+ 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;
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].field_id == ID_TIME_INTERVAL) {
+ hits[n] = (mail_time >= search_conditions[n].interval_start &&
+ (search_conditions[n].interval_stop == 0 ||
+ mail_time <= search_conditions[n].interval_stop));
+ }
+ }
+
+ /* 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;
+ }
+}
+
+/* We use the mail leading line time by default, and if we should and
+ can, we update with the Date: field */
+
+void update_time(int mbox_id, const char *mbox_value, time_t *t) {
+ const char *c;
+ struct tm tm;
+
+ if(mbox_id == ID_LEADING_LINE) {
+ c = mbox_value;
+ while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
+ strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
+ *t = mktime(&tm);
+ } else {
+ if(!global_use_leading_time) {
+ if(mbox_id == ID_DATE) {
+ if(strptime(mbox_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
+ strptime(mbox_value, "%d %b %Y %k:%M:%S", &tm)) {
+ *t = mktime(&tm);
+ }
+ }
+ }
+ }
+}
+
+int search_in_db(const char *db_filename,
+ int nb_search_conditions,
+ struct search_condition *search_conditions,
+ FILE *output_file) {
+
+ FILE *db_file;
+ char raw_db_line[BUFFER_SIZE];
+ char current_mail_filename[PATH_MAX + 1];
+ char mbox_key[TOKEN_BUFFER_SIZE];
+ char position_in_file_string[TOKEN_BUFFER_SIZE];
+ unsigned long int current_position_in_mail;
+ const char *mbox_value;
+ int mbox_id;
+ 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);
+ }
+
+ 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 {