ps: add lots of sorting functions

Use NULL in format_array to indicate that the print function shall be
used also for sorting. Change sr_nop() to NULL for all fields which
don't use pr_nop() for printing.

Before the commit (note that '--sort label' has no effect: the rows
are not sorted according to label field):
$ ps -A -o command,label --sort label | grep /lib/systemd
/lib/systemd/systemd-journa system_u:system_r:syslogd_t:s0
/lib/systemd/systemd-udevd  system_u:system_r:udev_t:s0-s0:c0.c1023
/lib/systemd/systemd-networ system_u:system_r:systemd_networkd_t:s0
/lib/systemd/systemd-resolv system_u:system_r:systemd_resolved_t:s0
/lib/systemd/systemd-timesy system_u:system_r:ntpd_t:s0
/lib/systemd/systemd-logind system_u:system_r:systemd_logind_t:s0
/lib/systemd/systemd --user user_u:user_r:user_t:s0
/lib/systemd/systemd --user root:sysadm_r:sysadm_t:s0-s0:c0.c1023
grep /lib/systemd           user_u:user_r:user_t:s0

After the commit, '--sort label' works and the output is sorted:
$ ps -A -o command,label --sort label | grep /lib/systemd
/lib/systemd/systemd --user root:sysadm_r:sysadm_t:s0-s0:c0.c1023
/lib/systemd/systemd-timesy system_u:system_r:ntpd_t:s0
/lib/systemd/systemd-journa system_u:system_r:syslogd_t:s0
/lib/systemd/systemd-logind system_u:system_r:systemd_logind_t:s0
/lib/systemd/systemd-networ system_u:system_r:systemd_networkd_t:s0
/lib/systemd/systemd-resolv system_u:system_r:systemd_resolved_t:s0
/lib/systemd/systemd-udevd  system_u:system_r:udev_t:s0-s0:c0.c1023
/lib/systemd/systemd --user user_u:user_r:user_t:s0
grep /lib/systemd           user_u:user_r:user_t:s0

Signed-off-by: Topi Miettinen <toiwoton@gmail.com>
This commit is contained in:
Topi Miettinen
2020-03-11 11:23:33 +02:00
committed by Craig Small
parent 5f859b30d3
commit 173d5214db
4 changed files with 72 additions and 54 deletions

View File

@@ -426,6 +426,7 @@ static void prep_forest_sort(void){
tmp_list = malloc(sizeof(sort_node));
tmp_list->reverse = 0;
tmp_list->typecode = '?'; /* what was this for? */
tmp_list->pr = incoming->pr;
tmp_list->sr = incoming->sr;
tmp_list->need = incoming->need;
tmp_list->next = sort_list;
@@ -437,6 +438,7 @@ static void prep_forest_sort(void){
tmp_list = malloc(sizeof(sort_node));
tmp_list->reverse = 0;
tmp_list->typecode = '?'; /* what was this for? */
tmp_list->pr = incoming->pr;
tmp_list->sr = incoming->sr;
tmp_list->need = incoming->need;
tmp_list->next = sort_list;
@@ -452,7 +454,21 @@ static int compare_two_procs(const void *a, const void *b){
sort_node *tmp_list = sort_list;
while(tmp_list){
int result;
result = (*tmp_list->sr)(*(const proc_t *const*)a, *(const proc_t *const*)b);
if (tmp_list->sr)
result = (*tmp_list->sr)(*(const proc_t *const*)a, *(const proc_t *const*)b);
else {
/*
* Compare by using printing function to "print" the strings
* into temporary buffers, then strcmp() can be used to compare
* the two results as usual. This is used where direct string
* comparison can't be used because the sort key is not directly
* in the proc_t structure.
*/
char buf_a[OUTBUF_SIZE], buf_b[OUTBUF_SIZE];
(void) (*tmp_list->pr)(buf_a, *(const proc_t *const*)a);
(void) (*tmp_list->pr)(buf_b, *(const proc_t *const*)b);
result = strcmp(buf_a, buf_b);
}
if(result) return (tmp_list->reverse) ? -result : result;
tmp_list = tmp_list->next;
}