From 402706ff9de9df0ee5c7c7c8038a2fb15cb7c999 Mon Sep 17 00:00:00 2001 From: Felix Weilbach Date: Sat, 1 May 2021 12:24:19 +0200 Subject: [PATCH] Move append_path_save in util Signed-off-by: Felix Weilbach --- common/util.c | 20 ++++++++++++++++++++ include/swaybar/icon.h | 2 -- include/util.h | 2 ++ swaybar/desktop.c | 3 ++- swaybar/icon.c | 19 +------------------ 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/common/util.c b/common/util.c index 5ea94f481..13ed712bc 100644 --- a/common/util.c +++ b/common/util.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include "log.h" #include "util.h" @@ -142,3 +144,21 @@ bool sway_set_cloexec(int fd, bool cloexec) { } return true; } + +char *append_path_safe(const char *base_path, const char *append_path) { + assert(base_path); + assert(append_path); + + size_t base_path_len = strlen(base_path); + if (base_path[base_path_len - 1] == '/') { + size_t path_len = snprintf(NULL, 0, "%s%s", base_path, append_path) + 1; + char *path = malloc(path_len); + snprintf(path, path_len, "%s%s", base_path, append_path); + return path; + } + + size_t path_len = snprintf(NULL, 0, "%s/%s", base_path, append_path) + 1; + char *path = malloc(path_len); + snprintf(path, path_len, "%s/%s", base_path, append_path); + return path; +} diff --git a/include/swaybar/icon.h b/include/swaybar/icon.h index 224e63ba2..3673674b9 100644 --- a/include/swaybar/icon.h +++ b/include/swaybar/icon.h @@ -31,8 +31,6 @@ struct icon_theme { void init_themes(list_t **themes, list_t **basedirs); void finish_themes(list_t *themes, list_t *basedirs); -char *append_path_safe(const char *base_path, const char *append_path); - /* * Finds an icon of a specified size given a list of themes and base directories. * If the icon is found, the pointers min_size & max_size are set to minimum & diff --git a/include/util.h b/include/util.h index c80da1cbc..1cb1ce18d 100644 --- a/include/util.h +++ b/include/util.h @@ -67,4 +67,6 @@ const char *sway_wl_output_subpixel_to_string(enum wl_output_subpixel subpixel); bool sway_set_cloexec(int fd, bool cloexec); +char *append_path_safe(const char *base_path, const char *append_path); + #endif diff --git a/swaybar/desktop.c b/swaybar/desktop.c index 8cd3301a2..d2303be39 100644 --- a/swaybar/desktop.c +++ b/swaybar/desktop.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 200809 +#define _POSIX_C_SOURCE 200809L #include #include #include @@ -10,6 +10,7 @@ #include "swaybar/icon.h" #include "desktop.h" #include "log.h" +#include "util.h" static list_t *get_desktop_files_basedirs() { list_t *basedirs = create_list(); diff --git a/swaybar/icon.c b/swaybar/icon.c index 2d9c72af5..ce9facac4 100644 --- a/swaybar/icon.c +++ b/swaybar/icon.c @@ -10,6 +10,7 @@ #include #include #include "swaybar/icon.h" +#include "util.h" #include "config.h" #include "list.h" #include "log.h" @@ -24,24 +25,6 @@ static bool dir_exists(char *path) { return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode); } -char* append_path_safe(const char * base_path, const char * append_path) { - assert(base_path); - assert(append_path); - - size_t base_path_len = strlen(base_path); - if (base_path[base_path_len - 1] == '/') { - size_t path_len = snprintf(NULL, 0, "%s%s", base_path, append_path) + 1; - char *path = malloc(path_len); - snprintf(path, path_len, "%s%s", base_path, append_path); - return path; - } - - size_t path_len = snprintf(NULL, 0, "%s/%s", base_path, append_path) + 1; - char *path = malloc(path_len); - snprintf(path, path_len, "%s/%s", base_path, append_path); - return path; -} - static list_t *get_basedirs(void) { list_t *basedirs = create_list(); list_add(basedirs, strdup("$HOME/.icons")); // deprecated