// 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;
} 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);
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))) {
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.
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:
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},
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))) {