From 1572abd03edcc3f278586b9075a364cf0643ce5d Mon Sep 17 00:00:00 2001 From: albert <> Date: Mon, 25 Nov 2002 23:24:40 +0000 Subject: [PATCH] part 1 --- sysctl.c | 42 +++++++++++++++++++++--------------------- w.c | 20 +++++++------------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/sysctl.c b/sysctl.c index 911ff7a9..946bc3b2 100644 --- a/sysctl.c +++ b/sysctl.c @@ -58,7 +58,7 @@ static const char ERR_PRELOAD_FILE[] = "error: unable to open preload file '%s'\ 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 */ @@ -76,7 +76,7 @@ static void slashdot(char *p, char old, char new){ * 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" @@ -116,16 +116,17 @@ static char *StripLeadingAndTrailingSpaces(char *oneline) { * 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); @@ -179,12 +180,12 @@ static int ReadSetting(const char *setting) { * 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); @@ -195,7 +196,7 @@ static int DisplayAll(const char *path, bool ShowTableUtil) { } 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) { @@ -206,15 +207,14 @@ static int DisplayAll(const char *path, bool ShowTableUtil) { DisplayAll(tmpdir, ShowTableUtil); } else { rc |= ReadSetting(tmpdir+strlen(PROC_PATH)); - } /* endif */ - } /* endif */ + } + } free(tmpdir); - } /* end while */ + } closedir(dp); - } /* endif */ - + } return rc; -} /* end DisplayAll() */ +} /* @@ -302,7 +302,7 @@ static int WriteSetting(const char *setting) { * - 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]; diff --git a/w.c b/w.c index e0f33223..3fb34e78 100644 --- a/w.c +++ b/w.c @@ -44,7 +44,7 @@ typedef struct utmp utmp_t; * 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; @@ -62,15 +62,8 @@ static void print_host(char* host, int len) { 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?) */ @@ -90,7 +83,7 @@ static void print_time_ival7(time_t t, int centi_sec, FILE* fout) { } /**** 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; @@ -128,7 +121,7 @@ static void print_logintime(time_t logt, FILE* fout) { * 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; @@ -144,7 +137,8 @@ static proc_t *getproc(utmp_t *u, char *tty, unsigned long long *jcpu, int *foun /* 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; -- 2.40.0