int show_tasks(char *argc){ int i,total=0; if (strcmp(argc,"-a")==0){ int total=0; for (i=1; i<NR_TASKS; i++) if (proc_table[i].status!=STOPPED) ++total; display_string("There are "); display_int(total); display_string(" task!\n"); for (i=1; i<NR_TASKS; i++) if (proc_table[i].status!=STOPPED) display_task(proc_table+i); return 0; } else if (strcmp(argc,"-r")==0){ for (i=1; i<NR_TASKS; i++) if (proc_table[i].status==RUNNING) ++total; display_string("There are "); display_int(total); display_string(" task RUNNING!\n"); for (i=1; i<NR_TASKS; i++) if (proc_table[i].status==RUNNING) display_task(proc_table+i); return 0; } /*else if (strcmp(argc,"-d")==0){ for (i=1; i<NR_TASKS; i++) if (proc_table[i].status==STOPPED) ++total; display_string("There are "); display_int(total); display_string(" task STOPPED!\n"); for (i=1; i<NR_TASKS; i++) if (proc_table[i].status==STOPPED) display_task(proc_table+i); return 0; } */else if (strcmp(argc,"-s")==0){ for (i=1; i<NR_TASKS; i++) if (proc_table[i].status==SLEEPING) ++total; display_string("There are "); display_int(total); display_string(" task SLEEPING!\n"); for (i=1; i<NR_TASKS; i++) if (proc_table[i].status==SLEEPING) display_task(proc_table+i); return 0; } else { for (i=1; i<NR_TASKS; i++) if (strcmp(proc_table[i].p_name,argc)==0){ display_task(proc_table+i); return 0; } } return -1; }
int main(int argc, char **argv) { memset(pids, 0, MAX_PIDS * sizeof(char)); int i; for(i=1;i<argc;i++) { if(argv[i][0] == '-') { int q=1; again1: switch(argv[i][q]) { case 'A': all=without_tty=1; break; case 'f': full=1; break; case 'L': loop=1; break; } if(argv[i][++q]) goto again1; } else { int w=0; again2: switch(argv[i][w]) { case 'x': without_tty=1; break; case 'a': all=1; break; case 'r': only_running=1; break; case 'P': proc_count=1; break; } if(argv[i][++w]) goto again2; } } int tmp = open("/dev/tty", O_RDWR); if(tmp < 0) { fprintf(stderr, "%s: couldn't open controlling terminal\n", basename(argv[0])); exit(1); } ioctl(tmp, 8, (int)&my_tty); close(tmp); struct task_stat s; if(!full && !proc_count) printf(" PID TTY\t TIME CMD\n"); else if(!proc_count) printf(" PID TTY\t UID GID UTIME STIME MEM STATE CMD\n"); i=0; int pc=0; int pcr=0; while(!task_stat(i, &s)) { if(s.pid >= MAX_PIDS) {i++; continue;} if(pids[s.pid]) {i++; continue;} pids[s.pid]++; if(!proc_count) display_task(&s); else s.state ? pc++ : pcr++; i++; } if(proc_count) printf("%d processes (%d running)\n", pc+pcr, pcr); fflush(0); return 0; }