Index: usr.bin/top/display.c =================================================================== --- usr.bin/top/display.c +++ usr.bin/top/display.c @@ -57,9 +57,8 @@ static int lmpid = 0; static int last_hi = 0; /* used in u_process and u_endscreen */ static int lastline = 0; -static int display_width = MAX_COLS; -#define lineindex(l) ((l)*display_width) +#define lineindex(l) ((l)*screen_width) /* things initialized by display_init and used thruout */ @@ -94,6 +93,9 @@ static void summary_format(char *, int *, const char * const *); static void line_update(char *, char *, int, int); +static int setup_buffer_bufsiz = 0; +static char * setup_buffer(char *, int); + int x_lastpid = 10; int y_lastpid = 0; int x_loadave = 33; @@ -138,17 +140,9 @@ if (lines < 0) lines = 0; - /* we don't want more than MAX_COLS columns, since the machine-dependent - modules make static allocations based on MAX_COLS and we don't want - to run off the end of their buffers */ - display_width = screen_width; - if (display_width >= MAX_COLS) - { - display_width = MAX_COLS - 1; - } /* now, allocate space for the screen buffer */ - screenbuf = calloc(lines, display_width); + screenbuf = calloc(lines, screen_width); if (screenbuf == NULL) { /* oops! */ @@ -336,7 +330,7 @@ } static int ltotal = 0; -static char procstates_buffer[MAX_COLS]; +static char *procstates_buffer = NULL; /* * *_procstates(total, brkdn, names) - print the process summary line @@ -350,6 +344,8 @@ { int i; + procstates_buffer = setup_buffer(procstates_buffer, 0); + /* write current number of processes and remember the value */ printf("%d %s:", total, (ps.thread) ? "threads" :"processes"); ltotal = total; @@ -372,9 +368,11 @@ void u_procstates(int total, int *brkdn) { - static char new[MAX_COLS]; + static char *new = NULL; int i; + new = setup_buffer(new, 0); + /* update number of processes only if it has changed */ if (ltotal != total) { @@ -551,11 +549,13 @@ * for i_memory ONLY: cursor is on the previous line */ -static char memory_buffer[MAX_COLS]; +static char *memory_buffer = NULL; void i_memory(int *stats) { + memory_buffer = setup_buffer(memory_buffer, 0); + fputs("\nMem: ", stdout); lastline++; @@ -567,8 +567,10 @@ void u_memory(int *stats) { - static char new[MAX_COLS]; + static char *new = NULL; + new = setup_buffer(new, 0); + /* format the new line */ summary_format(new, stats, memory_names); line_update(memory_buffer, new, x_mem, y_mem); @@ -580,11 +582,13 @@ * Assumptions: cursor is on "lastline" * for i_arc ONLY: cursor is on the previous line */ -static char arc_buffer[MAX_COLS]; +static char *arc_buffer = NULL; void i_arc(int *stats) { + arc_buffer = setup_buffer(arc_buffer, 0); + if (arc_names == NULL) return; @@ -599,8 +603,10 @@ void u_arc(int *stats) { - static char new[MAX_COLS]; + static char *new = NULL; + new = setup_buffer(new, 0); + if (arc_names == NULL) return; @@ -616,11 +622,13 @@ * Assumptions: cursor is on "lastline" * for i_carc ONLY: cursor is on the previous line */ -static char carc_buffer[MAX_COLS]; +static char *carc_buffer = NULL; void i_carc(int *stats) { + carc_buffer = setup_buffer(carc_buffer, 0); + if (carc_names == NULL) return; @@ -635,8 +643,10 @@ void u_carc(int *stats) { - static char new[MAX_COLS]; + static char *new = NULL; + new = setup_buffer(new, 0); + if (carc_names == NULL) return; @@ -652,11 +662,13 @@ * for i_swap ONLY: cursor is on the previous line */ -static char swap_buffer[MAX_COLS]; +static char *swap_buffer = NULL; void i_swap(int *stats) { + swap_buffer = setup_buffer(swap_buffer, 0); + fputs("\nSwap: ", stdout); lastline++; @@ -668,8 +680,10 @@ void u_swap(int *stats) { - static char new[MAX_COLS]; + static char *new = NULL; + new = setup_buffer(new, 0); + /* format the new line */ summary_format(new, stats, swap_names); line_update(swap_buffer, new, x_swap, y_swap); @@ -689,7 +703,7 @@ * respect to screen updates). */ -static char next_msg[MAX_COLS + 5]; +static char *next_msg = NULL; static int msglen = 0; /* Invariant: msglen is always the length of the message currently displayed on the screen (even when next_msg doesn't contain that message). */ @@ -697,6 +711,7 @@ void i_message(void) { + next_msg = setup_buffer(next_msg, 5); while (lastline < y_message) { @@ -736,7 +751,7 @@ int width; s = NULL; - width = display_width; + width = screen_width; header_length = strlen(text); if (header_length >= width) { s = strndup(text, width); @@ -807,7 +822,7 @@ } /* truncate the line to conform to our current screen width */ - thisline[display_width] = '\0'; + thisline[screen_width] = '\0'; /* write the line out */ fputs(thisline, stdout); @@ -817,7 +832,7 @@ p = stpcpy(base, thisline); /* zero fill the rest of it */ - memset(p, 0, display_width - (p - base)); + memset(p, 0, screen_width - (p - base)); } void @@ -831,7 +846,7 @@ bufferline = &screenbuf[lineindex(line)]; /* truncate the line to conform to our current screen width */ - newline[display_width] = '\0'; + newline[screen_width] = '\0'; /* is line higher than we went on the last display? */ if (line >= last_hi) @@ -856,7 +871,7 @@ optr = stpcpy(bufferline, newline); /* zero fill the rest of it */ - memset(optr, 0, display_width - (optr - bufferline)); + memset(optr, 0, screen_width - (optr - bufferline)); } else { @@ -1235,7 +1250,7 @@ } while (ch != '\0'); /* zero out the rest of the line buffer -- MUST BE DONE! */ - diff = display_width - newcol; + diff = screen_width - newcol; if (diff > 0) { memset(old, 0, diff); @@ -1327,3 +1342,31 @@ printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs); } } + +static char * +setup_buffer(char *buffer, int addlen) +{ + char *b = NULL; + + if (NULL == buffer) { + setup_buffer_bufsiz = screen_width; + b = calloc(setup_buffer_bufsiz + addlen, sizeof(char)); + } else { + if (screen_width > setup_buffer_bufsiz) { + setup_buffer_bufsiz = screen_width; + free(buffer); + b = calloc(setup_buffer_bufsiz + addlen, + sizeof(char)); + } else { + b = buffer; + } + } + + if (NULL == b) { + fprintf(stderr, "%s: can't allocate sufficient memory\n", + myname); + exit(4); + } + + return b; +} Index: usr.bin/top/machine.c =================================================================== --- usr.bin/top/machine.c +++ usr.bin/top/machine.c @@ -870,7 +870,6 @@ long p_tot, s_tot; char *cmdbuf = NULL; char **args; - const int cmdlen = 256; static struct sbuf* procbuf = NULL; /* clean up from last time. */ @@ -935,9 +934,9 @@ break; } - cmdbuf = calloc(cmdlen + 1, 1); + cmdbuf = calloc(screen_width + 1, 1); if (cmdbuf == NULL) { - warn("calloc(%d)", cmdlen + 1); + warn("calloc(%d)", screen_width + 1); return NULL; } @@ -944,22 +943,22 @@ if (!(flags & FMT_SHOWARGS)) { if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) { - snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm, + snprintf(cmdbuf, screen_width, "%s{%s%s}", pp->ki_comm, pp->ki_tdname, pp->ki_moretdname); } else { - snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm); + snprintf(cmdbuf, screen_width, "%s", pp->ki_comm); } } else { if (pp->ki_flag & P_SYSTEM || - (args = kvm_getargv(kd, pp, cmdlen)) == NULL || + (args = kvm_getargv(kd, pp, screen_width)) == NULL || !(*args)) { if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) { - snprintf(cmdbuf, cmdlen, + snprintf(cmdbuf, screen_width, "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname, pp->ki_moretdname); } else { - snprintf(cmdbuf, cmdlen, + snprintf(cmdbuf, screen_width, "[%s]", pp->ki_comm); } } else { @@ -969,7 +968,7 @@ size_t argbuflen; size_t len; - argbuflen = cmdlen * 4; + argbuflen = screen_width * 4; argbuf = calloc(argbuflen + 1, 1); if (argbuf == NULL) { warn("calloc(%zu)", argbuflen + 1); @@ -1005,21 +1004,21 @@ if (strcmp(cmd, pp->ki_comm) != 0) { if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) - snprintf(cmdbuf, cmdlen, + snprintf(cmdbuf, screen_width, "%s (%s){%s%s}", argbuf, pp->ki_comm, pp->ki_tdname, pp->ki_moretdname); else - snprintf(cmdbuf, cmdlen, + snprintf(cmdbuf, screen_width, "%s (%s)", argbuf, pp->ki_comm); } else { if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) - snprintf(cmdbuf, cmdlen, + snprintf(cmdbuf, screen_width, "%s{%s%s}", argbuf, pp->ki_tdname, pp->ki_moretdname); else - strlcpy(cmdbuf, argbuf, cmdlen); + strlcpy(cmdbuf, argbuf, screen_width); } free(argbuf); } Index: usr.bin/top/screen.c =================================================================== --- usr.bin/top/screen.c +++ usr.bin/top/screen.c @@ -62,8 +62,7 @@ char *term_name; int status; - /* set defaults in case we aren't smart */ - screen_width = MAX_COLS; + screen_width = 0; screen_length = 0; if (!interactive) Index: usr.bin/top/top.h =================================================================== --- usr.bin/top/top.h +++ usr.bin/top/top.h @@ -13,9 +13,6 @@ /* Number of lines of header information on the standard screen */ extern int Header_lines; -/* Maximum number of columns allowed for display */ -#define MAX_COLS 512 - /* Special atoi routine returns either a non-negative number or one of: */ #define Infinity -1 #define Invalid -2