reorder pgrep functions, c99 vmstat
This commit is contained in:
parent
d0bc469c10
commit
c941d496a2
250
pgrep.c
250
pgrep.c
@ -84,129 +84,6 @@ usage (int opt)
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
parse_opts (int argc, char **argv)
|
||||
{
|
||||
char opts[32] = "";
|
||||
int opt;
|
||||
int criteria_count = 0;
|
||||
|
||||
if (strstr (argv[0], "pkill")) {
|
||||
i_am_pkill = 1;
|
||||
progname = "pkill";
|
||||
/* Look for a signal name or number as first argument */
|
||||
if (argc > 1 && argv[1][0] == '-') {
|
||||
int sig;
|
||||
sig = signal_name_to_number (argv[1] + 1);
|
||||
if (sig == -1 && isdigit (argv[1][1]))
|
||||
sig = atoi (argv[1] + 1);
|
||||
if (sig != -1) {
|
||||
int i;
|
||||
for (i = 2; i < argc; i++)
|
||||
argv[i-1] = argv[i];
|
||||
--argc;
|
||||
opt_signal = sig;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* These options are for pgrep only */
|
||||
strcat (opts, "ld:");
|
||||
}
|
||||
|
||||
strcat (opts, "fnovxP:g:s:u:U:G:t:?V");
|
||||
|
||||
while ((opt = getopt (argc, argv, opts)) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
opt_full = 1;
|
||||
break;
|
||||
case 'l':
|
||||
opt_long = 1;
|
||||
break;
|
||||
case 'n':
|
||||
if (opt_oldest|opt_negate|opt_newest)
|
||||
usage (opt);
|
||||
opt_newest = 1;
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'o':
|
||||
if (opt_oldest|opt_negate|opt_newest)
|
||||
usage (opt);
|
||||
opt_oldest = 1;
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'v':
|
||||
if (opt_oldest|opt_negate|opt_newest)
|
||||
usage (opt);
|
||||
opt_negate = 1;
|
||||
break;
|
||||
case 'x':
|
||||
opt_exact = 1;
|
||||
break;
|
||||
case 'd':
|
||||
opt_delim = strdup (optarg);
|
||||
break;
|
||||
case 'P':
|
||||
opt_ppid = split_list (optarg, ',', conv_num);
|
||||
if (opt_ppid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'g':
|
||||
opt_pgrp = split_list (optarg, ',', conv_pgrp);
|
||||
if (opt_pgrp == NULL)
|
||||
usage (opt);
|
||||
break;
|
||||
case 's':
|
||||
opt_sid = split_list (optarg, ',', conv_sid);
|
||||
if (opt_sid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'u':
|
||||
opt_euid = split_list (optarg, ',', conv_uid);
|
||||
if (opt_euid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'U':
|
||||
opt_uid = split_list (optarg, ',', conv_uid);
|
||||
if (opt_uid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'G':
|
||||
opt_gid = split_list (optarg, ',', conv_gid);
|
||||
if (opt_gid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 't':
|
||||
opt_term = split_list (optarg, ',', conv_str);
|
||||
if (opt_term == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case '?':
|
||||
usage (opt);
|
||||
break;
|
||||
case 'V':
|
||||
display_pgrep_version();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
if (argc - optind == 1)
|
||||
opt_pattern = argv[optind];
|
||||
else if (argc - optind > 1)
|
||||
usage (0);
|
||||
else if (criteria_count == 0) {
|
||||
fprintf (stderr, "%s: No matching criteria specified\n",
|
||||
progname);
|
||||
usage (0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static union el *
|
||||
split_list (const char *str, char sep, int (*convert)(const char *, union el *))
|
||||
{
|
||||
@ -612,6 +489,129 @@ select_procs (void)
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
parse_opts (int argc, char **argv)
|
||||
{
|
||||
char opts[32] = "";
|
||||
int opt;
|
||||
int criteria_count = 0;
|
||||
|
||||
if (strstr (argv[0], "pkill")) {
|
||||
i_am_pkill = 1;
|
||||
progname = "pkill";
|
||||
/* Look for a signal name or number as first argument */
|
||||
if (argc > 1 && argv[1][0] == '-') {
|
||||
int sig;
|
||||
sig = signal_name_to_number (argv[1] + 1);
|
||||
if (sig == -1 && isdigit (argv[1][1]))
|
||||
sig = atoi (argv[1] + 1);
|
||||
if (sig != -1) {
|
||||
int i;
|
||||
for (i = 2; i < argc; i++)
|
||||
argv[i-1] = argv[i];
|
||||
--argc;
|
||||
opt_signal = sig;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* These options are for pgrep only */
|
||||
strcat (opts, "ld:");
|
||||
}
|
||||
|
||||
strcat (opts, "fnovxP:g:s:u:U:G:t:?V");
|
||||
|
||||
while ((opt = getopt (argc, argv, opts)) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
opt_full = 1;
|
||||
break;
|
||||
case 'l':
|
||||
opt_long = 1;
|
||||
break;
|
||||
case 'n':
|
||||
if (opt_oldest|opt_negate|opt_newest)
|
||||
usage (opt);
|
||||
opt_newest = 1;
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'o':
|
||||
if (opt_oldest|opt_negate|opt_newest)
|
||||
usage (opt);
|
||||
opt_oldest = 1;
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'v':
|
||||
if (opt_oldest|opt_negate|opt_newest)
|
||||
usage (opt);
|
||||
opt_negate = 1;
|
||||
break;
|
||||
case 'x':
|
||||
opt_exact = 1;
|
||||
break;
|
||||
case 'd':
|
||||
opt_delim = strdup (optarg);
|
||||
break;
|
||||
case 'P':
|
||||
opt_ppid = split_list (optarg, ',', conv_num);
|
||||
if (opt_ppid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'g':
|
||||
opt_pgrp = split_list (optarg, ',', conv_pgrp);
|
||||
if (opt_pgrp == NULL)
|
||||
usage (opt);
|
||||
break;
|
||||
case 's':
|
||||
opt_sid = split_list (optarg, ',', conv_sid);
|
||||
if (opt_sid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'u':
|
||||
opt_euid = split_list (optarg, ',', conv_uid);
|
||||
if (opt_euid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'U':
|
||||
opt_uid = split_list (optarg, ',', conv_uid);
|
||||
if (opt_uid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 'G':
|
||||
opt_gid = split_list (optarg, ',', conv_gid);
|
||||
if (opt_gid == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case 't':
|
||||
opt_term = split_list (optarg, ',', conv_str);
|
||||
if (opt_term == NULL)
|
||||
usage (opt);
|
||||
++criteria_count;
|
||||
break;
|
||||
case '?':
|
||||
usage (opt);
|
||||
break;
|
||||
case 'V':
|
||||
fprintf(stdout, "%s (%s)\n", progname, procps_version);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
if (argc - optind == 1)
|
||||
opt_pattern = argv[optind];
|
||||
else if (argc - optind > 1)
|
||||
usage (0);
|
||||
else if (criteria_count == 0) {
|
||||
fprintf (stderr, "%s: No matching criteria specified\n",
|
||||
progname);
|
||||
usage (0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@ -635,7 +635,3 @@ main (int argc, char **argv)
|
||||
}
|
||||
return ((procs[0].num) == 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
static void display_pgrep_version(){
|
||||
fprintf(stdout, "%s (%s)\n", progname, procps_version);
|
||||
}
|
||||
|
21
vmstat.c
21
vmstat.c
@ -1,7 +1,5 @@
|
||||
/* Copyright 1994 by Henry Ware <al172@yfn.ysu.edu>. Copyleft same year. */
|
||||
|
||||
#include "proc/sysinfo.h"
|
||||
#include "proc/version.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
@ -15,6 +13,9 @@
|
||||
#include <sys/dir.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "proc/sysinfo.h"
|
||||
#include "proc/version.h"
|
||||
|
||||
#define BUFFSIZE 8192
|
||||
#define FALSE 0
|
||||
#define TRUE 1
|
||||
@ -53,7 +54,7 @@ static void crash(const char *filename) {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void getrunners(unsigned int *running, unsigned int *blocked) {
|
||||
static void getrunners(unsigned int *restirct running, unsigned int *restrict blocked) {
|
||||
static struct direct *ent;
|
||||
DIR *proc;
|
||||
|
||||
@ -95,14 +96,14 @@ static void getrunners(unsigned int *running, unsigned int *blocked) {
|
||||
closedir(proc);
|
||||
}
|
||||
|
||||
static void getstat(jiff *cuse, jiff *cice, jiff *csys, jiff *cide, jiff *ciow,
|
||||
unsigned *pin, unsigned *pout, unsigned *s_in, unsigned *sout,
|
||||
unsigned *itot, unsigned *i1, unsigned *ct,
|
||||
unsigned int *running, unsigned int *blocked) {
|
||||
static void getstat(jiff *restrict cuse, jiff *restrict cice, jiff *restrict csys, jiff *restrict cide, jiff *restrict ciow,
|
||||
unsigned *restrict pin, unsigned *restrict pout, unsigned *restrict s_in, unsigned *restrict sout,
|
||||
unsigned *restrict itot, unsigned *restrict i1, unsigned *restrict ct,
|
||||
unsigned int *restrict running, unsigned int *restrict blocked) {
|
||||
static int fd;
|
||||
int need_vmstat_file = 0;
|
||||
int need_proc_scan = 0;
|
||||
char* b;
|
||||
const char* b;
|
||||
buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */
|
||||
|
||||
if(fd){
|
||||
@ -161,7 +162,7 @@ static void getstat(jiff *cuse, jiff *cice, jiff *csys, jiff *cide, jiff *ciow,
|
||||
|
||||
#if 0
|
||||
// produce: " 6 ", "123 ", "123k ", etc.
|
||||
static int format_1024(unsigned long long val64, char *dst){
|
||||
static int format_1024(unsigned long long val64, char *restrict dst){
|
||||
unsigned oldval;
|
||||
const char suffix[] = " kmgtpe";
|
||||
unsigned level = 0;
|
||||
@ -194,7 +195,7 @@ static int format_1024(unsigned long long val64, char *dst){
|
||||
|
||||
|
||||
// produce: " 6 ", "123 ", "123k ", etc.
|
||||
static int format_1000(unsigned long long val64, char *dst){
|
||||
static int format_1000(unsigned long long val64, char *restrict dst){
|
||||
unsigned oldval;
|
||||
const char suffix[] = " kmgtpe";
|
||||
unsigned level = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user