1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-08 20:58:20 +03:00

path-util: Add path_make_relative_cwd()

This commit is contained in:
Daan De Meyer 2022-09-26 13:06:47 +02:00
parent e624d4cf07
commit 15b4b4ed06
2 changed files with 28 additions and 0 deletions

View File

@ -221,6 +221,33 @@ int path_make_relative_parent(const char *from_child, const char *to, char **ret
return path_make_relative(from, to, ret);
}
int path_make_relative_cwd(const char *p, char **ret) {
char *c;
int r;
assert(p);
assert(ret);
if (path_is_absolute(p)) {
_cleanup_free_ char *cwd = NULL;
r = safe_getcwd(&cwd);
if (r < 0)
return r;
r = path_make_relative(cwd, p, &c);
if (r < 0)
return r;
} else {
c = strdup(p);
if (!c)
return -ENOMEM;
}
*ret = TAKE_PTR(c);
return 0;
}
char* path_startswith_strv(const char *p, char **set) {
STRV_FOREACH(s, set) {
char *t;

View File

@ -62,6 +62,7 @@ int safe_getcwd(char **ret);
int path_make_absolute_cwd(const char *p, char **ret);
int path_make_relative(const char *from, const char *to, char **ret);
int path_make_relative_parent(const char *from_child, const char *to, char **ret);
int path_make_relative_cwd(const char *from, char **ret);
char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) _pure_;
static inline char* path_startswith(const char *path, const char *prefix) {
return path_startswith_full(path, prefix, true);