From owner-svn-src-all@freebsd.org Sun Jun 3 23:40:56 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 44838FEDEB2; Sun, 3 Jun 2018 23:40:56 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id E68E083FD0; Sun, 3 Jun 2018 23:40:55 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C8A7E25606; Sun, 3 Jun 2018 23:40:55 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w53NetrT099894; Sun, 3 Jun 2018 23:40:55 GMT (envelope-from eadler@FreeBSD.org) Received: (from eadler@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w53Nesm4099887; Sun, 3 Jun 2018 23:40:54 GMT (envelope-from eadler@FreeBSD.org) Message-Id: <201806032340.w53Nesm4099887@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: eadler set sender to eadler@FreeBSD.org using -f From: Eitan Adler Date: Sun, 3 Jun 2018 23:40:54 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334593 - head/usr.bin/top X-SVN-Group: head X-SVN-Commit-Author: eadler X-SVN-Commit-Paths: head/usr.bin/top X-SVN-Commit-Revision: 334593 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 03 Jun 2018 23:40:56 -0000 Author: eadler Date: Sun Jun 3 23:40:54 2018 New Revision: 334593 URL: https://svnweb.freebsd.org/changeset/base/334593 Log: top(1): another pass of cleanup - avoid the need to call a function to get size of known array. I'll likely re-arrange some of the indirect in a later to avoid the magic constants. - use correct type - add const - replace caddr_t with void*. This corrects an alignment warning. - remove duplicated include from immediately prior commit Under base clang we're now down to: - 3 warning in top.c, 1 warning in mahcine.c, 4 warning in display.c, - 1 warning in utils.c Tested with base clang, gcc7, gcc9, base gcc (mips) Modified: head/usr.bin/top/display.c head/usr.bin/top/machine.c head/usr.bin/top/machine.h head/usr.bin/top/top.c head/usr.bin/top/top.h head/usr.bin/top/utils.c head/usr.bin/top/utils.h Modified: head/usr.bin/top/display.c ============================================================================== --- head/usr.bin/top/display.c Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/display.c Sun Jun 3 23:40:54 2018 (r334593) @@ -28,6 +28,7 @@ * *_process, u_endscreen. */ +#include #include #include @@ -90,8 +91,7 @@ static int cpustates_column; static enum { OFF, ON, ERASE } header_status = ON; -static int string_count(char **); -static void summary_format(char *, int *, char **); +static void summary_format(char *, int *, const char * const *); static void line_update(char *, char *, int, int); int x_lastpid = 10; @@ -203,23 +203,23 @@ int display_init(struct statics * statics) { /* save pointers and allocate space for names */ procstate_names = statics->procstate_names; - num_procstates = string_count(procstate_names); + num_procstates = 8; assert(num_procstates > 0); lprocstates = calloc(num_procstates, sizeof(int)); cpustate_names = statics->cpustate_names; swap_names = statics->swap_names; - num_swap = string_count(swap_names); + num_swap = 7; assert(num_swap > 0); lswap = calloc(num_swap, sizeof(int)); - num_cpustates = string_count(cpustate_names); + num_cpustates = CPUSTATES; assert(num_cpustates > 0); lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus); cpustate_columns = calloc(num_cpustates, sizeof(int)); memory_names = statics->memory_names; - num_memory = string_count(memory_names); + num_memory = 7; assert(num_memory > 0); lmemory = calloc(num_memory, sizeof(int)); @@ -422,8 +422,8 @@ i_cpustates(int *states) { int i = 0; int value; - char **names; - char *thisname; + const char * const *names; + const char *thisname; int cpu; for (cpu = 0; cpu < num_cpus; cpu++) { @@ -761,7 +761,7 @@ trim_header(const char *text) void i_header(const char *text) { - const char *s; + char *s; s = trim_header(text); if (s != NULL) @@ -1075,23 +1075,11 @@ readline(char *buffer, int size, int numeric) /* internal support routines */ -static int string_count(char **pp) +static void summary_format(char *str, int *numbers, const char * const *names) { - int cnt; - - cnt = 0; - while (*pp++ != NULL) - { - cnt++; - } - return(cnt); -} - -static void summary_format(char *str, int *numbers, char **names) -{ char *p; int num; - char *thisname; + const char *thisname; char rbuf[6]; /* format each number followed by its string */ Modified: head/usr.bin/top/machine.c ============================================================================== --- head/usr.bin/top/machine.c Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/machine.c Sun Jun 3 23:40:54 2018 (r334593) @@ -15,7 +15,6 @@ * $FreeBSD$ */ -#include #include #include #include @@ -147,12 +146,12 @@ static long cp_diff[CPUSTATES]; /* these are for detailing the process states */ -static int process_states[8]; static const char *procstatenames[] = { "", " starting, ", " running, ", " sleeping, ", " stopped, ", " zombie, ", " waiting, ", " lock, ", NULL }; +static int process_states[nitems(procstatenames)]; /* these are for detailing the cpu states */ @@ -163,29 +162,29 @@ static const char *cpustatenames[] = { /* these are for detailing the memory statistics */ -static int memory_stats[7]; static const char *memorynames[] = { "K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ", "K Free", NULL }; +static int memory_stats[nitems(memorynames)]; -static int arc_stats[7]; static const char *arcnames[] = { "K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other", NULL }; +static int arc_stats[nitems(arcnames)]; -static int carc_stats[4]; static const char *carcnames[] = { "K Compressed, ", "K Uncompressed, ", ":1 Ratio, ", NULL }; +static int carc_stats[nitems(carcnames)]; -static int swap_stats[7]; static const char *swapnames[] = { "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", NULL }; +static int swap_stats[nitems(swapnames)]; /* these are for keeping track of the proc array */ @@ -914,13 +913,13 @@ get_process_info(struct system_info *si, struct proces /* pass back a handle */ handle.next_proc = pref; handle.remaining = active_procs; - return ((caddr_t)&handle); + return ((void*)&handle); } static char fmt[512]; /* static area where result is built */ char * -format_next_process(caddr_t xhandle, char *(*get_userid)(int), int flags) +format_next_process(void* xhandle, char *(*get_userid)(int), int flags) { struct kinfo_proc *pp; const struct kinfo_proc *oldp; @@ -932,11 +931,11 @@ format_next_process(caddr_t xhandle, char *(*get_useri size_t state; struct rusage ru, *rup; long p_tot, s_tot; - char *proc_fmt; + const char *proc_fmt; char thr_buf[6]; char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1]; char *cmdbuf = NULL; - const char * const *args; + char **args; const int cmdlen = 128; /* find and remember the next proc structure */ @@ -1026,8 +1025,9 @@ format_next_process(caddr_t xhandle, char *(*get_useri "[%s]", pp->ki_comm); } } else { - char *src, *dst, *argbuf; - char *cmd; + const char *src; + char *dst, *argbuf; + const char *cmd; size_t argbuflen; size_t len; Modified: head/usr.bin/top/machine.h ============================================================================== --- head/usr.bin/top/machine.h Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/machine.h Sun Jun 3 23:40:54 2018 (r334593) @@ -10,8 +10,11 @@ #ifndef MACHINE_H #define MACHINE_H -#include "top.h" +#define NUM_AVERAGES 3 +/* Log base 2 of 1024 is 10 (2^10 == 1024) */ +#define LOG1024 10 + /* * the statics struct is filled in by machine_init */ @@ -76,7 +79,7 @@ struct process_select /* routines defined by the machine dependent module */ const char *format_header(const char *uname_field); -char *format_next_process(caddr_t handle, char *(*get_userid)(int), +char *format_next_process(void* handle, char *(*get_userid)(int), int flags); void toggle_pcpustats(void); void get_system_info(struct system_info *si); Modified: head/usr.bin/top/top.c ============================================================================== --- head/usr.bin/top/top.c Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/top.c Sun Jun 3 23:40:54 2018 (r334593) @@ -210,7 +210,7 @@ main(int argc, char *argv[]) const char *uname_field = "USERNAME"; const char *header_text; char *env_top; - char **preset_argv; + const char **preset_argv; int preset_argc = 0; char **av; int ac; Modified: head/usr.bin/top/top.h ============================================================================== --- head/usr.bin/top/top.h Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/top.h Sun Jun 3 23:40:54 2018 (r334593) @@ -18,9 +18,6 @@ extern int Header_lines; /* 7 */ /* Maximum number of columns allowed for display */ #define MAX_COLS 512 -/* Log base 2 of 1024 is 10 (2^10 == 1024) */ -#define LOG1024 10 - /* Special atoi routine returns either a non-negative number or one of: */ #define Infinity -1 #define Invalid -2 @@ -31,8 +28,6 @@ extern int Header_lines; /* 7 */ /* * The entire display is based on these next numbers being defined as is. */ - -#define NUM_AVERAGES 3 /* Exit code for system errors */ #define TOP_EX_SYS_ERROR 23 Modified: head/usr.bin/top/utils.c ============================================================================== --- head/usr.bin/top/utils.c Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/utils.c Sun Jun 3 23:40:54 2018 (r334593) @@ -142,7 +142,7 @@ int digits(int val) */ int -string_index(const char *string, char *array[]) +string_index(const char *string, const char * const *array) { size_t i = 0; @@ -165,8 +165,8 @@ string_index(const char *string, char *array[]) * squat about quotes. */ -char ** -argparse(char *line, int *cntp) +const char * const * +argparse(const char *line, int *cntp) { const char *from; char *to; @@ -175,7 +175,7 @@ argparse(char *line, int *cntp) int length; int lastch; char **argv; - char **argarray; + const char * const *argarray; char *args; /* unfortunately, the only real way to do this is to go thru the Modified: head/usr.bin/top/utils.h ============================================================================== --- head/usr.bin/top/utils.h Sun Jun 3 23:36:29 2018 (r334592) +++ head/usr.bin/top/utils.h Sun Jun 3 23:40:54 2018 (r334593) @@ -16,11 +16,11 @@ int atoiwi(const char *); char *itoa(unsigned int); char *itoa7(int); int digits(int); -char **argparse(char *, int *); +const char * const *argparse(const char *, int *); long percentages(int, int *, long *, long *, long *); char *format_time(long); char *format_k(int); char *format_k2(unsigned long long); -int string_index(const char *string, char *array[]); +int string_index(const char *string, const char * const *array); int find_pid(pid_t pid);