+ fprintf(out, " -h, --help\n");
+ fprintf(out, " show this help\n");
+ fprintf(out, " -v, --version\n");
+ fprintf(out, " print the version number\n");
+ fprintf(out, " -i, --index\n");
+ fprintf(out, " index mails\n");
+ fprintf(out, " -s <search pattern>, --search <search pattern>\n");
+ fprintf(out, " search for matching mails in the data-base file\n");
+ fprintf(out, " -d <db filename>, --db-file <db filename>\n");
+ fprintf(out, " set the data-base file\n");
+ fprintf(out, " -r <db root path>, --db-root <db root path>\n");
+ fprintf(out, " set the data-base root path for recursive search\n");
+}
+
+/*********************************************************************/
+
+int ignore_entry(const char *name) {
+ return
+ /* strcmp(name, ".") == 0 || */
+ /* strcmp(name, "..") == 0 || */
+ (name[0] == '.' && name[1] != '/');
+}
+
+int mbox_line_match_search(struct search_request *request,
+ int mbox_id, char *mbox_value) {
+ return
+ (request->field_id == mbox_id ||
+ (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST)))
+ &&
+ regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
+}
+
+void search_in_db(int nb_search_patterns,
+ struct search_request *search_requests,
+ FILE *db_file) {
+ int hits[MAX_NB_SEARCH_PATTERNS];
+ char raw_db_line[BUFFER_SIZE];
+ char raw_mbox_line[BUFFER_SIZE];
+ char current_mail_filename[PATH_MAX + 1];
+ unsigned long int current_position_in_mail;
+ char *mbox_name, *mbox_value;
+ int mbox_id;
+ int already_written, m, n;
+ int last_mbox_line_was_empty;
+
+ current_position_in_mail = 0;
+ already_written = 0;
+
+ for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
+
+ while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+ mbox_name = raw_db_line;
+ mbox_value = segment_next_field(raw_db_line);
+
+ if(strcmp("mail", mbox_name) == 0) {
+ char *position_in_file_string;
+ char *mail_filename;
+
+ for(n = 0; n < nb_search_patterns && xor(hits[n], search_requests[n].negation); n++);
+
+ /* for(n = 0; n < nb_search_patterns && */
+ /* ((hits[n] && !search_requests[n].negation) || */
+ /* (!hits[n] && search_requests[n].negation)); n++); */
+
+ if(n == nb_search_patterns) {
+ FILE *mail_file;
+
+ mail_file = fopen(current_mail_filename, "r");
+
+ if(!mail_file) {
+ fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
+ exit(EXIT_FAILURE);
+ }
+
+ fseek(mail_file, current_position_in_mail, SEEK_SET);
+
+ if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+ last_mbox_line_was_empty = 1;
+ printf("%s", raw_mbox_line);
+ while(1) {
+ if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+ (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
+ last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+ printf("%s", raw_mbox_line);
+ }
+ }
+
+ fclose(mail_file);
+ }
+
+ for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
+
+ position_in_file_string = mbox_value;
+ mail_filename = segment_next_field(mbox_value);
+ current_position_in_mail = atol(position_in_file_string);
+ strcpy(current_mail_filename, mail_filename);
+
+ remove_eof(current_mail_filename);
+ already_written = 0;
+ }
+
+ else {
+ mbox_id = -1;
+ for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
+ if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
+ mbox_id = m;
+ }
+ }
+ for(n = 0; n < nb_search_patterns; n++) {
+ hits[n] |= mbox_line_match_search(&search_requests[n],
+ mbox_id, mbox_value);
+ }
+ }
+ }