From 511907fbc5bde8e67f1d2bcd7f1d93d8b29d7edd Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 27 Aug 2014 22:45:11 -0400 Subject: [PATCH] better way to check if files exist --- src/misc_tools.c | 8 ++++++++ src/misc_tools.h | 3 +++ src/settings.c | 14 ++++++++------ src/toxic.c | 15 +++------------ 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/misc_tools.c b/src/misc_tools.c index 83fceff..3817854 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "toxic.h" #include "windows.h" @@ -303,3 +304,10 @@ void bytes_convert_str(char *buf, int size, uint64_t bytes) snprintf(buf, size, "%.1f %s", conv, unit); } + +/* checks if a file exists. Returns true or false */ +bool file_exists(const char *fp) +{ + struct stat s; + return stat(fp, &s) == 0; +} diff --git a/src/misc_tools.h b/src/misc_tools.h index 444943b..ab7ddbd 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -109,4 +109,7 @@ int char_rfind(const char *s, char ch, int len); /* Converts bytes to appropriate unit and puts in buf as a string */ void bytes_convert_str(char *buf, int size, uint64_t bytes); +/* checks if a file exists. Returns true or false */ +bool file_exists(const char *fp); + #endif /* #define _misc_tools_h */ diff --git a/src/settings.c b/src/settings.c index 7d99cc2..7f898f2 100644 --- a/src/settings.c +++ b/src/settings.c @@ -29,6 +29,7 @@ #include "windows.h" #include "configdir.h" #include "notify.h" +#include "misc_tools.h" #ifdef _AUDIO #include "device.h" @@ -222,13 +223,14 @@ int settings_load(struct user_settings *s, const char *patharg) free(user_config_dir); /* make sure path exists or is created on first time running */ - FILE *fp = fopen(path, "r"); - if (fp == NULL) { - if ((fp = fopen(path, "w")) == NULL) - return -1; - } + if (!file_exists(path)) { + FILE *fp = fopen(path, "w"); - fclose(fp); + if (fp == NULL) + return -1; + + fclose(fp); + } } else { snprintf(path, sizeof(path), "%s", patharg); } diff --git a/src/toxic.c b/src/toxic.c index 73b4563..35c59fb 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -638,12 +638,9 @@ static void parse_args(int argc, char *argv[]) case 'c': snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg); - FILE *conf_fp = fopen(arg_opts.config_path, "r"); - if (conf_fp == NULL) + if (!file_exists(arg_opts.config_path)) queue_init_message("Config file not found"); - else - fclose(conf_fp); break; @@ -670,12 +667,9 @@ static void parse_args(int argc, char *argv[]) case 'n': snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); - FILE *nodes_fp = fopen(arg_opts.nodes_path, "r"); - if (nodes_fp == NULL) + if (!file_exists(arg_opts.nodes_path)) queue_init_message("DHTnodes file not found"); - else - fclose(nodes_fp); break; @@ -696,12 +690,9 @@ static void parse_args(int argc, char *argv[]) case 'r': snprintf(arg_opts.dns_path, sizeof(arg_opts.dns_path), "%s", optarg); - FILE *dns_fp = fopen(arg_opts.dns_path, "r"); - if (dns_fp == NULL) + if (!file_exists(arg_opts.dns_path)) queue_init_message("DNSservers file not found"); - else - fclose(dns_fp); break;