chiark / gitweb /
util: improve overflow checks
[elogind.git] / src / shared / cgroup-show.c
index 19f87e4bc6c4289d88899feeda1aac310d35bc05..2ffed8b739acc2d36cd44a655e14642e4d1281dd 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "util.h"
 #include "macro.h"
+#include "path-util.h"
 #include "cgroup-util.h"
 #include "cgroup-show.h"
 
@@ -51,7 +52,7 @@ static unsigned ilog10(unsigned long ul) {
 }
 
 static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsigned n_columns, bool extra, bool more, bool kernel_threads) {
-        unsigned i, m;
+        unsigned i, m, pid_width;
         pid_t biggest = 0;
 
         /* Filter duplicates */
@@ -70,12 +71,13 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi
                         pids[m++] = pids[i];
         }
         n_pids = m;
+        pid_width = ilog10(biggest);
 
         /* And sort */
         qsort(pids, n_pids, sizeof(pid_t), compare);
 
-        if (n_columns > 8)
-                n_columns -= 8;
+        if (n_columns > pid_width+2)
+                n_columns -= pid_width+2;
         else
                 n_columns = 20;
 
@@ -87,7 +89,7 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi
                 printf("%s%s %*lu %s\n",
                        prefix,
                        extra ? "\342\200\243" : ((more || i < n_pids-1) ? "\342\224\234" : "\342\224\224"),
-                       (int) ilog10(biggest),
+                       pid_width,
                        (unsigned long) pids[i],
                        strna(t));
 
@@ -205,7 +207,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
                 }
 
                 if (last) {
-                        printf("%s\342\224\234 %s\n", prefix, file_name_from_path(last));
+                        printf("%s\342\224\234 %s\n", prefix, path_get_file_name(last));
 
                         if (!p1) {
                                 p1 = strappend(prefix, "\342\224\202 ");
@@ -230,7 +232,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
                 show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads);
 
         if (last) {
-                printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
+                printf("%s\342\224\224 %s\n", prefix, path_get_file_name(last));
 
                 if (!p2) {
                         p2 = strappend(prefix, "  ");