get_default_filepath refactoring

parent 7e35c98b
......@@ -9,7 +9,7 @@
void usage();
void die(const char *format, va_list args);
char *get_default_filepath(char *, char *);
char *get_default_filepath(const char *);
CURLcode download_file(char *url, char *filepath, CURL *curl);
int file_exists(const char *filename);
void download_result_message(CURLcode res,char *url, char *filepath);
......@@ -32,7 +32,7 @@ main(int argc, char** argv)
usage();
else if(argc == 2){
url = argv[1];
filepath = strchr(url, '/');
filepath = get_default_filepath(url);
}else{
for(int i = 1; i < argc; i++)
if(!strcmp(argv[i], "-f"))
......@@ -60,23 +60,24 @@ main(int argc, char** argv)
if(show_progressbar == 1)
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
FILE *url_file = NULL;
FILE *filepaths_file = NULL;
char *curr_url;
char *curr_filepath;
size_t url_len = 0;
size_t filepath_len = 0;
CURLcode result;
if (url != NULL && filepath != NULL && url_file_path == NULL && filepaths_file_path == NULL){ // only 1 file
//fprintf(stdout, "Starting %s\n", filepath);
result = download_file(url, filepath, curl);
download_result_message(result, url, filepath);
free(filepath);
}else if((url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path == NULL) || // -uf
(url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path != NULL)){ // -uf and -ff
FILE *url_file = NULL;
FILE *filepaths_file = NULL;
char *curr_url;
char *curr_filepath;
size_t url_len = 0;
size_t filepath_len = 0;
url_file = fopen(url_file_path, "r");
if(filepaths_file_path)
......@@ -86,7 +87,6 @@ main(int argc, char** argv)
if(filepaths_file_path)
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
......@@ -95,7 +95,7 @@ main(int argc, char** argv)
drop_newline(curr_filepath);
//fallback to default logic if EOF in filepath file or none filepath file given
}else
curr_filepath = strchr(curr_url, '/');
curr_filepath = get_default_filepath(curr_url);
//fprintf(stdout, "Starting %s\n", filepath);
result = download_file(curr_url, curr_filepath, curl);
......@@ -147,14 +147,9 @@ download_file(char *url, char *filepath, CURL *curl)
{
FILE *file;
file = fopen(filepath,"wb");
if(file == NULL){
fprintf(stderr, "Error: Could not open file for writing: %s\n", filepath);
return CURLE_WRITE_ERROR;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
CURLcode res = curl_easy_perform(curl);
......@@ -164,21 +159,26 @@ download_file(char *url, char *filepath, CURL *curl)
}
char*
get_default_filepath(char *url, char *place_holder)
get_default_filepath(const char *url)
{
char *url_copy = realloc(place_holder, strlen(url) + 1);
die_if_null(url_copy, "Malloc failed!");
char *url_cpy = malloc(strlen(url)+1);
strcpy(url_cpy, url);
die_if_null(url_cpy, "Malloc failed!");
strcpy(url_copy, url);
char *tok_pointer = strtok(url_cpy, "/");
char *tok_buf = tok_pointer;
char *filepath = url_copy;
char *tmp = strtok(filepath, "/");
while(tmp != NULL){
filepath = tmp;
tmp = strtok(NULL, "/");
//case of adress without '/' this will return adress as filename
while(tok_buf != NULL){
tok_pointer = tok_buf;
tok_buf = strtok(NULL, "/");
}
char *filepath = malloc(strlen(tok_pointer)+1);
die_if_null(filepath, "Malloc failed!");
strcpy(filepath, tok_pointer);
free(url_cpy);
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