diff options
Diffstat (limited to 'shared/utils.h')
-rw-r--r-- | shared/utils.h | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/shared/utils.h b/shared/utils.h index 1291d21..b6bf51c 100644 --- a/shared/utils.h +++ b/shared/utils.h @@ -8,9 +8,11 @@ #include <stdlib.h> #include <linux/if_packet.h> #include <sys/socket.h> +#include <sys/types.h> #include <netinet/in.h> #include <netinet/ip.h> #include <unistd.h> +#include <dirent.h> extern unsigned debug_mask; @@ -42,6 +44,7 @@ extern unsigned debug_mask; #include "list.h" #include "debug.h" #include "external.h" +#include "config.h" #include "ansi-colors.h" /* Length of longest DNS name = 253 + trailing dot */ @@ -68,6 +71,37 @@ struct saddr { struct list_head list; }; +int open_subdir(int dfd, const char *subdir, bool nofail); + +int open_xdg_dir(const char *envname, const char *altpath, bool nofail, + char **rpath); + +static inline int open_xdg_data_dir(bool nofail, char **rpath) +{ + return open_xdg_dir("XDG_DATA_HOME", ".local/share", nofail, rpath); +} + +static inline int open_xdg_cfg_dir(bool nofail, char **rpath) +{ + return open_xdg_dir("XDG_CONFIG_HOME", ".config", nofail, rpath); +} + +DIR *__open_dir(const char *user_override, + int (*base_dir)(bool nofail, char **rpath), + const char *fallback, char **rpath); + +static inline DIR *open_cfg_dir(const char *user_override, char **rpath) +{ + return __open_dir(user_override, open_xdg_cfg_dir, DEFAULT_CFG_DIR, + rpath); +} + +static inline DIR *open_data_dir(const char *user_override, char **rpath) +{ + return __open_dir(user_override, open_xdg_data_dir, DEFAULT_DATA_DIR, + rpath); +} + void enable_colors(); void free_password(char **password); @@ -91,6 +125,8 @@ int strtou16_strict(const char *str, uint16_t *result); char *xsprintf(size_t *rlen, const char *fmt, ...) _printf_(2, 3); +char *xstrcat(const char *a, ...); + static inline bool empty_str(const char *str) { if (!str || str[0] == '\0') @@ -136,7 +172,7 @@ static inline bool strcaseeq(const char *a, const char *b) static inline void closep(int *fd) { - if (*fd && *fd >= 0) + if (fd && *fd >= 0) close(*fd); } #define _cleanup_close_ _cleanup_(closep) @@ -146,4 +182,10 @@ static inline void freep(void *p) { } #define _cleanup_free_ _cleanup_(freep) +static inline void fclosep(FILE **f) { + if (f && *f) + fclose(*f); +} +#define _cleanup_fclose_ _cleanup_(fclosep) + #endif |