-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.c
79 lines (61 loc) · 1.63 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
#include "shell.h"
void pinfo(char* argv1[], int argc1, char* home)
{
if(argc1 > 2)
{
printf("pinfo: Usage: pinfo [pid]\n");
return;
}
int pid;
char* pid_string = (char*) malloc(16);
if(argc1 == 1)
{
pid = getpid();
sprintf(pid_string, "%d", pid);
}
else
pid_string = argv1[1];
struct stat fileinfo;
char* path = (char*) malloc(4096);
strcpy(path, "/proc/");
strcat(path, pid_string);
strcat(path, "/stat");
int file_exist = stat(path, &fileinfo);
if(file_exist < 0)
{
printf("pinfo: Invalid pid\n");
return;
}
char* file_buff = (char*) malloc(16384);
int fd = open(path,O_RDONLY);
int rd = read(fd, file_buff, 16384);
char* split = strtok(file_buff, " \n\t");
int i = 0;
while(split)
{
if(i==0)
printf("\tpid\t:%s\n", split);
else if(i==2)
printf("\tProcess Status\t:%s\n", split);
else if(i==22)
printf("\tVirtual Memory\t:%s\n", split);
i++;
split = strtok(NULL, " \n\t");
}
//Executable path
char* exec_path = (char*) malloc(4096);
char* readL_buf = (char*) malloc(4096);
strcpy(exec_path, "/proc/");
strcat(exec_path, pid_string);
strcat(exec_path, "/exe");
int exec_check = readlink(exec_path, readL_buf, 4095);
readL_buf[4095] = '\0';
if(exec_check<0)
{
printf("pinfo: Invalid pid or exe file access permission denied\n");
return;
}
readL_buf = ishome(home, readL_buf);
printf("\tExecutable Path\t: %s\n", readL_buf);
return;
}