Advertisement
duskykmh

method to output proc/pid/stat info in a table format

May 6th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. static void printOutput() {
  2.     //Layout the output table's first column
  3.     p1putstr(STDOUT_FILENO, "\npid\tcmd\tstatus\tstate\tutime\tstime\tpriority\tvsize\texit code\n");
  4.    
  5.     for (int i=0; i < procNum; i++) {
  6.         char path[100];
  7.         path[0] = '\0';
  8.  
  9.         // Create path to /proc/current_pid/stat to open
  10.         p1strcat(path, "/proc/");
  11.         p1strcat(path, p1itoa(procs[i].pid));
  12.         p1strcat(path, "/stat");
  13.  
  14.         char state[4];
  15.         state[0] = '\0';
  16.         char utime[32];
  17.         utime[0] = '\0';
  18.         char stime[32];
  19.         stime[0] = '\0';
  20.         char priority[8];
  21.         priority[0] = '\0';
  22.         char vsize[8];
  23.         vsize[0] = '\0';
  24.         char exitc[32];
  25.         exitc[0] = '\0';
  26.        
  27.         printf("This is the current path: %s\n", path); //TEST
  28.  
  29.         int fd = open(path, O_RDONLY);
  30.         printf("This is the current fd: %d\n", fd);
  31.         if (fd > 0) {
  32.             char stat[512];
  33.             long count = read(fd, &stat, 512);
  34.             stat[count] = '\0';
  35.             stat[p1strlen(stat)-1] = '\0'; //remove New Line Symbol from final path
  36.            
  37.             int index = 0;
  38.             int statIndex = 1;
  39.             do {
  40.                 char word[64];
  41.                 index = p1getword(stat, index, word);
  42.                 printf("This is the current index on line 151: %d\n", index);
  43.                 if (index == -1) {
  44.                     break;
  45.                 }
  46.  
  47.                 printf("This is the current statIndex on line 171: %d\n", statIndex);
  48.                 if (statIndex == 3) {
  49.                     p1strcat(state, word);
  50.                 } else if (statIndex == 14) {
  51.                     p1strcat(utime, word);
  52.                 } else if (statIndex == 15) {
  53.                     p1strcat(stime, word);
  54.                 } else if (statIndex == 18) {
  55.                     p1strcat(priority, word);
  56.                 } else if (statIndex == 23) {
  57.                     p1strcat(vsize, word);
  58.                 } else if (statIndex == 52) {
  59.                     p1strcat(exitc, word);
  60.                 }
  61.                 statIndex++;
  62.                 printf("This is the current statIndex on line 171: %d\n", statIndex);
  63.             } while (index >= 0);
  64.             close(fd);
  65.         }
  66.        
  67.         p1putint(STDOUT_FILENO, procs[i].pid);
  68.         p1putstr(STDOUT_FILENO, "\t");
  69.        
  70.         char cmd[32];
  71.         p1getword(procs[i].command, 0, cmd);
  72.         p1putstr(STDOUT_FILENO, cmd);
  73.         p1putstr(STDOUT_FILENO, "\t");
  74.        
  75.         if (procs[i].procStarted == 1) {
  76.             p1putstr(STDOUT_FILENO, "started");
  77.         } else if (procs[i].procRunning == 1) {
  78.             p1putstr(STDOUT_FILENO, "running");
  79.         } else if (procs[i].procRunning != 1 && procs[i].procEnded != 1) {
  80.             p1putstr(STDOUT_FILENO, "suspended");
  81.         } else if (procs[i].procEnded == 1) {
  82.             p1putstr(STDOUT_FILENO, "terminated");
  83.         }
  84.        
  85.         p1putstr(STDOUT_FILENO, state);
  86.         p1putstr(STDOUT_FILENO, "\t");
  87.         p1putstr(STDOUT_FILENO, utime);
  88.         p1putstr(STDOUT_FILENO, "\t");
  89.         p1putstr(STDOUT_FILENO, stime);
  90.         p1putstr(STDOUT_FILENO, "\t");
  91.         p1putstr(STDOUT_FILENO, priority);
  92.         p1putstr(STDOUT_FILENO, "\t");
  93.         p1putstr(STDOUT_FILENO, vsize);
  94.         p1putstr(STDOUT_FILENO, "\t");
  95.         p1putstr(STDOUT_FILENO, exitc);
  96.         p1putstr(STDOUT_FILENO, "\n");
  97.     }
  98.     p1putstr(STDOUT_FILENO, "\n");
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement