While most Android devices still have low pid_max values, my laptop and
desktop are using ever higher values. Auto-size the PID and PPID fields
based on the system's current configuration rather than hard-coding
values.
---
 toys/posix/ps.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
From edf280d5bfcbfafc3cccc6e578e07cd048d0600f Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Sat, 22 Feb 2020 15:14:36 -0800
Subject: [PATCH] ps/top: auto-size the PID/PPID fields.

While most Android devices still have low pid_max values, my laptop and
desktop are using ever higher values. Auto-size the PID and PPID fields
based on the system's current configuration rather than hard-coding
values.
---
 toys/posix/ps.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 0c8e8e81..07204835 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -213,7 +213,7 @@ GLOBALS(
   dev_t tty;
   void *fields, *kfields;
   long long ticks, bits, time;
-  int kcount, forcek, sortpos;
+  int kcount, forcek, sortpos, pid_width;
   int (*match_process)(long long *slot);
   void (*show_process)(void *tb);
 )
@@ -294,7 +294,8 @@ struct procpid {
  *        to a terminal (negative means right justified). Strings are truncated
  *        to fit, numerical fields are padded but not truncated (although
  *        the display code reclaims unused padding from later fields to try to
- *        get the overflow back).
+ *        get the overflow back). The special case 0 means "wide enough for a
+ *        pid on this system".
  *
  * slot: which slot[] out of procpid. Negative means it's a string field.
  *       value|XX requests extra display/sort processing.
@@ -316,8 +317,8 @@ struct typography {
   signed char width, slot;
 } static const typos[] = TAGGED_ARRAY(PS,
   // Numbers. (What's in slot[] is what's displayed, sorted numerically.)
-  {"PID", "Process ID", 5, SLOT_pid},
-  {"PPID", "Parent Process ID", 5, SLOT_ppid},
+  {"PID", "Process ID", 0, SLOT_pid},
+  {"PPID", "Parent Process ID", 0, SLOT_ppid},
   {"PRI", "Priority (dynamic 0 to 139)", 3, SLOT_priority},
   {"NI", "Niceness (static 19 to -20)", 3, SLOT_nice},
   {"ADDR", "Instruction pointer", 4+sizeof(long), SLOT_eip},
@@ -1050,6 +1051,18 @@ static int get_threads(struct dirtree *new)
   return 0;
 }
 
+static int pid_width()
+{
+  if (!TT.pid_width) {
+    FILE *fp = xfopen("/proc/sys/kernel/pid_max", "re");
+
+    // We don't actually care about the maximum, just how many digits it takes.
+    fscanf(fp, "%*d%n", &TT.pid_width);
+    fclose(fp);
+  }
+  return TT.pid_width;
+}
+
 // Parse one FIELD argument (with optional =name :width) into struct ofields
 static char *parse_ko(void *data, char *type, int length)
 {
@@ -1111,8 +1124,10 @@ static char *parse_ko(void *data, char *type, int length)
   }
   if (i==ARRAY_LEN(typos)) return type;
   if (!field->title) field->title = typos[field->which].name;
-  if (!field->len) field->len = typos[field->which].width;
-  else if (typos[field->which].width<0) field->len *= -1;
+  if (!field->len) {
+    field->len = typos[field->which].width;
+    if (!field->len) field->len = pid_width();
+  } else if (typos[field->which].width<0) field->len *= -1;
   dlist_add_nomalloc(data, (void *)field);
 
   return 0;
-- 
2.25.0.265.gbab2e86ba0-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to