Use a static buffer for base directory paths

master
C. McEnroe 2020-08-21 16:47:54 -04:00
parent 1abeece988
commit 8190d76086
3 changed files with 34 additions and 41 deletions

8
chat.h
View File

@ -294,12 +294,8 @@ void logFormat(uint id, const time_t *time, const char *format, ...)
__attribute__((format(printf, 3, 4))); __attribute__((format(printf, 3, 4)));
void logClose(void); void logClose(void);
const char *configPath( const char *configPath(const char **dirs, const char *path);
char *buf, size_t cap, const char **dirs, const char *path const char *dataPath(const char **dirs, const char *path);
);
const char *dataPath(
char *buf, size_t cap, const char **dirs, const char *path
);
FILE *configOpen(const char *path, const char *mode); FILE *configOpen(const char *path, const char *mode);
FILE *dataOpen(const char *path, const char *mode); FILE *dataOpen(const char *path, const char *mode);
void dataMkdir(const char *path); void dataMkdir(const char *path);

12
irc.c
View File

@ -27,7 +27,6 @@
#include <assert.h> #include <assert.h>
#include <err.h> #include <err.h>
#include <limits.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdarg.h> #include <stdarg.h>
@ -61,12 +60,9 @@ void ircConfig(bool insecure, const char *cert, const char *priv) {
tls_config_insecure_noverifyname(config); tls_config_insecure_noverifyname(config);
} }
const char *path;
const char *dirs;
char buf[PATH_MAX];
if (cert) { if (cert) {
dirs = NULL; const char *dirs = NULL;
while (NULL != (path = configPath(buf, sizeof(buf), &dirs, cert))) { for (const char *path; NULL != (path = configPath(&dirs, cert));) {
if (priv) { if (priv) {
error = tls_config_set_cert_file(config, path); error = tls_config_set_cert_file(config, path);
} else { } else {
@ -77,8 +73,8 @@ void ircConfig(bool insecure, const char *cert, const char *priv) {
if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config)); if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config));
} }
if (priv) { if (priv) {
dirs = NULL; const char *dirs = NULL;
while (NULL != (path = configPath(buf, sizeof(buf), &dirs, priv))) { for (const char *path; NULL != (path = configPath(&dirs, priv));) {
error = tls_config_set_key_file(config, path); error = tls_config_set_key_file(config, path);
if (!error) break; if (!error) break;
} }

55
xdg.c
View File

@ -58,14 +58,14 @@ static const struct Base Data = {
.defDirs = "/usr/local/share:/usr/share", .defDirs = "/usr/local/share:/usr/share",
}; };
static const char *basePath( static const char *
struct Base base, basePath(struct Base base, const char **dirs, const char *path) {
char *buf, size_t cap, const char **dirs, const char *path static char buf[PATH_MAX];
) {
if (*dirs) { if (*dirs) {
if (!**dirs) return NULL; if (!**dirs) return NULL;
size_t len = strcspn(*dirs, ":"); size_t len = strcspn(*dirs, ":");
snprintf(buf, cap, "%.*s/" SUBDIR "/%s", (int)len, *dirs, path); snprintf(buf, sizeof(buf), "%.*s/" SUBDIR "/%s", (int)len, *dirs, path);
*dirs += len; *dirs += len;
if (**dirs) *dirs += 1; if (**dirs) *dirs += 1;
return buf; return buf;
@ -82,29 +82,30 @@ static const char *basePath(
const char *home = getenv("HOME"); const char *home = getenv("HOME");
const char *baseHome = getenv(base.envHome); const char *baseHome = getenv(base.envHome);
if (baseHome) { if (baseHome) {
snprintf(buf, cap, "%s/" SUBDIR "/%s", baseHome, path); snprintf(buf, sizeof(buf), "%s/" SUBDIR "/%s", baseHome, path);
} else if (home) {
snprintf(
buf, sizeof(buf), "%s/%s/" SUBDIR "/%s",
home, base.defHome, path
);
} else { } else {
if (!home) return NULL; return NULL;
snprintf(buf, cap, "%s/%s/" SUBDIR "/%s", home, base.defHome, path);
} }
return buf; return buf;
} }
const char * const char *configPath(const char **dirs, const char *path) {
configPath(char *buf, size_t cap, const char **dirs, const char *path) { return basePath(Config, dirs, path);
return basePath(Config, buf, cap, dirs, path);
} }
const char * const char *
dataPath(char *buf, size_t cap, const char **dirs, const char *path) { dataPath(const char **dirs, const char *path) {
return basePath(Data, buf, cap, dirs, path); return basePath(Data, dirs, path);
} }
FILE *configOpen(const char *path, const char *mode) { FILE *configOpen(const char *path, const char *mode) {
const char *abs;
char buf[PATH_MAX];
const char *dirs = NULL; const char *dirs = NULL;
while (NULL != (abs = configPath(buf, sizeof(buf), &dirs, path))) { for (const char *abs; NULL != (abs = configPath(&dirs, path));) {
FILE *file = fopen(abs, mode); FILE *file = fopen(abs, mode);
if (file) return file; if (file) return file;
if (errno != ENOENT) warn("%s", abs); if (errno != ENOENT) warn("%s", abs);
@ -115,31 +116,31 @@ FILE *configOpen(const char *path, const char *mode) {
} }
void dataMkdir(const char *path) { void dataMkdir(const char *path) {
char buf[PATH_MAX];
const char *dirs = NULL; const char *dirs = NULL;
const char *abs = dataPath(buf, sizeof(buf), &dirs, path); const char *abs = dataPath(&dirs, path);
if (!abs) return;
int error = mkdir(abs, S_IRWXU); int error = mkdir(abs, S_IRWXU);
if (error && errno != EEXIST) warn("%s", abs); if (error && errno != EEXIST) warn("%s", abs);
} }
FILE *dataOpen(const char *path, const char *mode) { FILE *dataOpen(const char *path, const char *mode) {
const char *abs;
char buf[PATH_MAX];
const char *dirs = NULL; const char *dirs = NULL;
while (NULL != (abs = dataPath(buf, sizeof(buf), &dirs, path))) { for (const char *abs; NULL != (abs = dataPath(&dirs, path));) {
FILE *file = fopen(abs, mode); FILE *file = fopen(abs, mode);
if (file) return file; if (file) return file;
if (errno != ENOENT) warn("%s", abs); if (errno != ENOENT) warn("%s", abs);
} }
if (mode[0] != 'r') { if (mode[0] != 'r') {
dirs = NULL;
abs = dataPath(buf, sizeof(buf), &dirs, path);
if (!abs) return NULL;
dataMkdir(""); dataMkdir("");
FILE *file = fopen(abs, mode); dirs = NULL;
if (!file) warn("%s", abs); path = dataPath(&dirs, path);
if (!path) {
warn("HOME unset");
return NULL;
}
FILE *file = fopen(path, mode);
if (!file) warn("%s", path);
return file; return file;
} }