From 53a9bd75b4935cecd25edae72ad2f028013d8648 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 6 Apr 2024 17:51:19 +0530 Subject: meson: keep source files under ./src --- src/libnpass/util.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/libnpass/util.c (limited to 'src/libnpass/util.c') diff --git a/src/libnpass/util.c b/src/libnpass/util.c new file mode 100644 index 0000000..8e3a108 --- /dev/null +++ b/src/libnpass/util.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" + +int r_mkdir(const char *path, mode_t mode) +{ + int r; + size_t len; + char *p; + char tmp[NAME_MAX + 1]; + + strncpy(tmp, path, sizeof(tmp) - 1); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = '\0'; + + for (p = tmp + 1; *p; ++p) { + if (*p == '/') { + *p = '\0'; + + r = mkdir(tmp, mode); + if (r && !(errno & EEXIST)) + return r; + + *p = '/'; + } + } + + return mkdir(path, mode); +} + +int r_rmdir(const char *prefix_path, char *rm_path) +{ + int r; + char abs_path[PATH_MAX]; + + if (!strcmp(rm_path, ".")) + return 0; + + r = snprintf(abs_path, sizeof(abs_path), "%s/%s", prefix_path, rm_path); + if (r > (int) sizeof(abs_path)) + err_die(1, "path exceeded PATH_MAX"); + + r = rmdir(abs_path); + if (r && errno != EEXIST && errno != ENOTEMPTY) + err_die(1, "%s", strerror(errno)); + + return r_rmdir(prefix_path, dirname(rm_path)); +} + +void util_strtrim(char *s) +{ + char *rend; + + for (rend = s; *s; ++s) + if (!isspace(*s)) + rend = s; + + rend[1] = '\0'; +} -- cgit v1.2.3