-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinfo.c
136 lines (126 loc) · 2.96 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "headers.h"
void pinfo(char *str, int t_out)
{
if (t_out == -1)
t_out = dup(STDOUT_FILENO);
char pid[100] = "self\0";
char *temp = strtok(str, " ");
temp = strtok(NULL, " ");
if (temp != NULL)
strcpy(pid, temp);
int z = 1, flag = 0;
char arr[N], val[3][N];
char name[N] = "/proc/\0";
strcat(name, pid);
strcat(name, "/status\0");
FILE *fptr = fopen(name, "r");
if (fptr == NULL)
{
char buf[N];
sprintf(buf, RED);
write(t_out, buf, strlen(buf));
sprintf(buf, "error: not a valid pid\n");
write(t_out, buf, strlen(buf));
sprintf(buf, WHITE);
write(t_out, buf, strlen(buf));
return;
}
while (1)
{
fgets(arr, N, fptr);
if (z == 3)
strcpy(val[0], arr); // status
if (z == 6)
strcpy(val[1], arr); // pid
if (z == 18)
strcpy(val[2], arr); // memory
z++;
if (z > 18)
break;
}
fclose(fptr);
// pid
int c = 0, idx = 0;
int k = strlen(val[2]);
for (int i = 0; i < k; i++)
{
if (val[1][i] == '\n')
break;
if (c)
{
if (val[1][i] == ' ' || val[1][i] == '\t')
continue;
pid[idx++] = val[1][i];
}
if (val[1][i] == ':')
c++;
}
pid[idx] = '\0';
printf("pid : %s\n", pid);
// process status
k = strlen(name);
for (int i = k - 1; i >= 0; i--)
{
if (name[i] == 't')
{
name[i + 1] = '\0';
break;
}
}
char s, toll[N];
char pgrp[N], tpgid[N];
fptr = fopen(name, "r");
fscanf(fptr, "%s %s %s %s %s %s %s %s", toll, toll, &s, toll, pgrp, toll, toll, tpgid);
if (strcmp(pgrp, tpgid) == 0)
flag++;
fclose(fptr);
c = idx = 0;
k = strlen(val[0]);
printf("process status : ");
for (int i = 0; i < k; i++)
{
if (val[0][i] == '\n')
break;
if (c)
{
if (val[0][i] == ' ' || val[0][i] == '\t')
continue;
else
{
printf("%c", val[0][i]);
break;
}
}
if (val[0][i] == ':')
c++;
}
if (flag)
printf("+");
printf("\n");
// virtual memory
c = idx = 0;
char mem[100];
k = strlen(val[2]);
for (int i = 0; i < k; i++)
{
if (val[2][i] == '\n')
break;
if (c)
{
if (val[2][i] == ' ' || val[2][i] == '\t')
continue;
mem[idx++] = val[2][i];
}
if (val[2][i] == ':')
c++;
}
mem[idx] = '\0';
printf("memory : %s\n", mem);
// executable path
char buf[1024], path[N] = "/proc/\0";
strcat(path, pid);
strcat(path, "/exe\0");
ssize_t len = readlink(path, buf, 1023);
buf[len] = '\0';
printf("executable Path : %s\n", buf);
}