-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
105 lines (97 loc) · 2.01 KB
/
history.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
#include "headers.h"
void store(char *command, char *path)
{
FILE* hist = fopen(path,"r");
if(hist == NULL)
{
hist = fopen(path,"w");
}
if(lastcommand[0] == '\0')
strcpy(lastcommand,command);
else if(strcmp(lastcommand,command) == 0)
{
fclose(hist);
return;
}
char buf1[21][MAX_SIZE];
int j = 0;
while(fgets(buf1[j],MAX_SIZE,hist))
{
j++;
}
if(j >= 20)
{
fclose(hist);
hist = fopen(path,"w");
int n1 = strlen(buf1[1]);
fwrite(buf1[1],1,n1,hist);
fclose(hist);
hist = fopen(path,"a");
for(int j = 2;j<20;j++)
{
int n2 = strlen(buf1[j]);
fwrite(buf1[j],1,n2,hist);
}
}
else
{
fclose(hist);
hist = fopen(path,"a");
}
char buf2[MAX_SIZE];
int n1 = strlen(command);
strcpy(buf2,command);
strcat(buf2,"\n");
fwrite(buf2,1,n1+1,hist);
strcpy(lastcommand,command);
fclose(hist);
return;
}
void gethistory(char *command,char *path)
{
char buf[MAX_SIZE];
strcpy(buf,command);
char *saveptr;
strtok_r(buf," ",&saveptr);
int num;
char *arg = strtok_r(NULL," ",&saveptr);
if(arg == NULL)
num = 10;
else
num = atoi(arg);
if(num > 20)
{
printf("Please enter a number less than 20\n");
return;
}
FILE* hist = fopen(path,"r");
fseek(hist,0,SEEK_END);
long size = ftell(hist);
if(size >= 2010)
size = 2010;
char buf1[size+1];
fseek(hist,-size,SEEK_END);
fread(buf1,1,size,hist);
buf1[size+1] = '\0';
int count = 0;
int pos = -1;
for(int i = size + 1 ;i>=0;i--)
{
if(buf1[i] == '\n')
count++;
if(count == num + 1)
{
pos = i;
break;
}
}
buf1[size] ='\0';
if(pos == -1)
printf("%s",buf1);
else
{
printf("%s",buf1+pos+1);
}
fclose(hist);
return;
}