/************************************************************************* * Get the total number of leaf descendants under a given root node. *************************************************************************/ int get_num_descendants (TREE_T * const a_tree, BOOLEAN_T force) { check_null_tree(a_tree); compute_descendants(a_tree, force); return(a_tree->num_descendants); }
/************************************************************************* * Add a given child to a tree. *************************************************************************/ void add_child (const VERBOSE_T verbosity, TREE_T * const a_child, TREE_T * a_tree) { check_null_tree(a_tree); /* Make sure we don't put too many children in the tree. */ if (get_num_children(a_tree) >= MAX_DEGREE) { die("Attempted to add %s to tree (%s) with maximum degree (%d).\n", get_key(a_child), get_key(a_tree), get_num_children(a_tree)); } if (verbosity >= HIGHER_VERBOSE) { fprintf(stderr, "Adding child %s to tree %s.\n", get_key(a_child), get_key(a_tree)); } /* Add the child to the tree. */ a_tree->children[get_num_children(a_tree)] = a_child; (a_tree->num_children)++; /* Update the number of descendants, if necessary. */ if (a_tree->has_descendants) { compute_descendants(a_child, FALSE); a_tree->num_descendants += a_child->num_descendants; } }
/************************************************************************** * Compute and store the number of descendants of each node in a tree. **************************************************************************/ static void compute_descendants (TREE_T * a_tree, BOOLEAN_T force) { int i_child; int num_descendants; /* Don't do anything if we've already computed the descendants. */ if (!force && a_tree->has_descendants) { return; } /* Base case: A leaf has only itself as a descendant. */ if (is_leaf(a_tree)) { num_descendants = 1; a_tree->has_descendants = FALSE; } else { /* Recursive case: Descendants of each child. */ num_descendants = 0; a_tree->has_descendants = TRUE; for (i_child = 0; i_child < a_tree->num_children; i_child++) { compute_descendants(get_nth_child(i_child, a_tree), force); num_descendants += get_num_descendants(get_nth_child(i_child, a_tree), force); } } a_tree->num_descendants = num_descendants; }
int main(int argc, char **argv) { get_shells(); setflags(argc,argv); proc *ps[10000]; proc_structs = ps; if (unique_uids) { int uidstr[10000]; uuids = uidstr; int i = 10000; while(i--) uuids[i] = -1; } DIR *dp; struct dirent *ep; psinfo_t psinfo_buf; dp = opendir ("/proc"); char buf[128]; if (dp != NULL) { printf("%8s ","UID"); if(unique_uids) printf("\n"); else { if(pid) printf("%5s ","PID"); if(descendants) printf("%4s ","N"); printf("%16s %.24s\n","FNAME","STIME"); } while ((ep = readdir(dp))) { if(ep->d_name[0] >= '0' && ep->d_name[0] <= '9') { sprintf(buf,"/proc/%s/psinfo",ep->d_name); save_procinfo(buf,&psinfo_buf); //print_procinfo(buf,&psinfo_buf); } } (void)closedir(dp); } else perror ("Couldn't open the directory"); compute_descendants(); proc *p;int i = 0; while ((p = proc_structs[i++])) print_procinfo(p); return 0; }