-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.c
106 lines (91 loc) · 3.15 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
#include "headers.h"
//PINFO IMPLEMENTED
void p_info(char * arg,int flag_output){
int arg_num = argNum(arg);
if(arg_num == 0){
pid_t pid = getpid();
if(pid != 0){
char path[400];
sprintf(path, "/proc/%d/stat", pid);
FILE *fp = fopen(path, "r");
if(fp == NULL){
printf("No such process exists\n");
return;
}
char *line = NULL;
size_t len = 0;
ssize_t read;
char *token;
char *saveptr;
int i = 0;
char* process_status=(char*) calloc(200, sizeof(char));
char* memory=(char*) calloc(200, sizeof(char));
while ((read = getline(&line, &len, fp)) != -1) {
token = strtok_r(line, " ", &saveptr);
while (token != NULL) {
if(i == 2){
strcpy(process_status, token);
}
if(i == 22){
strcpy(memory, token);
}
token = strtok_r(NULL, " ", &saveptr);
i++;
}
}
sprintf(path, "/proc/%d/exe", pid);
char *exe_path = (char *)calloc(200, sizeof(char));
int path_len = readlink(path, exe_path, 200);
exe_path[path_len] = '\0';
printf("pid : %d\n", pid);
printf("Process Status : %s\n", process_status);
printf("memory : %s\n", memory);
printf("Executable Path : %s\n", exe_path);
free(process_status);
free(memory);
free(exe_path);
}
}
else if(arg_num == 1){
//get process status
char path[400];
sprintf(path, "/proc/%s/stat", arg);
FILE *fp = fopen(path, "r");
if(fp == NULL){
printf("No such process exists\n");
return;
}
char *line = NULL;
size_t len = 0;
ssize_t read;
char *token;
char *saveptr;
int i = 0;
char* process_status=(char*) calloc(200, sizeof(char));
char* memory=(char*) calloc(200, sizeof(char));
while ((read = getline(&line, &len, fp)) != -1) {
token = strtok_r(line, " ", &saveptr);
while (token != NULL) {
if(i == 2){
strcpy(process_status, token);
}
if(i == 22){
strcpy(memory, token);
}
token = strtok_r(NULL, " ", &saveptr);
i++;
}
}
sprintf(path, "/proc/%s/exe", arg);
char *exe_path = (char *)calloc(200, sizeof(char));
int path_len = readlink(path, exe_path, 200);
exe_path[path_len] = '\0';
printf("pid : %s\n", arg);
printf("Process Status : %s\n", process_status);
printf("memory : %s\n", memory);
printf("Executable Path : %s\n", exe_path);
free(process_status);
free(memory);
free(exe_path);
}
}