diff options
author | Christopher Faylor <me@cgf.cx> | 2000-02-17 19:38:33 +0000 |
---|---|---|
committer | Christopher Faylor <me@cgf.cx> | 2000-02-17 19:38:33 +0000 |
commit | 1fd5e000ace55b323124c7e556a7a864b972a5c4 (patch) | |
tree | dc4fcf1e5e22a040716ef92c496b8d94959b2baa /winsup/utils/ps.cc | |
parent | 369d8a8fd5e887eca547bf34bccfdf755c9e5397 (diff) | |
download | newlib-1fd5e000ace55b323124c7e556a7a864b972a5c4.zip newlib-1fd5e000ace55b323124c7e556a7a864b972a5c4.tar.gz newlib-1fd5e000ace55b323124c7e556a7a864b972a5c4.tar.bz2 |
import winsup-2000-02-17 snapshot
Diffstat (limited to 'winsup/utils/ps.cc')
-rw-r--r-- | winsup/utils/ps.cc | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/winsup/utils/ps.cc b/winsup/utils/ps.cc new file mode 100644 index 0000000..4767a6a --- /dev/null +++ b/winsup/utils/ps.cc @@ -0,0 +1,150 @@ +/* ps.cc + + Copyright 1996, 1997, 1998 Cygnus Solutions. + +This file is part of Cygwin. + +This software is a copyrighted work licensed under the terms of the +Cygwin license. Please consult the file "CYGWIN_LICENSE" for +details. */ + +#include <stdio.h> +#include <time.h> +#include <getopt.h> +#include <unistd.h> +#include <stdlib.h> +#include <pwd.h> +#include <sys/cygwin.h> +#include "winsup.h" +#include "external.h" + +static char * +start_time (external_pinfo *child) +{ + time_t st = child->start_time; + time_t t = time (NULL); + static char stime[40] = {'\0'}; + char now[40]; + + strncpy (stime, ctime (&st) + 4, 15); + strcpy (now, ctime (&t) + 4); + + if ((t - st) < (24 * 3600)) + return (stime + 7); + + stime[6] = '\0'; + + return stime; +} + +int +main (int argc, char *argv[]) +{ + external_pinfo *p; + int aflag, lflag, fflag, uid; + const char *dtitle = " PID TTY STIME COMMAND\n"; + const char *dfmt = "%5d%4d%10s %s\n"; + const char *ftitle = " UID PID PPID TTY STIME COMMAND\n"; + const char *ffmt = "%8.8s%6d%6d%4d%10s %s\n"; + const char *ltitle = " PID PPID PGID WINPID UID TTY STIME COMMAND\n"; + const char *lfmt = "%c %5d %5d %5d %8u %4d %3d %8s %s\n"; + char ch; + + aflag = lflag = fflag = 0; + uid = getuid (); + + while ((ch = getopt (argc, argv, "aelfu:")) != -1) + switch (ch) + { + case 'a': + case 'e': + aflag = 1; + break; + case 'f': + fflag = 1; + break; + case 'l': + lflag = 1; + break; + case 'u': + uid = atoi (optarg); + if (uid == 0) + { + struct passwd *pw; + + if ((pw = getpwnam (optarg))) + uid = pw->pw_uid; + else + { + fprintf (stderr, "user %s unknown\n", optarg); + exit (1); + } + } + break; + default: + fprintf (stderr, "Usage %s [-aefl] [-u uid]\n", argv[0]); + 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"); + exit (1); + } + + if (lflag) + printf (ltitle); + else if (fflag) + printf (ftitle); + else + printf (dtitle); + + (void) cygwin_internal (CW_LOCK_PINFO, 1000); + + for (int pid = 0; + (p = (external_pinfo *) cygwin_internal (CW_GETPINFO, + pid | CW_NEXTPID)); + pid = p->pid) + { + if (p->process_state == PID_NOT_IN_USE) + continue; + if (!aflag && p->uid != uid) + continue; + char status = ' '; + if (p->process_state & PID_STOPPED) + status = 'S'; + else if (p->process_state & PID_TTYIN) + status = 'I'; + else if (p->process_state & PID_TTYOU) + status = 'O'; + + char pname[MAX_PATH]; + if (p->process_state & PID_ZOMBIE) + strcpy (pname, "<defunct>"); + else + cygwin_conv_to_posix_path (p->progname, pname); + + char uname[128]; + + if (fflag) + { + struct passwd *pw; + + if ((pw = getpwuid (p->uid))) + strcpy (uname, pw->pw_name); + else + sprintf (uname, "%d", p->uid); + } + + if (lflag) + printf (lfmt, status, p->pid, p->ppid, p->pgid, + p->dwProcessId, p->uid, p->ctty, start_time (p), pname); + else if (fflag) + printf (ffmt, uname, p->pid, p->ppid, p->ctty, start_time (p), pname); + else + printf (dfmt, p->pid, p->ctty, start_time (p), pname); + + } + (void) cygwin_internal (CW_UNLOCK_PINFO); + + return 0; +} + |