procps: remove PSSCAN_STAT define, users were using it incorrectly
[oweals/busybox.git] / procps / pstree.c
index 25fb65d79c562d8e7912012a008bdc1129347081..8ba30795dc58a0eda26945029cfc87da597964f5 100644 (file)
@@ -16,7 +16,7 @@
 //config:      help
 //config:        Display a tree of processes.
 
-//applet:IF_PSTREE(APPLET(pstree, _BB_DIR_USR_BIN, _BB_SUID_DROP))
+//applet:IF_PSTREE(APPLET(pstree, BB_DIR_USR_BIN, BB_SUID_DROP))
 
 //kbuild:lib-$(CONFIG_PSTREE) += pstree.o
 
@@ -24,7 +24,6 @@
 //usage:       "[-p] [PID|USER]"
 //usage:#define pstree_full_usage "\n\n"
 //usage:       "Display process tree, optionally start from USER or PID\n"
-//usage:     "\nOptions:"
 //usage:     "\n       -p      Show pids"
 
 #include "libbb.h"
@@ -61,7 +60,7 @@ typedef struct child {
 #define first_3  "-+-"
 
 struct globals {
-       /* 0-based. IOW: the number of chars we printer on current line */
+       /* 0-based. IOW: the number of chars we printed on current line */
        unsigned cur_x;
        unsigned output_width;
 
@@ -76,7 +75,7 @@ struct globals {
 };
 #define G (*ptr_to_globals)
 #define INIT_G() do { \
-        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+       SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 } while (0)
 
 
@@ -95,26 +94,17 @@ static void ensure_buffer_capacity(int bufindex)
        }
 }
 
-#if ENABLE_FEATURE_CLEAN_UP
-static void maybe_free_buffers(void)
-{
-       free(G.width);
-       free(G.more);
-}
-#else
-# define maybe_free_buffers() ((void)0)
-#endif
-
 /* NB: this function is never called with "bad" chars
  * (control chars or chars >= 0x7f)
  */
 static void out_char(char c)
 {
        G.cur_x++;
+       if (G.cur_x > G.output_width)
+               return;
        if (G.cur_x == G.output_width)
                c = '+';
-       if (G.cur_x <= G.output_width)
-               putchar(c);
+       putchar(c);
 }
 
 /* NB: this function is never called with "bad" chars
@@ -348,12 +338,14 @@ static void dump_by_user(PROC *current, uid_t uid)
                dump_by_user(walk->child, uid);
 }
 
+#if ENABLE_FEATURE_SHOW_THREADS
 static void handle_thread(const char *comm, pid_t pid, pid_t ppid, uid_t uid)
 {
        char threadname[COMM_LEN + 2];
        sprintf(threadname, "{%.*s}", COMM_LEN - 2, comm);
        add_proc(threadname, pid, ppid, uid/*, 1*/);
 }
+#endif
 
 static void mread_proc(void)
 {
@@ -382,14 +374,13 @@ int pstree_main(int argc UNUSED_PARAM, char **argv)
 
        INIT_G();
 
-       get_terminal_width_height(1, &G.output_width, NULL);
+       get_terminal_width_height(0, &G.output_width, NULL);
 
+       opt_complementary = "?1";
        getopt32(argv, "p");
        argv += optind;
 
        if (argv[0]) {
-               if (argv[1])
-                       bb_show_usage();
                if (argv[0][0] >= '0' && argv[0][0] <= '9') {
                        pid = xatoi(argv[0]);
                } else {
@@ -408,6 +399,9 @@ int pstree_main(int argc UNUSED_PARAM, char **argv)
                }
        }
 
-       maybe_free_buffers();
+       if (ENABLE_FEATURE_CLEAN_UP) {
+               free(G.width);
+               free(G.more);
+       }
        return 0;
 }