-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinfo.c
61 lines (52 loc) · 1.23 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
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pinfo.h"
#include "util.h"
int pinfo(char **argv, int argc) {
char *procpid = (char*)malloc(256*sizeof(char));
if (argc == 1) {
sprintf(procpid, "%d", getpid());
}
else {
procpid = argv[1];
}
char *path = (char*)malloc(512*sizeof(char));
strcpy(path, "/proc/");
strcat(path, procpid);
strcat(path, "/stat");
strcat(path, "\0");
int temp;
ssize_t read;
size_t len = 0;
char *line = NULL;
char **linep = NULL;
FILE * file;
file = fopen(path , "r");
if(file) {
while ((read = getline(&line, &len, file)) != -1) {
linep = parseline(line, &temp);
printf("pid -- %s\n", linep[0]);
printf("Process Status -- %s\n", linep[2]);
printf("{Virtual Memory} -- %s\n", linep[22]);
}
fclose(file);
}
else {
perror("The process you requested does not exist");
return 1;
}
strcpy(path, "/proc/");
strcat(path, procpid);
strcat(path, "/exe");
strcat(path, "\0");
if(readlink(path, line, 1024) == -1) {
perror("The process you requested does not exist");
return 1;
}
strcat(line, "\0");
printf("Executable path - %s\n",line);
return 1;
}