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 <P@draigBrady.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2012-08-13 14:23:04 +02:00
parent 33e785491b
commit 17b1c1368d
1 changed files with 3 additions and 14 deletions

View File

@ -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);
}