-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
50 lines (38 loc) · 1.22 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//Custom Headers
#include<history.h>
#include<constants.h>
const size_t HISTORY_BUFFER = 1000;
HISTORY_STORE* create_history_store(void) {
HISTORY_STORE *history_store = (HISTORY_STORE *)malloc(sizeof(HISTORY_STORE));
if(history_store == NULL) {
fprintf(stderr, error_msg);
exit(1);
}
history_store->store = (char **)malloc(sizeof(char *)*HISTORY_BUFFER);
history_store->curr = 0;
history_store->max = HISTORY_BUFFER;
if(history_store->store == NULL) {
fprintf(stderr, error_msg);
exit(1);
}
return history_store;
}
char* get_history(HISTORY_STORE *history_store, size_t n) {
if(n < 0 || n > history_store->curr) {
fprintf(stderr, error_msg);
exit(1);
}
return history_store->store[n];
}
void add_history(HISTORY_STORE *history_store, char *cmd) {
//Reallocate to a bigger size
if(history_store->curr + 1 == history_store->max) {
history_store->max += HISTORY_BUFFER;
history_store->store = (char **)realloc(history_store->store, sizeof(char *)*(history_store->max));
}
(history_store->store)[(history_store->curr)++] = strdup(cmd);
return;
}