diff options
author | NeilBrown <neilb@suse.de> | 2012-10-04 08:34:20 +0200 |
---|---|---|
committer | NeilBrown <neilb@suse.de> | 2012-10-04 08:34:20 +0200 |
commit | 7103b9b88d8c27989e17c80d7296eda97370dc1e (patch) | |
tree | 225ef68ca743ca2bdf6e70a7581ac82aebfa29db /lib.c | |
parent | imsm: Allow to specify controller for --detail-platform. (diff) | |
download | mdadm-7103b9b88d8c27989e17c80d7296eda97370dc1e.tar.xz mdadm-7103b9b88d8c27989e17c80d7296eda97370dc1e.zip |
Handles spaces in array names better.
1/ When printing the "name=" entry for --brief output,
enclose name in quotes if it contains spaces etc.
Quotes are already supported for reading mdadm.conf
2/ When a name is used as a device name, translate spaces
and tabs to '_', as well as the current translation of
'/' to '-'.
Signed-off-by: NeilBrown <neilb@suse.de>
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -322,3 +322,71 @@ char *conf_word(FILE *file, int allow_key) } return word; } + +void print_quoted(char *str) +{ + /* Printf the string with surrounding quotes + * iff needed. + * If no space, tab, or quote - leave unchanged. + * Else print surrounded by " or ', swapping quotes + * when we find one that will cause confusion. + */ + + char first_quote = 0, q; + char *c; + + for (c = str; *c; c++) { + switch(*c) { + case '\'': + case '"': + first_quote = *c; + break; + case ' ': + case '\t': + first_quote = *c; + continue; + default: + continue; + } + break; + } + if (!first_quote) { + printf("%s", str); + return; + } + + if (first_quote == '"') + q = '\''; + else + q = '"'; + putchar(q); + for (c = str; *c; c++) { + if (*c == q) { + putchar(q); + q ^= '"' ^ '\''; + putchar(q); + } + putchar(*c); + } + putchar(q); +} + +void print_escape(char *str) +{ + /* print str, but change space and tab to '_' + * as is suitable for device names + */ + for (; *str ; str++) { + switch (*str) { + case ' ': + case '\t': + putchar('_'); + break; + case '/': + putchar('-'); + break; + default: + putchar(*str); + } + } +} |