-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinfo.c
70 lines (55 loc) · 1.36 KB
/
pinfo.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "pinfo.h"
#include "headers.h"
int tichnas_pinfo(char* root, char* pidStr) {
char dataFolder[1024] = "/proc/";
char fileName[1024];
int pid = -1;
char status = '-';
int memory = -1;
char* path = malloc(1024);
int pgrp = -1;
int tpgid = -1;
if (pidStr) {
strcat(dataFolder, pidStr);
} else {
strcat(dataFolder, "self");
}
strcpy(fileName, dataFolder);
strcat(fileName, "/stat");
FILE* file = fopen(fileName, "r");
if (file) {
fscanf(file,
"%d %*s %c %*s %d %*s %*s %d %*s %*s %*s %*s %*s %*s %*s %*s %*s "
"%*s %*s %*s %*s %*s %d",
&pid, &status, &pgrp, &tpgid, &memory);
printf("pid -- %d\nProcess Status -- %c%c\nmemory -- %d\n", pid, status,
pgrp == tpgid ? '+' : ' ', memory);
} else {
perror("Error");
return -1;
}
fclose(file);
strcpy(fileName, dataFolder);
strcat(fileName, "/exe");
int pathLen = readlink(fileName, path, 1023);
if (pathLen >= 0) {
int isRelative = 0;
path[pathLen] = '\0';
for (int i = 0;; i++) {
if (root[i] == '\0') {
isRelative = 1;
break;
}
if (i == pathLen || path[i] != root[i]) break;
}
if (isRelative) {
path += strlen(root) - 1;
path[0] = '~';
}
printf("Executable Path -- %s\n", path);
} else {
perror("Error");
return -1;
}
return 0;
}