From f8efcda2db612f6b6d33dc1a6ff78a1c048608f4 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sun, 16 Dec 2007 12:26:57 -0500 Subject: [PATCH] blkid: Output non-printing characters using ^ and M- notation When printing the value of tags in a formatted format, print control characters and characters with the high eight bit set using the ^ and M- notation, respectively. This prevents a filesystem with a garbage label from potentially screwing up the user's screen (for example, putting it into graphical mode). Addresses-Ubuntu-Bug: #78087 Signed-off-by: "Theodore Ts'o" --- misc/blkid.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/misc/blkid.c b/misc/blkid.c index 57cfd1a..c0fda73 100644 --- a/misc/blkid.c +++ b/misc/blkid.c @@ -53,6 +53,31 @@ static void usage(int error) exit(error); } +/* + * This function does "safe" printing. It will convert non-printable + * ASCII characters using '^' and M- notation. + */ +static void safe_print(const char *cp, int len) +{ + unsigned char ch; + + if (len < 0) + len = strlen(cp); + + while (len--) { + ch = *cp++; + if (ch > 128) { + fputs("M-", stdout); + ch -= 128; + } + if ((ch < 32) || (ch == 0x7f)) { + fputc('^', stdout); + ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */ + } + fputc(ch, stdout); + } +} + static void print_tags(blkid_dev dev, char *show[], int numtag, int output) { blkid_tag_iterate iter; @@ -76,14 +101,19 @@ static void print_tags(blkid_dev dev, char *show[], int numtag, int output) if (i >= numtag) continue; } - if (first && !(output & OUTPUT_VALUE_ONLY)) { - printf("%s: ", blkid_dev_devname(dev)); - first = 0; + if (output & OUTPUT_VALUE_ONLY) { + fputs(value, stdout); + fputc('\n', stdout); + } else { + if (first) { + printf("%s: ", blkid_dev_devname(dev)); + first = 0; + } + fputs(type, stdout); + fputs("=\"", stdout); + safe_print(value, -1); + fputs("\" ", stdout); } - if ((output & OUTPUT_VALUE_ONLY)) - printf("%s\n", value); - else - printf("%s=\"%s\" ", type, value); } blkid_tag_iterate_end(iter); -- 1.8.3.1