* ps.cc (prog_name): New global variable.

(longopts): Ditto.
(opts): Ditto.
(usage): New function.
(print_version): New function.
(main): Accomodate longopts and new --help, --version options.
This commit is contained in:
Christopher Faylor 2002-05-28 03:38:36 +00:00
parent 0c12979b72
commit ad39fa8cb0
2 changed files with 86 additions and 11 deletions

View File

@ -1,3 +1,12 @@
2002-05-27 Joshua Daniel Franklin <joshuadfranklin@yahoo.com>
* ps.cc (prog_name): New global variable.
(longopts): Ditto.
(opts): Ditto.
(usage): New function.
(print_version): New function.
(main): Accomodate longopts and new --help, --version options.
2002-05-26 Christopher Faylor <cgf@redhat.com> 2002-05-26 Christopher Faylor <cgf@redhat.com>
* strace.cc (attach_process): Don't tell process to start stracing * strace.cc (attach_process): Don't tell process to start stracing

View File

@ -1,6 +1,6 @@
/* ps.cc /* ps.cc
Copyright 1996, 1997, 1998, 1999, 2000, 2001 Red Hat, Inc. Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
This file is part of Cygwin. This file is part of Cygwin.
@ -19,6 +19,25 @@ details. */
#include <tlhelp32.h> #include <tlhelp32.h>
#include <psapi.h> #include <psapi.h>
static const char version[] = "$Revision$";
static char *prog_name;
static struct option longopts[] =
{
{"all", no_argument, NULL, 'a' },
{"everyone", no_argument, NULL, 'e' },
{"full", no_argument, NULL, 'f' },
{"help", no_argument, NULL, 'h' },
{"long", no_argument, NULL, 'l' },
{"summary", no_argument, NULL, 's' },
{"user", required_argument, NULL, 'u'},
{"version", no_argument, NULL, 'v'},
{"windows", no_argument, NULL, 'W'},
{NULL, 0, NULL, 0}
};
static char opts[] = "aefhlsu:vW";
typedef BOOL (WINAPI *ENUMPROCESSMODULES)( typedef BOOL (WINAPI *ENUMPROCESSMODULES)(
HANDLE hProcess, // handle to the process HANDLE hProcess, // handle to the process
HMODULE * lphModule, // array to receive the module handles HMODULE * lphModule, // array to receive the module handles
@ -177,6 +196,46 @@ ttynam (int ntty)
return buf; return buf;
} }
static void
usage (FILE * stream, int status)
{
fprintf (stream, "\
Usage: %s [-aefls] [-u UID]\n\
-a, --all show processes of all users\n\
-e, --everyone show processes of all users\n\
-f, --full show process uids, ppids\n\
-h, --help output usage information and exit\n\
-l, --long show process uids, ppids, pgids, winpids\n\
-s, --summary show process summary\n\
-u, --user list processes owned by UID\n\
-v, --version output version information and exit\n\
-W, --windows show windows as well as cygwin processes\n\
With options, %s outputs the long format by default\n", prog_name, prog_name);
exit (status);
}
static void
print_version ()
{
const char *v = strchr (version, ':');
int len;
if (!v)
{
v = "?";
len = 1;
}
else
{
v += 2;
len = strchr (v, ' ') - v;
}
printf ("\
%s (cygwin) %.*s\n\
Process Statistics\n\
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.\n\
Compiled on %s", prog_name, len, v, __DATE__);
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -195,7 +254,15 @@ main (int argc, char *argv[])
uid = getuid (); uid = getuid ();
lflag = 1; lflag = 1;
while ((ch = getopt (argc, argv, "aelfsu:W")) != -1) prog_name = strrchr (argv[0], '/');
if (prog_name == NULL)
prog_name = strrchr (argv[0], '\\');
if (prog_name == NULL)
prog_name = argv[0];
else
prog_name++;
while ((ch = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
switch (ch) switch (ch)
{ {
case 'a': case 'a':
@ -205,6 +272,8 @@ main (int argc, char *argv[])
case 'f': case 'f':
fflag = 1; fflag = 1;
break; break;
case 'h':
usage (stdout, 0);
case 'l': case 'l':
lflag = 1; lflag = 1;
break; break;
@ -221,25 +290,22 @@ main (int argc, char *argv[])
uid = pw->pw_uid; uid = pw->pw_uid;
else else
{ {
fprintf (stderr, "user %s unknown\n", optarg); fprintf (stderr, "%s: user %s unknown\n", prog_name, optarg);
exit (1); exit (1);
} }
} }
break; break;
case 'v':
print_version ();
exit (0);
break;
case 'W': case 'W':
query = CW_GETPINFO_FULL; query = CW_GETPINFO_FULL;
aflag = 1; aflag = 1;
break; break;
default: default:
fprintf (stderr, "Usage %s [-aefl] [-u uid]\n", argv[0]); usage (stderr, 1);
fprintf (stderr, "-f = show process uids, ppids\n");
fprintf (stderr, "-l = show process uids, ppids, pgids, winpids\n");
fprintf (stderr, "-u uid = list processes owned by uid\n");
fprintf (stderr, "-a, -e = show processes of all users\n");
fprintf (stderr, "-s = show process summary\n");
fprintf (stderr, "-W = show windows as well as cygwin processes\n");
exit (1);
} }
if (sflag) if (sflag)