From: James Sanford Date: Sat, 14 Oct 2017 07:57:20 +0000 (-0700) Subject: zramctl: fix show all non-zero zram devices X-Git-Tag: v2.31~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e1b1c7b082803446ceb6fbc860d37c916c45c321;p=thirdparty%2Futil-linux.git zramctl: fix show all non-zero zram devices Addresses: https://github.com/karelzak/util-linux/issues/521 Signed-off-by: Karel Zak --- diff --git a/sys-utils/zramctl.c b/sys-utils/zramctl.c index 425a8c3418..fb64150d61 100644 --- a/sys-utils/zramctl.c +++ b/sys-utils/zramctl.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include @@ -476,6 +478,8 @@ static void status(struct zram *z) { struct libscols_table *tb; size_t i; + DIR *dir; + struct dirent *d; scols_init_debug(0); @@ -493,22 +497,29 @@ static void status(struct zram *z) err(EXIT_FAILURE, _("failed to initialize output column")); } - if (z) - fill_table_row(tb, z); /* just one device specified */ - else { - /* list all used devices */ - z = new_zram(NULL); + if (z) { + /* just one device specified */ + fill_table_row(tb, z); + goto print_table; + } - for (i = 0; ; i++) { - zram_set_devname(z, NULL, i); - if (!zram_exist(z)) - break; - if (zram_used(z)) - fill_table_row(tb, z); - } - free_zram(z); + /* list all used devices */ + z = new_zram(NULL); + if (!(dir = opendir(_PATH_DEV))) + err(EXIT_FAILURE, _("cannot open %s"), _PATH_DEV); + + while ((d = readdir(dir))) { + int n; + if (sscanf(d->d_name, "zram%d", &n) != 1) + continue; + zram_set_devname(z, NULL, n); + if (zram_exist(z) && zram_used(z)) + fill_table_row(tb, z); } + closedir(dir); + free_zram(z); +print_table: scols_print_table(tb); scols_unref_table(tb); }