X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fpath-util.h;h=0a42de7e27093f0041c24b78a6009dbfbf5e3241;hb=295edddf5a2d884222a26935b40a4e99b924543d;hp=03f2cf273cdd0e8de63b21560fc21e2d04e66c38;hpb=fecffe5d0a1bd66d80e5a8728ff8a89673be0df7;p=elogind.git diff --git a/src/shared/path-util.h b/src/shared/path-util.h index 03f2cf273..0a42de7e2 100644 --- a/src/shared/path-util.h +++ b/src/shared/path-util.h @@ -52,5 +52,12 @@ int path_is_os_tree(const char *path); int find_binary(const char *name, char **filename); +/* Iterates through the path prefixes of the specified path, going up + * the tree, to root. Also returns "" (and not "/"!) for the root + * directory. Excludes the specified directory itself */ #define PATH_FOREACH_PREFIX(prefix, path) \ - for (char *_slash = strrchr(path_kill_slashes(strcpy(prefix, path)), '/'); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/')) + for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); streq(prefix, "/") ? NULL : strrchr(prefix, '/'); }); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/')) + +/* Same as PATH_FOREACH_PREFIX but also includes the specified path itself */ +#define PATH_FOREACH_PREFIX_MORE(prefix, path) \ + for (char *_slash = ({ path_kill_slashes(strcpy(prefix, path)); if (streq(prefix, "/")) prefix[0] = 0; strrchr(prefix, 0); }); _slash && !(*_slash = 0); _slash = strrchr((prefix), '/'))