-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinfo.c
77 lines (75 loc) · 1.6 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 "header.h"
int strToNum(char s[])
{
int ret = 0;
for (int i = 0; s[i] != '\0'; i++)
{
ret *= 10;
ret += s[i] - '0';
}
return ret;
}
int pinfo(char *par[], int numPar, char home_dir[])
{
int ID;
if (numPar == 0)
{
numPar = 1;
ID = getpid();
}
else
{
if (numPar > 1)
{
perror("pinfo: Too many arguments!");
return - 1;
}
ID = strToNum(par[0]);
}
char procFile[10000];
sprintf(procFile, "/proc/%d/stat", ID);
FILE *fd = fopen(procFile, "r");
if (fd == NULL)
{
perror("pinfo: /proc/../stat");
return -1;
}
printf("pid -- %d\n", ID);
char infoStr[10000];
for (int i = 0; fscanf(fd, "%s", infoStr); i++)
{
if (i == 23)break;
if (i == 2)
printf("Process Status -- %s\n", infoStr);
if (i == 21)
printf("memory -- %s\n", infoStr);
}
sprintf(procFile, "/proc/%d/exe", ID);
int n = readlink(procFile, infoStr, 10000);
if (n <= -1)
{
perror("error reading executable path");
return -1;
}
infoStr[n] = '\0';
int hlen = strlen(home_dir);
bool rel = true;
if (n < hlen)
rel = false;
if (rel)
for (int i = 0; i < hlen; i++)
{
if (home_dir[i] != infoStr[i])
rel = false;
}
char fin[100000];
if (!rel){
strcpy(fin, infoStr);
}
else {
strcpy(fin, "~");
strcat(fin, infoStr + hlen);
}
printf("Executable path -- %s\n", fin);
return 1;
}