get_default_filepath refactoring

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