]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
zramctl: fix show all non-zero zram devices
authorJames Sanford <jsanfordgit@froop.com>
Sat, 14 Oct 2017 07:57:20 +0000 (00:57 -0700)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Oct 2017 09:04:03 +0000 (11:04 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/521
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/zramctl.c

index 425a8c34184c3e921fb3692b608f637966b27113..fb64150d61249d8faafa6ce698ae9f825f490978 100644 (file)
@@ -23,6 +23,8 @@
 #include <string.h>
 #include <stdarg.h>
 #include <assert.h>
+#include <sys/types.h>
+#include <dirent.h>
 
 #include <libsmartcols.h>
 
@@ -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);
 }