X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=mymail.c;h=5b821bc56f859c722037fe7688edae953697dcd5;hb=234fd9058ed477559401bd581d7dbf78ce08e3a5;hp=6388365093510e913221db4b9e8fd5f8f7f9cef8;hpb=319a695493cac88fc6b14541d30ae0107f38caf9;p=mymail.git diff --git a/mymail.c b/mymail.c index 6388365..5b821bc 100644 --- a/mymail.c +++ b/mymail.c @@ -19,6 +19,18 @@ * */ +/* + + This command is a dumb mail indexer. It can either (1) scan + directories containing mbox files, and create a db file containing + for each mail a list of fields computed from the header, or (2) + read such a db file and get all the mails matching regexp-defined + conditions on the fields. + + It is low-tech, simple, light and fast. + +*/ + #define _GNU_SOURCE #include @@ -45,6 +57,7 @@ struct parsable_field { char *db_filename; char *search_pattern; +int paranoid; int action_index; char *segment_next_field(char *current) { @@ -89,7 +102,7 @@ void search_in_db(const char *search_name, const char *search_regexp_string, FILE *db_file) { char raw_line[BUFFER_SIZE]; char current_mail_filename[BUFFER_SIZE]; - unsigned int current_position_in_mail; + unsigned long int current_position_in_mail; char *name, *value; regex_t regexp; int already_written; @@ -111,20 +124,18 @@ void search_in_db(const char *search_name, const char *search_regexp_string, name = raw_line; value = segment_next_field(raw_line); - /* printf("LINE [%s] %s", name, value); */ - if(strcmp("mail", name) == 0) { char *position_in_file_string = value; char *mail_filename = segment_next_field(value); - current_position_in_mail = atoi(position_in_file_string); + current_position_in_mail = atol(position_in_file_string); strcpy(current_mail_filename, mail_filename); remove_eof(current_mail_filename); - /* printf("READING [%s]\n", current_mail_filename); */ already_written = 0; - } else if(!already_written) { - if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) { + } + + else if(!already_written) { + if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) { FILE *mail_file; - /* printf("%s:%u\n", current_mail_filename, current_position_in_mail); */ mail_file = fopen(current_mail_filename, "r"); if(!mail_file) { fprintf(stderr, "mymail: Can not open `%s'.\n", current_mail_filename); @@ -143,8 +154,9 @@ void search_in_db(const char *search_name, const char *search_regexp_string, } } } -} + regfree(®exp); +} /*********************************************************************/ @@ -154,13 +166,14 @@ void read_file(const char *input_filename, char raw_line[BUFFER_SIZE]; FILE *file; int in_header, new_header; - unsigned int position_in_file; + unsigned long int position_in_file; file = fopen(input_filename, "r"); if(!file) { fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename); - exit(EXIT_FAILURE); + if(paranoid) { exit(EXIT_FAILURE); } + return; } in_header = 0; @@ -172,10 +185,10 @@ void read_file(const char *input_filename, if(strncmp(raw_line, "From ", 5) == 0) { if(in_header) { fprintf(stderr, - "Got a 'From ' in the header in %s:%u.\n", + "Got a ^\"From \" in the header in %s:%lu.\n", input_filename, position_in_file); fprintf(stderr, "%s", raw_line); - exit(EXIT_FAILURE); + if(paranoid) { exit(EXIT_FAILURE); } } in_header = 1; new_header = 1; @@ -183,17 +196,11 @@ void read_file(const char *input_filename, if(in_header) { in_header = 0; } } - /* if(in_header) { */ - /* printf("LINE.H %s", raw_line); */ - /* } else { */ - /* printf("LINE.B %s", raw_line); */ - /* } */ - if(in_header) { int f; regmatch_t matches; if(new_header) { - fprintf(db_file, "mail %u %s\n", position_in_file, input_filename); + fprintf(db_file, "mail %lu %s\n", position_in_file, input_filename); new_header = 0; } for(f = 0; f < nb_fields_to_parse; f++) { @@ -213,8 +220,8 @@ void read_file(const char *input_filename, int ignore_entry(const char *name) { return - strcmp(name, ".") == 0 || - strcmp(name, "..") == 0 || + /* strcmp(name, ".") == 0 || */ + /* strcmp(name, "..") == 0 || */ (name[0] == '.' && name[1] != '/'); } @@ -232,11 +239,6 @@ void process_entry(const char *dir_name, dir_name, strerror(errno)); exit(EXIT_FAILURE); - } else { - } - - if(S_ISLNK(sb.st_mode)) { - return; } dir = opendir(dir_name); @@ -251,10 +253,7 @@ void process_entry(const char *dir_name, } closedir(dir); } else { - if(S_ISREG(sb.st_mode)) { - /* printf("Processing regular file '%s'.\n", dir_name); */ - read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file); - } + read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file); } } @@ -295,6 +294,7 @@ int main(int argc, char **argv) { char c; int f; + paranoid = 0; action_index = 0; search_pattern = 0; @@ -319,8 +319,7 @@ int main(int argc, char **argv) { case 's': if(search_pattern) { - fprintf(stderr, - "mymail: Search pattern already defined.\n"); + fprintf(stderr, "mymail: Search pattern already defined.\n"); exit(EXIT_FAILURE); } search_pattern = strdup(optarg); @@ -416,5 +415,7 @@ int main(int argc, char **argv) { } } + free(db_filename); + exit(EXIT_SUCCESS); }