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)
This commit is contained in:
eadler 2018-06-03 23:40:54 +00:00
parent ea3776a952
commit 0d7f83c56e
7 changed files with 35 additions and 49 deletions

View File

@ -28,6 +28,7 @@
* *_process, u_endscreen. * *_process, u_endscreen.
*/ */
#include <sys/resource.h>
#include <sys/time.h> #include <sys/time.h>
#include <assert.h> #include <assert.h>
@ -90,8 +91,7 @@ static int cpustates_column;
static enum { OFF, ON, ERASE } header_status = ON; static enum { OFF, ON, ERASE } header_status = ON;
static int string_count(char **); static void summary_format(char *, int *, const char * const *);
static void summary_format(char *, int *, char **);
static void line_update(char *, char *, int, int); static void line_update(char *, char *, int, int);
int x_lastpid = 10; int x_lastpid = 10;
@ -203,23 +203,23 @@ int display_init(struct statics * statics)
{ {
/* save pointers and allocate space for names */ /* save pointers and allocate space for names */
procstate_names = statics->procstate_names; procstate_names = statics->procstate_names;
num_procstates = string_count(procstate_names); num_procstates = 8;
assert(num_procstates > 0); assert(num_procstates > 0);
lprocstates = calloc(num_procstates, sizeof(int)); lprocstates = calloc(num_procstates, sizeof(int));
cpustate_names = statics->cpustate_names; cpustate_names = statics->cpustate_names;
swap_names = statics->swap_names; swap_names = statics->swap_names;
num_swap = string_count(swap_names); num_swap = 7;
assert(num_swap > 0); assert(num_swap > 0);
lswap = calloc(num_swap, sizeof(int)); lswap = calloc(num_swap, sizeof(int));
num_cpustates = string_count(cpustate_names); num_cpustates = CPUSTATES;
assert(num_cpustates > 0); assert(num_cpustates > 0);
lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus); lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus);
cpustate_columns = calloc(num_cpustates, sizeof(int)); cpustate_columns = calloc(num_cpustates, sizeof(int));
memory_names = statics->memory_names; memory_names = statics->memory_names;
num_memory = string_count(memory_names); num_memory = 7;
assert(num_memory > 0); assert(num_memory > 0);
lmemory = calloc(num_memory, sizeof(int)); lmemory = calloc(num_memory, sizeof(int));
@ -422,8 +422,8 @@ i_cpustates(int *states)
{ {
int i = 0; int i = 0;
int value; int value;
char **names; const char * const *names;
char *thisname; const char *thisname;
int cpu; int cpu;
for (cpu = 0; cpu < num_cpus; cpu++) { for (cpu = 0; cpu < num_cpus; cpu++) {
@ -761,7 +761,7 @@ trim_header(const char *text)
void void
i_header(const char *text) i_header(const char *text)
{ {
const char *s; char *s;
s = trim_header(text); s = trim_header(text);
if (s != NULL) if (s != NULL)
@ -1075,23 +1075,11 @@ readline(char *buffer, int size, int numeric)
/* internal support routines */ /* 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; char *p;
int num; int num;
char *thisname; const char *thisname;
char rbuf[6]; char rbuf[6];
/* format each number followed by its string */ /* format each number followed by its string */

View File

@ -15,7 +15,6 @@
* $FreeBSD$ * $FreeBSD$
*/ */
#include <sys/param.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/param.h> #include <sys/param.h>
@ -147,12 +146,12 @@ static long cp_diff[CPUSTATES];
/* these are for detailing the process states */ /* these are for detailing the process states */
static int process_states[8];
static const char *procstatenames[] = { static const char *procstatenames[] = {
"", " starting, ", " running, ", " sleeping, ", " stopped, ", "", " starting, ", " running, ", " sleeping, ", " stopped, ",
" zombie, ", " waiting, ", " lock, ", " zombie, ", " waiting, ", " lock, ",
NULL NULL
}; };
static int process_states[nitems(procstatenames)];
/* these are for detailing the cpu states */ /* these are for detailing the cpu states */
@ -163,29 +162,29 @@ static const char *cpustatenames[] = {
/* these are for detailing the memory statistics */ /* these are for detailing the memory statistics */
static int memory_stats[7];
static const char *memorynames[] = { static const char *memorynames[] = {
"K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ", "K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ",
"K Free", NULL "K Free", NULL
}; };
static int memory_stats[nitems(memorynames)];
static int arc_stats[7];
static const char *arcnames[] = { static const char *arcnames[] = {
"K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other", "K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
NULL NULL
}; };
static int arc_stats[nitems(arcnames)];
static int carc_stats[4];
static const char *carcnames[] = { static const char *carcnames[] = {
"K Compressed, ", "K Uncompressed, ", ":1 Ratio, ", "K Compressed, ", "K Uncompressed, ", ":1 Ratio, ",
NULL NULL
}; };
static int carc_stats[nitems(carcnames)];
static int swap_stats[7];
static const char *swapnames[] = { static const char *swapnames[] = {
"K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
NULL NULL
}; };
static int swap_stats[nitems(swapnames)];
/* these are for keeping track of the proc array */ /* these are for keeping track of the proc array */
@ -914,13 +913,13 @@ get_process_info(struct system_info *si, struct process_select *sel,
/* pass back a handle */ /* pass back a handle */
handle.next_proc = pref; handle.next_proc = pref;
handle.remaining = active_procs; handle.remaining = active_procs;
return ((caddr_t)&handle); return ((void*)&handle);
} }
static char fmt[512]; /* static area where result is built */ static char fmt[512]; /* static area where result is built */
char * 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; struct kinfo_proc *pp;
const struct kinfo_proc *oldp; const struct kinfo_proc *oldp;
@ -932,11 +931,11 @@ format_next_process(caddr_t xhandle, char *(*get_userid)(int), int flags)
size_t state; size_t state;
struct rusage ru, *rup; struct rusage ru, *rup;
long p_tot, s_tot; long p_tot, s_tot;
char *proc_fmt; const char *proc_fmt;
char thr_buf[6]; char thr_buf[6];
char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1]; char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1];
char *cmdbuf = NULL; char *cmdbuf = NULL;
const char * const *args; char **args;
const int cmdlen = 128; const int cmdlen = 128;
/* find and remember the next proc structure */ /* find and remember the next proc structure */
@ -1026,8 +1025,9 @@ format_next_process(caddr_t xhandle, char *(*get_userid)(int), int flags)
"[%s]", pp->ki_comm); "[%s]", pp->ki_comm);
} }
} else { } else {
char *src, *dst, *argbuf; const char *src;
char *cmd; char *dst, *argbuf;
const char *cmd;
size_t argbuflen; size_t argbuflen;
size_t len; size_t len;

View File

@ -10,7 +10,10 @@
#ifndef MACHINE_H #ifndef MACHINE_H
#define 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 * the statics struct is filled in by machine_init
@ -76,7 +79,7 @@ struct process_select
/* routines defined by the machine dependent module */ /* routines defined by the machine dependent module */
const char *format_header(const char *uname_field); 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); int flags);
void toggle_pcpustats(void); void toggle_pcpustats(void);
void get_system_info(struct system_info *si); void get_system_info(struct system_info *si);

View File

@ -210,7 +210,7 @@ main(int argc, char *argv[])
const char *uname_field = "USERNAME"; const char *uname_field = "USERNAME";
const char *header_text; const char *header_text;
char *env_top; char *env_top;
char **preset_argv; const char **preset_argv;
int preset_argc = 0; int preset_argc = 0;
char **av; char **av;
int ac; int ac;

View File

@ -18,9 +18,6 @@ extern int Header_lines; /* 7 */
/* Maximum number of columns allowed for display */ /* Maximum number of columns allowed for display */
#define MAX_COLS 512 #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: */ /* Special atoi routine returns either a non-negative number or one of: */
#define Infinity -1 #define Infinity -1
#define Invalid -2 #define Invalid -2
@ -32,8 +29,6 @@ extern int Header_lines; /* 7 */
* The entire display is based on these next numbers being defined as is. * The entire display is based on these next numbers being defined as is.
*/ */
#define NUM_AVERAGES 3
/* Exit code for system errors */ /* Exit code for system errors */
#define TOP_EX_SYS_ERROR 23 #define TOP_EX_SYS_ERROR 23

View File

@ -142,7 +142,7 @@ int digits(int val)
*/ */
int int
string_index(const char *string, char *array[]) string_index(const char *string, const char * const *array)
{ {
size_t i = 0; size_t i = 0;
@ -165,8 +165,8 @@ string_index(const char *string, char *array[])
* squat about quotes. * squat about quotes.
*/ */
char ** const char * const *
argparse(char *line, int *cntp) argparse(const char *line, int *cntp)
{ {
const char *from; const char *from;
char *to; char *to;
@ -175,7 +175,7 @@ argparse(char *line, int *cntp)
int length; int length;
int lastch; int lastch;
char **argv; char **argv;
char **argarray; const char * const *argarray;
char *args; char *args;
/* unfortunately, the only real way to do this is to go thru the /* unfortunately, the only real way to do this is to go thru the

View File

@ -16,11 +16,11 @@ int atoiwi(const char *);
char *itoa(unsigned int); char *itoa(unsigned int);
char *itoa7(int); char *itoa7(int);
int digits(int); int digits(int);
char **argparse(char *, int *); const char * const *argparse(const char *, int *);
long percentages(int, int *, long *, long *, long *); long percentages(int, int *, long *, long *, long *);
char *format_time(long); char *format_time(long);
char *format_k(int); char *format_k(int);
char *format_k2(unsigned long long); 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); int find_pid(pid_t pid);