aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toys/pending/ps.c49
1 files changed, 22 insertions, 27 deletions
diff --git a/toys/pending/ps.c b/toys/pending/ps.c
index 3e552574..3427557a 100644
--- a/toys/pending/ps.c
+++ b/toys/pending/ps.c
@@ -42,15 +42,12 @@ struct header_list {
// 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 @@ static void list_add(struct header_list *data, char *c_data)
} 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 @@ static void print_header(struct header_list *hdr, int hdr_len)
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 @@ static void print_header(struct header_list *hdr, int hdr_len)
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 @@ static void do_ps_line(int pid, int tid)
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 @@ void ps_main(void)
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 @@ void ps_main(void)
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))) {