-
Notifications
You must be signed in to change notification settings - Fork 0
/
pastevents.c
121 lines (109 loc) · 3.23 KB
/
pastevents.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
#include "pastevents.h"
void get_commands2(char *line)
{
char *command;
char copy1[1024], copy2[1024];
strcpy(copy1, line);
strcpy(copy2, line);
command = strtok(line, "&;");
int numberofstore = 0;
while (command != NULL)
{
command = trimspace(command);
numberofstore++, command = strtok(NULL, "&;");
}
char *store[numberofstore + 1];
int i = 0;
if (numberofstore <= 0)
return;
char *beg = copy1;
store[0] = strtok(copy1, "&;");
while (store[i] != NULL && strcmp(store[i], "") != 0)
{
i++;
store[i] = strtok(NULL, "&;");
}
// int past = 0;
int backup_stdout = dup(STDOUT_FILENO);
int backup_stdin = dup(STDIN_FILENO);
for (int j = 0; j < numberofstore; j++)
{
int processid = 0;
if (copy2[strlen(store[j]) + (store[j] - beg)] == '&')
processid = 1;
if (strcmp(store[j], "\n") == 0)
{
printf("%s", (store[j]));
continue;
}
// past += processInput(store[j], processid);
pipechecker(store[j], processid);
check_input(backup_stdin, backup_stdout);
}
}
int pasts(char *tokens[], int tk_freq, int process_identity)
{
if (process_identity)
{
printf("[%d]\n", getpid());
}
FILE *file_descriptor;
char *file_name = (char *)malloc(sizeof(char) * MAX);
strcpy(file_name, original);
char *concat = "/history.txt";
strcat(file_name, concat);
file_descriptor = fopen(file_name, "r+");
// Now checking various conditions according to the specified specifications!
if (file_descriptor != NULL)
{
if (tk_freq == 1)
{
char end_line[MAX] = "";
char dummy[MAX];
while (fgets(dummy, sizeof(dummy), file_descriptor) != NULL)
{
printf("%s", end_line);
strcpy(end_line, dummy);
}
}
else if (tk_freq == 3)
{
if (strcmp(tokens[1], "execute") == 0)
{
char *end_line[16];
char dummy[MAX];
for (int i = 0; i < 16; i++)
{
end_line[i] = (char *)malloc(sizeof(char) * MAX);
}
int ct = 0;
while (fgets(dummy, sizeof(dummy), file_descriptor) != NULL)
{
strcpy(end_line[ct++], dummy);
// printf("%s", end_line[ct - 1]);
}
// Now converting the string tokens[2] to integer and then executing the command.
printf("%s", end_line[ct - (atoi(tokens[2]))]);
get_commands2(end_line[ct - (atoi(tokens[2]))]);
}
}
else if (tk_freq == 2)
{
if (strcmp(tokens[1], "purge") == 0)
{
if (ftruncate(fileno(file_descriptor), 0) != 0)
{
perror("Error in file truncation");
fclose(file_descriptor);
return 1;
}
}
}
fclose(file_descriptor);
}
else
{
printf("Error in opening the file, check the file_descriptor!");
}
return 1;
}