-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproclore.c
More file actions
80 lines (78 loc) · 2.33 KB
/
proclore.c
File metadata and controls
80 lines (78 loc) · 2.33 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
#include "headers.h"
void proclore(char* input){
int pid = getpid();
char* temp = input;
char* another = input;
temp = strtok(another, " ");
temp = strtok(NULL, " ");
if(temp != NULL){
// mencaing there is a proces given
temp[strlen(temp) + 1] = '\0';
pid = atoi(temp);
}
char here[N * 10];
snprintf(here, sizeof(here), "/proc/%d/status", pid);
printf("pid : %d\n", pid);
char* dup = strtok(here, "\n");
FILE *f = fopen(here, "r");
char* line = NULL;
size_t length = 0;
int index = 0;
while((getline(&line, &length, f)) != -1){
// readin the file line by line
if(index == 2){
int take = getpgid(pid);
line[strcspn(line, "\n")] = 0;
if(take == pid) // foreground process
printf("process status : %c+\n", line[7]);
else // background process
printf("process status : %c\n", line[7]);
}
else if(index == 17){
char* proc = line;
char* pro = strtok(proc, ":");
pro = strtok(NULL, ":");
pro[strcspn(pro, "\n")] = 0;
printf("Process Group : %d\n", getpgid(pid));
char* real = (char*) malloc(N);
int str = 0;
while(str < strlen(pro) && (pro[str] == ' ' || pro[str] == '\t')){
str++;
}
int k = 0;
for(int i = str; i < strlen(pro) - 2; ++i)
real[k++] = pro[i];
real[k] = '\0';
printf("Virtual memory : %s\n", real);
break;
}
dup = strtok(NULL, "\n");
++index;
}
fclose(f);
sprintf(here, "/proc/%d/exe", pid);
char path[N];
int where;
while((where = readlink(here, path, N)) < 0){
printf("executable path : path does not exist.\n");
return;
}
path[where] = '\0';
char res[N];
res[0] = '~';
bool ok = 1;
for(int i = 0; i < strlen(homedir); ++i){
if(homedir[i] != path[i]){
ok = 0;
break;
}
}
if(ok){
for(int i = strlen(homedir); i < strlen(path); ++i)
res[i - strlen(homedir) + 1] = path[i];
res[strlen(path) - strlen(homedir) + 1] = '\0';
}
else
strcpy(res, path);
printf("exectuable path : %s\n", res);
}