-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpastevents.c
More file actions
122 lines (118 loc) · 2.74 KB
/
pastevents.c
File metadata and controls
122 lines (118 loc) · 2.74 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
#include "headers.h"
void add(char* input){
char location[N];
strcpy(location, homedir);
int x = strlen(location);
char here[15] = "/pastevents.txt";
for(int i = 0; i < 15; ++i)
location[x++] = here[i];
location[x] = '\0';
FILE *fp = fopen(location, "r");
if(fp == NULL){
printf("ok\n");
return;
}
char* line = NULL;
char last[N];
int cnt = 0;
size_t len = 0;
char store[20][N];
while(getline(&line, &len, fp) != -1){
strcpy(store[cnt], line);
++cnt;
strcpy(last, line);
}
if(strcmp(last, "") && !strcmp(input, last)){
fclose(fp);
return;
}
fclose(fp);
if(cnt >= 15){
// remove the first line
// and add in the end
fp = fopen(location, "w");
fclose(fp);
fp = fopen(location, "a");
for(int i = 1; i < cnt; ++i)
fputs(store[i], fp);
fputs(input, fp);
}
else{
fp = fopen(location, "a");
fputs(input, fp);
}
fclose(fp);
}
void execute(char* input){
char location[N];
strcpy(location, homedir);
int x = strlen(location);
char here[15] = "/pastevents.txt";
for(int i = 0; i < 15; ++i)
location[x++] = here[i];
location[x] = '\0';
FILE *fp = fopen(location, "r");
if(fp == NULL){
printf("ok\n");
return;
}
char* line = NULL;
size_t len = 0;
int cnt = 0;
char store[20][N];
while(getline(&line, &len, fp) != -1){
strcpy(store[cnt], line);
++cnt;
}
char* temp;
temp = strtok(input, " ");
temp = strtok(NULL, " ");
temp = strtok(NULL, " ");
if(temp == NULL){
printf("Provide a command in history to execute");
fclose(fp);
return;
}
int num = atoi(temp), which = 1;
for(int i = cnt - 1; i >= 0; --i){
if(which == num){
call(store[i]);
}
++which;
}
fclose(fp);
}
void print(){
char location[N];
strcpy(location, homedir);
int x = strlen(location);
char here[15] = "/pastevents.txt";
for(int i = 0; i < 15; ++i)
location[x++] = here[i];
location[x] = '\0';
FILE *fp = fopen(location, "r");
if(fp == NULL){
printf("Error\n");
return;
}
size_t len = 0;
char* line = NULL;
while(getline(&line, &len, fp) != -1)
printf("%s", line);
fclose(fp);
}
void purge(){
char location[N];
strcpy(location, homedir);
int x = strlen(location);
char here[15] = "/pastevents.txt";
for(int i = 0; i < 15; ++i)
location[x++] = here[i];
location[x] = '\0';
FILE *fp = fopen(location, "w");
if(fp == NULL){
printf("ok\n");
return;
}
fclose(fp);
}