pstree: fix width logic. +30 bytes

Signed-off-by: Lauri Kasanen <curaga@operamail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Lauri Kasanen 2010-12-05 15:53:55 +01:00 committed by Denys Vlasenko
parent 6578f2cf5b
commit e48e6f85bf

View File

@ -61,16 +61,17 @@ typedef struct child {
#define first_3 "-+-" #define first_3 "-+-"
struct globals { struct globals {
PROC *list; /* 0-based. IOW: the number of chars we printer on current line */
unsigned cur_x;
unsigned output_width;
/* The buffers will be dynamically increased in size as needed */ /* The buffers will be dynamically increased in size as needed */
unsigned capacity; unsigned capacity;
int *width; unsigned *width;
int *more; 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 */ smallint dumped; /* used by dump_by_user */
}; };
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
@ -83,8 +84,7 @@ struct globals {
* Allocates additional buffer space for width and more as needed. * Allocates additional buffer space for width and more as needed.
* The first call will allocate the first buffer. * The first call will allocate the first buffer.
* *
* bufindex the index that will be used after the call * bufindex the index that will be used after the call to this function.
* to this function.
*/ */
static void ensure_buffer_capacity(int bufindex) static void ensure_buffer_capacity(int bufindex)
{ {
@ -111,10 +111,10 @@ static void maybe_free_buffers(void)
static void out_char(char c) static void out_char(char c)
{ {
G.cur_x++; G.cur_x++;
// if (G.cur_x <= G.output_width) if (G.cur_x == G.output_width)
c = '+';
if (G.cur_x <= G.output_width)
putchar(c); putchar(c);
// else if (G.cur_x == G.output_width - 1)
// putchar('+');
} }
/* NB: this function is never called with "bad" chars /* NB: this function is never called with "bad" chars
@ -129,7 +129,7 @@ static void out_string(const char *str)
static void out_newline(void) static void out_newline(void)
{ {
putchar('\n'); putchar('\n');
G.cur_x = 1; G.cur_x = 0;
} }
static PROC *find_proc(pid_t pid) static PROC *find_proc(pid_t pid)
@ -249,6 +249,7 @@ dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
{ {
CHILD *walk, *next, **scan; CHILD *walk, *next, **scan;
int lvl, i, add, offset, count, comm_len, first; int lvl, i, add, offset, count, comm_len, first;
char tmp[sizeof(int)*3 + 4];
if (!current) if (!current)
return; return;
@ -275,17 +276,15 @@ dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
} }
} }
if (rep < 2) add = 0;
add = 0; if (rep > 1) {
else { add += sprintf(tmp, "%d*[", rep);
add = printf("%d", rep) + 2; out_string(tmp);
out_string("*[");
} }
comm_len = out_args(current->comm); comm_len = out_args(current->comm);
if (option_mask32 /*& OPT_PID*/) { if (option_mask32 /*& OPT_PID*/) {
out_char('('); comm_len += sprintf(tmp, "(%d)", (int)current->pid);
comm_len += printf("%d", (int)current->pid) + 2; out_string(tmp);
out_char(')');
} }
offset = G.cur_x; offset = G.cur_x;
@ -298,12 +297,12 @@ dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
G.more[level] = !last; G.more[level] = !last;
G.width[level] = comm_len + G.cur_x - offset + add; G.width[level] = comm_len + G.cur_x - offset + add;
// if (G.cur_x >= G.output_width) { if (G.cur_x >= G.output_width) {
// out_string(first_3); //out_string(first_3); - why? it won't print anything
// out_char('+'); //out_char('+');
// out_newline(); out_newline();
// return; return;
// } }
first = 1; first = 1;
for (walk = current->children; walk; walk = next) { for (walk = current->children; walk; walk = next) {
@ -382,9 +381,8 @@ int pstree_main(int argc UNUSED_PARAM, char **argv)
long uid = 0; long uid = 0;
INIT_G(); INIT_G();
G.cur_x = 1;
// get_terminal_width_height(1, &G.output_width, NULL); get_terminal_width_height(1, &G.output_width, NULL);
getopt32(argv, "p"); getopt32(argv, "p");
argv += optind; argv += optind;