-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_pinfo.c
61 lines (47 loc) · 1.4 KB
/
shell_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
#include "shell_pinfo.h"
#include "pathManip.h"
int shell_pinfo(char** args, char* root) {
char* pid = (char*)malloc(100);
if (args[1] == NULL) {
sprintf(pid, "%d", getpid());
}
else {
pid = args[1];
}
char* proc = (char*)malloc(1000);
sprintf(proc, "/proc/%s/stat", pid);
FILE* stat_file = fopen(proc, "r");
if (stat_file == NULL) {
fprintf(stderr, "Error reading %s", proc);
}
int p_id;
char status, *name = (char*)malloc(1000);
fscanf(stat_file, "%d", &p_id);
fscanf(stat_file, "%s", name);
fscanf(stat_file, " %c", &status);
fprintf(stdout, "pid: %d\n", p_id);
fprintf(stdout, "Process Status: %c\n", status);
fclose(stat_file);
sprintf(proc, "/proc/%s/statm", pid);
FILE* mem_file = fopen(proc, "r");
if (mem_file == NULL) {
fprintf(stderr, "Error reading %s", proc);
}
int size;
fscanf(mem_file, "%d", &size);
printf("Memory: %d\n", size);
fclose(mem_file);
sprintf(proc, "/proc/%s/exe", pid);
char* path = (char*)malloc(100);
int poss = readlink(proc, path, 100);
if (poss == -1) {
fprintf(stderr, "Executable path -- Not defined in proc\n\n");
}
else {
char* relpath = (char*)malloc(100);
relpath = getPath(path, root);
relpath[poss] = '\0';
fprintf(stdout, "Path : %s\n", relpath);
}
free(path);
}