+ fprintf(out, " -h, --help\n");
+ fprintf(out, " show this help\n");
+ fprintf(out, " -v, --version\n");
+ fprintf(out, " print the version number\n");
+ fprintf(out, " -i, --index\n");
+ fprintf(out, " index mails\n");
+ fprintf(out, " -s <search pattern>, --search <search pattern>\n");
+ fprintf(out, " search for matching mails in the data-base file\n");
+ fprintf(out, " -d <db filename>, --db-file <db filename>\n");
+ fprintf(out, " set the data-base file\n");
+ fprintf(out, " -r <db root path>, --db-root <db root path>\n");
+ fprintf(out, " set the data-base root path for recursive search\n");
+}
+
+/*********************************************************************/
+
+int ignore_entry(const char *name) {
+ return
+ /* strcmp(name, ".") == 0 || */
+ /* strcmp(name, "..") == 0 || */
+ (name[0] == '.' && name[1] != '/');
+}
+
+int mbox_line_match_search(struct search_request *request,
+ int mbox_id, char *mbox_value) {
+ return
+ (request->field_id == mbox_id ||
+ (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST)))
+ &&
+ regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
+}
+
+void search_in_db(int nb_search_requests,
+ struct search_request *search_requests,
+ FILE *db_file) {
+ int hits[MAX_NB_SEARCH_REQUESTS];
+ 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, *mbox_value;
+ int mbox_id;
+ int already_written, m, n;
+ int last_mbox_line_was_empty;
+
+ current_position_in_mail = 0;
+ already_written = 0;
+
+ for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
+
+ while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+ mbox_name = raw_db_line;
+ mbox_value = segment_next_field(raw_db_line);
+
+ if(strcmp("mail", mbox_name) == 0) {
+ char *position_in_file_string;
+ char *mail_filename;
+
+ for(n = 0; n < nb_search_requests && xor(hits[n], search_requests[n].negation); n++);
+
+ /* for(n = 0; n < nb_search_requests && */
+ /* ((hits[n] && !search_requests[n].negation) || */
+ /* (!hits[n] && search_requests[n].negation)); n++); */
+
+ if(n == nb_search_requests) {
+ FILE *mail_file;
+
+ mail_file = fopen(current_mail_filename, "r");
+
+ if(!mail_file) {
+ fprintf(stderr, "mymail: Cannot open mbox '%s'.\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 = 1;
+ printf("%s", raw_mbox_line);
+ while(1) {
+ if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+ (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
+ last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+ printf("%s", raw_mbox_line);
+ }
+ }
+
+ fclose(mail_file);
+ }
+
+ for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
+
+ position_in_file_string = mbox_value;
+ mail_filename = segment_next_field(mbox_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 {
+ mbox_id = -1;
+ for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
+ if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
+ mbox_id = m;
+ }
+ }
+ for(n = 0; n < nb_search_requests; n++) {
+ hits[n] |= mbox_line_match_search(&search_requests[n],
+ mbox_id, mbox_value);
+ }
+ }
+ }
+}
+
+void recursive_search_in_db(const char *entry_name,
+ int nb_search_requests,
+ struct search_request *search_requests) {
+ DIR *dir;
+ struct dirent *dir_e;
+ struct stat sb;
+ char raw_db_line[BUFFER_SIZE];
+ char subname[PATH_MAX + 1];
+
+ if(lstat(entry_name, &sb) != 0) {
+ fprintf(stderr,
+ "mymail: Cannot 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,
+ nb_search_requests,
+ search_requests);
+ }
+ }
+ 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: Cannot open \"%s\" for reading: %s\n",
+ db_filename,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ 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,
+ "mymail: Header line in '%s' does not match the mymail db format.\n",
+ entry_name);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ fprintf(stderr,
+ "mymail: Cannot read the header line in '%s'.\n",
+ entry_name);
+ exit(EXIT_FAILURE);
+ }
+
+ search_in_db(nb_search_requests, search_requests, db_file);
+
+ fclose(db_file);
+ }
+ }