From 17b1c1368de17d31e053d4bef3f2e0a74bbbd73b Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 13 Aug 2012 14:23:04 +0200 Subject: [PATCH] lib/tt: always escape '\' to simplify parsing in scripts MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The commands echo(1) and printf(1) are usable for escape sequences decoding, for example for x in $(findmnt --noheading --raw --output TARGET); do printf "%b" $x done but it's necessary to escape all '\' chars, otherwise for example \b in foo\bar will be interpreted as backspace. It means that for example findmnt(8) has to use \x5c for the backslash. # findmnt --noheading --raw --output TARGET /dev/sda1 /mnt/ugly/foo\x5cbar Reported-by: Pádraig Brady Signed-off-by: Karel Zak --- lib/tt.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/tt.c b/lib/tt.c index a538af66c5..6ec967d832 100644 --- a/lib/tt.c +++ b/lib/tt.c @@ -666,18 +666,12 @@ void tt_fputs_quoted(const char *data, FILE *out) fputc('"', out); for (p = data; p && *p; p++) { - if ((unsigned char) *p == 0x22 || + if ((unsigned char) *p == 0x22 || /* " */ + (unsigned char) *p == 0x5c || /* \ */ !isprint((unsigned char) *p) || iscntrl((unsigned char) *p)) { fprintf(out, "\\x%02x", (unsigned char) *p); - - } else if (*p == '\\' && - *(p + 1) == 'x' && - isxdigit((unsigned char) *(p + 2)) && - isxdigit((unsigned char) *(p + 3))) { - - fprintf(out, "\\x%02x", (unsigned char) *p); } else fputc(*p, out); } @@ -690,17 +684,12 @@ void tt_fputs_nonblank(const char *data, FILE *out) for (p = data; p && *p; p++) { if (isblank((unsigned char) *p) || + (unsigned char) *p == 0x5c || /* \ */ !isprint((unsigned char) *p) || iscntrl((unsigned char) *p)) { fprintf(out, "\\x%02x", (unsigned char) *p); - } else if (*p == '\\' && - *(p + 1) == 'x' && - isxdigit((unsigned char) *(p + 2)) && - isxdigit((unsigned char) *(p + 3))) { - - fprintf(out, "\\x%02x", (unsigned char) *p); } else fputc(*p, out); } -- 2.47.3