pgrep: use c style comments

The c++ comments e.g.  // should not be used in c code.  Additionally
white spaces are converted to tabs as a small clean up.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2012-02-11 20:02:16 +01:00
parent 4ffc7c4f20
commit e756a3db40

132
pgrep.c
View File

@ -28,8 +28,8 @@
#include <errno.h> #include <errno.h>
#include <getopt.h> #include <getopt.h>
// EXIT_SUCCESS is 0 /* EXIT_SUCCESS is 0 */
// EXIT_FAILURE is 1 /* EXIT_FAILURE is 1 */
#define EXIT_USAGE 2 #define EXIT_USAGE 2
#define EXIT_FATAL 3 #define EXIT_FATAL 3
#define XALLOC_EXIT_CODE EXIT_FATAL #define XALLOC_EXIT_CODE EXIT_FATAL
@ -127,13 +127,13 @@ static struct el *split_list (const char *restrict str, int (*convert)(const cha
do { do {
if (i == size) { if (i == size) {
size = size * 5 / 4 + 4; size = size * 5 / 4 + 4;
// add 1 because slot zero is a count /* add 1 because slot zero is a count */
list = xrealloc (list, 1 + size * sizeof *list); list = xrealloc (list, 1 + size * sizeof *list);
} }
sep_pos = strchr (ptr, ','); sep_pos = strchr (ptr, ',');
if (sep_pos) if (sep_pos)
*sep_pos = 0; *sep_pos = 0;
// Use ++i instead of i++ because slot zero is a count /* Use ++i instead of i++ because slot zero is a count */
if (list && !convert (ptr, &list[++i])) if (list && !convert (ptr, &list[++i]))
exit (EXIT_USAGE); exit (EXIT_USAGE);
if (sep_pos) if (sep_pos)
@ -150,8 +150,8 @@ static struct el *split_list (const char *restrict str, int (*convert)(const cha
return list; return list;
} }
// strict_atol returns a Boolean: TRUE if the input string /* strict_atol returns a Boolean: TRUE if the input string
// contains a plain number, FALSE if there are any non-digits. * contains a plain number, FALSE if there are any non-digits. */
static int strict_atol (const char *restrict str, long *restrict value) static int strict_atol (const char *restrict str, long *restrict value)
{ {
int res = 0; int res = 0;
@ -176,10 +176,11 @@ static int strict_atol (const char *restrict str, long *restrict value)
#include <sys/file.h> #include <sys/file.h>
// Seen non-BSD code do this: /* Seen non-BSD code do this:
// *
//if (fcntl_lock(pid_fd, F_SETLK, F_WRLCK, SEEK_SET, 0, 0) == -1) *if (fcntl_lock(pid_fd, F_SETLK, F_WRLCK, SEEK_SET, 0, 0) == -1)
// return -1; * return -1;
*/
int fcntl_lock(int fd, int cmd, int type, int whence, int start, int len) int fcntl_lock(int fd, int cmd, int type, int whence, int start, int len)
{ {
struct flock lock[1]; struct flock lock[1];
@ -192,19 +193,18 @@ int fcntl_lock(int fd, int cmd, int type, int whence, int start, int len)
return fcntl(fd, cmd, lock); return fcntl(fd, cmd, lock);
} }
/* We try a read lock. The daemon should have a write lock.
// We try a read lock. The daemon should have a write lock. * Seen using flock: FreeBSD code */
// Seen using flock: FreeBSD code
static int has_flock(int fd) static int has_flock(int fd)
{ {
return flock(fd, LOCK_SH|LOCK_NB)==-1 && errno==EWOULDBLOCK; return flock(fd, LOCK_SH|LOCK_NB)==-1 && errno==EWOULDBLOCK;
} }
// We try a read lock. The daemon should have a write lock. /* We try a read lock. The daemon should have a write lock.
// Seen using fcntl: libslack * Seen using fcntl: libslack */
static int has_fcntl(int fd) static int has_fcntl(int fd)
{ {
struct flock f; // seriously, struct flock is for a fnctl lock! struct flock f; /* seriously, struct flock is for a fnctl lock! */
f.l_type = F_RDLCK; f.l_type = F_RDLCK;
f.l_whence = SEEK_SET; f.l_whence = SEEK_SET;
f.l_start = 0; f.l_start = 0;
@ -226,7 +226,7 @@ static struct el *read_pidfile(void)
goto just_ret; goto just_ret;
if(fstat(fd,&sbuf) || !S_ISREG(sbuf.st_mode) || sbuf.st_size<1) if(fstat(fd,&sbuf) || !S_ISREG(sbuf.st_mode) || sbuf.st_size<1)
goto out; goto out;
// type of lock, if any, is not standardized on Linux /* type of lock, if any, is not standardized on Linux */
if(opt_lock && !has_flock(fd) && !has_fcntl(fd)) if(opt_lock && !has_flock(fd) && !has_fcntl(fd))
goto out; goto out;
memset(buf,'\0',sizeof buf); memset(buf,'\0',sizeof buf);
@ -366,7 +366,7 @@ static void output_numlist (const struct el *restrict list, int num)
static void output_strlist (const struct el *restrict list, int num) static void output_strlist (const struct el *restrict list, int num)
{ {
// FIXME: escape codes /* FIXME: escape codes */
int i; int i;
const char *delim = opt_delim; const char *delim = opt_delim;
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
@ -388,7 +388,7 @@ static PROCTAB *do_openproc (void)
if (opt_oldest || opt_newest || opt_pgrp || opt_sid || opt_term) if (opt_oldest || opt_newest || opt_pgrp || opt_sid || opt_term)
flags |= PROC_FILLSTAT; flags |= PROC_FILLSTAT;
if (!(flags & PROC_FILLSTAT)) if (!(flags & PROC_FILLSTAT))
flags |= PROC_FILLSTATUS; // FIXME: need one, and PROC_FILLANY broken flags |= PROC_FILLSTATUS; /* FIXME: need one, and PROC_FILLANY broken */
if (opt_euid && !opt_negate) { if (opt_euid && !opt_negate) {
int num = opt_euid[0].num; int num = opt_euid[0].num;
int i = num; int i = num;
@ -435,8 +435,8 @@ static struct el * select_procs (int *num)
{ {
PROCTAB *ptp; PROCTAB *ptp;
proc_t task; proc_t task;
unsigned long long saved_start_time; // for new/old support unsigned long long saved_start_time; /* for new/old support */
pid_t saved_pid = 0; // for new/old support pid_t saved_pid = 0; /* for new/old support */
int matches = 0; int matches = 0;
int size = 0; int size = 0;
regex_t *preg; regex_t *preg;
@ -536,7 +536,7 @@ static struct el * select_procs (int *num)
list = xrealloc(list, size * sizeof *list); list = xrealloc(list, size * sizeof *list);
} }
if (list && (opt_long || opt_echo)) { if (list && (opt_long || opt_echo)) {
char buff[5096]; // FIXME char buff[5096]; /* FIXME */
list[matches].num = task.XXXID; list[matches].num = task.XXXID;
list[matches++].str = xstrdup (cmd); list[matches++].str = xstrdup (cmd);
} else if (list) { } else if (list) {
@ -623,40 +623,40 @@ static void parse_opts (int argc, char **argv)
case 'e': case 'e':
opt_echo = 1; opt_echo = 1;
break; break;
// case 'D': // FreeBSD: print info about non-matches for debugging /* case 'D': / * FreeBSD: print info about non-matches for debugging * /
// break; * break; */
case 'F': // FreeBSD: the arg is a file containing a PID to match case 'F': /* FreeBSD: the arg is a file containing a PID to match */
opt_pidfile = xstrdup (optarg); opt_pidfile = xstrdup (optarg);
++criteria_count; ++criteria_count;
break; break;
case 'G': // Solaris: match rgid/rgroup case 'G': /* Solaris: match rgid/rgroup */
opt_rgid = split_list (optarg, conv_gid); opt_rgid = split_list (optarg, conv_gid);
if (opt_rgid == NULL) if (opt_rgid == NULL)
usage (opt); usage (opt);
++criteria_count; ++criteria_count;
break; break;
// case 'I': // FreeBSD: require confirmation before killing /* case 'I': / * FreeBSD: require confirmation before killing * /
// break; * break;
// case 'J': // Solaris: match by project ID (name or number) /* case 'J': / * Solaris: match by project ID (name or number) * /
// break; * break; */
case 'L': // FreeBSD: fail if pidfile (see -F) not locked case 'L': /* FreeBSD: fail if pidfile (see -F) not locked */
opt_lock++; opt_lock++;
break; break;
// case 'M': // FreeBSD: specify core (OS crash dump) file /* case 'M': / * FreeBSD: specify core (OS crash dump) file * /
// break; * break; */
// case 'N': // FreeBSD: specify alternate namelist file (for us, System.map -- but we don't need it) /* case 'N': / * FreeBSD: specify alternate namelist file (for us, System.map -- but we don't need it) * /
// break; * break; */
case 'P': // Solaris: match by PPID case 'P': /* Solaris: match by PPID */
opt_ppid = split_list (optarg, conv_num); opt_ppid = split_list (optarg, conv_num);
if (opt_ppid == NULL) if (opt_ppid == NULL)
usage (opt); usage (opt);
++criteria_count; ++criteria_count;
break; break;
// case 'S': // FreeBSD: don't ignore the built-in kernel tasks /* case 'S': / * FreeBSD: don't ignore the built-in kernel tasks * /
// break; * break; */
// case 'T': // Solaris: match by "task ID" (probably not a Linux task) /* case 'T': / * Solaris: match by "task ID" (probably not a Linux task) * /
// break; * break; */
case 'U': // Solaris: match by ruid/rgroup case 'U': /* Solaris: match by ruid/rgroup */
opt_ruid = split_list (optarg, conv_uid); opt_ruid = split_list (optarg, conv_uid);
if (opt_ruid == NULL) if (opt_ruid == NULL)
usage (opt); usage (opt);
@ -665,74 +665,74 @@ static void parse_opts (int argc, char **argv)
case 'V': case 'V':
printf(PROCPS_NG_VERSION); printf(PROCPS_NG_VERSION);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
// case 'c': // Solaris: match by contract ID /* case 'c': / * Solaris: match by contract ID * /
// break; * break; */
case 'c': case 'c':
opt_count = 1; opt_count = 1;
break; break;
case 'd': // Solaris: change the delimiter case 'd': /* Solaris: change the delimiter */
opt_delim = xstrdup (optarg); opt_delim = xstrdup (optarg);
break; break;
case 'f': // Solaris: match full process name (as in "ps -f") case 'f': /* Solaris: match full process name (as in "ps -f") */
opt_full = 1; opt_full = 1;
break; break;
case 'g': // Solaris: match pgrp case 'g': /* Solaris: match pgrp */
opt_pgrp = split_list (optarg, conv_pgrp); opt_pgrp = split_list (optarg, conv_pgrp);
if (opt_pgrp == NULL) if (opt_pgrp == NULL)
usage (opt); usage (opt);
++criteria_count; ++criteria_count;
break; break;
// case 'i': // FreeBSD: ignore case. OpenBSD: withdrawn. See -I. This sucks. /* case 'i': / * FreeBSD: ignore case. OpenBSD: withdrawn. See -I. This sucks. * /
// if (opt_case) * if (opt_case)
// usage (opt); * usage (opt);
// opt_case = REG_ICASE; * opt_case = REG_ICASE;
// break; * break; */
// case 'j': // FreeBSD: restricted to the given jail ID /* case 'j': / * FreeBSD: restricted to the given jail ID * /
// break; * break; */
case 'l': // Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? case 'l': /* Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? */
opt_long = 1; opt_long = 1;
break; break;
case 'n': // Solaris: match only the newest case 'n': /* Solaris: match only the newest */
if (opt_oldest|opt_negate|opt_newest) if (opt_oldest|opt_negate|opt_newest)
usage (opt); usage (opt);
opt_newest = 1; opt_newest = 1;
++criteria_count; ++criteria_count;
break; break;
case 'o': // Solaris: match only the oldest case 'o': /* Solaris: match only the oldest */
if (opt_oldest|opt_negate|opt_newest) if (opt_oldest|opt_negate|opt_newest)
usage (opt); usage (opt);
opt_oldest = 1; opt_oldest = 1;
++criteria_count; ++criteria_count;
break; break;
case 's': // Solaris: match by session ID -- zero means self case 's': /* Solaris: match by session ID -- zero means self */
opt_sid = split_list (optarg, conv_sid); opt_sid = split_list (optarg, conv_sid);
if (opt_sid == NULL) if (opt_sid == NULL)
usage (opt); usage (opt);
++criteria_count; ++criteria_count;
break; break;
case 't': // Solaris: match by tty case 't': /* Solaris: match by tty */
opt_term = split_list (optarg, conv_str); opt_term = split_list (optarg, conv_str);
if (opt_term == NULL) if (opt_term == NULL)
usage (opt); usage (opt);
++criteria_count; ++criteria_count;
break; break;
case 'u': // Solaris: match by euid/egroup case 'u': /* Solaris: match by euid/egroup */
opt_euid = split_list (optarg, conv_uid); opt_euid = split_list (optarg, conv_uid);
if (opt_euid == NULL) if (opt_euid == NULL)
usage (opt); usage (opt);
++criteria_count; ++criteria_count;
break; break;
case 'v': // Solaris: as in grep, invert the matching (uh... applied after selection I think) case 'v': /* Solaris: as in grep, invert the matching (uh... applied after selection I think) */
if (opt_oldest|opt_negate|opt_newest) if (opt_oldest|opt_negate|opt_newest)
usage (opt); usage (opt);
opt_negate = 1; opt_negate = 1;
break; break;
// OpenBSD -x, being broken, does a plain string /* OpenBSD -x, being broken, does a plain string */
case 'x': // Solaris: use ^(regexp)$ in place of regexp (FreeBSD too) case 'x': /* Solaris: use ^(regexp)$ in place of regexp (FreeBSD too) */
opt_exact = 1; opt_exact = 1;
break; break;
// case 'z': // Solaris: match by zone ID /* case 'z': / * Solaris: match by zone ID * /
// break; * break; */
case 'h': case 'h':
usage (opt); usage (opt);
break; break;
@ -790,7 +790,7 @@ int main (int argc, char **argv)
continue; continue;
} }
if (errno==ESRCH) if (errno==ESRCH)
// gone now, which is OK /* gone now, which is OK */
continue; continue;
xwarn(_("killing pid %d failed"), procs[i].num); xwarn(_("killing pid %d failed"), procs[i].num);
} }
@ -804,5 +804,5 @@ int main (int argc, char **argv)
output_numlist (procs,num); output_numlist (procs,num);
} }
} }
return !num; // exit(EXIT_SUCCESS) if match, otherwise exit(EXIT_FAILURE) return !num; /* exit(EXIT_SUCCESS) if match, otherwise exit(EXIT_FAILURE) */
} }