From: Francois Fleuret Date: Mon, 28 Jan 2013 13:09:13 +0000 (+0100) Subject: Fixed two memory leaks. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=mymail.git;a=commitdiff_plain;h=d763d790abe7d93b4572c38b261a4f480a8aa961 Fixed two memory leaks. --- diff --git a/mymail.c b/mymail.c index 6388365..99a76d8 100644 --- a/mymail.c +++ b/mymail.c @@ -89,7 +89,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 +111,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,6 +141,8 @@ void search_in_db(const char *search_name, const char *search_regexp_string, } } } + + regfree(®exp); } @@ -154,7 +154,7 @@ 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"); @@ -172,7 +172,7 @@ 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); @@ -193,7 +193,7 @@ void read_file(const char *input_filename, 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 +213,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,12 +232,11 @@ void process_entry(const char *dir_name, dir_name, strerror(errno)); exit(EXIT_FAILURE); - } else { } - if(S_ISLNK(sb.st_mode)) { - return; - } + /* if(S_ISLNK(sb.st_mode)) { */ + /* return; */ + /* } */ dir = opendir(dir_name); @@ -251,10 +250,9 @@ void process_entry(const char *dir_name, } closedir(dir); } else { - if(S_ISREG(sb.st_mode)) { - /* printf("Processing regular file '%s'.\n", dir_name); */ + /* if(S_ISREG(sb.st_mode)) { */ read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file); - } + /* } */ } } @@ -416,5 +414,7 @@ int main(int argc, char **argv) { } } + free(db_filename); + exit(EXIT_SUCCESS); }