-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.h
147 lines (135 loc) · 3.53 KB
/
history.h
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
void resize()
{
ll cnt = 0;
FILE *fd = fopen(HISTORYY, "a+");
if (fd == 0)
{
perror("History file deleted");
return;
}
char **tokens = (char **)malloc(sizeof(char *) * MAX_COMMANDS);
if(!tokens)
{
perror("Malloc error");
exit(0);
}
for (ll o = 0; o < MAX_COMMANDS; o++)
{
tokens[o] = NULL;
}
ll i = 0;
char *u = NULL;
size_t len;
int a = getline(&u, &len, fd);
while (a != -1)
{
tokens[i] = (char *)malloc(sizeof(char)*BUFFER_SIZE);
if(!tokens[i])
{
perror("Malloc error");
exit(0);
}
strcpy(tokens[i++], u);
a = getline(&u, &len, fd);
}
fclose(fd);
ll final;
if (i < 25)
{
final = i;
}
else
{
final = 25;
}
// Write back first 25 to same file
FILE *fd2 = fopen(HISTORYY,"w");
for (ll j = i - final; j < i; j++)
{
fputs(tokens[j],fd2);
}
fclose(fd2);
}
void history_vcsh(int argc, char *argv, ll flag)
{
if (flag == 0)
{
FILE *fd = fopen(HISTORYY, "a+");
if (fd == 0)
{
printf("Can't open .vcsh_history\n");
}
// Put command into history file
fputs(buffer, fd);
fputs("\n", fd);
fclose(fd);
}
else
{
// Extract top num commands
FILE *fd = fopen(HISTORYY, "r");
if (!fd)
{
perror("Can't open .vcsh_history");
}
char **tokens = (char **)malloc(sizeof(char *) * MAX_COMMANDS);
if(!tokens)
{
perror("Malloc error");
exit(0);
}
for (ll o = 0; o < MAX_TOKENS; o++)
{
tokens[o] = NULL;
}
char *line = (char *)malloc(sizeof(char) * (MAX_COMMANDS));
if(!line)
{
perror("Malloc error");
exit(0);
}
line = strtok(argv, " ");
ll i = 0;
while (line != NULL)
{
// tokens[i] = (char *)malloc(sizeof(char *)*BUFFER_SIZE);
tokens[i++] = line;
line = strtok(NULL, " ");
}
if (tokens[2] != NULL)
{
printf("Please do not provide multiple arguments\n");
}
ll p = 1;
ll num = 0;
if (tokens[1])
{
for (ll i = 0; i < strlen(tokens[1]); i++)
{
num += (tokens[1][strlen(tokens[1]) - 1 - i] - '0') * p;
p *= 10;
}
}
ll cnt = 0;
size_t len;
char *u = NULL;
ll a = getline(&u, &len, fd);
ll final = num > 10 ? 10 : num;
if (tokens[1] == NULL)
{
final = 10;
}
char BUF[512][512];
while (a != -1)
{
strcpy(BUF[cnt], u);
cnt++;
a = getline(&u, &len, fd);
}
for (ll l = cnt - final; l < cnt; l++)
{
printf("%s", BUF[l]);
}
fclose(fd);
}
}