mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 00:53:03 +01:00
better way to check if files exist
This commit is contained in:
parent
155e194174
commit
511907fbc5
@ -26,6 +26,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "toxic.h"
|
#include "toxic.h"
|
||||||
#include "windows.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);
|
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;
|
||||||
|
}
|
||||||
|
@ -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 */
|
/* Converts bytes to appropriate unit and puts in buf as a string */
|
||||||
void bytes_convert_str(char *buf, int size, uint64_t bytes);
|
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 */
|
#endif /* #define _misc_tools_h */
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include "windows.h"
|
#include "windows.h"
|
||||||
#include "configdir.h"
|
#include "configdir.h"
|
||||||
#include "notify.h"
|
#include "notify.h"
|
||||||
|
#include "misc_tools.h"
|
||||||
|
|
||||||
#ifdef _AUDIO
|
#ifdef _AUDIO
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
@ -222,13 +223,14 @@ int settings_load(struct user_settings *s, const char *patharg)
|
|||||||
free(user_config_dir);
|
free(user_config_dir);
|
||||||
|
|
||||||
/* make sure path exists or is created on first time running */
|
/* make sure path exists or is created on first time running */
|
||||||
FILE *fp = fopen(path, "r");
|
if (!file_exists(path)) {
|
||||||
if (fp == NULL) {
|
FILE *fp = fopen(path, "w");
|
||||||
if ((fp = fopen(path, "w")) == NULL)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(fp);
|
if (fp == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
snprintf(path, sizeof(path), "%s", patharg);
|
snprintf(path, sizeof(path), "%s", patharg);
|
||||||
}
|
}
|
||||||
|
15
src/toxic.c
15
src/toxic.c
@ -638,12 +638,9 @@ static void parse_args(int argc, char *argv[])
|
|||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
|
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");
|
queue_init_message("Config file not found");
|
||||||
else
|
|
||||||
fclose(conf_fp);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -670,12 +667,9 @@ static void parse_args(int argc, char *argv[])
|
|||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
|
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");
|
queue_init_message("DHTnodes file not found");
|
||||||
else
|
|
||||||
fclose(nodes_fp);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -696,12 +690,9 @@ static void parse_args(int argc, char *argv[])
|
|||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
snprintf(arg_opts.dns_path, sizeof(arg_opts.dns_path), "%s", optarg);
|
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");
|
queue_init_message("DNSservers file not found");
|
||||||
else
|
|
||||||
fclose(dns_fp);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user