-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
62 lines (51 loc) · 1.28 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
#include "history.h"
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
struct hist_ent *history = NULL;
void history_clear() {
struct hist_ent *it = history;
while (it) {
struct hist_ent *tmp;
free(it->data);
tmp = it;
it = it->next;
free(tmp);
}
history = NULL;
}
void history_raise(size_t num) {
size_t i;
struct hist_ent **ent, *tmp;
for(i=0,ent = &history;*ent;ent=&(*ent)->next,i++) {
if(i == num) {
tmp = *ent;
*ent = (*ent)->next;
tmp->next = history;
history = tmp;
break;
}
}
}
void history_add_paste(char *data, size_t data_sz) {
struct hist_ent *nh;
for(struct hist_ent **ent = &history;*ent;ent = &(*ent)->next) {
if(data_sz == (*ent)->data_sz-1 &&
!memcmp((*ent)->data, data, data_sz)) {
nh = *ent;
*ent = (*ent)->next;
nh->next = history;
history = nh;
return;
}
}
nh = malloc(sizeof(struct hist_ent));
nh->data = malloc(data_sz+1);
memcpy(nh->data, data, data_sz);
nh->data[data_sz] = '\0';
nh->data_sz = data_sz+1;
nh->next = history;
history = nh;
}