From f8e6d758cd65b28f94b62344bd95e7bab94370d4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 31 Jul 2013 17:48:47 -0400 Subject: add various escaping/path handling utility functions A portion of systemd commit 4fe88d28a4cfa504c1f2362d4a7030fae4c4af29 , needed to support changes made by commit 6284c1ca41dca85e968e8d304cdc154c5a46d6d2 Signed-off-by: Ian Stakenvicius --- src/libudev/util.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/libudev/util.c') diff --git a/src/libudev/util.c b/src/libudev/util.c index f625cfe99b..8fec6856e0 100644 --- a/src/libudev/util.c +++ b/src/libudev/util.c @@ -1046,3 +1046,31 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, return NULL; } +char *xescape(const char *s, const char *bad) { + char *r, *t; + const char *f; + + /* Escapes all chars in bad, in addition to \ and all special + * chars, in \xFF style escaping. May be reversed with + * cunescape. */ + + if (!(r = new(char, strlen(s)*4+1))) + return NULL; + + for (f = s, t = r; *f; f++) { + + if (*f < ' ' || *f >= 127 || + *f == '\\' || strchr(bad, *f)) { + *(t++) = '\\'; + *(t++) = 'x'; + *(t++) = hexchar(*f >> 4); + *(t++) = hexchar(*f); + } else + *(t++) = *f; + } + + *t = 0; + + return r; +} + -- cgit v1.2.3-54-g00ecf