+ char current_mail_filename[BUFFER_SIZE];
+ unsigned long int current_position_in_mail;
+ char *name, *value;
+ regex_t regexp;
+ int already_written;
+
+ if(regcomp(®exp,
+ search_regexp_string,
+ REG_ICASE)) {
+ fprintf(stderr,
+ "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
+ search_regexp_string,
+ search_name);
+ exit(EXIT_FAILURE);
+ }
+
+ current_position_in_mail = 0;
+ already_written = 0;
+
+ while(fgets(raw_line, BUFFER_SIZE, db_file)) {
+ name = raw_line;
+ value = segment_next_field(raw_line);
+
+ if(strcmp("mail", name) == 0) {
+ char *position_in_file_string = value;
+ char *mail_filename = segment_next_field(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 if(!already_written) {
+ if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
+ FILE *mail_file;
+ mail_file = fopen(current_mail_filename, "r");
+ if(!mail_file) {
+ fprintf(stderr, "mymail: Can not open `%s'.\n", current_mail_filename);
+ exit(EXIT_FAILURE);
+ }
+ fseek(mail_file, current_position_in_mail, SEEK_SET);
+ if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
+ printf("%s", raw_line);
+ while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
+ strncmp(raw_line, "From ", 5)) {
+ printf("%s", raw_line);
+ }
+ }
+ fclose(mail_file);
+ already_written = 1;
+ }
+ }
+ }
+
+ regfree(®exp);
+}
+
+/*********************************************************************/
+
+void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
+ char *raw_line, FILE *db_file) {
+ regmatch_t matches;
+ int f;
+ for(f = 0; f < nb_fields_to_parse; f++) {
+ if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
+ fprintf(db_file, "%s %s\n",
+ fields_to_parse[f].name,
+ raw_line + matches.rm_eo);
+ }
+ }
+}
+
+void index_mbox(const char *input_filename,
+ int nb_fields_to_parse, struct parsable_field *fields_to_parse,
+ FILE *db_file) {
+ char raw_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
+ char *end_of_full_line;