X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=util.c;h=b54c24a3dcd9efaecbe70ddb39efe8129b3eabd7;hp=3ce506b0c64f6265e173ab4af3dbf10899300835;hb=ef886c6ab86436f0d8b5128799a8f801c62d859a;hpb=1dccbe197cc480c1f161f967d180cbc3cc3d2d66 diff --git a/util.c b/util.c index 3ce506b0c..b54c24a3d 100644 --- a/util.c +++ b/util.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "macro.h" #include "util.h" @@ -88,6 +89,25 @@ bool startswith(const char *s, const char *prefix) { return memcmp(s, prefix, pl) == 0; } +bool first_word(const char *s, const char *word) { + size_t sl, wl; + + assert(s); + assert(word); + + sl = strlen(s); + wl = strlen(word); + + if (sl < wl) + return false; + + if (memcmp(s, word, wl) != 0) + return false; + + return (s[wl] == 0 || + strchr(WHITESPACE, s[wl])); +} + int close_nointr(int fd) { assert(fd >= 0); @@ -547,10 +567,10 @@ int unhexchar(char c) { return c - '0'; if (c >= 'a' && c <= 'f') - return c - 'a'; + return c - 'a' + 10; if (c >= 'A' && c <= 'F') - return c - 'A'; + return c - 'A' + 10; return -1; } @@ -784,6 +804,66 @@ char *xescape(const char *s, const char *bad) { return r; } +char *bus_path_escape(const char *s) { + assert(s); + + char *r, *t; + const char *f; + + /* Escapes all chars that D-Bus' object path cannot deal + * with. Can be reverse with bus_path_unescape() */ + + if (!(r = new(char, strlen(s)*3+1))) + return NULL; + + for (f = s, t = r; *f; f++) { + + if (!(*f >= 'A' && *f <= 'Z') && + !(*f >= 'a' && *f <= 'z') && + !(*f >= '0' && *f <= '9')) { + *(t++) = '_'; + *(t++) = hexchar(*f >> 4); + *(t++) = hexchar(*f); + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + +char *bus_path_unescape(const char *s) { + assert(s); + + char *r, *t; + const char *f; + + if (!(r = new(char, strlen(s)+1))) + return NULL; + + for (f = s, t = r; *f; f++) { + + if (*f == '_') { + int a, b; + + if ((a = unhexchar(f[1])) < 0 || + (b = unhexchar(f[2])) < 0) { + /* Invalid escape code, let's take it literal then */ + *(t++) = '_'; + } else { + *(t++) = (char) ((a << 4) | b); + f += 2; + } + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + char *path_kill_slashes(char *path) { char *f, *t; bool slash = false;