Move append_path_save in util

Signed-off-by: Felix Weilbach <felix.weilbach@t-online.de>
This commit is contained in:
Felix Weilbach 2021-05-01 12:24:19 +02:00
parent 59d245cf5b
commit 402706ff9d
5 changed files with 25 additions and 21 deletions

View file

@ -6,6 +6,8 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <assert.h>
#include <wayland-server-protocol.h>
#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;
}

View file

@ -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 &

View file

@ -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

View file

@ -1,4 +1,4 @@
#define _POSIX_C_SOURCE 200809
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <string.h>
#include <dirent.h>
@ -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();

View file

@ -10,6 +10,7 @@
#include <unistd.h>
#include <wordexp.h>
#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