-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinfo.c
80 lines (73 loc) · 1.65 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
71
72
73
74
75
76
77
78
79
80
#include "header.h"
// Files used : /proc/pid/status /proc/pid/exec
void pinfo(char *inst) // Executes pinfo command
{
int pid;
if(inst == NULL)
{
pid = getpid();
}
else
{
pid = atoi(inst);
}
char *st=malloc(50*sizeof(char)), *exe=malloc(50*sizeof(char));
if(st==NULL)
{
fprintf(stderr,"%s Error in assigning memory %s",RED,NORMAL);
status = 0;
exit(0);
}
if(exe==NULL)
{
fprintf(stderr,"%s Error in assigning memory %s",RED,NORMAL);
status = 0;
exit(0);
}
sprintf(st,"/proc/%d/status",pid);
sprintf(exe,"/proc/%d/exe",pid);
FILE *f;
if((f=fopen(st,"r"))==NULL)
{
printf("Process ID doesn't exist\n");
status = 0;
return;
}
int n=3, i=0,m=18;
char *mem=malloc(10000*sizeof(char)), *status1 = malloc(10000*sizeof(char));
if(mem==NULL)
{
printf("%s Error in assigning memory %s",RED,NORMAL);
status = 0;
exit(0);
}
char str[1000];
char* ex_path = malloc(10000*sizeof(char));
while(fgets(str,1000,f))
{
i++;
if(i==n)
{
strcpy(status1,str);
}
if(i==m)
{
strcpy(mem,str);
}
}
fclose(f);
int path = readlink(exe,ex_path,2000);
strtok(mem,":");
mem = strtok(NULL,":");
if(path==-1)
{
strcpy(ex_path,"Executable path not found");
}
else
{
ex_path[path] = '\0';
ex_path = trim_dir(ex_path);
}
printf("Procces ID: %d\nProcess %sMemory: %sExecutable Path: %s\n",pid,status1,mem,ex_path);
return;
}