X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=util.c;h=517c99c45ef5cbe9fab4f50a1e53a97527391392;hp=33b143d3bbcf225c9cc37df3a1206d642cbf4e9d;hb=dfcd764ea64a61280eeb3902505b58aca06111f9;hpb=a7334b0952ab66c17ee787e36e6d2c5ceb387de6 diff --git a/util.c b/util.c index 33b143d3b..517c99c45 100644 --- a/util.c +++ b/util.c @@ -30,16 +30,19 @@ #include #include #include +#include +#include #include "macro.h" #include "util.h" #include "ioprio.h" #include "missing.h" +#include "log.h" -usec_t now(clockid_t clock) { +usec_t now(clockid_t clock_id) { struct timespec ts; - assert_se(clock_gettime(clock, &ts) == 0); + assert_se(clock_gettime(clock_id, &ts) == 0); return timespec_load(&ts); } @@ -284,6 +287,22 @@ char *split_spaces(const char *c, size_t *l, char **state) { return (char*) current; } +/* Split a path into filenames. */ +char *split_slash(const char *c, size_t *l, char **state) { + char *current; + + current = *state ? *state : (char*) c; + + if (!*current || *c == 0) + return NULL; + + current += strspn(current, "/"); + *l = strcspn(current, "/"); + *state = current+*l; + + return (char*) current; +} + /* Split a string into words, but consider strings enclosed in '' and * "" as words even if they include spaces. */ char *split_quoted(const char *c, size_t *l, char **state) { @@ -574,6 +593,39 @@ char *file_in_same_dir(const char *path, const char *filename) { return r; } +int mkdir_parents(const char *path, mode_t mode) { + const char *p, *e; + + assert(path); + + /* Creates every parent directory in the path except the last + * component. */ + + p = path + strspn(path, "/"); + for (;;) { + int r; + char *t; + + e = p + strcspn(p, "/"); + p = e + strspn(e, "/"); + + /* Is this the last component? If so, then we're + * done */ + if (*p == 0) + return 0; + + if (!(t = strndup(path, e - path))) + return -ENOMEM; + + r = mkdir(t, mode); + + free(t); + + if (r < 0 && errno != EEXIST) + return -errno; + } +} + char hexchar(int x) { static const char table[16] = "0123456789abcdef"; @@ -824,11 +876,11 @@ char *xescape(const char *s, const char *bad) { } char *bus_path_escape(const char *s) { - assert(s); - char *r, *t; const char *f; + assert(s); + /* Escapes all chars that D-Bus' object path cannot deal * with. Can be reverse with bus_path_unescape() */ @@ -853,11 +905,11 @@ char *bus_path_escape(const char *s) { } char *bus_path_unescape(const char *s) { - assert(s); - char *r, *t; const char *f; + assert(s); + if (!(r = new(char, strlen(s)+1))) return NULL;