better die func

parent d4e18be0
......@@ -7,7 +7,7 @@
#include <unistd.h>
void usage();
void die(const char *);
void die(const char *format, ...);
char *get_default_filepath(char *, char *);
void download_file(char *url, char *filepath, CURL *curl);
int file_exists(const char *filename);
......@@ -27,6 +27,7 @@ main(int argc, char** argv)
char *url_file_path = NULL;
char *filepaths_file_path = NULL;
char *place_holder = NULL; // for get_default_filepath func
//get args
if(argc == 1)
usage();
......@@ -83,9 +84,9 @@ main(int argc, char** argv)
url_file = fopen(url_file_path, "r");
if(!url_file)
die("Could not open given url file!");
die("Could not open given url file %s!", url_file_path);
while(getline(&curr_url, &url_len, url_file) != -1){
while(getline(&curr_url, &url_len, url_file) != -1){
if (curr_url[strlen(curr_url) - 1] == '\n')
curr_url[strlen(curr_url) - 1] = '\0';
......@@ -101,9 +102,9 @@ main(int argc, char** argv)
filepaths_file = fopen(filepaths_file_path, "r");
if(!url_file)
die("Could not open given url file!");
die("Could not open given url file %s!", url_file_path);
if(!filepaths_file)
die("Could not open given filepaths file!");
die("Could not open given filepath file %s!", filepaths_file_path);
while(getline(&curr_url, &filepath_len, url_file) != -1){
......@@ -119,6 +120,7 @@ main(int argc, char** argv)
}
free(place_holder);
free(curr_url);
free(curr_filepath);
fclose(url_file);
fclose(filepaths_file);
break;
......@@ -126,12 +128,14 @@ main(int argc, char** argv)
curl_easy_cleanup(curl);
return 0;
}
void
die(const char * error)
{
fprintf(stderr, "Error: %s\n", error);
exit(EXIT_FAILURE);
void
die(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
void
......@@ -173,7 +177,6 @@ get_default_filepath(char *url, char *place_holder)
filepath = tmp;
tmp = strtok(NULL, "/");
}
// TODO: Move memory allocation and deallocation logic to the caller function for better resource management.
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