static const char WARN_BAD_LINE[] = "warning: %s(%d): invalid syntax, continuing...\n";
-static void slashdot(char *p, char old, char new){
+static void slashdot(char *restrict p, char old, char new){
p = strpbrk(p,"/.");
if(!p) return; /* nothing -- can't be, but oh well */
if(*p==new) return; /* already in desired format */
* Display the usage format
*
*/
-static int Usage(const char *name) {
+static int Usage(const char *restrict const name) {
printf("usage: %s [-n] variable ... \n"
" %s [-n] -w variable=value ... \n"
" %s [-n] -a \n"
* Read a sysctl setting
*
*/
-static int ReadSetting(const char *setting) {
+static int ReadSetting(const char *restrict const name) {
int rc = 0;
- char *tmpname, *outname;
+ char *restrict tmpname;
+ char *restrict outname;
char inbuf[1025];
- const char *name = setting;
- FILE *fp;
+ FILE *restrict fp;
- if (!setting || !*setting) {
- fprintf(stderr, ERR_INVALID_KEY, setting);
- } /* endif */
+ if (!name || !*name) {
+ fprintf(stderr, ERR_INVALID_KEY, name);
+ return -1;
+ }
/* used to open the file */
tmpname = malloc(strlen(name)+strlen(PROC_PATH)+1);
* Display all the sysctl settings
*
*/
-static int DisplayAll(const char *path, bool ShowTableUtil) {
+static int DisplayAll(const char *restrict const path, bool ShowTableUtil) {
int rc = 0;
int rc2;
- DIR *dp;
- struct dirent *de;
- char *tmpdir;
+ DIR *restrict dp;
+ struct dirent *restrict de;
+ char *restrict tmpdir;
struct stat ts;
dp = opendir(path);
} else {
readdir(dp); readdir(dp); /* skip . and .. */
while (( de = readdir(dp) )) {
- tmpdir = (char *)malloc(strlen(path)+strlen(de->d_name)+2);
+ tmpdir = (char *restrict)malloc(strlen(path)+strlen(de->d_name)+2);
sprintf(tmpdir, "%s%s", path, de->d_name);
rc2 = stat(tmpdir, &ts); /* should check this return code */
if (rc2 != 0) {
DisplayAll(tmpdir, ShowTableUtil);
} else {
rc |= ReadSetting(tmpdir+strlen(PROC_PATH));
- } /* endif */
- } /* endif */
+ }
+ }
free(tmpdir);
- } /* end while */
+ }
closedir(dp);
- } /* endif */
-
+ }
return rc;
-} /* end DisplayAll() */
+}
/*
* - we parse the file and then reform it (strip out whitespace)
*
*/
-static void Preload(const char *filename) {
+static void Preload(const char *restrict const filename) {
FILE *fp;
char oneline[257];
char buffer[257];
* unprintable. Always outputs at least 16 chars padded with spaces
* on the right if necessary.
*/
-static void print_host(char* host, int len) {
+static void print_host(const char *restrict host, int len) {
char *last;
int width = 0;
break;
}
}
- if(!width){ /* blank fields screw up parsers */
- fputc('-', stdout);
- ++width;
- }
- /* if *any* unprintables(or blanks), replace rest of line with spaces */
- while (width < 16) {
- fputc(' ', stdout);
- ++width;
- }
+ // space-fill, and a '-' too if needed to ensure the column exists
+ if(width < 16) fputs("- "+width, stderr);
}
/***** compact 7 char format for time intervals (belongs in libproc?) */
}
/**** stat the device file to get an idle time */
-static time_t idletime(char *tty) {
+static time_t idletime(const char *restrict const tty) {
struct stat sbuf;
if (stat(tty, &sbuf) != 0)
return 0;
* for the "best" process to report as "(w)hat" the user for that login
* session is doing currently. This the essential core of 'w'.
*/
-static proc_t *getproc(utmp_t *u, char *tty, unsigned long long *jcpu, int *found_utpid) {
+static proc_t *getproc(const utmp_t *restrict const u, const char *restrict const tty, const unsigned long long *restrict jcpu, const int *restrict found_utpid) {
int line;
proc_t **p, *best = NULL, *secondbest = NULL;
unsigned uid = ~0U;
/* OK to have passwd_data go out of scope here */
}
line = tty_to_dev(tty);
- *jcpu = *found_utpid = 0;
+ *jcpu = 0;
+ *found_utpid = 0;
for(p = procs; *p; p++) {
if((**p).pid == u->ut_pid) {
*found_utpid = 1;