From: Karel Zak Date: Mon, 13 Aug 2012 12:23:04 +0000 (+0200) Subject: lib/tt: always escape '\' to simplify parsing in scripts X-Git-Tag: v2.22-rc2~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=17b1c1368de17d31e053d4bef3f2e0a74bbbd73b;p=thirdparty%2Futil-linux.git lib/tt: always escape '\' to simplify parsing in scripts 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 --- 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); }