-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.c
88 lines (85 loc) · 1.86 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
81
82
83
84
85
86
87
88
#include "headers.h"
void pinfo(char *command)
{
char buf1[MAX_SIZE];
int pid;
strcpy(buf1,command);
char *temp = strtok(buf1," ");
char *piduser = strtok(NULL," ");
if(piduser)
pid = atoi(piduser);
else
pid = getpid();
char buf[500];
char *p;
char state;
long long vmemory;
sprintf(buf, "/proc/%d/stat", pid);
FILE *procf = fopen(buf, "r");
if(procf == NULL)
{
perror("Could not find process");
return;
}
char info[MAX_SIZE];
int size = 200;
int i = 0;
int tpgid;
while (fscanf(procf, "%s", info))
{
i++;
if (i == 52)
break;
switch (i)
{
case 1:
pid = atoi(info);
break;
case 3:
state = info[0];
break;
case 8:
tpgid = atoi(info);
break;
case 23:
vmemory = atoll(info);
break;
default:
continue;
}
}
printf("pid -- %d\n", pid);
if (tpgid == pid)
printf("Process Status -- %c+\n", state);
else
printf("Process Status -- %c\n", state);
printf("memory -- %llu {Virtual Memory}\n", vmemory);
sprintf(buf, "/proc/%d/exe", pid);
int execlen;
if((execlen = readlink(buf, info, size))<0)
{
printf("Executable Path -- Could not get executable path\n");
return;
}
info[execlen] = '\0';
int n1 = strlen(home);
int n2 = strlen(info);
char path[MAX_SIZE];
path[0] = '~';
i = 1;
int var = 0;
if (strncmp(home, info, n1) == 0)
{
var = 1;
while (info[n1 + i - 1] != '\0')
path[i++] = info[n1 + i - 1];
path[i] = '\0';
}
else
{
strcpy(path,info);
}
printf("Executable Path -- %s\n", path);
fclose(procf);
return;
}