diff options
-rw-r--r-- | include/tt.h | 2 | ||||
-rw-r--r-- | lib/tt.c | 18 | ||||
-rw-r--r-- | misc-utils/findmnt.c | 20 | ||||
-rw-r--r-- | misc-utils/lsblk.c | 7 | ||||
-rw-r--r-- | partx/partx.c | 7 |
5 files changed, 35 insertions, 19 deletions
diff --git a/include/tt.h b/include/tt.h index c3dcb2b36..5f6e8172b 100644 --- a/include/tt.h +++ b/include/tt.h @@ -83,7 +83,7 @@ extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent); extern int tt_line_set_data(struct tt_line *ln, int colnum, const char *data); extern int tt_line_set_userdata(struct tt_line *ln, void *data); -extern int tt_parse_columns_list(const char *list, int cols[], int *ncols, +extern int tt_parse_columns_list(const char *list, int ary[], size_t arrsz, int (name2id)(const char *, size_t)); #endif /* UTIL_LINUX_TT_H */ @@ -661,16 +661,19 @@ int tt_print_table(struct tt *tb) return 0; } -int tt_parse_columns_list(const char *list, int cols[], int *ncols, +/* Returns: >= 0 : number of items added to ary[] + * -1 : parse error or unknown item + * -2 : arysz reached + */ +int tt_parse_columns_list(const char *list, int ary[], size_t arysz, int (name2id)(const char *, size_t)) { const char *begin = NULL, *p; + int n = 0; - if (!list || !*list || !cols || !ncols || !name2id) + if (!list || !*list || !ary || !arysz || !name2id) return -1; - *ncols = 0; - for (p = list; p && *p; p++) { const char *end = NULL; int id; @@ -689,13 +692,14 @@ int tt_parse_columns_list(const char *list, int cols[], int *ncols, id = name2id(begin, end - begin); if (id == -1) return -1; - cols[ *ncols ] = id; - (*ncols)++; + ary[ n++ ] = id; + if (n >= arysz) + return -2; begin = NULL; if (end && !*end) break; } - return 0; + return n; } #ifdef TEST_PROGRAM diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c index 86f3fd687..2dbb60e5a 100644 --- a/misc-utils/findmnt.c +++ b/misc-utils/findmnt.c @@ -897,19 +897,25 @@ int main(int argc, char *argv[]) disable_columns_truncate(); break; case 'o': - if (tt_parse_columns_list(optarg, columns, &ncolumns, - column_name_to_id)) + ncolumns = tt_parse_columns_list( + optarg, + columns, ARRAY_SIZE(columns), + column_name_to_id); + if (ncolumns < 0) exit(EXIT_FAILURE); break; case 'O': set_match(COL_OPTIONS, optarg); break; case 'p': - if (optarg && - tt_parse_columns_list(optarg, actions, &nactions, - poll_action_name_to_id)) - exit(EXIT_FAILURE); - + if (optarg) { + nactions = tt_parse_columns_list( + optarg, + actions, ARRAY_SIZE(actions), + poll_action_name_to_id); + if (nactions < 0) + exit(EXIT_FAILURE); + } flags |= FL_POLL; tt_flags &= ~TT_FL_TREE; break; diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 2e2c457df..b458486f0 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -971,8 +971,11 @@ int main(int argc, char *argv[]) tt_flags |= TT_FL_NOHEADINGS; break; case 'o': - if (tt_parse_columns_list(optarg, columns, &ncolumns, - column_name_to_id)) + ncolumns = tt_parse_columns_list( + optarg, + columns, ARRAY_SIZE(columns), + column_name_to_id); + if (ncolumns < 0) return EXIT_FAILURE; break; case 'P': diff --git a/partx/partx.c b/partx/partx.c index 4f827a822..2451993e4 100644 --- a/partx/partx.c +++ b/partx/partx.c @@ -676,8 +676,11 @@ int main(int argc, char **argv) errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range")); break; case 'o': - if (tt_parse_columns_list(optarg, columns, &ncolumns, - column_name_to_id)) + ncolumns = tt_parse_columns_list( + optarg, + columns, ARRAY_SIZE(columns), + column_name_to_id); + if (ncolumns < 0) return EXIT_FAILURE; break; case 'P': |