diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2013-09-10 07:05:19 -0400 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2013-09-10 07:05:19 -0400 |
commit | 254466e4a05d8110e4b5a999d0c5911a6c9eebb2 (patch) | |
tree | 060317eeccaa7cdcb7005b122ccca524848dccdb /src/libudev | |
parent | 15ab35551bbaf525ef7a45af58ce7284ed84f3c6 (diff) |
src/libudev/util.c: reintroduce cescape and octchar
Diffstat (limited to 'src/libudev')
-rw-r--r-- | src/libudev/util.c | 80 | ||||
-rw-r--r-- | src/libudev/util.h | 2 |
2 files changed, 82 insertions, 0 deletions
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_; |