-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinfo.c
110 lines (107 loc) · 2.34 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include<unistd.h>
#include<string.h>
#include <dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<time.h>
#include<grp.h>
#include <sys/wait.h>
#include<fcntl.h>
#include "cd.h"
#include "command.h"
#include "display.h"
#include "echo.h"
#include "execute.h"
#include "ls.h"
#include "nightswatch.h"
#include "pinfo.h"
#include "pwd.h"
#include "global.h"
#include "global.c"
// This function stores the pid into a string
void ConvertPID(char stringpid[],int pid)
{
int temp = pid;
int count = 0;
while(temp != 0)
{
count++;
temp = temp/10;
}
temp = pid;
stringpid[count] = 0;
while(temp != 0)
{
stringpid[count-1] = temp%10 + '0';
temp = temp/10;
count--;
}
return;
}
// THis function executed the function pinfo
void ExecutePINFO(char *token,const char delemiter[])
{
token = strtok(NULL,delemiter);
int pid;
char stringpid[1000],c[1000000];
if(token == NULL)
{
pid = getpid();
ConvertPID(stringpid,pid);
}
else
{
strcpy(stringpid,token);
}
int source,link;
char s[1000];
char p[1000];
strcpy(s,"/proc/");
strcpy(p,"/proc/");
char ExecutePath[1000];
strcat(s,stringpid);
strcat(s,"/stat");
strcat(p,stringpid);
strcat(p,"/exe");
source = open(s,O_RDONLY);
link = readlink(p,ExecutePath,1000);
if(source < 0 || link < 0)
{
fprintf(stderr,"Invalid process id \n");
close(source);
close(link);
return;
}
ExecutePath[link] = 0;
int byte = read(source,c,1000000);
int count = 1;
char Id[100],Status[100],VMemory[100],*content;
content = strtok(c,delemiter);
while(content != NULL)
{
if(count == 1)
{
strcpy(Id,content);
}
if(count == 3)
{
strcpy(Status,content);
}
if(count == 25)
{
strcpy(VMemory,content);
}
count++;
content = strtok(NULL,delemiter);
}
printf("pid -- %s\n",Id);
printf("Process Status -- %s\n",Status);
printf("Virtual Memory -- %s\n",VMemory);
printf("Executable Path -- %s\n",ExecutePath);
close(source);
close(link);
return;
}