better die func

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