1k lines commit

parent 69b53949
...@@ -8,17 +8,13 @@ ...@@ -8,17 +8,13 @@
#include <unistd.h> #include <unistd.h>
void usage(); void usage();
void die(const char *format, ...); void die(const char *format, va_list args);
char *get_default_filepath(char *, char *); char *get_default_filepath(char *, 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 *filepath); void download_result_message(CURLcode res,char *url, char *filepath);
void drop_newline(char *); void drop_newline(char *);
void die_if_null(void *obj_ptr, const char *format, ...);
enum Download_types{
URL_ARG,
URL_FILE,
};
int int
main(int argc, char** argv) main(int argc, char** argv)
...@@ -29,14 +25,14 @@ main(int argc, char** argv) ...@@ -29,14 +25,14 @@ 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 int show_progressbar = 0;
//get args //get args
if(argc == 1) if(argc == 1 || !(strcmp(argv[1], "-h") != 0))
usage(); usage();
else if(argc == 2){ else if(argc == 2){
url = argv[1]; url = argv[1];
filepath = get_default_filepath(url, place_holder); filepath = strchr(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"))
...@@ -47,6 +43,10 @@ main(int argc, char** argv) ...@@ -47,6 +43,10 @@ main(int argc, char** argv)
url_file_path = argv[++i]; url_file_path = argv[++i];
else if(!strcmp(argv[i], "-ff")) else if(!strcmp(argv[i], "-ff"))
filepaths_file_path = argv[++i]; filepaths_file_path = argv[++i];
else if(!strcmp(argv[i], "-h"))
usage();
else if(!strcmp(argv[i], "-p"))
show_progressbar = 1;
else{ else{
usage(); usage();
} }
...@@ -55,20 +55,10 @@ main(int argc, char** argv) ...@@ -55,20 +55,10 @@ main(int argc, char** argv)
printf("URL: %s\nFilepath: %s\nURL File: %s\nFilepaths File: %s\n", url, filepath, url_file_path, filepaths_file_path); printf("URL: %s\nFilepath: %s\nURL File: %s\nFilepaths File: %s\n", url, filepath, url_file_path, filepaths_file_path);
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
if(!curl) die_if_null(curl, "Curl init failed!");
die("Curl init failed!");
enum Download_types download_type; if(show_progressbar == 1)
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
if (url != NULL && filepath != NULL && url_file_path == NULL && filepaths_file_path == NULL){ // only 1 file
download_type = URL_ARG;
}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
download_type = URL_FILE;
}else{
puts("Unsupported arguments combination given!");
usage();
}
FILE *url_file = NULL; FILE *url_file = NULL;
FILE *filepaths_file = NULL; FILE *filepaths_file = NULL;
...@@ -81,66 +71,73 @@ main(int argc, char** argv) ...@@ -81,66 +71,73 @@ main(int argc, char** argv)
CURLcode result; CURLcode result;
switch(download_type){ if (url != NULL && filepath != NULL && url_file_path == NULL && filepaths_file_path == NULL){ // only 1 file
case URL_ARG: //fprintf(stdout, "Starting %s\n", filepath);
result = download_file(url, filepath, curl); result = download_file(url, filepath, curl);
download_result_message(result, filepath); download_result_message(result, url, filepath);
break; }else if((url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path == NULL) || // -uf
case URL_FILE: (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)
filepaths_file = fopen(filepaths_file_path, "r"); filepaths_file = fopen(filepaths_file_path, "r");
die_if_null(url_file, "Could not open given url file %s!", url_file_path);
if(!url_file) if(filepaths_file_path)
die("Could not open given url file %s!", url_file_path); die_if_null(filepaths_file,"Could not open given file file %s!", filepaths_file_path);
if(filepaths_file_path && !filepaths_file)
die("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
if (filepaths_file_path != NULL) { if (filepaths_file_path != NULL && getline(&curr_filepath, &filepath_len, filepaths_file) != -1) {
// Try to read a filepath from the filepaths file // Try to read a filepath from the filepaths file
if(getline(&curr_filepath, &filepath_len, filepaths_file) != -1) {
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 none filepath file given }else
}else{ curr_filepath = strchr(curr_url, '/');
curr_filepath = get_default_filepath(curr_url, place_holder);
} //fprintf(stdout, "Starting %s\n", filepath);
result = download_file(curr_url, curr_filepath, curl); result = download_file(curr_url, curr_filepath, curl);
download_result_message(result, curr_filepath); download_result_message(result, curr_url, curr_filepath);
} }
free(curr_url); free(curr_url);
if (filepaths_file_path != NULL) {
free(curr_filepath); free(curr_filepath);
if (filepaths_file_path != NULL)
fclose(filepaths_file); fclose(filepaths_file);
}else
free(place_holder);
fclose(url_file); fclose(url_file);
break; }else{
puts("Unsupported arguments combination given!");
usage();
} }
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
return 0; return 0;
} }
void void
die(const char *format, ...) { die(const char *format, va_list args) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
va_end(args);
fputc('\n', stderr); fputc('\n', stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void void
download_result_message(CURLcode res, char *filepath) die_if_null(void *obj_ptr, const char *format, ...) {
va_list args;
va_start(args, format);
if(obj_ptr == NULL)
die(format, args);
va_end(args);
}
void
download_result_message(CURLcode res, char *url, char *filepath)
{ {
if(res != CURLE_OK) if(res != CURLE_OK)
fprintf(stderr, "Failed: %s\n to download %s", curl_easy_strerror(res), filepath); fprintf(stderr, "Failed: %s\n to download %s from %s!\n", curl_easy_strerror(res), filepath, url);
else else
fprintf(stdout, "%s; Downloaded %s \n", curl_easy_strerror(res), filepath); fprintf(stdout, "%s; Downloaded %s \n", curl_easy_strerror(res), filepath);
} }
...@@ -150,17 +147,17 @@ download_file(char *url, char *filepath, CURL *curl) ...@@ -150,17 +147,17 @@ download_file(char *url, char *filepath, CURL *curl)
{ {
FILE *file; FILE *file;
file = fopen(filepath,"wb"); file = fopen(filepath,"wb");
if(file == NULL){
if(!file){ fprintf(stderr, "Error: Could not open file for writing: %s\n", filepath);
die("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);
fprintf(stdout, "Starting %s\n", filepath);
fclose(file); fclose(file);
return res; return res;
...@@ -170,14 +167,14 @@ char* ...@@ -170,14 +167,14 @@ char*
get_default_filepath(char *url, char *place_holder) get_default_filepath(char *url, char *place_holder)
{ {
char *url_copy = realloc(place_holder, strlen(url) + 1); char *url_copy = realloc(place_holder, strlen(url) + 1);
if (!url_copy) {
die("Malloc failed!"); die_if_null(url_copy, "Malloc failed!");
}
strcpy(url_copy, url); strcpy(url_copy, url);
char *filepath = url_copy; char *filepath = url_copy;
char *tmp = strtok(filepath, "/"); char *tmp = strtok(filepath, "/");
while(tmp != NULL){ while(tmp != NULL){
filepath = tmp; filepath = tmp;
tmp = strtok(NULL, "/"); tmp = strtok(NULL, "/");
...@@ -193,8 +190,10 @@ usage() ...@@ -193,8 +190,10 @@ usage()
"\t-f <filepath> to save file to. Can be omitted; filename from URL will be used.\n" "\t-f <filepath> to save file to. Can be omitted; filename from URL will be used.\n"
"\t-uf <filepath> file with URLs to fetch files from.\n" "\t-uf <filepath> file with URLs to fetch files from.\n"
"\t-ff <filepath> file with paths to save files from URLs file. Can be omitted; filename from URL will be used.\n" "\t-ff <filepath> file with paths to save files from URLs file. Can be omitted; filename from URL will be used.\n"
"\t-h print this.\n"
"\t-p enable default curl progressbar.\n"
"At least one of the -u arguments should be given.\n" "At least one of the -u arguments should be given.\n"
"Supported combinations:\n" "Supported combinations for url/filepath options:\n"
"\t<url>\n" "\t<url>\n"
"\t-u <url>\n" "\t-u <url>\n"
"\t-u <url> -f <path to save file to>\n" "\t-u <url> -f <path to save file to>\n"
......
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