X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=util.c;h=bf74695854932a79f93d629c8f79a0664a144358;hb=451a074fd589fd90aff91fcf89b2e599b20ae6a0;hp=4847310aee57d084d5009c0cf0132002ee260e8b;hpb=79d6d8160834fdfc964f4b8b2a6b7fb32c7b36c9;p=elogind.git diff --git a/util.c b/util.c index 4847310ae..bf7469585 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,24 @@ /*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + #include #include #include @@ -10,16 +29,17 @@ #include #include #include +#include #include "macro.h" #include "util.h" #include "ioprio.h" #include "missing.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); } @@ -566,10 +586,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; } @@ -803,6 +823,66 @@ char *xescape(const char *s, const char *bad) { return r; } +char *bus_path_escape(const char *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() */ + + 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) { + char *r, *t; + const char *f; + + assert(s); + + 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;