-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinfo.c
More file actions
59 lines (57 loc) · 1.33 KB
/
pinfo.c
File metadata and controls
59 lines (57 loc) · 1.33 KB
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
void pinfo(int shellid,char* home_path)
{
char *procpath=(char *)malloc(30*sizeof(char));
char *temp_path=(char *)malloc(100*sizeof(char));
sprintf(procpath,"/proc/%d/stat",shellid);
FILE *file;
file=fopen(procpath,"r");
if(!file)
{
perror("Error:");
return;
}
else
{
char *cont=NULL;
char tokens[50][30];
// char *token=(char *)malloc(30*sizeof(char));
size_t len=0;
int args;
if(getline(&cont,&len,file)==-1)
{
perror("Error");
return;
}//==-1 for error
fclose(file);
char *token=strtok(cont," ");
for(args=0;token!=NULL;args++)
{
strcpy(tokens[args],token);
token=strtok(NULL," ");
}
printf("pid -- %s\nProcess Status -- %s\nmemory -- %s\n",tokens[0],tokens[2],tokens[22]);
char *exepath=(char *)malloc(30*sizeof(char));
sprintf(exepath,"/proc/%d/exe",shellid);
if(readlink(exepath,cont,200)==-1)
{
perror("Error");
return;
}
cont[199]='\0';
int homelen=strlen(home_path);
strcpy(temp_path,cont);
temp_path[homelen]='\0';
if(homelen>strlen(cont)||strcmp(temp_path,home_path))
{
printf("Executable path -- %s\n",cont);
}
else
{
printf("Executable path -- ~%s\n",&cont[homelen]);
}
}
}