-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproclore.c
More file actions
70 lines (63 loc) · 1.72 KB
/
proclore.c
File metadata and controls
70 lines (63 loc) · 1.72 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
#include "headers.h"
void proclore(char **command, int bg, int cmd_count, int *pipe_fd, int *pipe_fd2, int active_pipe)
{
int pid = 0;
if (cmd_count == 1)
{
pid = getpid();
}
else
{
pid = atoi(command[1]);
}
char virtual_path[256];
snprintf(virtual_path, sizeof(virtual_path), "/proc/%d/statm", pid);
FILE *vir_file = fopen(virtual_path, "r");
unsigned long virtual_size;
if (vir_file)
{
fscanf(vir_file, "%lu", &virtual_size);
fclose(vir_file);
}
char status_path[256];
char status[8];
snprintf(status_path, sizeof(status_path), "/proc/%d/status", pid);
FILE *status_file = fopen(status_path, "r");
if (status_file)
{
char line[280];
while (fgets(line, sizeof(line), status_file))
{
if (strncmp(line, "State:", 6) == 0)
{
sscanf(line + 7, "%s", status);
break;
}
}
fclose(status_file);
}
char proc_path[256];
snprintf(proc_path, sizeof(proc_path), "/proc/%d/exe", pid);
readlink(proc_path, proc_path, sizeof(proc_path) - 1);
char *home_dir = gethome_len();
if (strlen(proc_path) >= strlen(home_dir))
{
snprintf(proc_path, sizeof(proc_path), "~%s", proc_path + strlen(home_dir));
}
pid_t group_pid = tcgetpgrp(STDIN_FILENO);
;
char plus = '\0';
if (pid == group_pid)
{
plus = '+';
}
else
{
plus = '\0';
}
printf("pid : %d\n", pid);
printf("Process status : %s%c\n", status, plus);
printf("Process Group : %d\n", getpgid(pid));
printf("Virtual memory : %ld\n", virtual_size);
printf("executable path : %s\n", proc_path);
}