-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinfo.c
77 lines (66 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
#include "headers.h"
void pinfo(int argc, char **argv)
{
int pid;
if (argc > 2)
{
printf("pinfo: too many arguments\n");
return;
}
// (argc) ? pid=getpid():pid=atoi(argv[1]);
if (argc == 1)
pid = getpid();
else
pid = atoi(argv[1]);
if (pid == 0)
{
printf("Invalid PID\n");
return;
}
FILE *fptr;
char path[MAX], row[MAX];
int row_num = 0;
char pgrpid[50], tgid[50];
char status[3] = ""; //'+' for a foreground process
char memory[50] = "";
sprintf(path, "/proc/%d/stat", pid);
if ((fptr = fopen(path, "r")) != NULL)
{
if (fgets(row, sizeof(row), fptr) == NULL)
{
printf("bash: can't access %s such file or directory\n", path);
return;
}
}
char* token=strtok(row," ");
while(token!=NULL && row_num<23)
{
row_num++;
if(row_num==3)
strcpy(status,token);
else if(row_num==5)
strcpy(pgrpid,token);
else if(row_num==8)
strcpy(tgid,token);
else if(row_num==23)
strcpy(memory,token);
token=strtok(NULL," ");
}
if(strcmp(pgrpid,tgid)==0)
strcat(status,"+");
char linker[MAX];
char ex_path[MAX];
sprintf(linker, "/proc/%d/exe", pid);
int len = readlink(linker, ex_path,MAX);
printf("pid -- %d\n", pid);
printf("Process Status -- {%s}\n", status);
printf("memory -- %s {Virtual Memory}\n", memory);
if (len == -1)
{
printf("Error in opening %s\n", linker);
return;
}
ex_path[len]='\0';
printf("Executable Path -- %s\n", ex_path);
return;
}