commit bc94d15c47fa0d0c2e55b0857280173f2a96d108
parent 2fca1fe364fd3a7d736be9324a353c7eb84325e1
Author: hhvn <dev@hhvn.uk>
Date: Sun, 8 May 2022 11:19:39 +0100
Die if the config file in argv cannot be read
Diffstat:
3 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/src/config.c b/src/config.c
@@ -239,17 +239,18 @@ inval:
goto end;
}
-void
+int
config_read(char *filename) {
static char **bt = NULL;
static int btoffset = 0;
+ int ret = 0, serrno;
char buf[8192];
char *path;
FILE *file;
int save, i;
if (!filename)
- return;
+ return -2;
path = realpath(filename, NULL);
@@ -259,7 +260,7 @@ config_read(char *filename) {
if (strcmp_n(path, *(bt + i)) == 0) {
ui_error("recursive read of '%s' is not allowed", filename);
pfree(&path);
- return;
+ return -2;
}
}
}
@@ -275,6 +276,8 @@ config_read(char *filename) {
/* Read and execute */
if ((file = fopen(filename, "rb")) == NULL) {
+ serrno = errno;
+ ret = -1;
ui_error("cannot open file '%s': %s", filename, strerror(errno));
goto shrink;
}
@@ -300,6 +303,9 @@ shrink:
bt = erealloc(bt, (sizeof(char *)) * btoffset);
assert(bt != NULL);
}
+
+ errno = serrno;
+ return ret;
}
static int
diff --git a/src/hirc.h b/src/hirc.h
@@ -41,6 +41,7 @@ size_t wcslcpy(wchar_t *, const wchar_t *, size_t);
#endif /* HIST_WCSLCPY */
/* main.c */
+void die(int code, char *format, ...);
void cleanup(char *quitmsg);
/* params.c */
@@ -200,7 +201,7 @@ void config_set(char *name, char *str);
void config_setl(struct Config *conf, long num);
void config_sets(struct Config *conf, char *str);
void config_setr(struct Config *conf, long a, long b);
-void config_read(char *filename);
+int config_read(char *filename);
/* complete.c */
void complete(wchar_t *str, size_t size, unsigned *counter);
diff --git a/src/main.c b/src/main.c
@@ -35,6 +35,25 @@ struct Server *servers = NULL;
struct HistInfo *main_buf;
void
+die(int code, char *format, ...) {
+ static int dying = 0;
+ va_list ap;
+
+ /* prevent loop if a function in cleanup() calls die() */
+ if (!dying) {
+ dying = 1;
+ cleanup("Client error");
+ dying = 0;
+
+ fprintf(stderr, "Fatal: ");
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+ exit(code);
+ }
+}
+
+void
cleanup(char *quitmsg) {
struct Server *sp, *prev;
@@ -96,7 +115,8 @@ main(int argc, char *argv[]) {
ui_init();
if (argc == 2)
- config_read(argv[1]);
+ if (config_read(argv[1]) == -1)
+ die(1, "cannot read config file '%s': %s\n", argv[1], strerror(errno));
for (;;) {
/* 25 seems fast enough not to cause any visual lag */