-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpastevents.c
More file actions
118 lines (109 loc) · 2.75 KB
/
pastevents.c
File metadata and controls
118 lines (109 loc) · 2.75 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
#include "headers.h"
char histo_link[255];
int get_a_line(char **result)
{
strcpy(histo_link, gethome_len());
strcat(histo_link, "/");
strcat(histo_link, "history.bin");
int fd = open(histo_link, O_RDONLY | O_CREAT, 0644);
char lines[1000] = {'\0'};
read(fd, lines, sizeof(lines));
result[0] = strtok(lines, "\n");
int i = 0;
for (i = 0; i < 15 && result[i]; i++)
{
result[i + 1] = strtok(NULL, "\n");
}
close(fd);
return i;
}
void getfromfile(char *result)
{
char *resulter[200] = {'\0'};
get_a_line(resulter);
char lines[1000] = {'\0'};
int i = 0;
while (resulter[i] && i < 15)
{
strcat(lines, resulter[i]);
strcat(lines, "\n");
i++;
}
strcpy(result, lines);
}
void get_first_line(char *first_line)
{
char line[1000] = {'\0'};
getfromfile(line);
int i = 0;
while (line[i] && line[i] != '\n')
{
first_line[i] = line[i];
i++;
}
first_line[i] = '\n';
first_line[i + 1] = '\0';
};
void save_to_pastevents(char *input)
{
char result[2000] = {'\0'};
strcpy(histo_link, gethome_len());
strcat(histo_link, "/");
strcat(histo_link, "history.bin");
getfromfile(result);
char first_line[200];
get_first_line(first_line);
int fd = open(histo_link, O_RDWR | O_CREAT | O_TRUNC, 0644);
char temp[2000] = {'\0'};
if (input[strlen(input) - 1] != '\n')
{
strcat(input, "\n");
}
strcpy(temp, input);
if (strcmp(temp, first_line) && strcmp(temp, "\n") && strcmp(temp, " \n") && (!strstr(temp, "pastevents")))
{
strcat(temp, result);
write(fd, temp, strlen(temp));
}
else
{
write(fd, result, strlen(result) - 1);
}
close(fd);
}
void pastevents(char **command, int bg, int cmd_len, int *pipe_fd, int *pipe_fd2, int active_pipe)
{
strcpy(histo_link, gethome_len());
strcat(histo_link, "/");
strcat(histo_link, "history.bin");
int fd = open(histo_link, O_RDWR | O_CREAT, 0644);
char lines[1000] = {'\0'};
read(fd, lines, sizeof(lines));
if (cmd_len == 1)
{
char *result[2000];
int num = get_a_line(result);
int i = num - 1;
while (i >= 0 && result[i])
{
printf("%s\n", result[i]);
i--;
}
}
else if (cmd_len == 2 && !strcmp(command[1], "purge"))
{
ftruncate(fd, 0);
close(fd);
memset(&lines, '\0', sizeof(lines));
return;
}
else if (cmd_len == 3 && !strcmp(command[1], "execute"))
{
lseek(fd, 0, SEEK_CUR);
char *result[200] = {'\0'};
get_a_line(result);
strcat(result[atoi(command[2]) - 1], "\n");
verify(result[atoi(command[2]) - 1]);
}
close(fd);
}