fprintf(out, "\n");
}
+/*********************************************************************/
+
+void search_in_db(const char *search_name, const char *search_regexp_string,
+ FILE *db_file) {
+ char raw_line[BUFFER_SIZE];
+ char current_mail[BUFFER_SIZE];
+ char *name, *value;
+ regex_t regexp;
+
+ 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);
+ }
+
+ while(fgets(raw_line, BUFFER_SIZE, db_file)) {
+ name = raw_line;
+ value = raw_line;
+ while(*value && *value != ' ') value++;
+ *value = '\0'; value++;
+ while(*value && *value == ' ') value++;
+
+ /* printf("LINE [%s] %s", name, value); */
+
+ if(strcmp("mail", name) == 0) {
+ strcpy(current_mail, value);
+ /* printf("READING [%s]\n", current_mail); */
+ } else {
+ if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
+ printf("%s", current_mail);
+ }
+ }
+ }
+}
+
+
+/*********************************************************************/
+
void read_file(const char *input_filename,
int nb_fields_to_parse, struct parsable_field *fields_to_parse,
FILE *db_file) {
else {
if(search_pattern) {
+ FILE *db_file;
char *search_name;
- char *search_regexp;
+ char *search_regexp_string;
search_name = search_pattern;
- search_regexp = search_pattern;
- while(*search_regexp && *search_regexp != ' ') search_regexp++;
- *search_regexp = '\0'; search_regexp++;
- while(*search_regexp && *search_regexp == ' ') search_regexp++;
- if(!*search_regexp) {
+ search_regexp_string = search_pattern;
+ while(*search_regexp_string && *search_regexp_string != ' ') search_regexp_string++;
+ *search_regexp_string = '\0'; search_regexp_string++;
+ while(*search_regexp_string && *search_regexp_string == ' ') search_regexp_string++;
+ if(!*search_regexp_string) {
fprintf(stderr,
"Syntax error in the search pattern.\n");
exit(EXIT_FAILURE);
}
+
printf("Starting search in %s for field \"%s\" matching \"%s\".\n",
db_filename,
search_name,
- search_regexp);
+ search_regexp_string);
+
+ db_file = fopen(db_filename, "r");
+
+ if(!db_file) {
+ fprintf(stderr,
+ "mymail: Can not open \"%s\" for reading: %s\n",
+ db_filename,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ search_in_db(search_name, search_regexp_string, db_file);
+
+ fclose(db_file);
free(search_pattern);
}
}