+ char current_mail_filename[PATH_MAX + 1];
+ 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 recursive_search_in_db(const char *entry_name,
+ const char *search_name, const char *search_regexp_string) {
+ DIR *dir;
+ struct dirent *dir_e;
+ struct stat sb;
+ char raw_line[BUFFER_SIZE];
+ char subname[PATH_MAX + 1];
+
+ if(lstat(entry_name, &sb) != 0) {
+ fprintf(stderr,
+ "mymail: Can not stat \"%s\": %s\n",
+ entry_name,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ 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);
+ recursive_search_in_db(subname, search_name, search_regexp_string);
+ }
+ }
+ closedir(dir);
+ } else {
+ const char *s = entry_name, *filename = entry_name;
+ while(*s) { if(*s == '/') { filename = s+1; } s++; }
+
+ if(strcmp(filename, db_filename) == 0) {
+ FILE *db_file = fopen(entry_name, "r");
+
+ if(!db_file) {
+ fprintf(stderr,
+ "mymail: Can not open \"%s\" for reading: %s\n",
+ db_filename,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if(fgets(raw_line, BUFFER_SIZE, db_file)) {
+ if(strncmp(raw_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",
+ entry_name);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ fprintf(stderr,
+ "mymail: Can not read the header line in '%s'.\n",
+ entry_name);
+ exit(EXIT_FAILURE);
+ }
+
+ search_in_db(search_name, search_regexp_string, db_file);
+
+ fclose(db_file);
+ }
+ }
+}
+
+/*********************************************************************/
+
+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 *mbox_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;