From e5530e33bfb71a9c98b0ca20dbe0c16e316b6b66 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Mon, 31 Jul 2023 18:00:08 +0200 Subject: [PATCH] fincore: refactor output formatting MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will make the upcoming addition of cachestat() columns nicer. Signed-off-by: Thomas Weißschuh --- misc-utils/fincore.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/misc-utils/fincore.c b/misc-utils/fincore.c index d574eae501..96c8ce3994 100644 --- a/misc-utils/fincore.c +++ b/misc-utils/fincore.c @@ -49,6 +49,7 @@ struct colinfo { double whint; int flags; const char *help; + unsigned int pages : 1; }; enum { @@ -59,7 +60,7 @@ enum { }; static const struct colinfo infos[] = { - [COL_PAGES] = { "PAGES", 1, SCOLS_FL_RIGHT, N_("file data resident in memory in pages")}, + [COL_PAGES] = { "PAGES", 1, SCOLS_FL_RIGHT, N_("file data resident in memory in pages"), 1}, [COL_RES] = { "RES", 5, SCOLS_FL_RIGHT, N_("file data resident in memory in bytes")}, [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the file")}, [COL_FILE] = { "FILE", 4, 0, N_("file name")}, @@ -114,6 +115,7 @@ static int add_output_data(struct fincore_control *ctl, { size_t i; char *tmp; + uint64_t value; struct libscols_line *ln; assert(ctl); @@ -125,26 +127,18 @@ static int add_output_data(struct fincore_control *ctl, for (i = 0; i < ncolumns; i++) { int rc = 0; + int column_id = get_column_id(i); + int format_value = 0; - switch(get_column_id(i)) { + switch(column_id) { case COL_FILE: rc = scols_line_set_data(ln, i, name); break; case COL_PAGES: - xasprintf(&tmp, "%jd", (intmax_t) count_incore); - rc = scols_line_refer_data(ln, i, tmp); - break; case COL_RES: - { - uintmax_t res = (uintmax_t) count_incore * ctl->pagesize; - - if (ctl->bytes) - xasprintf(&tmp, "%ju", res); - else - tmp = size_to_human_string(SIZE_SUFFIX_1LETTER, res); - rc = scols_line_refer_data(ln, i, tmp); + value = count_incore; + format_value = 1; break; - } case COL_SIZE: if (ctl->bytes) xasprintf(&tmp, "%jd", (intmax_t) file_size); @@ -156,6 +150,19 @@ static int add_output_data(struct fincore_control *ctl, return -EINVAL; } + if (format_value) { + if (infos[column_id].pages) { + xasprintf(&tmp, "%ju", (uintmax_t) value); + } else { + value *= ctl->pagesize; + if (ctl->bytes) + xasprintf(&tmp, "%ju", (uintmax_t) value); + else + tmp = size_to_human_string(SIZE_SUFFIX_1LETTER, value); + } + rc = scols_line_refer_data(ln, i, tmp); + } + if (rc) err(EXIT_FAILURE, _("failed to add output data")); } -- 2.47.3