X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Futil.c;h=78d8d5d9c5d99fd1a91d72858fda6d17a09407fe;hb=d0b170c8133d0c155b18aabef919693dcba406dd;hp=2363ea27b2b87766a459aa31522f7a71a968874d;hpb=eb22ac37f3e07b9c49a3f8fdc8cc02631faabcb4;p=elogind.git diff --git a/src/util.c b/src/util.c index 2363ea27b..78d8d5d9c 100644 --- a/src/util.c +++ b/src/util.c @@ -893,6 +893,53 @@ int mkdir_p(const char *path, mode_t mode) { return 0; } +int rmdir_parents(const char *path, const char *stop) { + size_t l; + int r = 0; + + assert(path); + assert(stop); + + l = strlen(path); + + /* Skip trailing slashes */ + while (l > 0 && path[l-1] == '/') + l--; + + while (l > 0) { + char *t; + + /* Skip last component */ + while (l > 0 && path[l-1] != '/') + l--; + + /* Skip trailing slashes */ + while (l > 0 && path[l-1] == '/') + l--; + + if (l <= 0) + break; + + if (!(t = strndup(path, l))) + return -ENOMEM; + + if (path_startswith(stop, t)) { + free(t); + return 0; + } + + r = rmdir(t); + free(t); + + if (r < 0) + if (errno != ENOENT) + return -errno; + } + + return 0; +} + + char hexchar(int x) { static const char table[16] = "0123456789abcdef";