ash: add INT_OFF/ON around allocations
[oweals/busybox.git] / procps / pstree.c
index f09e8da34a8d64a870bd7ef1b25fd0e46a3f49ed..f97e99639f3303b46a944c189224faa823a51330 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"
 
 struct child;
 
+#ifdef ENABLE_FEATURE_SHOW_THREADS
+/* For threads, we add {...} around the comm, so we need two extra bytes */
+# define COMM_DISP_LEN (COMM_LEN + 2)
+#else
+# define COMM_DISP_LEN COMM_LEN
+#endif
+
 typedef struct proc {
-       char comm[COMM_LEN + 1];
+       char comm[COMM_DISP_LEN + 1];
 //     char flags; - unused, delete?
        pid_t pid;
        uid_t uid;
@@ -61,21 +67,22 @@ typedef struct child {
 #define first_3  "-+-"
 
 struct globals {
-       PROC *list;
+       /* 0-based. IOW: the number of chars we printed on current line */
+       unsigned cur_x;
+       unsigned output_width;
 
        /* The buffers will be dynamically increased in size as needed */
        unsigned capacity;
-       int *width;
-       int *more;
+       unsigned *width;
+       uint8_t *more;
+
+       PROC *list;
 
-// Disabled, since code is broken anyway and needs fixing
-//     unsigned output_width;
-       unsigned cur_x;
        smallint dumped; /* used by dump_by_user */
 };
 #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)
 
 
@@ -83,8 +90,7 @@ struct globals {
  * Allocates additional buffer space for width and more as needed.
  * The first call will allocate the first buffer.
  *
- * bufindex  the index that will be used after the call
- *           to this function.
+ * bufindex  the index that will be used after the call to this function.
  */
 static void ensure_buffer_capacity(int bufindex)
 {
@@ -95,26 +101,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)
-               putchar(c);
-//     else if (G.cur_x == G.output_width - 1)
-//             putchar('+');
+       if (G.cur_x > G.output_width)
+               return;
+       if (G.cur_x == G.output_width)
+               c = '+';
+       putchar(c);
 }
 
 /* NB: this function is never called with "bad" chars
@@ -129,7 +126,7 @@ static void out_string(const char *str)
 static void out_newline(void)
 {
        putchar('\n');
-       G.cur_x = 1;
+       G.cur_x = 0;
 }
 
 static PROC *find_proc(pid_t pid)
@@ -249,6 +246,7 @@ dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
 {
        CHILD *walk, *next, **scan;
        int lvl, i, add, offset, count, comm_len, first;
+       char tmp[sizeof(int)*3 + 4];
 
        if (!current)
                return;
@@ -275,17 +273,15 @@ dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
                }
        }
 
-       if (rep < 2)
-               add = 0;
-       else {
-               add = printf("%d", rep) + 2;
-               out_string("*[");
+       add = 0;
+       if (rep > 1) {
+               add += sprintf(tmp, "%d*[", rep);
+               out_string(tmp);
        }
        comm_len = out_args(current->comm);
        if (option_mask32 /*& OPT_PID*/) {
-               out_char('(');
-               comm_len += printf("%d", (int)current->pid) + 2;
-               out_char(')');
+               comm_len += sprintf(tmp, "(%d)", (int)current->pid);
+               out_string(tmp);
        }
        offset = G.cur_x;
 
@@ -298,12 +294,12 @@ dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
        G.more[level] = !last;
 
        G.width[level] = comm_len + G.cur_x - offset + add;
-//     if (G.cur_x >= G.output_width) {
-//             out_string(first_3);
-//             out_char('+');
-//             out_newline();
-//             return;
-//     }
+       if (G.cur_x >= G.output_width) {
+               //out_string(first_3); - why? it won't print anything
+               //out_char('+');
+               out_newline();
+               return;
+       }
 
        first = 1;
        for (walk = current->children; walk; walk = next) {
@@ -349,17 +345,21 @@ 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);
+       char threadname[COMM_DISP_LEN + 1];
+       sprintf(threadname, "{%.*s}", (int)sizeof(threadname) - 3, comm);
        add_proc(threadname, pid, ppid, uid/*, 1*/);
 }
+#endif
 
 static void mread_proc(void)
 {
        procps_status_t *p = NULL;
+#if ENABLE_FEATURE_SHOW_THREADS
        pid_t parent = 0;
+#endif
        int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
 
        while ((p = procps_scan(p, flags)) != NULL) {
@@ -370,7 +370,9 @@ static void mread_proc(void)
 #endif
                {
                        add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
+#if ENABLE_FEATURE_SHOW_THREADS
                        parent = p->pid;
+#endif
                }
        }
 }
@@ -382,16 +384,14 @@ int pstree_main(int argc UNUSED_PARAM, char **argv)
        long uid = 0;
 
        INIT_G();
-       G.cur_x = 1;
 
-//     get_terminal_width_height(1, &G.output_width, NULL);
+       G.output_width = get_terminal_width(0);
 
+       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 {
@@ -410,6 +410,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;
 }