fix segfault in get_default_filepath

parent 23119ece
......@@ -89,7 +89,8 @@ main(int argc, char** argv)
die_if_null(filepaths_file,"Could not open given file file %s!", filepaths_file_path);
while(getline(&curr_url, &url_len, url_file) != -1){
drop_newline(curr_url);
// Determine the filepath
// Determine the filepath
// Memory leak.
if (filepaths_file_path != NULL && getline(&curr_filepath, &filepath_len, filepaths_file) != -1) {
// Try to read a filepath from the filepaths file
drop_newline(curr_filepath);
......@@ -161,24 +162,17 @@ download_file(char *url, char *filepath, CURL *curl)
char*
get_default_filepath(const char *url)
{
char *url_cpy = malloc(strlen(url)+1);
strcpy(url_cpy, url);
die_if_null(url_cpy, "Malloc failed!");
char *tok_pointer = strtok(url_cpy, "/");
char *tok_buf = tok_pointer;
//case of adress without '/' this will return adress as filename
while(tok_buf != NULL){
tok_pointer = tok_buf;
tok_buf = strtok(NULL, "/");
}
char *last_slash = strrchr(url, '/');
if(last_slash == NULL)
last_slash = (char *)url;
else
last_slash+=1; // remove '/'
char *filepath = malloc(strlen(tok_pointer)+1);
die_if_null(filepath, "Malloc failed!");
strcpy(filepath, tok_pointer);
char *filepath = NULL;
filepath = malloc(strlen(last_slash)+1);
die_if_null(filepath, "Malloc failed!");
free(url_cpy);
strcpy(filepath, last_slash);
return filepath;
}
......
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