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,7 +84,7 @@ 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){
......@@ -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,11 +128,13 @@ main(int argc, char** argv)
curl_easy_cleanup(curl);
return 0;
}
void
die(const char * error)
{
fprintf(stderr, "Error: %s\n", error);
die(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
......@@ -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