1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 17:07:46 +02:00

better way to check if files exist

This commit is contained in:
Jfreegman 2014-08-27 22:45:11 -04:00
parent 155e194174
commit 511907fbc5
4 changed files with 22 additions and 18 deletions

View File

@ -26,6 +26,7 @@
#include <time.h>
#include <limits.h>
#include <dirent.h>
#include <sys/stat.h>
#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;
}

View File

@ -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 */

View File

@ -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);
}

View File

@ -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;