diff options
-rw-r--r-- | src/libudev/util.c | 28 | ||||
-rw-r--r-- | src/libudev/util.h | 2 |
2 files changed, 30 insertions, 0 deletions
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; +} + diff --git a/src/libudev/util.h b/src/libudev/util.h index 60b300da3c..96a42bf3d3 100644 --- a/src/libudev/util.h +++ b/src/libudev/util.h @@ -272,6 +272,8 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg); +char *xescape(const char *s, const char *bad); + static inline void _reset_errno_(int *saved_errno) { errno = *saved_errno; } |