-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.c
51 lines (49 loc) · 1.15 KB
/
jobs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "headers.h"
void jobs(int id[100], int num[100])
{
char path[100], exepath[100], line[100], *p;
strcpy(path, "/proc/");
int cur = 1;
DIR * dp = opendir(path);
struct dirent * ptr;
ptr = readdir(dp);
for (int i = 0; i < 100; ++i)
{
struct stat st;
char aux[200], state[200], name[200];
strcpy(aux, path);
sprintf(aux + strlen(aux), "%ld", (long) id[i]);
stat(aux, &st);
strcat(aux, "/");
strcat(aux, "status");
FILE* fd = fopen(aux,"r");
if(fd <= 0 || id[i]==0)
{
continue;
}
printf("[%d]\t",num[i]);
while(fgets(line, 100, fd))
{
if(strncmp(line, "Name:", 5) != 0)
continue;
p = line + 6;
while(*p == ' ') ++p;
strcpy(name, p);
name[strlen(name) - 1] = '\0';
break;
}
while(fgets(line, 100, fd))
{
if(strncmp(line, "State:", 6) != 0)
continue;
p = line + 7;
while(*p == ' ') ++p;
strcpy(state, p);
state[strlen(state) - 1] = '\0';
break;
}
printf("%s\t%s [%d]\n",state,name,id[i] );
fclose(fd);
}
closedir(dp);
}