-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_pinfo.c
More file actions
63 lines (45 loc) · 1.42 KB
/
command_pinfo.c
File metadata and controls
63 lines (45 loc) · 1.42 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
#include "shell.h"
#include "utils.h"
#include "command_pinfo.h"
#include <stdio.h>
void pinfo(char* str){
int pid;
char file_stat[MAX_STAT_LEN + 1] = {'\0'};
char file_path[PATH_MAX];
str = trim_str(str);
if(strlen(str) < 1){
pid = getpid();
}
else {
pid = atoi(str);
}
if(!pid){
perror("Enter a correct process ID");
return;
}
char* stat_path = (char*)malloc(sizeof(char) * 25);
sprintf(stat_path, "/proc/%d/status", pid);
FILE* proc_file = fopen(stat_path, "r");
if(!proc_file){
perror("There was an error while getting proccess info");
return;
}
fread(file_stat, MAX_STAT_LEN, 1, proc_file);
fclose(proc_file);
char* attrs[STAT_COUNT] = {NULL};
attrs[0] = strtok(file_stat, " ");
for(int i = 1; attrs[i-1] != NULL; i++)
attrs[i] = strtok(NULL, " ");
printf("pid -- %d\n", (int)pid);
printf("Process Status -- %s\n", attrs[2]);
printf("memory -- %s\n", attrs[22]);
sprintf(file_path, "/proc/%d/exe", pid);
char execPath[PATH_MAX + 1];
memset(execPath, 0, PATH_MAX);
int readStat = readlink(file_path, execPath, PATH_MAX);
// if (readStat != -1)
// shortenPath(INVOC_LOC, execPath);
// printf("Executable Path -- %s\n", readStat == -1 ? "Doesn't exist" : execPath);
// return 0;
printf("Executable Path -- %s\n", get_relative_path(execPath));
}