From 254466e4a05d8110e4b5a999d0c5911a6c9eebb2 Mon Sep 17 00:00:00 2001 From: "Anthony G. Basile" Date: Tue, 10 Sep 2013 07:05:19 -0400 Subject: src/libudev/util.c: reintroduce cescape and octchar --- src/libudev/util.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libudev/util.h | 2 ++ 2 files changed, 82 insertions(+) (limited to 'src/libudev') diff --git a/src/libudev/util.c b/src/libudev/util.c index d2adf042ce..dfe46bcb8c 100644 --- a/src/libudev/util.c +++ b/src/libudev/util.c @@ -551,6 +551,86 @@ char hexchar(int x) { return table[x & 15]; } +char octchar(int x) { + return '0' + (x & 7); +} + +char *cescape(const char *s) { + char *r, *t; + const char *f; + + assert(s); + + /* Does C style string escaping. */ + + r = new(char, strlen(s)*4 + 1); + if (!r) + return NULL; + + for (f = s, t = r; *f; f++) + + switch (*f) { + + case '\a': + *(t++) = '\\'; + *(t++) = 'a'; + break; + case '\b': + *(t++) = '\\'; + *(t++) = 'b'; + break; + case '\f': + *(t++) = '\\'; + *(t++) = 'f'; + break; + case '\n': + *(t++) = '\\'; + *(t++) = 'n'; + break; + case '\r': + *(t++) = '\\'; + *(t++) = 'r'; + break; + case '\t': + *(t++) = '\\'; + *(t++) = 't'; + break; + case '\v': + *(t++) = '\\'; + *(t++) = 'v'; + break; + case '\\': + *(t++) = '\\'; + *(t++) = '\\'; + break; + case '"': + *(t++) = '\\'; + *(t++) = '"'; + break; + case '\'': + *(t++) = '\\'; + *(t++) = '\''; + break; + + default: + /* For special chars we prefer octal over + * hexadecimal encoding, simply because glib's + * g_strescape() does the same */ + if ((*f < ' ') || (*f >= 127)) { + *(t++) = '\\'; + *(t++) = octchar((unsigned char) *f >> 6); + *(t++) = octchar((unsigned char) *f >> 3); + *(t++) = octchar((unsigned char) *f); + } else + *(t++) = *f; + break; + } + + *t = 0; + + return r; +} + char *xescape(const char *s, const char *bad) { char *r, *t; const char *f; diff --git a/src/libudev/util.h b/src/libudev/util.h index 7b6571ab10..18109938e0 100644 --- a/src/libudev/util.h +++ b/src/libudev/util.h @@ -131,8 +131,10 @@ char *strnappend(const char *s, const char *suffix, size_t length); char *truncate_nl(char *s); +char octchar(int x) _const_; char hexchar(int x) _const_; +char *cescape(const char *s); char *xescape(const char *s, const char *bad); bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_; -- cgit v1.2.3-54-g00ecf