-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.c
98 lines (77 loc) · 1.71 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "parse.h"
#include "commands.h"
#include "ls.h"
#include "background.h"
#include "main.h"
#include "pinfo.h"
void pinfo_command(char * input)
{
pid_t pidinfo;
char buf[1000],ch;
char stats[1000];
char exec[1000];
char * line_buffer = malloc(1000 * sizeof(char));
long unsigned int virtual_mem = 0;
char *temp = malloc (1 + strlen(input));
strcpy(temp,input);
char * token = strtok(NULL, " \t\r\n");
if(token == NULL)
{
pidinfo = getpid();
printf("Pid-- %d\n", pidinfo);
}
int pidstring;
if(token != NULL)
{
pidstring = atoi(token);
pidinfo = pidstring;
printf("Pid-- %d\n",pidstring);
}
sprintf(stats, "/proc/%d/stat", pidinfo);
sprintf(exec, "/proc/%d/exe", pidinfo);
FILE* fp = fopen(stats, "r");
if(!fp)
{
printf("Error: Process doesn't exist\n");
//fclose(fp);
return;
}
if(fp)
{
fscanf(fp, "%s", buf);
fscanf(fp, "%s", buf);
fscanf(fp," %c", &ch);
printf("Process status -- %c\n", ch);
fscanf(fp, "%lu", &virtual_mem);
printf("Virtual memory -- %lu kB\n", virtual_mem);
fclose(fp);
}
long long int len = readlink(exec, line_buffer, 1000);
if (len < 0)
printf("Error: Permission Denied\n");
if(len >= 0)
{
line_buffer[len] = '\0';
int i;
int flag = 0;
for(i=0;i<homepathlength;i++)
if(line_buffer[i]!= homepath[i])
{
flag = 1;
break;
}
if(i>= homepathlength)
printf("Executable Path -- ~%s\n", &line_buffer[i]);
else
printf("Executable Path -- ~%s\n", line_buffer);
}
return;
}