-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomPinfo.c
More file actions
85 lines (80 loc) · 1.99 KB
/
customPinfo.c
File metadata and controls
85 lines (80 loc) · 1.99 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
#include "Header.h"
#include "customCd.h"
void customPinfo(char *in_text){
int start = 5;
int end = strlen(in_text);
for (int i = 5; i < strlen(in_text); i++){
if( (in_text[i]==' ') || (in_text[i]=='\t'))
start = i;
else
break;
}
for (int i = strlen(in_text)-1; i >= 0; i--){
if( (in_text[i]==' ') || (in_text[i]=='\t'))
end = i;
else
break;
}
// printf("start:%d;end:%d", start, end);
for( int i=start+1; i<end; i++ ){
in_text[i-start-1] = in_text[i];
}
in_text[end-start-1] = '\0';
int pid;
if( start>=end ){
pid = getpid();
}
else{
// count num args
for(int i=0;i<strlen(in_text);i++){
if( (in_text[i]==' ') || (in_text[i]=='\t') ){
printf("pinfo: Too many arguments.\n");
return;
}
}
pid = atoi(in_text);
}
// printf("%d\n",pid);
// printf("%s\n",in_text );
char process[1024];
sprintf(process, "/proc/%d/stat", pid);
FILE *fp = fopen(process, "r");
char info[1024];
if( fp==NULL ){
printf("pinfo: Process doesn't exist\n");
return;
}
else{
fscanf(fp, "%[^\n]s", info);
fclose(fp);
}
char stat_stuff[106][4096];
int arg_counter = 0, no_no = 0, info_counter = 0;
while (info[info_counter] != '\0'){
if (info[info_counter] != ' ' ){
while (info[info_counter] != ' ' && info[info_counter] != '\0'){
stat_stuff[arg_counter][no_no++] = info[info_counter++];
}
stat_stuff[arg_counter++][no_no] = '\0';
no_no = 0;
}
info_counter++;
}
printf("pid: %d\n", pid);
printf("Process Status: %s", stat_stuff[2]);
// check if pid==this executable's pid
if( (pid==getpid()) && ((*stat_stuff[2]=='R')||(*stat_stuff[2]=='S')) ){
printf("+");
}
printf("\nMemory: %s\n", stat_stuff[22]);
char exe_path[1024];
char exe_path_link[1024];
sprintf(exe_path,"/proc/%d/exe", pid);
int check_read=readlink(exe_path, exe_path_link, 1024);
if(check_read<0)
printf("Executable path does not exist\n");
else{
printf("Executable Path: %s\n", exe_path_link);
}
return;
}