-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpastevents.c
More file actions
325 lines (263 loc) · 7.55 KB
/
pastevents.c
File metadata and controls
325 lines (263 loc) · 7.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "headers.h"
// extern char** process;
void AddPastEvent(char** strings, int count, char** laststrings, int lastcount, char* abs_path)
{
//open fiel for write, check:
//printf("abs: %s\n", abs_path);
// printf("Entered AddPastEvents:\n");
// for(int g = 0; g < count; g++)
// {
// printf("to add_%d: %s\n", g, strings[g]);
// }
FILE* readfile = fopen(abs_path, "r");
if(readfile == NULL)
{
perror(RED"fopen for pastevents"reset);
}
//array to store file content
char line[MAX_LINES][MAX_INPUT_LENGTH];
//read file
int i;
for(i = 0; fgets(line[i], MAX_INPUT_LENGTH, readfile); i++)
{
line[i][strlen(line[i]) - 1] = '\0';
}
int total = i;
// printf("\n File content: \n");
// for(i = 0; i < total; i++)
// {
// printf("%s\n", line[i]);
// }
//printf("\ni: %d\n", i);
fclose(readfile);
char** filecommand;
int* newcount = malloc(sizeof(int*));
*newcount = 0;
filecommand = tokenize(line[i - 1], newcount);
//printf("line[%d]: %s\nnewcount: %d\ncount: %d\n", i - 1, line[i - 1], *newcount, count);
// for(int q = 0; q < count; q++)
// {
// printf("strings%d: '%s'\n", q, strings[q]);
// }
FILE* appfile = fopen(abs_path, "a+");
if(i >= 0) //file not empty
{
int diff = 0;
//check if last command is same
if(i > 0 && *newcount == count)
{
//printf("ROMANIANNNN\n");
for(int p = 0; p < count; p++)
{
if(strcmp(strings[p], filecommand[p]) != 0)
{
diff = 1;
}
}
if(diff == 0) //same
{
// printf("returned\n");
return;
}
}
}
if(i == 15) //append the lasat fourteen to the empty file
{
fclose(appfile);
FILE* wrfile = fopen(abs_path, "w");
for(int p = 1; p < 15; p++)
{
// if(strcmp(strings[p], "pastevents") != 0)
// {
int ret = fprintf(wrfile, "%s\n", line[p]);
//printf("ret val:%d\n", ret);
if(ret < 0)
{
perror(RED"fprintf append"reset);
}
//}
}
int flag = 0;
for(int p = 0; p < count; p++)
{
if(strcmp(strings[p], "pastevents") == 0)
{
flag = 1;
}
}
for(int p = 0; p < count; p++)
{
if(flag == 0)
{
int ret = fprintf(wrfile, "%s ", strings[p]);
//printf("ret val:%d\n", ret);
if(ret < 0)
{
perror(RED"fprintf append"reset);
}
}
}
if(flag == 0)
{
fprintf(wrfile, "\n");
}
fclose(wrfile);
return;
}
//check if pastevents dont add
for(int f = 0; f < count; f++)
{
if(strcmp(strings[f], "pastevents") == 0)
{
////add edited string to pastfile
//ill add it earlier if its an execute so itll only come here as pastevents if its a purge or normal in which case just return nothing to add to file
//printf("pastevent alert!\n");
return;
}
}
//add the strings space separated
//printf("reached append\n");
int flag = 0;
for(int p = 0; p < count; p++)
{
if(strcmp(strings[p], "pastevents") == 0)
{
flag = 1;
}
}
for(int p = 0; p < count; p++)
{
if(flag == 0)
{
int ret = fprintf(appfile, "%s ", strings[p]);
//printf("ret val:%d\n", ret);
if(ret < 0)
{
perror(RED"fprintf append"reset);
}
}
//}
}
if(flag == 0)
{
fprintf(appfile, "\n");
}
fclose(appfile);
for(int g = count; g < MAX_INPUT_WORDS; g++)
{
if(filecommand[g] != NULL)
{
free(filecommand[g]);
}
}
free(filecommand);
}
void FindPastEvent(char** strings, int* count, Command cmd, int address, char* abs_path, char* invokeddir, char** pwd, int* timetaken, char* process)
{
//printf("Indside Find Past Event:\ncmd: %s\narg1: %s\narg2: %s\nindex: %d\nargcount: %d\n", cmd->command, cmd->args[0], cmd->args[1], cmd->index, cmd->argcount);
for(int q = 0; q < cmd->index + cmd->argcount + 1; q++)
{
//printf("strings%d: '%s'\n", q, strings[q]);
}
FILE* pfile = fopen(abs_path, "r");
if(pfile == NULL)
{
perror(RED"fopen for pastevents"reset);
}
//array to store file content
char line[MAX_LINES][MAX_INPUT_LENGTH];
//read file
int i;
for(i = 0; fgets(line[i], MAX_INPUT_LENGTH, pfile); i++)
{
line[i][strlen(line[i]) - 1] = '\0';
//printf("line%d: '%s'\n", i, line[i]);
}
int total = i;
fclose(pfile);
//now execute line[address]
char** filecommand;
// for(int h = 0; h < i; h++)
// {
// printf("line%d : %s\n", h, line[h]);
// }
// printf("address: %d\n", address);
// printf("line add:%s\n", line[i - address]);
int * subcount = malloc(sizeof(int*));
*subcount = 0;
filecommand = tokenize(line[i - address], subcount);
//printf("subcount: %d\n", *subcount);
//dup the first part of the command, dup the second part, dup the new command, strcat them
char** concatenated = malloc(sizeof(char*)*(*subcount + *count - cmd->argcount - 1));
for(int d = 0; d < cmd->index; d++)
{
concatenated[d] = strings[d];
// printf("%d: %s ", d, concatenated[d]);
}
for(int d = cmd->index; d < cmd->index + *subcount; d++)
{
free(strings[d]);
concatenated[d] = filecommand[d - cmd->index];
// printf("%d: %s ", d, concatenated[d]);
}
for(int d = cmd->index + cmd->argcount + 1; d < *count; d++)
{
// printf("ind: %d str: %s\n", d - cmd->argcount - 1 + *subcount, strings[d]);
concatenated[d - cmd->argcount - 1 + *subcount] = strings[d];
// printf("%s ", concatenated[d - cmd->argcount - 1 + *subcount]);
}
// printf("\n");
for(int k = 0; k < *count; k++)
{
strings[k] = NULL;
}
//printf("\nConcatenated:\n");
for(int k = 0; k < *subcount + *count - cmd->argcount - 1; k++)
{
strings[k] = concatenated[k];
//printf("%d: %s\n", k, concatenated[k]);
}
// for(int k = 0; k < *subcount + *count - cmd->argcount - 1; k++)
// {
// free(concatenated[k]);
// }
// free(concatenated);
*count = *count + *subcount - cmd->argcount - 1;
for(int g = *subcount; g < MAX_INPUT_WORDS; g++)
{
if(filecommand[g] != NULL)
{
free(filecommand[g]);
}
}
free(filecommand);
}
void PastPurge(char* path)
{
FILE* file = fopen(path, "w");
fclose(file);
}
void PastPrint(char* path)
{
//printf("Indside Print Past\n");
FILE* file = fopen(path, "r");
if(file == NULL)
{
perror(RED"fopen for pastevents"reset);
}
//array to store file content
char line[MAX_LINES][MAX_INPUT_LENGTH];
//read file
int i;
for(i = 0; fgets(line[i], MAX_INPUT_LENGTH, file); i++)
{
line[i][strlen(line[i]) - 1] = '\0';
}
int total = i;
for(int i = 0; i < total; i++)
{
printf("%s\n", line[i]);
}
//printf("\n");
fclose(file);
}