Provide str_usrconfig()

This commit is contained in:
stefanrueger
2026-08-01 00:10:40 +02:00
parent 5a20f08289
commit d0160e4dec
2 changed files with 44 additions and 0 deletions

View File

@@ -1783,6 +1783,7 @@ extern "C" {
int pgmid_is(const char *str);
char *mmt_realpath(const char *);
char *str_sysconfig(void);
char *str_usrconfig(void);
int led_set(const PROGRAMMER *pgm, int led);
int led_clr(const PROGRAMMER *pgm, int led);

View File

@@ -28,6 +28,7 @@
#include <ctype.h>
#include <math.h>
#include <unistd.h>
#include <sys/stat.h>
#include "avrdude.h"
#include "libavrdude.h"
@@ -1784,3 +1785,45 @@ char *str_sysconfig(void) {
return mmt_realpath(CONFIG_DIR "/" SYSTEM_CONF_FILE);
#endif
}
#if !defined(WIN32)
// Safely concatenate dir/file into dst that has size n
static char *concatpath(char *dst, char *dir, char *file, size_t n) {
// Dir or file empty?
if(!dir || !*dir || !file || !*file)
return NULL;
size_t len = strlen(dir);
// Insufficient space?
if(len + (dir[len - 1] != '/') + strlen(file) > n - 1)
return NULL;
if(dst != dir)
strcpy(dst, dir);
if(dst[len - 1] != '/')
strcat(dst, "/");
strcat(dst, file);
return dst;
}
#endif
// Return mmt_malloc'd location of personal configuration file
char *str_usrconfig(void) {
char config[PATH_MAX] = { 0 };
struct stat sb;
#if defined(WIN32)
win_set_path(config, sizeof config, USER_CONF_FILE);
#else
if(!concatpath(config, getenv("XDG_CONFIG_HOME"), XDG_USER_CONF_FILE, sizeof config))
concatpath(config, getenv("HOME"), ".config/" XDG_USER_CONF_FILE, sizeof config);
if(stat(config, &sb) < 0 || (sb.st_mode & S_IFREG) == 0)
concatpath(config, getenv("HOME"), USER_CONF_FILE, sizeof config);
#endif
return mmt_realpath(config);
}