part 1
This commit is contained in:
parent
3f6928c4fb
commit
1572abd03e
42
sysctl.c
42
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 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,"/.");
|
p = strpbrk(p,"/.");
|
||||||
if(!p) return; /* nothing -- can't be, but oh well */
|
if(!p) return; /* nothing -- can't be, but oh well */
|
||||||
if(*p==new) return; /* already in desired format */
|
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
|
* Display the usage format
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static int Usage(const char *name) {
|
static int Usage(const char *restrict const name) {
|
||||||
printf("usage: %s [-n] variable ... \n"
|
printf("usage: %s [-n] variable ... \n"
|
||||||
" %s [-n] -w variable=value ... \n"
|
" %s [-n] -w variable=value ... \n"
|
||||||
" %s [-n] -a \n"
|
" %s [-n] -a \n"
|
||||||
@ -116,16 +116,17 @@ static char *StripLeadingAndTrailingSpaces(char *oneline) {
|
|||||||
* Read a sysctl setting
|
* Read a sysctl setting
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static int ReadSetting(const char *setting) {
|
static int ReadSetting(const char *restrict const name) {
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
char *tmpname, *outname;
|
char *restrict tmpname;
|
||||||
|
char *restrict outname;
|
||||||
char inbuf[1025];
|
char inbuf[1025];
|
||||||
const char *name = setting;
|
FILE *restrict fp;
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
if (!setting || !*setting) {
|
if (!name || !*name) {
|
||||||
fprintf(stderr, ERR_INVALID_KEY, setting);
|
fprintf(stderr, ERR_INVALID_KEY, name);
|
||||||
} /* endif */
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* used to open the file */
|
/* used to open the file */
|
||||||
tmpname = malloc(strlen(name)+strlen(PROC_PATH)+1);
|
tmpname = malloc(strlen(name)+strlen(PROC_PATH)+1);
|
||||||
@ -179,12 +180,12 @@ static int ReadSetting(const char *setting) {
|
|||||||
* Display all the sysctl settings
|
* 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 rc = 0;
|
||||||
int rc2;
|
int rc2;
|
||||||
DIR *dp;
|
DIR *restrict dp;
|
||||||
struct dirent *de;
|
struct dirent *restrict de;
|
||||||
char *tmpdir;
|
char *restrict tmpdir;
|
||||||
struct stat ts;
|
struct stat ts;
|
||||||
|
|
||||||
dp = opendir(path);
|
dp = opendir(path);
|
||||||
@ -195,7 +196,7 @@ static int DisplayAll(const char *path, bool ShowTableUtil) {
|
|||||||
} else {
|
} else {
|
||||||
readdir(dp); readdir(dp); /* skip . and .. */
|
readdir(dp); readdir(dp); /* skip . and .. */
|
||||||
while (( de = readdir(dp) )) {
|
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);
|
sprintf(tmpdir, "%s%s", path, de->d_name);
|
||||||
rc2 = stat(tmpdir, &ts); /* should check this return code */
|
rc2 = stat(tmpdir, &ts); /* should check this return code */
|
||||||
if (rc2 != 0) {
|
if (rc2 != 0) {
|
||||||
@ -206,15 +207,14 @@ static int DisplayAll(const char *path, bool ShowTableUtil) {
|
|||||||
DisplayAll(tmpdir, ShowTableUtil);
|
DisplayAll(tmpdir, ShowTableUtil);
|
||||||
} else {
|
} else {
|
||||||
rc |= ReadSetting(tmpdir+strlen(PROC_PATH));
|
rc |= ReadSetting(tmpdir+strlen(PROC_PATH));
|
||||||
} /* endif */
|
}
|
||||||
} /* endif */
|
}
|
||||||
free(tmpdir);
|
free(tmpdir);
|
||||||
} /* end while */
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
} /* endif */
|
}
|
||||||
|
|
||||||
return rc;
|
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)
|
* - 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;
|
FILE *fp;
|
||||||
char oneline[257];
|
char oneline[257];
|
||||||
char buffer[257];
|
char buffer[257];
|
||||||
|
20
w.c
20
w.c
@ -44,7 +44,7 @@ typedef struct utmp utmp_t;
|
|||||||
* unprintable. Always outputs at least 16 chars padded with spaces
|
* unprintable. Always outputs at least 16 chars padded with spaces
|
||||||
* on the right if necessary.
|
* 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;
|
char *last;
|
||||||
int width = 0;
|
int width = 0;
|
||||||
|
|
||||||
@ -62,15 +62,8 @@ static void print_host(char* host, int len) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!width){ /* blank fields screw up parsers */
|
// space-fill, and a '-' too if needed to ensure the column exists
|
||||||
fputc('-', stdout);
|
if(width < 16) fputs("- "+width, stderr);
|
||||||
++width;
|
|
||||||
}
|
|
||||||
/* if *any* unprintables(or blanks), replace rest of line with spaces */
|
|
||||||
while (width < 16) {
|
|
||||||
fputc(' ', stdout);
|
|
||||||
++width;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** compact 7 char format for time intervals (belongs in libproc?) */
|
/***** 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 */
|
/**** 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;
|
struct stat sbuf;
|
||||||
if (stat(tty, &sbuf) != 0)
|
if (stat(tty, &sbuf) != 0)
|
||||||
return 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
|
* for the "best" process to report as "(w)hat" the user for that login
|
||||||
* session is doing currently. This the essential core of 'w'.
|
* 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;
|
int line;
|
||||||
proc_t **p, *best = NULL, *secondbest = NULL;
|
proc_t **p, *best = NULL, *secondbest = NULL;
|
||||||
unsigned uid = ~0U;
|
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 */
|
/* OK to have passwd_data go out of scope here */
|
||||||
}
|
}
|
||||||
line = tty_to_dev(tty);
|
line = tty_to_dev(tty);
|
||||||
*jcpu = *found_utpid = 0;
|
*jcpu = 0;
|
||||||
|
*found_utpid = 0;
|
||||||
for(p = procs; *p; p++) {
|
for(p = procs; *p; p++) {
|
||||||
if((**p).pid == u->ut_pid) {
|
if((**p).pid == u->ut_pid) {
|
||||||
*found_utpid = 1;
|
*found_utpid = 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user