-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory.c
89 lines (85 loc) · 2.04 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
#include "history.h"
#include "headers.h"
char hist[40][PATH_MAX];
int nlines;
extern char homedir[PATH_MAX];
extern char historypath[PATH_MAX];
int stoi (char * input){
int output;
char * end;
output = strtol (input, & end, 10);
if (end == input)
return 10;
if(output>=1 || output<=20)
return output;
else
return 10;
}
int add_history(char *command){
FILE *fhi;
char * line=NULL;
int i=0, j=0, k=0, nf=0;
ssize_t read, len=0;
fhi=fopen(historypath, "r");
if (!fhi) {
nf=1;
fhi=fopen(historypath, "w");
if (!fhi) {
printf("Unable to save to history, history won't be logged\n");
perror("Error: ");
return -1;
}
fclose(fhi);
}
if(nf!=1){
while ((read = getline(&line, &len, fhi)) != -1) {
strcpy(hist[i], line);
i++;
}
nlines=i;
if(nlines>=20){
for(j=0;j<19;j++)
strcpy(hist[j], hist[j+1]);
fclose(fhi);
fhi=fopen(historypath, "w");
for(j=0;j<19;j++){
fprintf(fhi, "%s", hist[j]);
}
fclose(fhi);
}
}
fhi=fopen(historypath, "a");
fprintf(fhi, "%s\n", command);
fclose(fhi);
return 0;
}
int print_history(char * input){
int n=stoi(input);
char * line=NULL;
int i=0, j=0, cmdInHistory=0;
ssize_t read, len=0;
n=n>20?20:n; // If input is greater than 20, print only 20;
FILE *fhi;
fhi=fopen(historypath, "r");
if (!fhi) {
printf("Unable to open history\n");
return -1;
}
// Printing History
while ((read = getline(&line, &len, fhi)) != -1) {
strcpy(hist[i], line);
i++;
}
if(n>i){
printf("Only %d entries\n", i);
for(j=0;j<i;j++)
printf("%s", hist[j]);
fclose(fhi);
return 0;
}else{
for(j=i-n;j<i;j++)
printf("%s", hist[j]);
fclose(fhi);
return 0;
}
}