From 7c17e82cbe42d01511e86465cb9bc20d56f03325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Sat, 22 Jan 2022 23:24:14 +0100 Subject: [PATCH] zfs: get: only accept whole column for -o, not col[=whatever] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia ZiemiaƄska Closes #12996 --- cmd/zfs/zfs_main.c | 60 ++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index 2a16bd0b49..5d7599d3e0 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -2038,11 +2038,17 @@ zfs_do_get(int argc, char **argv) * the structure to give us a blank slate. */ memset(&cb.cb_columns, 0, sizeof (cb.cb_columns)); + i = 0; - while (*optarg != '\0') { - static char *col_subopts[] = - { "name", "property", "value", "received", - "source", "all", NULL }; + for (char *tok; (tok = strsep(&optarg, ",")); ) { + static const char *const col_subopts[] = + { "name", "property", "value", + "received", "source", "all" }; + static const zfs_get_column_t col_subopt_col[] = + { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE, + GET_COL_RECVD, GET_COL_SOURCE }; + static const int col_subopt_flags[] = + { 0, 0, 0, ZFS_ITER_RECVD_PROPS, 0 }; if (i == ZFS_GET_NCOLS) { (void) fprintf(stderr, gettext("too " @@ -2051,25 +2057,16 @@ zfs_do_get(int argc, char **argv) usage(B_FALSE); } - switch (getsubopt(&optarg, col_subopts, - &value)) { - case 0: - cb.cb_columns[i++] = GET_COL_NAME; - break; - case 1: - cb.cb_columns[i++] = GET_COL_PROPERTY; - break; - case 2: - cb.cb_columns[i++] = GET_COL_VALUE; - break; - case 3: - cb.cb_columns[i++] = GET_COL_RECVD; - flags |= ZFS_ITER_RECVD_PROPS; - break; - case 4: - cb.cb_columns[i++] = GET_COL_SOURCE; - break; - case 5: + for (c = 0; c < ARRAY_SIZE(col_subopts); ++c) + if (strcmp(tok, col_subopts[c]) == 0) + goto found; + + (void) fprintf(stderr, + gettext("invalid column name '%s'\n"), tok); + usage(B_FALSE); + +found: + if (c >= 5) { if (i > 0) { (void) fprintf(stderr, gettext("\"all\" conflicts " @@ -2077,19 +2074,14 @@ zfs_do_get(int argc, char **argv) "given to -o option\n")); usage(B_FALSE); } - cb.cb_columns[0] = GET_COL_NAME; - cb.cb_columns[1] = GET_COL_PROPERTY; - cb.cb_columns[2] = GET_COL_VALUE; - cb.cb_columns[3] = GET_COL_RECVD; - cb.cb_columns[4] = GET_COL_SOURCE; + + memcpy(cb.cb_columns, col_subopt_col, + sizeof (col_subopt_col)); flags |= ZFS_ITER_RECVD_PROPS; i = ZFS_GET_NCOLS; - break; - default: - (void) fprintf(stderr, - gettext("invalid column name " - "'%s'\n"), value); - usage(B_FALSE); + } else { + cb.cb_columns[i++] = col_subopt_col[c]; + flags |= col_subopt_flags[c]; } } break;