changeset 1739:1134cfb98257

More ps cleanup, mostly refactoring.
author Rob Landley <rob@landley.net>
date Sat, 14 Mar 2015 12:34:14 -0500
parents 783dc27590de
children ca358f8731ab
files toys/pending/ps.c
diffstat 1 files changed, 22 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/ps.c	Fri Mar 13 21:33:34 2015 -0500
+++ b/toys/pending/ps.c	Sat Mar 14 12:34:14 2015 -0500
@@ -42,15 +42,12 @@
 // and width of attributes.
 static void list_add(struct header_list *data, char *c_data)
 {
-  struct header_list *temp = TT.o, *new = xzalloc(sizeof(struct header_list));
+  struct header_list *temp = TT.o, *new;
 
-  new->name = data->name;   
+  memcpy(new = xmalloc(sizeof(*new)), data, sizeof(*new));
   if (c_data) new->header = c_data;
-  else new->header = xstrdup(data->header);  
+  else new->header = xstrdup(data->header);
   if (c_data && (strlen(c_data) > data->width)) new->width = strlen(c_data);
-  else new->width = data->width;
-  new->format = data->format;
-  new->position = data->position;
 
   if (temp) {
     while (temp->next) temp = temp->next;
@@ -58,21 +55,13 @@
   } else TT.o = new;
 }
 
-//print the default header OR header with -o args
-static void print_header(struct header_list *hdr, int hdr_len)
+// parse -o arguments
+static void parse_o(struct header_list *hdr)
 {
-  int i = 0;
-  char *ptr = NULL, *str, *temp;
+  int i;
+  char *ptr, *str, *temp;
   struct arg_list *node = TT.llist_o;
 
-  // Default pid, user, time, comm
-  if (!node) {
-    list_add(hdr+4, 0);
-    list_add(hdr, 0);
-    list_add(hdr+11, 0);
-    list_add(hdr+3, 0);
-  }
-
   while (node) {
     char *s = str = xstrdup(node->arg);
 
@@ -80,8 +69,7 @@
     while (str) {
       if ((ptr = strsep(&str, ","))) { //seprate list
         if ((temp = strchr(ptr, '='))) { // Handle ppid = MOM
-          *temp = 0;
-          temp++;
+          *temp++ = 0;
           while (hdr[i].name) {
             // search from default header
             if (!(strcmp(hdr[i].name, ptr))) {
@@ -111,10 +99,6 @@
     free(s);
     node = node->next;
   }
-
-  for (hdr = TT.o; hdr; hdr = hdr->next)
-    printf(hdr->format , hdr->width, hdr->header);
-  xputc('\n');
 }
 
 //get uid/gid for processes.
@@ -304,7 +288,7 @@
         gr = getgrgid(rgid);
         if (!gr) rgroup = xmprintf("%d",(int)stats.st_gid);
         else rgroup = xmprintf("%s", gr->gr_name);
-        printf("%-*.*s", width,width, rgroup);
+        printf("%-*.*s", width, width, rgroup);
         free(rgroup);
         break;
       case 10:
@@ -372,7 +356,7 @@
   DIR *dp;
   struct dirent *entry;
   int pid;
-  struct header_list def_header[] = { 
+  struct header_list *hdr, def_header[] = { 
     {0, "user", "USER", "%-*s ", 8, 0},
     {0, "group", "GROUP", "%-*s ", 8, 1},
     {0, "comm", "COMMAND", "%-*s ",16, 2},
@@ -394,7 +378,18 @@
   
   TT.screen_width = 80; //default width
   terminal_size(&TT.screen_width, NULL);
-  print_header(def_header, ARRAY_LEN(def_header));
+
+  // Default pid, user, time, comm
+  if (!TT.llist_o) {
+    list_add(def_header+4, 0);
+    list_add(def_header, 0);
+    list_add(def_header+11, 0);
+    list_add(def_header+3, 0);
+  } else parse_o(def_header); // ARRAY_LEN(def_header)
+
+  for (hdr = TT.o; hdr; hdr = hdr->next)
+    printf(hdr->format , hdr->width, hdr->header);
+  xputc('\n');
 
   if (!(dp = opendir("/proc"))) perror_exit("opendir");
   while ((entry = readdir(dp))) {