From: Karel Zak Date: Tue, 26 Sep 2023 12:45:13 +0000 (+0200) Subject: lsblk: support normalized column names on command line X-Git-Tag: v2.40-rc1~151^2~51 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=30edd8e1eb20fa8a2e16e371ad48ed866b0e5458;p=thirdparty%2Futil-linux.git lsblk: support normalized column names on command line For example column name "LOG-SEC" is possible to use as LOG_SEC in some output formats and in filter expressions. Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 7d207b5de1..47b73fee2c 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -345,12 +345,30 @@ static int column_name_to_id(const char *name, size_t namesz) { size_t i; + /* name as diplayed for users */ for (i = 0; i < ARRAY_SIZE(infos); i++) { const char *cn = infos[i].name; if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) return i; } + + /* name as used in expressions, JSON output etc. */ + if (strnchr(name, namesz, '_')) { + char *buf = NULL; + size_t bufsz = 0; + + for (i = 0; i < ARRAY_SIZE(infos); i++) { + if (scols_shellvar_name(infos[i].name, &buf, &bufsz) != 0) + continue; + if (!strncasecmp(name, buf, namesz) && !*(buf + namesz)) { + free(buf); + return i; + } + } + free(buf); + } + warnx(_("unknown column: %s"), name); return -1; }