Now use db_filename_regexp_string for recursive search of db files.
[mymail.git] / mymail.c
1
2 /*
3  *  Copyright (c) 2013 Francois Fleuret
4  *  Written by Francois Fleuret <francois@fleuret.org>
5  *
6  *  This file is part of mymail.
7  *
8  *  mymail is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 3 as
10  *  published by the Free Software Foundation.
11  *
12  *  mymail is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with mymail.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 /*
23
24   This command is a dumb mail indexer. It can either (1) scan
25   directories containing mbox files, and create a db file containing
26   for each mail a list of fields computed from the header, or (2)
27   read such a db file and get all the mails matching regexp-defined
28   conditions on the fields.
29
30   It is low-tech, simple, light and fast.
31
32 */
33
34 #define _GNU_SOURCE
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <locale.h>
42 #include <getopt.h>
43 #include <limits.h>
44 #include <dirent.h>
45 #include <regex.h>
46
47 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
48 #define VERSION "0.9"
49
50 #define MAX_NB_SEARCH_REQUESTS 10
51
52 #define BUFFER_SIZE 65536
53
54 char *db_filename;
55 char *db_filename_regexp_string;
56 char *db_root_path;
57 char *db_filename_list;
58
59 int paranoid;
60 int action_index;
61
62 /********************************************************************/
63
64 enum {
65   ID_MAIL = 0,
66   ID_FROM,
67   ID_TO,
68   ID_SUBJECT,
69   ID_DATE,
70   ID_PARTICIPANT,
71   MAX_ID
72 };
73
74 static char *field_names[] = {
75   "mail",
76   "from",
77   "to",
78   "subject",
79   "date",
80   "part"
81 };
82
83 /********************************************************************/
84
85 struct search_request {
86   int field_id;
87   int negation;
88   regex_t regexp;
89 };
90
91 /********************************************************************/
92
93 struct parsable_field {
94   int id;
95   char *regexp_string;
96   regex_t regexp;
97 };
98
99 static struct parsable_field fields_to_parse[] = {
100   {
101     ID_FROM,
102     "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)",
103     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
104   },
105
106   {
107     ID_TO,
108     "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
109     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
110   },
111
112   {
113     ID_SUBJECT,
114     "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
115     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
116   },
117
118   {
119     ID_DATE,
120     "^[Dd][Aa][Tt][Ee]: ",
121     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
122   },
123
124 };
125
126 /********************************************************************/
127
128 int xor(int a, int b) {
129   return (a && !b) || (!a && b);
130 }
131
132 char *segment_next_field(char *current) {
133   while(*current && *current != ' ') current++;
134   *current = '\0'; current++;
135   while(*current && *current == ' ') current++;
136   return current;
137 }
138
139 void remove_eof(char *c) {
140   while(*c && *c != '\n' && *c != '\r') c++;
141   *c = '\0';
142 }
143
144 /********************************************************************/
145
146 /* malloc with error checking.  */
147
148 void *safe_malloc(size_t n) {
149   void *p = malloc(n);
150   if(!p && n != 0) {
151     fprintf(stderr,
152             "mymail: cannot allocate memory: %s\n", strerror(errno));
153     exit(EXIT_FAILURE);
154   }
155   return p;
156 }
157
158 /*********************************************************************/
159
160 void print_version(FILE *out) {
161   fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
162 }
163
164 void print_usage(FILE *out) {
165   print_version(out);
166   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
167   fprintf(out, "\n");
168   fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
169   fprintf(out, "\n");
170   fprintf(out, " -h, --help\n");
171   fprintf(out, "         show this help\n");
172   fprintf(out, " -v, --version\n");
173   fprintf(out, "         print the version number\n");
174   fprintf(out, " -i, --index\n");
175   fprintf(out, "         index mails\n");
176   fprintf(out, " -s <search pattern>, --search <search pattern>\n");
177   fprintf(out, "         search for matching mails in the data-base file\n");
178   fprintf(out, " -d <db filename>, --db-file <db filename>\n");
179   fprintf(out, "         set the data-base file\n");
180   fprintf(out, " -r <db root path>, --db-root <db root path>\n");
181   fprintf(out, "         set the data-base root path for recursive search\n");
182 }
183
184 /*********************************************************************/
185
186 int ignore_entry(const char *name) {
187   return
188     /* strcmp(name, ".") == 0 || */
189     /* strcmp(name, "..") == 0 || */
190     (name[0] == '.' && name[1] != '/');
191 }
192
193 int mbox_line_match_search(struct search_request *request,
194                            int mbox_id, char *mbox_value) {
195   return
196     (request->field_id == mbox_id ||
197      (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_TO)))
198     &&
199     regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
200 }
201
202 void search_in_db(int nb_search_requests,
203                   struct search_request *search_requests,
204                   FILE *db_file) {
205   int hits[MAX_NB_SEARCH_REQUESTS];
206   char raw_db_line[BUFFER_SIZE];
207   char raw_mbox_line[BUFFER_SIZE];
208   char current_mail_filename[PATH_MAX + 1];
209   unsigned long int current_position_in_mail;
210   char *mbox_name, *mbox_value;
211   int mbox_id;
212   int already_written, m, n;
213   int last_mbox_line_was_empty;
214
215   current_position_in_mail = 0;
216   already_written = 0;
217
218   for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
219
220   while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
221     mbox_name = raw_db_line;
222     mbox_value = segment_next_field(raw_db_line);
223
224     if(strcmp("mail", mbox_name) == 0) {
225       char *position_in_file_string;
226       char *mail_filename;
227
228       for(n = 0; n < nb_search_requests && xor(hits[n], search_requests[n].negation); n++);
229
230       /* for(n = 0; n < nb_search_requests && */
231             /* ((hits[n] && !search_requests[n].negation) || */
232              /* (!hits[n] && search_requests[n].negation)); n++); */
233
234       if(n == nb_search_requests) {
235         FILE *mail_file;
236
237         mail_file = fopen(current_mail_filename, "r");
238
239         if(!mail_file) {
240           fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
241           exit(EXIT_FAILURE);
242         }
243
244         fseek(mail_file, current_position_in_mail, SEEK_SET);
245
246         if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
247           last_mbox_line_was_empty = 1;
248           printf("%s", raw_mbox_line);
249           while(1) {
250             if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
251                (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
252             last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
253             printf("%s", raw_mbox_line);
254           }
255         }
256
257         fclose(mail_file);
258       }
259
260       for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
261
262       position_in_file_string = mbox_value;
263       mail_filename = segment_next_field(mbox_value);
264       current_position_in_mail = atol(position_in_file_string);
265       strcpy(current_mail_filename, mail_filename);
266
267       remove_eof(current_mail_filename);
268       already_written = 0;
269     }
270
271     else {
272       mbox_id = -1;
273       for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
274         if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
275           mbox_id = m;
276         }
277       }
278       for(n = 0; n < nb_search_requests; n++) {
279         hits[n] |= mbox_line_match_search(&search_requests[n],
280                                           mbox_id, mbox_value);
281       }
282     }
283   }
284 }
285
286 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
287                             int nb_search_requests, struct search_request *search_requests) {
288   DIR *dir;
289   struct dirent *dir_e;
290   struct stat sb;
291   char raw_db_line[BUFFER_SIZE];
292   char subname[PATH_MAX + 1];
293
294   if(lstat(entry_name, &sb) != 0) {
295     fprintf(stderr,
296             "mymail: Cannot stat \"%s\": %s\n",
297             entry_name,
298             strerror(errno));
299     exit(EXIT_FAILURE);
300   }
301
302   dir = opendir(entry_name);
303
304   if(dir) {
305     while((dir_e = readdir(dir))) {
306       if(!ignore_entry(dir_e->d_name)) {
307         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
308         recursive_search_in_db(subname, db_filename_regexp,
309                                nb_search_requests, search_requests);
310       }
311     }
312     closedir(dir);
313   }
314
315   else {
316     const char *s = entry_name, *filename = entry_name;
317     while(*s) { if(*s == '/') { filename = s+1; } s++; }
318
319     if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
320       FILE *db_file = fopen(entry_name, "r");
321
322       if(!db_file) {
323         fprintf(stderr,
324                 "mymail: Cannot open \"%s\" for reading: %s\n",
325                 db_filename,
326                 strerror(errno));
327         exit(EXIT_FAILURE);
328       }
329
330       if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
331         if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
332           fprintf(stderr,
333                   "mymail: Header line in '%s' does not match the mymail db format.\n",
334                   entry_name);
335           exit(EXIT_FAILURE);
336         }
337       } else {
338         fprintf(stderr,
339                 "mymail: Cannot read the header line in '%s'.\n",
340                 entry_name);
341         exit(EXIT_FAILURE);
342       }
343
344       search_in_db(nb_search_requests, search_requests, db_file);
345
346       fclose(db_file);
347     }
348   }
349 }
350
351 /*********************************************************************/
352
353 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
354                          char *raw_mbox_line, FILE *db_file) {
355   regmatch_t matches;
356   int f;
357   for(f = 0; f < nb_fields_to_parse; f++) {
358     if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
359       fprintf(db_file, "%s %s\n",
360               field_names[fields_to_parse[f].id],
361               raw_mbox_line + matches.rm_eo);
362     }
363   }
364 }
365
366 void index_mbox(const char *mbox_filename,
367                 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
368                 FILE *db_file) {
369   char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
370   char *end_of_full_line;
371   FILE *file;
372   int in_header, new_header, last_mbox_line_was_empty;
373   unsigned long int position_in_file;
374
375   file = fopen(mbox_filename, "r");
376
377   if(!file) {
378     fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
379     if(paranoid) { exit(EXIT_FAILURE); }
380     return;
381   }
382
383   in_header = 0;
384   new_header = 0;
385
386   position_in_file = 0;
387   end_of_full_line = 0;
388   full_line[0] = '\0';
389   last_mbox_line_was_empty = 1;
390
391   while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
392     if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
393       if(in_header) {
394         fprintf(stderr,
395                 "Got a ^\"From \" in the header in %s:%lu.\n",
396                 mbox_filename, position_in_file);
397         fprintf(stderr, "%s", raw_mbox_line);
398         if(paranoid) { exit(EXIT_FAILURE); }
399       }
400       in_header = 1;
401       new_header = 1;
402     } else if(raw_mbox_line[0] == '\n') {
403       if(in_header) { in_header = 0; }
404     }
405
406     last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
407
408     if(in_header) {
409       if(new_header) {
410         fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
411         new_header = 0;
412       }
413
414       if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
415         char *start = raw_mbox_line;
416         while(*start == ' ' || *start == '\t') start++;
417         *(end_of_full_line++) = ' ';
418         strcpy(end_of_full_line, start);
419         while(*end_of_full_line && *end_of_full_line != '\n') {
420           end_of_full_line++;
421         }
422         *end_of_full_line = '\0';
423       }
424
425       else {
426         /*
427           if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
428           (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
429           fprintf(stderr,
430           "Header line syntax error %s:%lu.\n",
431           mbox_filename, position_in_file);
432           fprintf(stderr, "%s", raw_mbox_line);
433           }
434         */
435
436         if(full_line[0]) {
437           index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
438         }
439
440         end_of_full_line = full_line;
441         strcpy(end_of_full_line, raw_mbox_line);
442         while(*end_of_full_line && *end_of_full_line != '\n') {
443           end_of_full_line++;
444         }
445         *end_of_full_line = '\0';
446       }
447
448     }
449
450     position_in_file += strlen(raw_mbox_line);
451   }
452
453   fclose(file);
454 }
455
456 void recursive_index_mbox(FILE *db_file,
457                           const char *entry_name,
458                           int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
459   DIR *dir;
460   struct dirent *dir_e;
461   struct stat sb;
462   char subname[PATH_MAX + 1];
463
464   if(lstat(entry_name, &sb) != 0) {
465     fprintf(stderr,
466             "mymail: Cannot stat \"%s\": %s\n",
467             entry_name,
468             strerror(errno));
469     exit(EXIT_FAILURE);
470   }
471
472   dir = opendir(entry_name);
473
474   if(dir) {
475     while((dir_e = readdir(dir))) {
476       if(!ignore_entry(dir_e->d_name)) {
477         snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
478         recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
479       }
480     }
481     closedir(dir);
482   } else {
483     index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
484   }
485 }
486
487 /*********************************************************************/
488
489 /* For long options that have no equivalent short option, use a
490    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
491 enum {
492   OPT_BASH_MODE = CHAR_MAX + 1
493 };
494
495 static struct option long_options[] = {
496   { "help", no_argument, 0, 'h' },
497   { "version", no_argument, 0, 'v' },
498   { "db-file", 1, 0, 'd' },
499   { "db-pattern", 1, 0, 'p' },
500   { "db-root", 1, 0, 'r' },
501   { "db-list", 1, 0, 'l' },
502   { "search", 1, 0, 's' },
503   { "index", 0, 0, 'i' },
504   { 0, 0, 0, 0 }
505 };
506
507 /*********************************************************************/
508
509 int main(int argc, char **argv) {
510   int error = 0, show_help = 0;
511   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
512   char c;
513   int f;
514   int nb_search_requests;
515   char *search_request_strings[MAX_NB_SEARCH_REQUESTS];
516
517   /* for(f = 0; f < argc; f++) { */
518   /* printf("arg %d \"%s\"\n", f, argv[f]); */
519   /* } */
520
521   paranoid = 0;
522   action_index = 0;
523   db_filename = 0;
524   db_root_path = 0;
525   db_filename_list = 0;
526
527   setlocale(LC_ALL, "");
528
529   nb_search_requests = 0;
530
531   while ((c = getopt_long(argc, argv, "hvip:s:d:r:l:",
532                           long_options, NULL)) != -1) {
533
534     switch(c) {
535
536     case 'h':
537       show_help = 1;
538       break;
539
540     case 'v':
541       print_version(stdout);
542       break;
543
544     case 'i':
545       action_index = 1;
546       break;
547
548     case 'd':
549       db_filename = strdup(optarg);
550       break;
551
552     case 'p':
553       db_filename_regexp_string = strdup(optarg);
554       break;
555
556     case 'r':
557       db_root_path = strdup(optarg);
558       break;
559
560     case 'l':
561       db_filename_list = strdup(optarg);
562       break;
563
564     case 's':
565       if(nb_search_requests == MAX_NB_SEARCH_REQUESTS) {
566         fprintf(stderr, "mymail: Too many search patterns.\n");
567         exit(EXIT_FAILURE);
568       }
569       search_request_strings[nb_search_requests++] = strdup(optarg);
570       break;
571
572     default:
573       error = 1;
574       break;
575     }
576   }
577
578   if(!db_filename) {
579     char *default_db_filename = getenv("MYMAIL_DB_FILE");
580
581     if(!default_db_filename) {
582       default_db_filename = "mymail.db";
583     }
584
585     db_filename = strdup(default_db_filename);
586   }
587
588   if(!db_filename_regexp_string) {
589     char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
590
591     if(!default_db_filename_regexp_string) {
592       default_db_filename_regexp_string = "^mymail.db$";
593     }
594
595     db_filename_regexp_string = strdup(default_db_filename_regexp_string);
596   }
597
598   if(!db_root_path) {
599     char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
600
601     if(default_db_root_path) {
602       db_root_path = strdup(default_db_root_path);
603     }
604   }
605
606   if(!db_filename_list) {
607     char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
608
609     if(default_db_filename_list) {
610       db_filename_list = strdup(default_db_filename_list);
611     }
612   }
613
614   if(error) {
615     print_usage(stderr);
616     exit(EXIT_FAILURE);
617   }
618
619   if(show_help) {
620     print_usage(stdout);
621     exit(EXIT_SUCCESS);
622   }
623
624   if(action_index) {
625     FILE *db_file;
626
627     db_file = fopen(db_filename, "w");
628
629     if(!db_file) {
630       fprintf(stderr,
631               "mymail: Cannot open \"%s\" for writing: %s\n",
632               db_filename,
633               strerror(errno));
634       exit(EXIT_FAILURE);
635     }
636
637     for(f = 0; f < nb_fields_to_parse; f++) {
638       if(regcomp(&fields_to_parse[f].regexp,
639                  fields_to_parse[f].regexp_string,
640                  REG_ICASE)) {
641         fprintf(stderr,
642                 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
643                 fields_to_parse[f].regexp_string,
644                 field_names[fields_to_parse[f].id]);
645         exit(EXIT_FAILURE);
646       }
647     }
648
649     fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
650
651     while(optind < argc) {
652       recursive_index_mbox(db_file,
653                            argv[optind],
654                            nb_fields_to_parse, fields_to_parse);
655       optind++;
656     }
657
658     fclose(db_file);
659
660     for(f = 0; f < nb_fields_to_parse; f++) {
661       regfree(&fields_to_parse[f].regexp);
662     }
663   }
664
665   else {
666
667     if(nb_search_requests > 0) {
668       struct search_request search_requests[MAX_NB_SEARCH_REQUESTS];
669       char *search_field, *search_regexp_string;
670       int m, n;
671
672       for(n = 0; n < nb_search_requests; n++) {
673         search_field = search_request_strings[n];
674         search_regexp_string = segment_next_field(search_request_strings[n]);
675
676         if(search_field[0] == '!') {
677           search_field++;
678           search_requests[n].negation = 1;
679         } else {
680           search_requests[n].negation = 0;
681         }
682
683         search_requests[n].field_id = -1;
684         for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) {
685           if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
686             search_requests[n].field_id = m;
687           }
688         }
689
690         if(search_requests[n].field_id == -1) {
691           fprintf(stderr,
692                   "mymail: Syntax error in field name \"%s\".\n",
693                   search_field);
694           exit(EXIT_FAILURE);
695         }
696
697         if(regcomp(&search_requests[n].regexp,
698                    search_regexp_string,
699                    REG_ICASE)) {
700           fprintf(stderr,
701                   "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
702                   search_regexp_string,
703                   field_names[search_requests[n].field_id]);
704           exit(EXIT_FAILURE);
705         }
706       }
707
708       /* Recursive search if db_root_path is set */
709
710       if(db_root_path) {
711         regex_t db_filename_regexp;
712         if(regcomp(&db_filename_regexp,
713                    db_filename_regexp_string,
714                    0)) {
715           fprintf(stderr,
716                   "mymail: Syntax error in regexp \"%s\".\n",
717                   db_filename_regexp_string);
718           exit(EXIT_FAILURE);
719         }
720
721         recursive_search_in_db(db_root_path, &db_filename_regexp,
722                                nb_search_requests, search_requests);
723
724         regfree(&db_filename_regexp);
725       }
726
727       /* Search in all db files listed in db_filename_list */
728
729       if(db_filename_list) {
730         char db_filename[PATH_MAX + 1];
731         char *s, *t;
732         FILE *db_file;
733
734         s = db_filename_list;
735
736         while(*s) {
737           t = db_filename;
738           while(*s == ';') { s++; }
739           while(*s && *s != ';') { *t++ = *s++; }
740           *t++ = '\0';
741
742           if(db_filename[0]) {
743             db_file = fopen(db_filename, "r");
744
745             if(!db_file) {
746               fprintf(stderr,
747                       "mymail: Cannot open \"%s\" for reading: %s\n",
748                       argv[optind],
749                       strerror(errno));
750               exit(EXIT_FAILURE);
751             }
752
753             search_in_db(nb_search_requests, search_requests, db_file);
754
755             fclose(db_file);
756           }
757         }
758       }
759
760       /* Search in all db files listed in the command arguments */
761
762       while(optind < argc) {
763         FILE *db_file = fopen(argv[optind], "r");
764
765         if(!db_file) {
766           fprintf(stderr,
767                   "mymail: Cannot open \"%s\" for reading: %s\n",
768                   argv[optind],
769                   strerror(errno));
770           exit(EXIT_FAILURE);
771         }
772
773         search_in_db(nb_search_requests, search_requests, db_file);
774
775         fclose(db_file);
776         optind++;
777       }
778
779       for(n = 0; n < nb_search_requests; n++) {
780         regfree(&search_requests[n].regexp);
781         free(search_request_strings[n]);
782       }
783     }
784   }
785
786   free(db_filename);
787   free(db_filename_regexp_string);
788   free(db_root_path);
789   free(db_filename_list);
790
791   exit(EXIT_SUCCESS);
792 }