From b32361cadac51603e12ab8f01c3f56337ee6ee3b Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 29 Jul 2022 15:00:06 -0500 Subject: [PATCH] The unquoted strings in -o escape were bothering me. Things like volume labels are user-defined strings... --- toys/other/blkid.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/toys/other/blkid.c b/toys/other/blkid.c index fef3d6a3..4ba176f5 100644 --- a/toys/other/blkid.c +++ b/toys/other/blkid.c @@ -66,6 +66,19 @@ struct fstype { {"vfat", 0x31544146, 4, 54, 39, 11, 43} // fat1 }; +static void escape(char *str, int force) +{ + if (!force && str[strcspn(str, "\" \\")]) force++; + if (!force) return xputsn(str); + + putchar('"'); + while (*str) { + if (strchr("\" \\", *str)) putchar('\\'); + putchar(*str++); + } + putchar('"'); +} + static void show_tag(char *key, char *value) { int show = 0; @@ -76,9 +89,11 @@ static void show_tag(char *key, char *value) } else show = 1; if (!show || !*value) return; - if (!strcasecmp(TT.o, "full")) printf(" %s=\"%s\"", key, value); - else if (!strcasecmp(TT.o, "export")) printf("%s=%s\n", key, value); - else if (!strcasecmp(TT.o, "value")) xputs(value); + if (!strcasecmp(TT.o, "full") || !strcasecmp(TT.o, "export")) { + printf(" %s="+!(*TT.o=='f'), key); + escape(value, *TT.o=='f'); + if (*TT.o=='e') xputc('\n'); + } else if (!strcasecmp(TT.o, "value")) xputs(value); else error_exit("bad -o %s", TT.o); } -- 2.39.2