-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinfo.c
77 lines (76 loc) · 1.75 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 pinfo_func(char* token,char homedir[]){
char arr[200] = {0}, temp[200] = {0};
strcpy(arr,"/proc/");
token = strtok(NULL, " \n\t");
if(token!=NULL)
strcat(arr,token);
else
strcat(arr,"self");
strcpy(temp,arr);
strcat(arr,"/stat");
FILE * file = fopen(arr,"r");
if(file == NULL){
printf("Process not found\n");
return 1;
}
int pid;
char proc_status;
char name[100]={0};
int err = fscanf(file,"%d",&pid);
if(err<0)
perror("fscanf");
err = fscanf(file, "%s", name);
if(err<0)
perror("fscanf");
err = fscanf(file, " %c", &proc_status);
if(err<0)
perror("fscanf");
printf( "pid: %d\n", pid);
printf( "Process Status: %c\n", proc_status);
fclose(file);
strcpy(arr,temp);
strcat(arr,"/statm");
FILE * mem = fopen(arr, "r");
if(mem == NULL){
printf("Process not found\n");
return 1;
}
int memSize;
err = fscanf(mem, "%d", &memSize);
if(err<0)
perror("fscanf");
err = fprintf(stdout, "Memory: %d\n", memSize);
if(err<0)
perror("fscanf");
fclose(mem);
char exp[1000]={0};
strcpy(arr, temp);
strcat(arr, "/exe");
int len = readlink(arr, exp, sizeof(exp));
if(len<0){
perror("readlink");
return 1;
}
int sameChars = 0, baseL = strlen(homedir);
for(sameChars = 0; sameChars < baseL; sameChars++)
if(homedir[sameChars] != exp[sameChars])
break;
char relPath[1000]={0};
if(sameChars == baseL) {
relPath[0] = '~';
relPath[1] = '\0';
strcat(relPath, &exp[baseL]);
}
else{
strcpy(relPath, exp);
relPath[strlen(exp)] = '\0';
}
int i = 0;
while(exp[i]){
exp[i] = '\0';
i++;
}
printf( "Executable Path: %s\n", relPath);
return 0;
}