diff options
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/top/display.c | 74 | ||||
-rw-r--r-- | usr.bin/top/top.c | 4 | ||||
-rw-r--r-- | usr.bin/top/username.c | 14 | ||||
-rw-r--r-- | usr.bin/top/utils.c | 69 | ||||
-rw-r--r-- | usr.bin/top/utils.h | 5 |
5 files changed, 74 insertions, 92 deletions
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c index e8ed5fd24a8..ef8bb8e00d6 100644 --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.15 2003/06/18 08:36:31 deraadt Exp $ */ +/* $OpenBSD: display.c,v 1.16 2003/06/19 22:40:45 millert Exp $ */ /* * Top users/processes display for Unix @@ -75,7 +75,7 @@ static int display_width = MAX_COLS; static char *cpustates_tag(void); static int string_count(char **); -static void summary_format(char *, int *, char **); +static void summary_format(char *, size_t, int *, char **); static void line_update(char *, char *, int, int); #define lineindex(l) ((l)*display_width) @@ -277,7 +277,8 @@ i_procstates(int total, int *brkdn) } /* format and print the process state summary */ - summary_format(procstates_buffer, brkdn, procstate_names); + summary_format(procstates_buffer, sizeof(procstates_buffer), brkdn, + procstate_names); if (fputs(procstates_buffer, stdout) == EOF) exit(1); @@ -320,7 +321,7 @@ u_procstates(int total, int *brkdn) /* see if any of the state numbers has changed */ if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0) { /* format and update the line */ - summary_format(new, brkdn, procstate_names); + summary_format(new, sizeof(new), brkdn, procstate_names); line_update(procstates_buffer, new, x_brkdn, y_brkdn); memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); } @@ -461,7 +462,8 @@ i_memory(int *stats) lastline++; /* format and print the memory summary */ - summary_format(memory_buffer, stats, memory_names); + summary_format(memory_buffer, sizeof(memory_buffer), stats, + memory_names); if (fputs(memory_buffer, stdout) == EOF) exit(1); } @@ -472,7 +474,7 @@ u_memory(int *stats) static char new[MAX_COLS]; /* format the new line */ - summary_format(new, stats, memory_names); + summary_format(new, sizeof(new), stats, memory_names); line_update(memory_buffer, new, x_mem, y_mem); } @@ -566,7 +568,8 @@ u_header(char *text) void i_process(int line, char *thisline) { - char *p, *base; + char *base; + size_t len; /* make sure we are on the correct line */ while (lastline < y_procs + line) { @@ -584,17 +587,19 @@ i_process(int line, char *thisline) /* copy it in to our buffer */ base = smart_terminal ? screenbuf + lineindex(line) : screenbuf; - p = strecpy(base, thisline); - - /* zero fill the rest of it */ - memset(p, 0, display_width - (p - base)); + len = strlcpy(base, thisline, display_width); + if (len < (size_t)display_width) { + /* zero fill the rest of it */ + memset(base + len, 0, display_width - len); + } } void u_process(int linenum, char *linebuf) { int screen_line = linenum + Header_lines; - char *optr, *bufferline; + char *bufferline; + size_t len; /* remember a pointer to the current line in the screen buffer */ bufferline = &screenbuf[lineindex(linenum)]; @@ -620,10 +625,11 @@ u_process(int linenum, char *linebuf) exit(1); /* copy it in to the buffer */ - optr = strecpy(bufferline, linebuf); - - /* zero fill the rest of it */ - memset(optr, 0, display_width - (optr - bufferline)); + len = strlcpy(bufferline, linebuf, display_width); + if (len < (size_t)display_width) { + /* zero fill the rest of it */ + memset(bufferline + len, 0, display_width - len); + } } else { line_update(bufferline, linebuf, 0, linenum + Header_lines); } @@ -826,14 +832,24 @@ string_count(char **pp) return (cnt); } +#define COPYLEFT(to, from) \ + do { \ + len = strlcpy((to), (from), left); \ + if (len >= left) \ + return; \ + p += len; \ + left -= len; \ + } while (0) + static void -summary_format(char *str, int *numbers, char **names) +summary_format(char *buf, size_t left, int *numbers, char **names) { char *p, *thisname; + size_t len; int num; /* format each number followed by its string */ - p = str; + p = buf; while ((thisname = *names++) != NULL) { /* get the number to format */ num = *numbers++; @@ -842,26 +858,32 @@ summary_format(char *str, int *numbers, char **names) /* is this number in kilobytes? */ if (thisname[0] == 'K') { /* yes: format it as a memory value */ - p = strecpy(p, format_k(num)); + COPYLEFT(p, format_k(num)); /* * skip over the K, since it was included by * format_k */ - p = strecpy(p, thisname + 1); + COPYLEFT(p, thisname + 1); } else if (num > 0) { - p = strecpy(p, itoa(num)); - p = strecpy(p, thisname); + len = snprintf(p, left, "%d%s", num, thisname); + if (len == (size_t)-1 || len >= left) + return; + p += len; + left -= len; } + } else { + /* + * Ignore negative numbers, but display corresponding + * string. + */ + COPYLEFT(p, thisname); } - /* ignore negative numbers, but display corresponding string */ - else - p = strecpy(p, thisname); } /* if the last two characters in the string are ", ", delete them */ p -= 2; - if (p >= str && p[0] == ',' && p[1] == ' ') + if (p >= buf && p[0] == ',' && p[1] == ' ') *p = '\0'; } diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c index b30f0e5d404..9472a5567bd 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -1,4 +1,4 @@ -/* $OpenBSD: top.c,v 1.25 2003/06/18 08:42:17 deraadt Exp $ */ +/* $OpenBSD: top.c,v 1.26 2003/06/19 22:40:45 millert Exp $ */ /* * Top users/processes display for Unix @@ -280,7 +280,7 @@ main(int argc, char *argv[]) /* set constants for username/uid display correctly */ if (!do_unames) { uname_field = " UID "; - get_userid = itoa7; + get_userid = format_uid; } /* initialize the kernel memory interface */ if (machine_init(&statics) == -1) diff --git a/usr.bin/top/username.c b/usr.bin/top/username.c index 42cdd37c780..ff99c932e57 100644 --- a/usr.bin/top/username.c +++ b/usr.bin/top/username.c @@ -1,4 +1,4 @@ -/* $OpenBSD: username.c,v 1.11 2003/06/15 16:24:44 millert Exp $ */ +/* $OpenBSD: username.c,v 1.12 2003/06/19 22:40:45 millert Exp $ */ /* * Top users/processes display for Unix @@ -57,7 +57,7 @@ struct hash_el { uid_t uid; - char name[9]; + char name[_PW_NAME_LEN + 1]; }; static int enter_user(uid_t, char *, int); @@ -65,15 +65,11 @@ static int get_user(uid_t); #define is_empty_hash(x) (hash_table[x].name[0] == 0) -/* simple minded hashing function */ /* - * Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for - * the hash_table. Applied abs() function to fix. 2/16/96 tpugh + * Simple minded hashing function, assumes i is unsigned. */ -#define hashit(i) (abs(i) % Table_size) +#define hashit(i) (i % Table_size) -/* K&R requires that statically declared tables be initialized to zero. */ -/* We depend on that for hash_table and YOUR compiler had BETTER do it! */ struct hash_el hash_table[Table_size]; char * @@ -177,5 +173,5 @@ get_user(uid_t uid) } #endif /* if we can't find the name at all, then use the uid as the name */ - return (enter_user(uid, itoa7(uid), 1)); + return (enter_user(uid, format_uid(uid), 1)); } diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c index 8d22390cf8d..d0a36b4db81 100644 --- a/usr.bin/top/utils.c +++ b/usr.bin/top/utils.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.c,v 1.10 2003/06/12 23:09:30 deraadt Exp $ */ +/* $OpenBSD: utils.c,v 1.11 2003/06/19 22:40:45 millert Exp $ */ /* * Top users/processes display for Unix @@ -60,57 +60,36 @@ atoiwi(char *str) } /* - * itoa - convert integer (decimal) to ascii string for positive numbers - * only (we don't bother with negative numbers since we know we - * don't use them). + * itoa - convert integer (decimal) to ascii string. */ char * itoa(int val) { static char buffer[16]; /* result is built here */ - char *ptr; /* * 16 is sufficient since the largest number we will ever convert * will be 2^32-1, which is 10 digits. */ - ptr = buffer + sizeof(buffer); - *--ptr = '\0'; - if (val == 0) { - *--ptr = '0'; - } else { - while (val != 0) { - *--ptr = (val % 10) + '0'; - val /= 10; - } - } - return (ptr); + (void)snprintf(buffer, sizeof(buffer), "%d", val); + return (buffer); } /* - * itoa7(val) - like itoa, except the number is right justified in a 7 - * character field. This code is a duplication of itoa instead of - * a front end to a more general routine for efficiency. + * format_uid(uid) - like itoa, except for uid_t and the number is right + * justified in a 6 character field to match uname_field in top.c. */ char * -itoa7(int val) +format_uid(uid_t uid) { - static char buffer[25]; /* result is built here */ - char *ptr; + static char buffer[16]; /* result is built here */ - ptr = buffer + sizeof(buffer); - *--ptr = '\0'; - if (val == 0) { - *--ptr = '0'; - } else { - while (val != 0) { - *--ptr = (val % 10) + '0'; - val /= 10; - } - } - while (ptr > buffer + sizeof(buffer) - 7) - *--ptr = ' '; - return (ptr); + /* + * 16 is sufficient since the largest uid we will ever convert + * will be 2^32-1, which is 10 digits. + */ + (void)snprintf(buffer, sizeof(buffer), "%6u", uid); + return (buffer); } /* @@ -130,18 +109,6 @@ digits(int val) } /* - * strecpy(to, from) - copy string "from" into "to" and return a pointer - * to the END of the string "to". - */ -char * -strecpy(char *to, char *from) -{ - while ((*to++ = *from++) != '\0') - ; - return (--to); -} - -/* * string_index(string, array) - find string in array and return index */ int @@ -344,9 +311,9 @@ format_k(int amt) { static char retarray[NUM_STRINGS][16]; static int index = 0; - char *p, *ret, tag = 'K'; + char *ret, tag = 'K'; - p = ret = retarray[index]; + ret = retarray[index]; index = (index + 1) % NUM_STRINGS; if (amt >= 10000) { @@ -357,8 +324,6 @@ format_k(int amt) tag = 'G'; } } - p = strecpy(p, itoa(amt)); - *p++ = tag; - *p = '\0'; + snprintf(ret, sizeof(retarray[0]), "%d%c", amt, tag); return (ret); } diff --git a/usr.bin/top/utils.h b/usr.bin/top/utils.h index eada1f3149a..5c411f47267 100644 --- a/usr.bin/top/utils.h +++ b/usr.bin/top/utils.h @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.h,v 1.5 2002/07/15 17:20:36 deraadt Exp $ */ +/* $OpenBSD: utils.h,v 1.6 2003/06/19 22:40:45 millert Exp $ */ /* * Top users/processes display for Unix @@ -32,9 +32,8 @@ int atoiwi(char *); char *itoa(int); -char *itoa7(int); +char *format_uid(uid_t); int digits(int); -char *strecpy(char *, char *); int string_index(char *, char **); char **argparse(char *, int *); int percentages(int, int *, long *, long *, long *); |