forked from AngelDevil1223/PROC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
158 lines (146 loc) · 3.46 KB
/
parser.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "parser.h"
//Global Variables
static const int BUFFER = 255;
static const int numFields = 15;
static const int stateField = 3;
static const int userField = 14;
static const int sysField = 15;
char *procDIR = "/proc/";
char *statDIR = "/stat";
char *statmDIR = "/statm";
char *cmdlineDIR = "/cmdline";
//Allocates memory for the process info and prints the output
int generatePrint(struct options *flags, int processID)
{
struct info *pInfo = (struct info *) malloc(sizeof(struct info));
initInfo(pInfo, processID);
printf("%s: ", pInfo->pid);
if (flags->s == ON && pInfo->state != '0')
printf("%c ", pInfo->state);
if (flags->U == OFF)
printf("utime=%s ", pInfo->userTime);
if (flags->S == ON && pInfo->systemTime != NULL)
printf("stime=%s ", pInfo->systemTime);
if (flags->v == ON && pInfo->vm != NULL)
printf("size=%s ", pInfo->vm);
if (flags->c == OFF && pInfo->cmdline != NULL)
printf("[%s]", pInfo->cmdline);
printf("\n");
free(pInfo);
return 0;
}
//Stores the process ID and calls the next three functions
void initInfo(struct info *pInfo, int processID)
{
char pidStr[BUFFER];
sprintf(pidStr, "%d", processID);
pInfo->pid = pidStr;
pInfo->state = '0';
pInfo->userTime = NULL;
pInfo->systemTime = NULL;
pInfo->vm = NULL;
pInfo->cmdline = NULL;
parseStat(pInfo);
parseStatm(pInfo);
parseCmdline(pInfo);
}
//Parses /proc/<pid>/stat and stores the current state,
//user time, and system time
void parseStat(struct info *pInfo)
{
char buff[BUFFER];
char fileLocation[BUFFER];
char *p;
strcpy(fileLocation, procDIR);
strcat(fileLocation, pInfo->pid);
strcat(fileLocation, statDIR);
FILE * fp;
fp = fopen(fileLocation, "r");
int i = 0;
if (fp != NULL)
{
while (i < numFields)
{
i++; //at i of each field it stores corresponding value.
fscanf(fp, "%s", buff);
if (i == stateField)
pInfo->state = buff[0];
if (i == userField)
{
p = (char*) malloc(sizeof(buff));
strcpy(p, buff);
pInfo->userTime = p;
p = NULL;
}
if (i == sysField)
{
p = (char*) malloc(sizeof(buff));
strcpy(p, buff);
pInfo->systemTime = p;
p = NULL;
}
}
fclose(fp);
}
}
//Parses /proc/<pid>/statm and stores the amount of
//virtual memory used
void parseStatm(struct info *pInfo)
{
char buff[BUFFER];
char fileLocation[BUFFER];
char *p;
strcpy(fileLocation, procDIR);
strcat(fileLocation, pInfo->pid);
strcat(fileLocation, statmDIR);
FILE * fp;
fp = fopen(fileLocation, "r");
if (fp != NULL)
{
p = (char*) malloc(sizeof(buff));
fscanf(fp, "%s", buff);
strcpy(p, buff); //vm is first value in file
pInfo->vm = p;
p = NULL;
fclose(fp);
}
}
//Parses the command line and stores the entire
//command for a given pid
void parseCmdline(struct info *pInfo)
{
char buff[BUFFER];
char fileLocation[BUFFER];
char *p;
strcpy(fileLocation, procDIR);
strcat(fileLocation, pInfo->pid);
strcat(fileLocation, cmdlineDIR);
FILE * fp;
fp = fopen(fileLocation, "r");
if (fp != NULL)
{
p = (char*) malloc(sizeof(buff));
fgets(buff, sizeof buff, fp);
strcpy(p, buff); //if it's larger than buff, then that section is truncated.
pInfo->cmdline = p;
p = NULL;
fclose(fp);
}
FILE *cmdline = fopen(fileLocation, "rb");
char *arg = 0;
size_t size = 0;
p = (char*) malloc(sizeof(buff));
while (getdelim(&arg, &size, 0, cmdline) != -1)
{
//puts(arg);
strcat(p, arg);
strcat(p, " ");
}
pInfo->cmdline = p;
p = NULL;
free(arg);
fclose(cmdline);
}