-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom_pinfo.h
More file actions
130 lines (130 loc) · 3.2 KB
/
com_pinfo.h
File metadata and controls
130 lines (130 loc) · 3.2 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef COM_PINFO_H
#define COM_PINFO_H
#include "global.h"
int doesexist()
{
char processFile[1500] = "/proc/";
processFile[6] = '\0';
strcat(processFile, commands[1]);
processFile[7 + strlen(commands[1])] = '\0';
struct stat stats;
if (stat(processFile, &stats) < 0 && errno == ENOENT)
{
return 0;
}
return 1;
}
int check_for_plus(char *line)
{
char fourword[1000] = {'\0'};
int count = 0, j = 0;
for (int i = 0; i < strlen(line); i++)
{
if (line[i] == ' ')
count++;
if (count == 4)
break;
if (count == 4 - 1)
fourword[j++] = line[i];
}
char sevenword[1000] = {'\0'};
count = 0;
j = 0;
for (int i = 0; i < strlen(line); i++)
{
if (line[i] == ' ')
count++;
if (count == 7)
break;
if (count == 7 - 1)
sevenword[j++] = line[i];
}
// printf("+%s+\n +%s+\n", fourword, sevenword);
if (strcmp(sevenword, fourword) == 0)
{
return 1;
}
return 0;
}
void nwordfun(char *line, int n)
{
char nword[1000] = {'\0'};
int count = 0, j = 0;
for (int i = 0; i < strlen(line); i++)
{
if (line[i] == ' ')
count++;
if (count == n)
break;
if (count == n - 1)
nword[j++] = line[i];
}
if (n == 3)
{
if (check_for_plus(line) == 1)
{
printf("%s+\n", nword);
return;
}
}
printf("%s\n", nword);
return;
}
int call_pinfo()
{
char processFile[1500] = "/proc/";
processFile[6] = '\0';
if (numargs == 2)
{
//( check if process exists
if (doesexist() == 0)
{
printf("Process with pid %s does not exist\n", commands[1]);
return 1;
}
printf("pid : %s\n", commands[1]);
strcat(processFile, commands[1]);
processFile[7 + strlen(commands[1])] = '\0';
}
else if (numargs > 2)
{
perror("pinfo: Too many arguments");
return 0;
}
else
{
printf("pid : %d\n", Shell_Id);
char *tempshellid = my_itoa(Shell_Id);
strcat(processFile, tempshellid);
processFile[7 + strlen(tempshellid)] = '\0';
}
strcat(processFile, "/stat");
FILE *reader = fopen(processFile, "r");
char line[10000];
fgets(line, 10000, reader);
printf("process Status : ");
nwordfun(line, 3);
printf("memory : ");
nwordfun(line, 23);
processFile[strlen(processFile) - 1] = '\0';
processFile[strlen(processFile) - 1] = processFile[strlen(processFile) - 3] = 'e';
processFile[strlen(processFile) - 2] = 'x';
char newline[10000] = {'\0'};
readlink(processFile, newline, 10000);
if (strncmp(newline, shelldir, strlen(shelldir)) == 0)
{
newline[0] = '~';
int len_line = strlen(newline);
for (int i = strlen(shelldir); i < len_line; i++)
{
newline[i - strlen(shelldir) + 1] = newline[i];
}
for (int i = len_line - strlen(shelldir) + 1; i < len_line; i++)
{
newline[i] = '\0';
}
}
printf("executable Path : %s\n", newline);
return 1;
}
#endif