-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinfo.c
67 lines (63 loc) · 2.49 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
#include "headers.h"
// function to read the nth line from a file
char* r_line(char *filename, int n)
{
FILE* file = fopen(filename, "r");
char line[256];
char *data = malloc(sizeof(char) * 256);
int i = 0;
while (fgets(line, sizeof(line), file))
{
i++;
if(i == n)
strcpy(data, line);
}
fclose(file);
return data;
}
// function to get process details
void pinfo(pid_t p_id)
{
char *exe = malloc(sizeof(char) *(50));
char *status = malloc(sizeof(char) *(50));
sprintf(status,"/proc/%d/status", p_id); // getting much of stat info in an easily understandable format
sprintf(exe,"/proc/%d/exe", p_id); // getting symbolic link containing the pathname of the executed command
FILE *op;
if(!(op = fopen(status, "r")))
{
printf(DFLT "Process %d does not exist\n", p_id);
strcpy(emoji,":'(");
}
else
{
fclose(op);
char *status_data = r_line(status, 3);
char *memory_data = r_line(status, 18);
char *token1 = strtok(status_data, ":\t"); // getting status info
token1 = strtok(NULL, ":\t");
char *token2 = strtok(memory_data, ":\t\r "); // getting memory info
token2 = strtok(NULL, ":\t\r");
char process_path[MX_L2];
char *path;
printf(DFLT "pid -- %d\nProcess Status -- %smemory (Virtual Memory) -- %sExecutable Path -- ", p_id, token1, token2);
int ret = readlink(exe, process_path, MX_L1); // getting executable path
if(ret == -1)
printf(DFLT "no path\n");
else
{
process_path[ret] = '\0';
path = strstr(process_path, HOME);
if(path)
{
path = path + strlen(HOME);
printf(DFLT "~%s\n", path);
}
else
printf(DFLT "~%s\n", process_path);
}
free(status_data);
free(memory_data);
}
free(status);
free(exe);
}