X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Futil.c;h=766aa02965825651481d6bdf4146372a72f50def;hb=f872ec33973b2f508d36939e917ffafd47a87b07;hp=2363ea27b2b87766a459aa31522f7a71a968874d;hpb=eb22ac37f3e07b9c49a3f8fdc8cc02631faabcb4;p=elogind.git diff --git a/src/util.c b/src/util.c index 2363ea27b..766aa0296 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"; @@ -1452,7 +1499,7 @@ char *format_timestamp(char *buf, size_t l, usec_t t) { if (t <= 0) return NULL; - sec = (time_t) t / USEC_PER_SEC; + sec = (time_t) (t / USEC_PER_SEC); if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) return NULL;