return string;
}
-void remove_eof(char *c) {
- while(*c && *c != '\n' && *c != '\r') c++;
- *c = '\0';
-}
-
/********************************************************************/
/* malloc with error checking. */
fclose(mail_file);
}
+void write_mail(const char *mail_filename, unsigned long int position_in_mail,
+ FILE *output_file) {
+ char raw_mbox_line[BUFFER_SIZE];
+ int last_mbox_line_was_empty;
+ FILE *mail_file;
+
+ mail_file = fopen(mail_filename, "r");
+
+ if(!mail_file) {
+ fprintf(stderr,
+ "mymail: Cannot open mbox '%s' for mail extraction.\n",
+ mail_filename);
+ exit(EXIT_FAILURE);
+ }
+
+ fseek(mail_file, position_in_mail, SEEK_SET);
+
+ if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+ last_mbox_line_was_empty = 0;
+ fprintf(output_file, "%s", raw_mbox_line);
+ while(1) {
+ if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+ (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
+ )
+ break;
+ last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+ fprintf(output_file, "%s", raw_mbox_line);
+ }
+ }
+
+ fclose(mail_file);
+}
+
void search_in_db(const char *db_filename,
int nb_search_conditions,
struct search_condition *search_conditions,
int hits[MAX_NB_SEARCH_CONDITIONS];
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[TOKEN_BUFFER_SIZE], *mbox_value;
int mbox_id;
int already_written, m, n;
- int last_mbox_line_was_empty;
int nb_body_conditions, nb_fulfilled_body_conditions;
FILE *db_file;
exit(EXIT_FAILURE);
}
+ /* 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,
exit(EXIT_FAILURE);
}
+ /* Then parse the said db file */
+
current_position_in_mail = 0;
already_written = 0;
if(n == nb_search_conditions) {
- /* all conditions but the body ones are fine, check the body
- ones */
+ /* Now check the body ones */
if(nb_body_conditions > 0) {
update_body_hits(current_mail_filename, current_position_in_mail,
}
if(nb_body_conditions == nb_fulfilled_body_conditions) {
- FILE *mail_file;
-
- mail_file = fopen(current_mail_filename, "r");
-
- if(!mail_file) {
- fprintf(stderr,
- "mymail: Cannot open mbox '%s' for mail extraction.\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 = 0;
- fprintf(output_file, "%s", raw_mbox_line);
- while(1) {
- if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
- (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
- )
- break;
- last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
- fprintf(output_file, "%s", raw_mbox_line);
- }
- }
-
- fclose(mail_file);
+ write_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, ' ', mbox_value);
+ mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
current_position_in_mail = atol(position_in_file_string);
- remove_eof(current_mail_filename);
already_written = 0;
}