#include "top.h" /* new header for top specific things */
static int *cpu_mapping;
-static int nr_cpu;
/*#######################################################################
*#### Startup routines: parse_options, get_options, ##############
char Options[256] = "";
int i;
- nr_cpu = sysconf (_SC_NPROCESSORS_ONLN);
- if (nr_cpu < 1) nr_cpu = 1;
- cpu_mapping = (int *) xmalloc (sizeof (int) * nr_cpu);
+ cpu_mapping = (int *) xmalloc (sizeof (int) * smp_num_cpus);
/* read cpuname */
- for (i=0; i< nr_cpu; i++) cpu_mapping[i]=i;
- header_lines = 6 + nr_cpu;
+ for (i=0; i< smp_num_cpus; i++) cpu_mapping[i]=i;
+ header_lines = 6 + smp_num_cpus;
fp = fopen(SYS_TOPRC, "r");
if (fp != NULL) {
fgets(Options, 254, fp);
breakargv:
}
- if (nr_cpu > 1 && CPU_states)
+ if (smp_num_cpus > 1 && CPU_states)
header_lines++;
meminfo(); /* need kb_main_total value filled in */
} symb;
static const symb fail = { "?", 0 };
-static const char *dash = "-";
+static const char dash[] = "-";
/* These mostly rely on POSIX to make them zero. */
/* for ELF executables, notes are pushed before environment and args */
static unsigned long find_elf_note(unsigned long findme){
unsigned long *ep = (unsigned long *)environ;
- unsigned long ret = 42;
while(*ep++);
-// while(*ep++);
while(*ep){
-// printf("%08lx %08lx %011ld %011ld%s\n",ep[0],ep[1],ep[0],ep[1],ep[0]==findme?" <<<":"");
- if(ep[0]==findme) ret=ep[1];
+ if(ep[0]==findme) return ep[1];
ep+=2;
}
- return ret;
+ return 42;
}
static void init_libproc(void) __attribute__((constructor));
/* ought to count CPUs in /proc/stat instead of relying
* on glibc, which foolishly tries to parse /proc/cpuinfo
*/
- smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
+ smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); // or _SC_NPROCESSORS_ONLN
if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
Hertz = find_elf_note(AT_CLKTCK);
#endif
-static char *saved_personality_text = "You found a bug!";
+static char saved_personality_text[] = "You found a bug!";
int all_processes = -1;
char *bsd_j_format = (char *)0xdeadbeef;
static const char *parse_pid(char *str, sel_union *ret){
char *endp;
unsigned long num;
- static const char *pidrange = "Process ID out of range.";
- static const char *pidsyntax = "Process ID list syntax error.";
+ static const char pidrange[] = "Process ID out of range.";
+ static const char pidsyntax[] = "Process ID list syntax error.";
num = strtoul(str, &endp, 0);
if(*endp != '\0') return pidsyntax;
if(num<1) return pidrange;
struct passwd *passwd_data;
char *endp;
unsigned long num;
- static const char *uidrange = "User ID out of range.";
- static const char *uidexist = "User name does not exist.";
+ static const char uidrange[] = "User ID out of range.";
+ static const char uidexist[] = "User name does not exist.";
num = strtoul(str, &endp, 0);
if(*endp != '\0'){ /* hmmm, try as login name */
passwd_data = getpwnam(str);
struct group *group_data;
char *endp;
unsigned long num;
- static const char *gidrange = "Group ID out of range.";
- static const char *gidexist = "Group name does not exist.";
+ static const char gidrange[] = "Group ID out of range.";
+ static const char gidexist[] = "Group name does not exist.";
num = strtoul(str, &endp, 0);
if(*endp != '\0'){ /* hmmm, try as login name */
group_data = getgrnam(str);
static const char *parse_tty(char *str, sel_union *ret){
struct stat sbuf;
- static const char *missing = "TTY could not be found.";
- static const char *not_tty = "List member was not a TTY.";
+ static const char missing[] = "TTY could not be found.";
+ static const char not_tty[] = "List member was not a TTY.";
char path[4096];
if(str[0]=='/'){
if(stat(str, &sbuf) >= 0) goto found_it;
* reparse as formatting codes.
*/
static const char *verify_short_sort(const char *arg){
- const char *all = "CGJKMNPRSTUcfgjkmnoprstuvy+-";
+ const char all[] = "CGJKMNPRSTUcfgjkmnoprstuvy+-";
char checkoff[256];
int i;
const char *walk;
/*
* Function Prototypes
*/
-int Usage(const char *name);
-void Preload(const char *filename);
-int WriteSetting(const char *setting);
-int ReadSetting(const char *setting);
-int DisplayAll(const char *path, bool ShowTableUtil);
+static int Usage(const char *name);
+static void Preload(const char *filename);
+static int WriteSetting(const char *setting);
+static int ReadSetting(const char *setting);
+static int DisplayAll(const char *path, bool ShowTableUtil);
/*
* Globals...
*/
-const char *PROC_PATH = "/proc/sys/";
-const char *DEFAULT_PRELOAD = "/etc/sysctl.conf";
+static const char PROC_PATH[] = "/proc/sys/";
+static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf";
static bool PrintName;
static bool PrintNewline;
/* error messages */
-const char *ERR_UNKNOWN_PARAMETER = "error: Unknown parameter '%s'\n";
-const char *ERR_MALFORMED_SETTING = "error: Malformed setting '%s'\n";
-const char *ERR_NO_EQUALS = "error: '%s' must be of the form name=value\n";
-const char *ERR_INVALID_KEY = "error: '%s' is an unknown key\n";
-const char *ERR_UNKNOWN_WRITING = "error: unknown error %d setting key '%s'\n";
-const char *ERR_UNKNOWN_READING = "error: unknown error %d reading key '%s'\n";
-const char *ERR_PERMISSION_DENIED = "error: permission denied on key '%s'\n";
-const char *ERR_OPENING_DIR = "error: unable to open directory '%s'\n";
-const char *ERR_PRELOAD_FILE = "error: unable to open preload file '%s'\n";
-const char *WARN_BAD_LINE = "warning: %s(%d): invalid syntax, continuing...\n";
+static const char ERR_UNKNOWN_PARAMETER[] = "error: Unknown parameter '%s'\n";
+static const char ERR_MALFORMED_SETTING[] = "error: Malformed setting '%s'\n";
+static const char ERR_NO_EQUALS[] = "error: '%s' must be of the form name=value\n";
+static const char ERR_INVALID_KEY[] = "error: '%s' is an unknown key\n";
+static const char ERR_UNKNOWN_WRITING[] = "error: unknown error %d setting key '%s'\n";
+static const char ERR_UNKNOWN_READING[] = "error: unknown error %d reading key '%s'\n";
+static const char ERR_PERMISSION_DENIED[] = "error: permission denied on key '%s'\n";
+static const char ERR_OPENING_DIR[] = "error: unable to open directory '%s'\n";
+static const char ERR_PRELOAD_FILE[] = "error: unable to open preload file '%s'\n";
+static const char WARN_BAD_LINE[] = "warning: %s(%d): invalid syntax, continuing...\n";
static void slashdot(char *p, char old, char new){
*
*/
int main(int argc, char **argv) {
-const char *me = (const char *)basename(argv[0]);
-bool SwitchesAllowed = true;
-bool WriteMode = false;
-int ReturnCode = 0;
-const char *preloadfile = DEFAULT_PRELOAD;
+ const char *me = (const char *)basename(argv[0]);
+ bool SwitchesAllowed = true;
+ bool WriteMode = false;
+ int ReturnCode = 0;
+ const char *preloadfile = DEFAULT_PRELOAD;
PrintName = true;
PrintNewline = true;
* Display the usage format
*
*/
-int Usage(const char *name) {
+static int Usage(const char *name) {
printf("usage: %s [-n] variable ... \n"
" %s [-n] -w variable=value ... \n"
" %s [-n] -a \n"
" %s [-n] -p <file> (default /etc/sysctl.conf) \n"
" %s [-n] -A\n", name, name, name, name, name);
-return -1;
+ return -1;
} /* end Usage() */
* Strip the leading and trailing spaces from a string
*
*/
-char *StripLeadingAndTrailingSpaces(char *oneline) {
-char *t;
+static char *StripLeadingAndTrailingSpaces(char *oneline) {
+ char *t;
-if (!oneline || !*oneline)
- return oneline;
+ if (!oneline || !*oneline)
+ return oneline;
-t = oneline;
-t += strlen(oneline)-1;
+ t = oneline;
+ t += strlen(oneline)-1;
-while ((*t == ' ' || *t == '\t' || *t == '\n' || *t == '\r') && t != oneline)
- *t-- = 0;
+ while ((*t==' ' || *t=='\t' || *t=='\n' || *t=='\r') && t!=oneline)
+ *t-- = 0;
-t = oneline;
+ t = oneline;
-while ((*t == ' ' || *t == '\t') && *t != 0)
- t++;
+ while ((*t==' ' || *t=='\t') && *t!=0)
+ t++;
-return t;
+ return t;
} /* end StripLeadingAndTrailingSpaces() */
* - we parse the file and then reform it (strip out whitespace)
*
*/
-void Preload(const char *filename) {
-FILE *fp;
-char oneline[257];
-char buffer[257];
-char *t;
-int n = 0;
-char *name, *value;
+static void Preload(const char *filename) {
+ FILE *fp;
+ char oneline[257];
+ char buffer[257];
+ char *t;
+ int n = 0;
+ char *name, *value;
if (!filename || ((fp = fopen(filename, "r")) == NULL)) {
fprintf(stderr, ERR_PRELOAD_FILE, filename);
* Write a sysctl setting
*
*/
-int WriteSetting(const char *setting) {
-int rc = 0;
-const char *name = setting;
-const char *value;
-const char *equals;
-char *tmpname;
-FILE *fp;
-char *outname;
+static int WriteSetting(const char *setting) {
+ int rc = 0;
+ const char *name = setting;
+ const char *value;
+ const char *equals;
+ char *tmpname;
+ FILE *fp;
+ char *outname;
if (!name) { /* probably don't want to display this err */
return 0;
free(tmpname);
free(outname);
-return rc;
+ return rc;
} /* end WriteSetting() */
* Read a sysctl setting
*
*/
-int ReadSetting(const char *setting) {
-int rc = 0;
-char *tmpname, *outname;
-char inbuf[1025];
-const char *name = setting;
-FILE *fp;
+static int ReadSetting(const char *setting) {
+ int rc = 0;
+ char *tmpname, *outname;
+ char inbuf[1025];
+ const char *name = setting;
+ FILE *fp;
if (!setting || !*setting) {
fprintf(stderr, ERR_INVALID_KEY, setting);
free(tmpname);
free(outname);
-return rc;
+ return rc;
} /* end ReadSetting() */
* Display all the sysctl settings
*
*/
-int DisplayAll(const char *path, bool ShowTableUtil) {
-int rc = 0;
-int rc2;
-DIR *dp;
-struct dirent *de;
-char *tmpdir;
-struct stat ts;
+static int DisplayAll(const char *path, bool ShowTableUtil) {
+ int rc = 0;
+ int rc2;
+ DIR *dp;
+ struct dirent *de;
+ char *tmpdir;
+ struct stat ts;
dp = opendir(path);
closedir(dp);
} /* endif */
-return rc;
+ return rc;
} /* end DisplayAll() */
* in some proc cmdlines, a choice was offered twix space or null. */
static char *strim (int sp, char *str)
{
- static const char *ws = "\b\f\n\r\t\v";
+ static const char ws[] = "\b\f\n\r\t\v";
char *p;
if (sp)
#ifdef PRETEND4CPUS
Cpu_tot = 4;
#else
- Cpu_tot = sysconf(_SC_NPROCESSORS_ONLN);
+ Cpu_tot = smp_num_cpus;
#endif
- if (1 > Cpu_tot) Cpu_tot = 1;
Cpu_map = alloc_r(NULL, sizeof(int) * Cpu_tot);
for (i = 0; i < Cpu_tot; i++)
Cpu_map[i] = i;
* line c: contains w->summclr, msgsclr, headclr, taskclr */
static void configs_read (void)
{
- static const char *err_rc = "bad rcfile, you should delete '%s'";
+ static const char err_rc[] = "bad rcfile, you should delete '%s'";
char fbuf[RCFBUFSIZ];
FILE *fp;
float delay = DEF_DELAY;
* Change order of displayed fields. */
static void fields_reorder (void)
{
- static const char *prompt =
+ static const char prompt[] =
"Upper case letter moves field left, lower case right";
char c, *p;
int i;
* Select sort field. */
static void fields_sort (void)
{
- static const char *prompt =
+ static const char prompt[] =
"Select sort field via field letter, type any other key to return";
char phoney[PFLAGSSIZ];
char c, *p;
* Toggle displayed fields. */
static void fields_toggle (void)
{
- static const char *prompt =
+ static const char prompt[] =
"Toggle fields via field letter, type any other key to return";
char c, *p;
int i;
* Display a window/field group (ie. make it "current"). */
static void win_select (char ch)
{
- static const char *prompt = "Choose field group (1 - 4)";
+ static const char prompt[] = "Choose field group (1 - 4)";
/* if there's no ch, it means we're supporting the normal do_key routine,
so we must try to get our own darn ch by begging the user... */
static void do_key (unsigned c)
{
/* standardized 'secure mode' errors */
- static const char *err_secure = "\aUnavailable in secure mode";
+ static const char err_secure[] = "\aUnavailable in secure mode";
#ifdef WARN_NOT_SMP
/* standardized 'smp' errors */
- static const char *err_smp = "\aSorry, only 1 cpu detected";
+ static const char err_smp[] = "\aSorry, only 1 cpu detected";
#endif
switch (c) {
/***** 7 character formatted login time */
static void print_logintime(time_t logt, FILE* fout) {
- char *weekday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
- *month [] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
+ char weekday[4][] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
+ month [4][] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec" };
time_t curt;
struct tm *logtm, *curtm;