From: Craig Small <csmall@enc.com.au>
Date: Sat, 15 Aug 2015 06:23:37 +0000 (+1000)
Subject: ps: sort by cgroup
X-Git-Tag: v3.3.12~77
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4bd0e539aff370e7e15a4eadfb90cc425004fa30;p=procps-ng

ps: sort by cgroup

A rather small fix to sort by cgroup. This sorting function
could be used for other string vector entries, but I can't
see why you want to for, say, environment.

Reference:
 https://bugs.debian.org/692279

Signed-off-by: Craig Small <csmall@enc.com.au>
---

diff --git a/NEWS b/NEWS
index 43552c83..50c7b7ae 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,6 @@
 procps-ng-NEXT
 ----------------
+  * ps: sort by cgroup Debian #692279
 
 procps-ng-3.3.11
 ----------------
diff --git a/ps/output.c b/ps/output.c
index 63ba2688..35286c0b 100644
--- a/ps/output.c
+++ b/ps/output.c
@@ -96,6 +96,18 @@ static int sr_nop(const proc_t* a, const proc_t* b){
   return 0;
 }
 
+static int sr_cgroup(const proc_t* a, const proc_t* b)
+{
+    int i;
+    int cmpval;
+    for (i=0; a->cgroup[i] != NULL && b->cgroup[i] != NULL; i++) {
+        if ((cmpval = strcmp(a->cgroup[i], b->cgroup[i])) != 0)
+            return cmpval;
+    }
+    return 0;
+}
+
+
 #define CMP_STR(NAME) \
 static int sr_ ## NAME(const proc_t* P, const proc_t* Q) { \
     return strcmp(P->NAME, Q->NAME); \
@@ -1439,7 +1451,7 @@ static const format_struct format_array[] = {
 {"bsdtime",   "TIME",    pr_bsdtime,  sr_nop,     6,   0,    LNX, ET|RIGHT},
 {"c",         "C",       pr_c,        sr_pcpu,    2,   0,    SUN, ET|RIGHT},
 {"caught",    "CAUGHT",  pr_sigcatch, sr_nop,     9,   0,    BSD, TO|SIGNAL}, /*sigcatch*/
-{"cgroup",    "CGROUP",  pr_cgroup,   sr_nop,    27,CGRP,    LNX, PO|UNLIMITED},
+{"cgroup",    "CGROUP",  pr_cgroup,   sr_cgroup, 27,CGRP,    LNX, PO|UNLIMITED},
 {"class",     "CLS",     pr_class,    sr_sched,   3,   0,    XXX, TO|LEFT},
 {"cls",       "CLS",     pr_class,    sr_sched,   3,   0,    HPU, TO|RIGHT}, /*says HPUX or RT*/
 {"cmaj_flt",  "-",       pr_nop,      sr_cmaj_flt, 1,  0,    LNX, AN|RIGHT},