home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.linux
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!batcomputer!cornell!uw-beaver!news.u.washington.edu!stein.u.washington.edu!barr
- From: barr@stein.u.washington.edu (David Barr)
- Subject: ps that uses /proc
- Message-ID: <1992Dec14.032831.9921@u.washington.edu>
- Sender: news@u.washington.edu (USENET News System)
- Organization: University of Washington
- Date: Mon, 14 Dec 1992 03:28:31 GMT
- Lines: 100
-
- Has anyone written a version of ps that uses the proc file system? I
- couldn't get my old version of ps to work with linux 0.99, so I wrote
- my own ps program. It uses the proc file system, so it should work
- with any version of the kernel which includes this file system. There
- are a few major disadvantages of my program. Because I wrote it in a
- hurry, the output is kind of non-standard and there are only 3 options
- (a, u and x). I may fix it later if there is any interest. There
- were a few fields that I couldn't figure out how to get out of the
- proc system. These fields include MEM%, CPU%, MEM% and TIME. I also
- don't do nearly as much error checking as I should. All that aside,
- it does just about everything I need it to. So here is the code
- (comments are welcome):
-
- #include <stdio.h>
- #include <sys/dir.h>
- #include <regex.h>
- #include <sys/stat.h>
- #include <pwd.h>
- #include <string.h>
- #include <unistd.h>
-
- struct direct *ent;
- char filename[80];
-
- void mycpy(ret, what)
- char *ret;
- char *what;
- {
- FILE *fp;
- int i=0;
- char c;
-
- sprintf(filename, "/proc/%s/%s", ent->d_name, what);
- fp = fopen(filename, "r");
- while(!feof(fp))
- ret[i++] = ((c = getc(fp)) ? c : ' ');
- ret[i-1] = '\0';
- fclose(fp);
- }
-
- void ttynam(c, x)
- char *c;
- int x;
- {
- switch(x) {
- case 1: case 2: case 3: case 4:
- sprintf(c, "c%d", x);
- break;
- case 64: case 65: case 66: case 67:
- sprintf(c, "s%d", x - 63);
- break;
- default:
- strcpy(c, "??");
- }
- }
-
- int main(argc, argv)
- int argc;
- char *argv[];
- {
- DIR *proc;
- char cmdline[1024], stat_str[80], cmd1[30], cmd2[30], state, ttyc[3];
- struct stat sb;
- int pid, ppid, pgrp, session, tty, i, uid;
- int opt[256];
-
- /* read command line options (if any) */
- bzero(opt, sizeof(opt));
- if(argc > 1)
- for(i=0; argv[i]; i++)
- opt[(int)argv[1][i]]++;
-
- printf("%s PID PPID TT STAT COMMAND\n", opt['u'] ? "USER " : "");
- proc = opendir("/proc");
- re_comp("^[0-9]*$");
- uid = getuid();
- while(ent = readdir(proc)) {
- if(!re_exec(ent->d_name))
- continue;
- sprintf(filename, "/proc/%s", ent->d_name);
- stat(filename, &sb);
- if(!opt['a'] && (sb.st_uid != uid))
- continue;
- mycpy(cmdline, "cmdline");
- mycpy(stat_str, "stat");
- sscanf(stat_str, "%d (%[^)]) %c %d %d %d %d", &pid, cmd1, &state,
- &ppid, &pgrp, &session, &tty);
- if(!opt['x'] && (tty == -1))
- continue;
- sprintf(cmd2, "(%s)", cmd1);
- ttynam(ttyc, tty);
- if(opt['u'])
- printf("%-9s", getpwuid(sb.st_uid)->pw_name);
- printf("%4d %4d %s %c %s\n", pid, ppid, ttyc, state, strlen(cmdline) ?
- cmdline : cmd2);
- }
- closedir(proc);
- exit(0);
- }
-
-