diff options
author | Theodore Ts'o <tytso@mit.edu> | 2007-12-16 12:26:57 -0500 |
---|---|---|
committer | Theodore Ts'o <tytso@mit.edu> | 2007-12-16 12:26:57 -0500 |
commit | f8efcda2db612f6b6d33dc1a6ff78a1c048608f4 (patch) | |
tree | c4e6f1a3c8eb14f8935ccbb6eea6bfe5defcfc89 | |
parent | 43f0cbc1f91f6e104ae7cbee367af7f3746ecc4c (diff) | |
download | e2fsprogs-f8efcda2db612f6b6d33dc1a6ff78a1c048608f4.tar.gz |
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" <tytso@mit.edu>
-rw-r--r-- | misc/blkid.c | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/misc/blkid.c b/misc/blkid.c index 57cfd1a4..c0fda73c 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); |