-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivities.c
More file actions
91 lines (81 loc) · 2.18 KB
/
activities.c
File metadata and controls
91 lines (81 loc) · 2.18 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "headers.h"
int is_digit(char c)
{
return c >= '0' && c <= '9';
}
void get_full_stat(char *state_status, char *state)
{
if (!strcmp(state, "R"))
{
strcpy(state_status, "Running");
}
else if (!strcmp(state, "Z"))
{
strcpy(state_status, "Stopped");
}
else if (!strcmp(state, "S"))
{
strcpy(state_status, "Running");
}
else
{
strcpy(state_status, "Stopped");
}
}
void activities()
{
char shell_pid[32];
int pid = getpid();
sprintf(shell_pid, "%d", pid);
struct dirent **namelist;
int n;
n = scandir("/proc", &namelist, NULL, NULL);
if (n < 0)
{
perror("scandir");
return;
}
for (int i = 0; i < n; i++)
{
if (is_digit(namelist[i]->d_name[0]))
{
char status_path[280];
sprintf(status_path, "/proc/%s/status", namelist[i]->d_name);
FILE *file = fopen(status_path, "r");
if (file)
{
char line[256];
char cmd_name[256] = "";
char state[32] = "";
int is_shell_process = 0;
while (fgets(line, sizeof(line), file) != NULL)
{
if (!strncmp(line, "Name:", 5))
{
sscanf(line, "Name:\t%s", cmd_name);
}
else if (!strncmp(line, "State:", 6))
{
sscanf(line, "State:\t%s", state);
}
else if (!strncmp(line, "PPid:", 5))
{
if (!strcmp(shell_pid, strtok(line + 6, " \n")))
{
is_shell_process = 1;
}
}
}
fclose(file);
char state_status[20];
get_full_stat(state_status, state);
if (is_shell_process)
{
printf("[%s] : %s - %s\n", namelist[i]->d_name, cmd_name, state_status);
}
}
free(namelist[i]);
}
}
free(namelist);
}