-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.c
53 lines (44 loc) · 944 Bytes
/
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
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <pwd.h>
#include "headerfile.h"
void run_pinfo(char *argv[ARG_MAX] , int argc)
{
char *my_pid = "self";
if(argc == 2)
my_pid = argv[1];
char path1[ARG_MAX];
sprintf(path1,"%s%s%s","/proc/",my_pid,"/status");
FILE *file = fopen (path1,"r");
if (file != NULL)
{
int i = 0;
char line [128];
while (fgets ( line, sizeof(line), file ) != NULL )
{
i++;
if(i == 3 || i == 6 || i == 18)
fputs (line,stdout);
}
fclose(file);
}
char path2[ARG_MAX];
sprintf(path2,"%s%s%s","/proc/",my_pid,"/exe");
char execpath[ARG_MAX];
int len = readlink(path2,execpath,ARG_MAX-1);
if(len < 0){
perror("bash");
return;
}
execpath[len] = '\0';
printf("%s\n",execpath);
}