-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinfo_data.c
More file actions
77 lines (66 loc) · 1.55 KB
/
pinfo_data.c
File metadata and controls
77 lines (66 loc) · 1.55 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "main.h"
void pid_data(char * pid)
{
ssize_t r,bufsiz;
struct stat sb;
char * stat = malloc(MAX_LENGTH*sizeof(char));
char * exe = malloc(MAX_LENGTH*sizeof(char));
char line[MAX_LENGTH];
char * linkname ;
strcpy(stat,"/proc/");
strcat(stat,pid);
strcat(stat,"/stat");
strcpy(exe,"/proc/");
strcat(exe,pid);
strcat(exe,"/exe");
FILE * fd_input1 = fopen(stat,"r");
if(fd_input1 == NULL)
{
fprintf(stderr, "Failed to open file \n");
return ;
}
int lno = 0;
printf("pid: %s\n",pid);
while (fscanf(fd_input1, " %1023s", line) == 1) {
lno++;
if(lno == 3)
printf("Status: %s\n",line);
else if(lno == 18)
{
printf("Virtual Memory: %s\n",line);
break;
}
}
if (lstat(exe, &sb) == -1) {
exit(EXIT_FAILURE);
}
else
{
bufsiz = sb.st_size + 1;
if (sb.st_size == 0)
bufsiz = MAX_LENGTH;
linkname = malloc(bufsize);
if (linkname == NULL) {
fprintf(stderr, "insufficient memory\n");
exit(EXIT_FAILURE);
}
r = readlink(exe, linkname,bufsiz);
if (r < 0) {
perror("lstat");
exit(EXIT_FAILURE);
}
printf("Executable: %s\n", linkname);
free(linkname);
}
free(stat);
free(exe);
fclose(fd_input1);
return ;
}