]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/tt: always escape '\' to simplify parsing in scripts
authorKarel Zak <kzak@redhat.com>
Mon, 13 Aug 2012 12:23:04 +0000 (14:23 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 13 Aug 2012 12:34:47 +0000 (14:34 +0200)
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>
lib/tt.c

index a538af66c5b817f27d4fdf0435c15fa3cc6a9a77..6ec967d832aa86e7ed2a91b294c874f397dd2b5d 100644 (file)
--- 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);
        }