home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / linux / 20262 < prev    next >
Encoding:
Text File  |  1992-12-13  |  3.0 KB  |  111 lines

  1. Newsgroups: comp.os.linux
  2. 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
  3. From: barr@stein.u.washington.edu (David Barr)
  4. Subject: ps that uses /proc
  5. Message-ID: <1992Dec14.032831.9921@u.washington.edu>
  6. Sender: news@u.washington.edu (USENET News System)
  7. Organization: University of Washington
  8. Date: Mon, 14 Dec 1992 03:28:31 GMT
  9. Lines: 100
  10.  
  11. Has anyone written a version of ps that uses the proc file system?  I
  12. couldn't get my old version of ps to work with linux 0.99, so I wrote
  13. my own ps program.  It uses the proc file system, so it should work
  14. with any version of the kernel which includes this file system.  There
  15. are a few major disadvantages of my program.  Because I wrote it in a
  16. hurry, the output is kind of non-standard and there are only 3 options
  17. (a, u and x).  I may fix it later if there is any interest.  There
  18. were a few fields that I couldn't figure out how to get out of the
  19. proc system.  These fields include MEM%, CPU%, MEM% and TIME.  I also
  20. don't do nearly as much error checking as I should.  All that aside,
  21. it does just about everything I need it to.  So here is the code
  22. (comments are welcome):
  23.  
  24. #include <stdio.h>
  25. #include <sys/dir.h>
  26. #include <regex.h>
  27. #include <sys/stat.h>
  28. #include <pwd.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31.  
  32. struct direct *ent;
  33. char filename[80];
  34.  
  35. void mycpy(ret, what)
  36.      char *ret;
  37.      char *what;
  38. {
  39.   FILE *fp;
  40.   int i=0;
  41.   char c;
  42.  
  43.   sprintf(filename, "/proc/%s/%s", ent->d_name, what);
  44.   fp = fopen(filename, "r");
  45.   while(!feof(fp))
  46.     ret[i++] = ((c = getc(fp)) ? c : ' ');
  47.   ret[i-1] = '\0';
  48.   fclose(fp);
  49. }
  50.  
  51. void ttynam(c, x)
  52.      char *c;
  53.      int x;
  54. {
  55.   switch(x) {
  56.   case 1: case 2: case 3: case 4:
  57.     sprintf(c, "c%d", x);
  58.     break;
  59.   case 64: case 65: case 66: case 67:
  60.     sprintf(c, "s%d", x - 63);
  61.     break;
  62.   default:
  63.     strcpy(c, "??");
  64.   }
  65. }
  66.  
  67. int main(argc, argv)
  68.      int argc;
  69.      char *argv[];
  70. {
  71.   DIR *proc;
  72.   char cmdline[1024], stat_str[80], cmd1[30], cmd2[30], state, ttyc[3];
  73.   struct stat sb;
  74.   int pid, ppid, pgrp, session, tty, i, uid;
  75.   int opt[256];
  76.  
  77.   /* read command line options (if any) */
  78.   bzero(opt, sizeof(opt));
  79.   if(argc > 1)
  80.     for(i=0; argv[i]; i++)
  81.       opt[(int)argv[1][i]]++;
  82.  
  83.   printf("%s PID PPID TT STAT COMMAND\n", opt['u'] ? "USER     " : "");
  84.   proc = opendir("/proc");
  85.   re_comp("^[0-9]*$");
  86.   uid = getuid();
  87.   while(ent = readdir(proc)) {
  88.     if(!re_exec(ent->d_name))
  89.       continue;
  90.     sprintf(filename, "/proc/%s", ent->d_name);
  91.     stat(filename, &sb);
  92.     if(!opt['a'] && (sb.st_uid != uid))
  93.       continue;
  94.     mycpy(cmdline, "cmdline");
  95.     mycpy(stat_str, "stat");
  96.     sscanf(stat_str, "%d (%[^)]) %c %d %d %d %d", &pid, cmd1, &state,
  97.        &ppid, &pgrp, &session, &tty);
  98.     if(!opt['x'] && (tty == -1))
  99.       continue;
  100.     sprintf(cmd2, "(%s)", cmd1);
  101.     ttynam(ttyc, tty);
  102.     if(opt['u'])
  103.       printf("%-9s", getpwuid(sb.st_uid)->pw_name);
  104.     printf("%4d %4d %s    %c %s\n", pid, ppid, ttyc, state, strlen(cmdline) ?
  105.        cmdline : cmd2);
  106.   }
  107.   closedir(proc);
  108.   exit(0);
  109. }
  110.  
  111.