Фикс памяти в get_default_filepath

parent 65a62362
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
void usage(); void usage();
void die(const char *); void die(const char *);
char *get_default_filepath(char *); char *get_default_filepath(char *, char *);
void download_file(char *url, char *filepath, CURL *curl); void download_file(char *url, char *filepath, CURL *curl);
int file_exists(const char *filename); int file_exists(const char *filename);
...@@ -26,13 +26,13 @@ main(int argc, char** argv) ...@@ -26,13 +26,13 @@ main(int argc, char** argv)
char *url_file_path = NULL; char *url_file_path = NULL;
char *filepaths_file_path = NULL; char *filepaths_file_path = NULL;
char *place_holder = NULL; // for get_default_filepath func
//get args //get args
if(argc == 1) if(argc == 1)
usage(); usage();
else if(argc == 2){ else if(argc == 2){
url = argv[1]; url = argv[1];
filepath = get_default_filepath(url); filepath = get_default_filepath(url, place_holder);
}else{ }else{
for(int i = 1; i < argc; i++) for(int i = 1; i < argc; i++)
if(!strcmp(argv[i], "-f")) if(!strcmp(argv[i], "-f"))
...@@ -74,7 +74,6 @@ main(int argc, char** argv) ...@@ -74,7 +74,6 @@ main(int argc, char** argv)
size_t url_len = 0; size_t url_len = 0;
size_t filepath_len = 0; size_t filepath_len = 0;
switch(download_type){ switch(download_type){
case URL_ONLY: case URL_ONLY:
download_file(url, filepath, curl); download_file(url, filepath, curl);
...@@ -91,9 +90,9 @@ main(int argc, char** argv) ...@@ -91,9 +90,9 @@ main(int argc, char** argv)
if (curr_url[strlen(curr_url) - 1] == '\n') if (curr_url[strlen(curr_url) - 1] == '\n')
curr_url[strlen(curr_url) - 1] = '\0'; curr_url[strlen(curr_url) - 1] = '\0';
download_file(curr_url, get_default_filepath(curr_url), curl); download_file(curr_url, get_default_filepath(curr_url, place_holder), curl);
} }
free(place_holder);
free(curr_url); free(curr_url);
fclose(url_file); fclose(url_file);
break; break;
...@@ -109,7 +108,7 @@ main(int argc, char** argv) ...@@ -109,7 +108,7 @@ main(int argc, char** argv)
while(getline(&curr_url, &filepath_len, url_file) != -1){ while(getline(&curr_url, &filepath_len, url_file) != -1){
if(getline(&curr_filepath, &url_len, filepaths_file) == -1) if(getline(&curr_filepath, &url_len, filepaths_file) == -1)
curr_filepath = get_default_filepath(curr_url); curr_filepath = get_default_filepath(curr_url, place_holder);
if (curr_filepath[strlen(curr_filepath) - 1] == '\n') if (curr_filepath[strlen(curr_filepath) - 1] == '\n')
curr_filepath[strlen(curr_filepath) - 1] = '\0'; curr_filepath[strlen(curr_filepath) - 1] = '\0';
...@@ -118,10 +117,10 @@ main(int argc, char** argv) ...@@ -118,10 +117,10 @@ main(int argc, char** argv)
download_file(curr_url, curr_filepath, curl); download_file(curr_url, curr_filepath, curl);
} }
free(place_holder);
free(curr_url); free(curr_url);
fclose(url_file); fclose(url_file);
fclose(filepaths_file);
break; break;
} }
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
...@@ -159,9 +158,9 @@ download_file(char *url, char *filepath, CURL *curl) ...@@ -159,9 +158,9 @@ download_file(char *url, char *filepath, CURL *curl)
} }
char* char*
get_default_filepath(char *url) get_default_filepath(char *url, char *place_holder)
{ {
char *url_copy = malloc(strlen(url) + 1); char *url_copy = realloc(place_holder, strlen(url) + 1);
if (!url_copy) { if (!url_copy) {
die("Malloc failed!"); die("Malloc failed!");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment