-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.c
More file actions
98 lines (94 loc) · 1.47 KB
/
history.c
File metadata and controls
98 lines (94 loc) · 1.47 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
#include"header.h"
char historypath[10000];
void inithistory(char *hom)
{
char buf[10000];
strcpy(historypath,hom);
strcat(historypath,"/history");
FILE *fd=fopen(historypath,"r");
if(fd==NULL)
{
fd=fopen(historypath,"w");
fprintf(fd,"0\n");
}
else
{
fseek(fd,0,SEEK_END);
if(ftell(fd)==0)
{
fd=fopen(historypath,"w");
fprintf(fd,"0\n");
}
}
fseek(fd,0,SEEK_SET);
fscanf(fd,"%d",&histsize);
fclose(fd);
}
void addtohistory(char *c)
{
FILE *fd=fopen(historypath,"r");
if(fd==NULL)
{
perror(historypath);
exit(0);
}
char waste,buf[10000];
fseek(fd,0,SEEK_SET);
for(int i=0;i<=histsize;i++)
{
fscanf(fd,"%[^\n]s",buf);
fscanf(fd,"%c",&waste);
if(i!=0)
{
strcpy(hist[i-1],buf);
}
}
fclose(fd);
if(histsize==20)
{
for(int i=0;i<19;i++)
{
strcpy(hist[i],hist[i+1]);
}
histsize--;
}
strcpy(hist[histsize++],c);
fd=fopen(historypath,"w+");
if(fd==NULL)
{
perror(historypath);
exit(0);
}
fseek(fd,0,SEEK_SET);
fprintf(fd,"%d\n",histsize);
for(int i=0;i<histsize;i++)
{
fprintf(fd,"%s\n",hist[i]);
}
fclose(fd);
}
void historyexe(char **c)
{
int num=10;
if(size1>1)
{
num=atoi(c[1]);
}
FILE *fd=fopen(historypath,"r");
fseek(fd,0,SEEK_SET);
char buf[10000],waste;
for(int i=0;i<=histsize;i++)
{
fscanf(fd,"%[^\n]s",buf);
fscanf(fd,"%c",&waste);
if(i!=0)
{
strcpy(hist[i-1],buf);
}
}
fclose(fd);
for(int i=(0>histsize-num? 0:histsize-num);i<histsize;i++)
{
printf(" %d %s\n",i+1,hist[i]);
}
}