-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory.c
79 lines (54 loc) · 1.53 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
#include "history.h"
#include "headers.h"
int getHistory(char* root, char** commands) {
long unsigned size = 1024;
char path[1024];
int noOfCommands = 0;
commands[0] = NULL;
strcpy(path, root);
strcat(path, "/.history");
FILE* file = fopen(path, "r");
if (file) {
for (int i = 0; i < 20; i++) {
commands[i + 1] = NULL;
if (getline(&commands[i], &size, file) > 0) {
noOfCommands++;
} else {
break;
}
}
fclose(file);
}
return noOfCommands;
}
void addHistory(char* root, char* str) {
int valid = 0;
for (int i = 0; str[i] != '\0' && !valid; i++) {
if (str[i] != ' ' && str[i] != '\t') valid = 1;
}
if (!valid) return;
char* commands[21];
long unsigned size = 1024;
char path[1024];
int noOfCommands = getHistory(root, commands);
if (noOfCommands > 0 && !strcmp(str, commands[noOfCommands - 1])) return;
strcpy(path, root);
strcat(path, "/.history");
FILE* file = fopen(path, "w");
if (file) {
if (noOfCommands && noOfCommands < 20) fprintf(file, "%s", commands[0]);
for (int i = 1; i < noOfCommands; i++) fprintf(file, "%s", commands[i]);
fprintf(file, "%s", str);
fclose(file);
}
}
int tichnas_history(char* root, char* limitStr) {
int limit = 10;
if (limitStr) sscanf(limitStr, "%d", &limit);
char* commands[21];
int noOfCommands = getHistory(root, commands);
if (noOfCommands < limit) limit = noOfCommands;
for (int i = noOfCommands - limit; i < noOfCommands; i++)
printf("%s", commands[i]);
return 0;
}